From 6f4ec5592bf0db7235d0c4f74fd9f65ccce8742a Mon Sep 17 00:00:00 2001 From: Martin Polden Date: Wed, 6 Dec 2017 14:48:51 +0100 Subject: Update name service interface --- .../api/integration/dns/MemoryNameService.java | 17 +++++++- .../api/integration/dns/NameService.java | 12 ++++-- .../controller/api/integration/dns/Record.java | 18 ++++---- .../controller/api/integration/dns/RecordData.java | 49 ++++++++++++++++++++++ .../controller/api/integration/dns/RecordName.java | 47 +++++++++++++++++++++ 5 files changed, 129 insertions(+), 14 deletions(-) create mode 100644 controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/dns/RecordData.java create mode 100644 controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/dns/RecordName.java (limited to 'controller-api') diff --git a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/dns/MemoryNameService.java b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/dns/MemoryNameService.java index c82a3fd571d..9f4af82c5b0 100644 --- a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/dns/MemoryNameService.java +++ b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/dns/MemoryNameService.java @@ -22,19 +22,32 @@ public class MemoryNameService implements NameService { } @Override - public RecordId createCname(String alias, String canonicalName) { + public RecordId createCname(RecordName alias, RecordData canonicalName) { RecordId id = new RecordId(UUID.randomUUID().toString()); records.put(id, new Record(id, Record.Type.CNAME, alias, canonicalName)); return id; } @Override - public Optional findRecord(Record.Type type, String name) { + public Optional findRecord(Record.Type type, RecordName name) { return records.values().stream() .filter(record -> record.type() == type && record.name().equals(name)) .findFirst(); } + @Override + public Optional findRecord(Record.Type type, RecordData data) { + return records.values() + .stream() + .filter(record -> record.type() == type && record.value().equals(data)) + .findFirst(); + } + + @Override + public void updateRecord(RecordId id, RecordData newData) { + records.computeIfPresent(id, (k, record) -> new Record(id, record.type(), record.name(), newData)); + } + @Override public void removeRecord(RecordId id) { records.remove(id); diff --git a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/dns/NameService.java b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/dns/NameService.java index 9b7c527db63..078a7e7cefb 100644 --- a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/dns/NameService.java +++ b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/dns/NameService.java @@ -14,12 +14,18 @@ public interface NameService { * Create a new CNAME record * * @param alias The alias to create - * @param canonicalName The canonical name which the alias should point to. This must be a domain. + * @param canonicalName The canonical name which the alias should point to. This must be a FQDN. */ - RecordId createCname(String alias, String canonicalName); + RecordId createCname(RecordName alias, RecordData canonicalName); /** Find record by type and name */ - Optional findRecord(Record.Type type, String name); + Optional findRecord(Record.Type type, RecordName name); + + /** Find record by type and data */ + Optional findRecord(Record.Type type, RecordData data); + + /** Update existing record */ + void updateRecord(RecordId id, RecordData newData); /** Remove record by ID */ void removeRecord(RecordId id); diff --git a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/dns/Record.java b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/dns/Record.java index f190d11cf8f..fd9bddac2c6 100644 --- a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/dns/Record.java +++ b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/dns/Record.java @@ -12,14 +12,14 @@ public class Record { private final RecordId id; private final Type type; - private final String name; - private final String value; + private final RecordName name; + private final RecordData value; - public Record(RecordId id, Type type, String name, String value) { - this.id = id; - this.type = type; - this.name = name; - this.value = value; + public Record(RecordId id, Type type, RecordName name, RecordData value) { + this.id = Objects.requireNonNull(id, "id cannot be null"); + this.type = Objects.requireNonNull(type, "type cannot be null"); + this.name = Objects.requireNonNull(name, "name cannot be null"); + this.value = Objects.requireNonNull(value, "value cannot be null"); } /** Unique identifier for this */ @@ -33,12 +33,12 @@ public class Record { } /** Value for this, e.g. IP address for "A" record */ - public String value() { + public RecordData value() { return value; } /** Name of this, e.g. a FQDN for "A" record */ - public String name() { + public RecordName name() { return name; } diff --git a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/dns/RecordData.java b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/dns/RecordData.java new file mode 100644 index 00000000000..444ee28f672 --- /dev/null +++ b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/dns/RecordData.java @@ -0,0 +1,49 @@ +// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.vespa.hosted.controller.api.integration.dns; + +import java.util.Objects; + +/** + * Represents the data field of a DNS record (RDATA). + * + * E.g. this may be an IP address for A records, or a FQDN for CNAME records. + * + * @author mpolden + */ +public class RecordData { + + private final String data; + + private RecordData(String data) { + this.data = Objects.requireNonNull(data, "data cannot be null"); + } + + public String asString() { + return data; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + RecordData that = (RecordData) o; + return Objects.equals(data, that.data); + } + + @Override + public int hashCode() { + return Objects.hash(data); + } + + @Override + public String toString() { + return "RecordValue{" + + "value='" + data + '\'' + + '}'; + } + + public static RecordData from(String data) { + return new RecordData(data); + } + +} diff --git a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/dns/RecordName.java b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/dns/RecordName.java new file mode 100644 index 00000000000..aa239ece588 --- /dev/null +++ b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/dns/RecordName.java @@ -0,0 +1,47 @@ +// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.vespa.hosted.controller.api.integration.dns; + +import java.util.Objects; + +/** + * Represents the name field of a DNS record (NAME). This is typically a FQDN. + * + * @author mpolden + */ +public class RecordName { + + private final String name; + + private RecordName(String name) { + this.name = Objects.requireNonNull(name, "name cannot be null"); + } + + public String asString() { + return name; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + RecordName that = (RecordName) o; + return Objects.equals(name, that.name); + } + + @Override + public int hashCode() { + return Objects.hash(name); + } + + @Override + public String toString() { + return "RecordName{" + + "name='" + name + '\'' + + '}'; + } + + public static RecordName from(String name) { + return new RecordName(name); + } + +} -- cgit v1.2.3