summaryrefslogtreecommitdiffstats
path: root/config-provisioning
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@gmail.com>2022-10-13 14:30:07 +0200
committerGitHub <noreply@github.com>2022-10-13 14:30:07 +0200
commit918adc0fa08d999a151a5d07a83d99f54567c1bd (patch)
tree1d51aacee160da1312763b9ff30b91b67ef002ab /config-provisioning
parent7969913ee302ec4a19c38c529b99ce12edddb5b7 (diff)
parent11fb09016d44de8514019d29908dfb7d4c623840 (diff)
Merge pull request #24406 from vespa-engine/bratseth/instance-tags
Add instance tags [run-systemtest]
Diffstat (limited to 'config-provisioning')
-rw-r--r--config-provisioning/src/main/java/com/yahoo/config/provision/Tags.java64
-rw-r--r--config-provisioning/src/test/java/com/yahoo/config/provision/TagsTest.java57
2 files changed, 121 insertions, 0 deletions
diff --git a/config-provisioning/src/main/java/com/yahoo/config/provision/Tags.java b/config-provisioning/src/main/java/com/yahoo/config/provision/Tags.java
new file mode 100644
index 00000000000..3a54a0b9ebb
--- /dev/null
+++ b/config-provisioning/src/main/java/com/yahoo/config/provision/Tags.java
@@ -0,0 +1,64 @@
+// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.config.provision;
+
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * A deployment may have a list of tags associated with it. Config files may have variants for these tags similar
+ * to how they may have variants for instance and zone.
+ *
+ * @author bratseth
+ */
+public class Tags {
+
+ private final Set<String> tags;
+
+ public Tags(Set<String> tags) {
+ this.tags = Set.copyOf(tags);
+ }
+
+ public boolean contains(String tag) {
+ return tags.contains(tag);
+ }
+
+ public boolean intersects(Tags other) {
+ return this.tags.stream().anyMatch(other::contains);
+ }
+
+ public boolean isEmpty() { return tags.isEmpty(); }
+
+ public boolean containsAll(Tags other) { return tags.containsAll(other.tags); }
+
+ /** Returns this as a space-separated string which can be used to recreate this by calling fromString(). */
+ public String asString() {
+ return tags.stream().sorted().collect(Collectors.joining(" "));
+ }
+
+ @Override
+ public String toString() {
+ return asString();
+ }
+
+ @Override
+ public boolean equals(Object other) {
+ if (other == this) return true;
+ if (other == null || other.getClass() != getClass()) return false;
+ return tags.equals(((Tags)other).tags);
+ }
+
+ @Override
+ public int hashCode() {
+ return tags.hashCode();
+ }
+
+ public static Tags empty() { return new Tags(Set.of()); }
+
+ /**
+ * Creates this from a space-separated string or null. */
+ public static Tags fromString(String tagsString) {
+ if (tagsString == null || tagsString.isBlank()) return empty();
+ return new Tags(Set.of(tagsString.trim().split(" +")));
+ }
+
+}
diff --git a/config-provisioning/src/test/java/com/yahoo/config/provision/TagsTest.java b/config-provisioning/src/test/java/com/yahoo/config/provision/TagsTest.java
new file mode 100644
index 00000000000..a20e674c66e
--- /dev/null
+++ b/config-provisioning/src/test/java/com/yahoo/config/provision/TagsTest.java
@@ -0,0 +1,57 @@
+// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.config.provision;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.Set;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * @author bratseth
+ */
+public class TagsTest {
+
+ @Test
+ public void testEmpty() {
+ assertEquals(Tags.empty(), Tags.fromString(null));
+ assertEquals(Tags.empty(), Tags.fromString(""));
+ assertEquals(Tags.empty(), Tags.fromString(" "));
+ }
+
+ @Test
+ public void testDeserialization() {
+ assertEquals(new Tags(Set.of("tag1", "tag2")), Tags.fromString(" tag1 tag2 "));
+ }
+
+ @Test
+ public void testSerialization() {
+ Tags tags = new Tags(Set.of("a", "tag2", "3"));
+ assertEquals(tags, Tags.fromString(tags.toString())); // Required by automatic serialization
+ assertEquals(tags, Tags.fromString(tags.asString())); // Required by automatic serialization
+ }
+
+ @Test
+ public void testContains() {
+ Tags tags = new Tags(Set.of("a", "tag2", "3"));
+ assertTrue(tags.contains("a"));
+ assertTrue(tags.contains("tag2"));
+ assertTrue(tags.contains("3"));
+ assertFalse(tags.contains("other"));
+
+ Tags subTags = new Tags(Set.of("a", "3"));
+ assertTrue(tags.containsAll(subTags));
+ assertFalse(subTags.containsAll(tags));
+ }
+
+ @Test
+ public void testIntersects() {
+ Tags tags1 = new Tags(Set.of("a", "tag2", "3"));
+ Tags tags2 = new Tags(Set.of("a", "tag3"));
+ assertTrue(tags1.intersects(tags2));
+ assertTrue(tags2.intersects(tags1));
+ }
+
+}