aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/dns/CreateRecords.java
blob: d560dbd8db90c663e33875eeb6d450d7af32e195 (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
79
80
81
82
83
84
85
86
87
// 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.AliasTarget;
import com.yahoo.vespa.hosted.controller.api.integration.dns.DirectTarget;
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;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Create or update multiple records of the same type and name.
 *
 * @author mpolden
 */
public class CreateRecords extends AbstractNameServiceRequest {

    private final Record.Type type;
    private final List<Record> records;

    /** DO NOT USE. Public for serialization purposes */
    public CreateRecords(Optional<TenantAndApplicationId> owner, List<Record> records) {
        super(owner, requireOneOf(Record::name, records));
        this.type = requireOneOf(Record::type, records);
        this.records = List.copyOf(Objects.requireNonNull(records, "records must be non-null"));
        if (type != Record.Type.ALIAS && type != Record.Type.TXT && type != Record.Type.DIRECT) {
            throw new IllegalArgumentException("Records of type " + type + " are not supported: " + records);
        }
    }

    public List<Record> records() {
        return records;
    }

    @Override
    public void dispatchTo(NameService nameService) {
        switch (type) {
            case ALIAS -> {
                var targets = records.stream().map(Record::data).map(AliasTarget::unpack).collect(Collectors.toSet());
                nameService.createAlias(name(), targets);
            }
            case DIRECT -> {
                var targets = records.stream().map(Record::data).map(DirectTarget::unpack).collect(Collectors.toSet());
                nameService.createDirect(name(), targets);
            }
            case TXT -> {
                var dataFields = records.stream().map(Record::data).toList();
                nameService.createTxtRecords(name(), dataFields);
            }
        }
    }

    @Override
    public String toString() {
        return "create records " + records();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        CreateRecords that = (CreateRecords) o;
        return owner().equals(that.owner()) && records.equals(that.records);
    }

    @Override
    public int hashCode() {
        return Objects.hash(owner(), records);
    }

    /** Find exactly one distinct value of field in given list */
    private static <T, V> T requireOneOf(Function<V, T> field, List<V> list) {
        Set<T> values = list.stream().map(field).collect(Collectors.toSet());
        if (values.size() != 1) {
            throw new IllegalArgumentException("Expected one distinct value, but found " + values + " in " + list);
        }
        return values.iterator().next();
    }

}