From 04aea0d7b78516dc02388e66052f9c02879c40fd Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Thu, 20 Feb 2020 23:15:40 +0100 Subject: feat: Add support for resource tagging --- .../com/atlarge/opendc/core/resource/Resource.kt | 37 +++++++++++++ .../atlarge/opendc/core/resource/TagContainer.kt | 62 ++++++++++++++++++++++ .../opendc/core/resource/TagContainerImpl.kt | 51 ++++++++++++++++++ .../com/atlarge/opendc/core/resource/TagKey.kt | 30 +++++++++++ 4 files changed, 180 insertions(+) create mode 100644 opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/resource/Resource.kt create mode 100644 opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/resource/TagContainer.kt create mode 100644 opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/resource/TagContainerImpl.kt create mode 100644 opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/resource/TagKey.kt (limited to 'opendc/opendc-core/src') 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> + + /** + * 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 get(key: TagKey): T + + /** + * Return the result of associating the specified [value] with the specified [key] in this container. + */ + public fun put(key: TagKey, 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, Any>) : TagContainer { + /** + * Construct an empty [TagContainerImpl]. + */ + public constructor() : this(emptyMap()) + + override val keys: Collection> + get() = map.keys + + override fun contains(key: TagKey<*>): Boolean = map.contains(key) + + override fun get(key: TagKey): T { + @Suppress("UNCHECKED_CAST") + return map.getValue(key) as T + } + + override fun put(key: TagKey, 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(val name: String) -- cgit v1.2.3 From 49b673e26ebf9f6a1c96f083d3876b19027b2abc Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Fri, 21 Feb 2020 15:47:33 +0100 Subject: feat: Simplify tagging of cloud resources --- .../atlarge/opendc/core/resource/TagContainer.kt | 38 +++------------- .../opendc/core/resource/TagContainerImpl.kt | 51 ---------------------- .../com/atlarge/opendc/core/resource/TagKey.kt | 30 ------------- 3 files changed, 6 insertions(+), 113 deletions(-) delete mode 100644 opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/resource/TagContainerImpl.kt delete mode 100644 opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/resource/TagKey.kt (limited to 'opendc/opendc-core/src') 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 index 4167cd20..6ba1cf0b 100644 --- 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 @@ -27,36 +27,10 @@ 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> +typealias TagContainer = Map - /** - * 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 get(key: TagKey): T - - /** - * Return the result of associating the specified [value] with the specified [key] in this container. - */ - public fun put(key: TagKey, value: T): TagContainer - - /** - * Return the result of removing the specified [key] and its corresponding value from this container. - */ - public fun remove(key: TagKey<*>): TagContainer -} +/** + * Obtain the value of the tag with the specified [key] of type [T]. If the tag does not exist or the tag is of + * different type, `null` is returned. + */ +inline fun TagContainer.typed(key: String): T? = this[key] as? T 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 deleted file mode 100644 index 1acc7de4..00000000 --- a/opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/resource/TagContainerImpl.kt +++ /dev/null @@ -1,51 +0,0 @@ -/* - * 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, Any>) : TagContainer { - /** - * Construct an empty [TagContainerImpl]. - */ - public constructor() : this(emptyMap()) - - override val keys: Collection> - get() = map.keys - - override fun contains(key: TagKey<*>): Boolean = map.contains(key) - - override fun get(key: TagKey): T { - @Suppress("UNCHECKED_CAST") - return map.getValue(key) as T - } - - override fun put(key: TagKey, 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 deleted file mode 100644 index 759f4461..00000000 --- a/opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/resource/TagKey.kt +++ /dev/null @@ -1,30 +0,0 @@ -/* - * 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(val name: String) -- cgit v1.2.3