aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/dns/CreateRecord.java
blob: c4c76bc7954bae46c87717adafc2020a9f72ef69 (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
// Copyright Vespa.ai. 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 extends AbstractNameServiceRequest {

    private final Record record;

    /** DO NOT USE. Public for serialization purposes */
    public CreateRecord(Optional<TenantAndApplicationId> owner, Record record) {
        super(owner, record.name());
        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 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);
    }

}