summaryrefslogtreecommitdiff
path: root/opendc/opendc-core/src
diff options
context:
space:
mode:
authorFabian Mastenbroek <mail.fabianm@gmail.com>2020-02-20 23:15:40 +0100
committerFabian Mastenbroek <mail.fabianm@gmail.com>2020-02-21 11:24:54 +0100
commit04aea0d7b78516dc02388e66052f9c02879c40fd (patch)
tree420bce5a334a4fe794e4c0262c03e670caeaf8fe /opendc/opendc-core/src
parent37a5902767bc787bcc470e42b5b078e340a67b18 (diff)
feat: Add support for resource tagging
Diffstat (limited to 'opendc/opendc-core/src')
-rw-r--r--opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/resource/Resource.kt37
-rw-r--r--opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/resource/TagContainer.kt62
-rw-r--r--opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/resource/TagContainerImpl.kt51
-rw-r--r--opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/resource/TagKey.kt30
4 files changed, 180 insertions, 0 deletions
diff --git a/opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/resource/Resource.kt b/opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/resource/Resource.kt
new file mode 100644
index 00000000..25a494bc
--- /dev/null
+++ b/opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/resource/Resource.kt
@@ -0,0 +1,37 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2020 atlarge-research
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+package com.atlarge.opendc.core.resource
+
+import com.atlarge.opendc.core.Identity
+
+/**
+ * Represents a generic cloud resource.
+ */
+public interface Resource : Identity {
+ /**
+ * The tags of this cloud resource.
+ */
+ public val tags: TagContainer
+}
diff --git a/opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/resource/TagContainer.kt b/opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/resource/TagContainer.kt
new file mode 100644
index 00000000..4167cd20
--- /dev/null
+++ b/opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/resource/TagContainer.kt
@@ -0,0 +1,62 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2020 atlarge-research
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+package com.atlarge.opendc.core.resource
+
+/**
+ * An immutable map containing the tags of some resource.
+ */
+public interface TagContainer {
+ /**
+ * The keys in this container.
+ */
+ public val keys: Collection<TagKey<*>>
+
+ /**
+ * Determine if this container contains a tag with the specified [TagKey].
+ *
+ * @param key The key of the tag to check for.
+ * @return `true` if the tag is in the container, `false` otherwise.
+ */
+ public operator fun contains(key: TagKey<*>): Boolean
+
+ /**
+ * Obtain the tag with the specified [TagKey].
+ *
+ * @param key The key of the tag to obtain.
+ * @return The value of the tag.
+ * @throws IllegalArgumentException if the tag does not exists in the container.
+ */
+ public operator fun <T : Any> get(key: TagKey<T>): T
+
+ /**
+ * Return the result of associating the specified [value] with the specified [key] in this container.
+ */
+ public fun <T : Any> put(key: TagKey<T>, value: T): TagContainer
+
+ /**
+ * Return the result of removing the specified [key] and its corresponding value from this container.
+ */
+ public fun remove(key: TagKey<*>): TagContainer
+}
diff --git a/opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/resource/TagContainerImpl.kt b/opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/resource/TagContainerImpl.kt
new file mode 100644
index 00000000..1acc7de4
--- /dev/null
+++ b/opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/resource/TagContainerImpl.kt
@@ -0,0 +1,51 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2020 atlarge-research
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+package com.atlarge.opendc.core.resource
+
+/**
+ * Default implementation of [TagContainer] interface.
+ *
+ * @property map The internal map object to hold the tags.
+ */
+public class TagContainerImpl private constructor(private val map: Map<TagKey<*>, Any>) : TagContainer {
+ /**
+ * Construct an empty [TagContainerImpl].
+ */
+ public constructor() : this(emptyMap())
+
+ override val keys: Collection<TagKey<*>>
+ get() = map.keys
+
+ override fun contains(key: TagKey<*>): Boolean = map.contains(key)
+
+ override fun <T : Any> get(key: TagKey<T>): T {
+ @Suppress("UNCHECKED_CAST")
+ return map.getValue(key) as T
+ }
+
+ override fun <T : Any> put(key: TagKey<T>, value: T): TagContainer = TagContainerImpl(map.plus(key to value))
+
+ override fun remove(key: TagKey<*>): TagContainer = TagContainerImpl(map.minus(key))
+}
diff --git a/opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/resource/TagKey.kt b/opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/resource/TagKey.kt
new file mode 100644
index 00000000..759f4461
--- /dev/null
+++ b/opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/resource/TagKey.kt
@@ -0,0 +1,30 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2020 atlarge-research
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+package com.atlarge.opendc.core.resource
+
+/**
+ * Represents a typed key for a resource tag with a unique name.
+ */
+public data class TagKey<T : Any>(val name: String)