blob: f1e4ca3b82b16ca6f30b9ae1afd4a687f47eccd1 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.dns;
import com.yahoo.vespa.hosted.controller.api.integration.dns.NameService;
import com.yahoo.vespa.hosted.controller.api.integration.dns.Record;
import com.yahoo.vespa.hosted.controller.api.integration.dns.RecordName;
import com.yahoo.vespa.hosted.controller.application.TenantAndApplicationId;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
/**
* Create or update a record.
*
* @author mpolden
*/
public class CreateRecord implements NameServiceRequest {
private final Optional<TenantAndApplicationId> owner;
private final Record record;
/** DO NOT USE. Public for serialization purposes */
public CreateRecord(Optional<TenantAndApplicationId> owner, Record record) {
this.owner = Objects.requireNonNull(owner, "owner must be non-null");
this.record = Objects.requireNonNull(record, "record must be non-null");
if (record.type() != Record.Type.CNAME && record.type() != Record.Type.A) {
throw new IllegalArgumentException("Record of type " + record.type() + " is not supported: " + record);
}
}
public Record record() {
return record;
}
@Override
public Optional<RecordName> name() {
return Optional.of(record.name());
}
@Override
public Optional<TenantAndApplicationId> owner() {
return owner;
}
@Override
public void dispatchTo(NameService nameService) {
List<Record> records = nameService.findRecords(record.type(), record.name());
records.forEach(r -> {
// Ensure that existing record has correct data
if (!r.data().equals(record.data())) {
nameService.updateRecord(r, record.data());
}
});
if (records.isEmpty()) {
nameService.createRecord(record.type(), record.name(), record.data());
}
}
@Override
public String toString() {
return "create record " + record;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CreateRecord that = (CreateRecord) o;
return owner.equals(that.owner) && record.equals(that.record);
}
@Override
public int hashCode() {
return Objects.hash(owner, record);
}
}
|