aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/dns/RemoveRecords.java
blob: 273136ba0a63894d6262e1f5582906989739dccd (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
88
89
90
91
92
93
94
// 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.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.RecordData;
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;

/**
 * Permanently removes all matching records by type and matching either:
 *
 * - name and data
 * - only name
 *
 * @author mpolden
 */
public class RemoveRecords extends AbstractNameServiceRequest {

    private final Record.Type type;
    private final Optional<RecordData> data;

    public RemoveRecords(Optional<TenantAndApplicationId> owner, Record.Type type, RecordName name) {
        this(owner, type, name, Optional.empty());
    }

    public RemoveRecords(Optional<TenantAndApplicationId> owner, Record.Type type, RecordName name, RecordData data) {
        this(owner, type, name, Optional.of(data));
    }

    /** DO NOT USE. Public for serialization purposes */
    public RemoveRecords(Optional<TenantAndApplicationId> owner, Record.Type type, RecordName name, Optional<RecordData> data) {
        super(owner, name);
        this.type = Objects.requireNonNull(type, "type must be non-null");
        this.data = Objects.requireNonNull(data, "data must be non-null");
    }

    public Record.Type type() {
        return type;
    }

    public Optional<RecordData> data() {
        return data;
    }

    @Override
    public void dispatchTo(NameService nameService) {
        // Deletions require all records fields to match exactly, data may be incomplete even if present. To ensure
        // completeness we search for the record(s) first
        List<Record> completeRecords = nameService.findRecords(type, name()).stream()
                                                  .filter(record -> data.isEmpty() || matchingFqdnIn(data.get(), record))
                                                  .toList();
        nameService.removeRecords(completeRecords);
    }

    @Override
    public String toString() {
        return "remove records of type " + type + ", by name " + name() +
               data.map(d -> " data " + d).orElse("");
    }

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

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

    private static boolean matchingFqdnIn(RecordData data, Record record) {
        String dataValue = switch (record.type()) {
            case ALIAS -> AliasTarget.unpack(record.data()).name().value();
            case DIRECT -> DirectTarget.unpack(record.data()).recordData().asString();
            default -> record.data().asString();
        };
        return fqdn(dataValue).equals(fqdn(data.asString()));
    }

    private static String fqdn(String name) {
        return name.endsWith(".") ? name : name + ".";
    }

}