From fc8196a2d90b6aea5b58af45b8266099cef00f7c Mon Sep 17 00:00:00 2001 From: Hans Song Date: Wed, 3 Jan 2024 18:14:24 +1000 Subject: [PATCH 1/4] feat: expose tags in tagset data source --- docs/data-sources/tag_sets.md | 13 +++++++++++++ octopusdeploy/schema_tag.go | 11 +++++++++++ octopusdeploy/schema_tag_set.go | 26 ++++++++++++++++++++++++++ 3 files changed, 50 insertions(+) diff --git a/docs/data-sources/tag_sets.md b/docs/data-sources/tag_sets.md index f126a66fd..454f1f2bf 100644 --- a/docs/data-sources/tag_sets.md +++ b/docs/data-sources/tag_sets.md @@ -20,6 +20,7 @@ Provides information about existing tag sets. - `ids` (List of String) A filter to search by a list of IDs. - `partial_name` (String) A filter to search by the partial match of a name. - `skip` (Number) A filter to specify the number of items to skip in the response. +- `space_id` (String) The space ID associated with this resource. - `take` (Number) A filter to specify the number of items to take (or return) in the response. ### Read-Only @@ -37,5 +38,17 @@ Read-Only: - `name` (String) The name of this resource. - `sort_order` (Number) The sort order associated with this resource. - `space_id` (String) The space ID associated with this resource. +- `tags` (List of Object) A list of tags associated with this tag set. (see [below for nested schema](#nestedatt--tag_sets--tags)) + + +### Nested Schema for `tag_sets.tags` + +Read-Only: + +- `canonical_tag_name` (String) +- `color` (String) +- `description` (String) +- `name` (String) +- `sort_order` (Number) diff --git a/octopusdeploy/schema_tag.go b/octopusdeploy/schema_tag.go index e9657ae61..ce6bbd8a3 100644 --- a/octopusdeploy/schema_tag.go +++ b/octopusdeploy/schema_tag.go @@ -60,6 +60,17 @@ func expandTag(d *schema.ResourceData) *tagsets.Tag { return tag } +func flattenTag(tag *tagsets.Tag) map[string]interface{} { + return map[string]interface{}{ + "canonical_tag_name": tag.CanonicalTagName, + "color": tag.Color, + "description": tag.Description, + "id": tag.ID, + "name": tag.Name, + "sort_order": tag.SortOrder, + } +} + func setTag(ctx context.Context, d *schema.ResourceData, tag *tagsets.Tag, tagSet *tagsets.TagSet) error { d.Set("canonical_tag_name", tag.CanonicalTagName) d.Set("color", tag.Color) diff --git a/octopusdeploy/schema_tag_set.go b/octopusdeploy/schema_tag_set.go index db5c31a23..0e7487131 100644 --- a/octopusdeploy/schema_tag_set.go +++ b/octopusdeploy/schema_tag_set.go @@ -25,6 +25,14 @@ func expandTagSet(d *schema.ResourceData) *tagsets.TagSet { tagSet.SpaceID = v.(string) } + if v, ok := d.GetOk("tags"); ok { + if tags, ok := v.([]*schema.ResourceData); ok { + for _, tag := range tags { + tagSet.Tags = append(tagSet.Tags, expandTag(tag)) + } + } + } + return tagSet } @@ -33,17 +41,35 @@ func flattenTagSet(tagSet *tagsets.TagSet) map[string]interface{} { return nil } + tags := make([]map[string]interface{}, len(tagSet.Tags)) + for i, tag := range tagSet.Tags { + tags[i] = flattenTag(tag) + } + return map[string]interface{}{ "description": tagSet.Description, "id": tagSet.GetID(), "name": tagSet.Name, "sort_order": tagSet.SortOrder, "space_id": tagSet.SpaceID, + "tags": tags, } } func getTagSetDataSchema() map[string]*schema.Schema { + tagSchema := getTagSchema() + delete(tagSchema, "tag_set_id") + delete(tagSchema, "tag_set_space_id") + setDataSchema(&tagSchema) + dataSchema := getTagSetSchema() + dataSchema["tags"] = &schema.Schema{ + Computed: true, + Description: "A list of tags associated with this tag set.", + Elem: &schema.Resource{Schema: tagSchema}, + Optional: true, + Type: schema.TypeList, + } setDataSchema(&dataSchema) return map[string]*schema.Schema{ From 929459d83e95ec742854affed675a56cc9c182cf Mon Sep 17 00:00:00 2001 From: andrew2224 Date: Thu, 4 Jan 2024 09:43:06 +1000 Subject: [PATCH 2/4] add tagsetds --- terraform/21a-tagsetds/config.tf | 7 +++++++ terraform/21a-tagsetds/provider.tf | 5 +++++ terraform/21a-tagsetds/provider_vars.tf | 18 ++++++++++++++++++ terraform/21a-tagsetds/space.tf | 3 +++ terraform/21a-tagsetds/tagset.tf | 13 +++++++++++++ 5 files changed, 46 insertions(+) create mode 100644 terraform/21a-tagsetds/config.tf create mode 100644 terraform/21a-tagsetds/provider.tf create mode 100644 terraform/21a-tagsetds/provider_vars.tf create mode 100644 terraform/21a-tagsetds/space.tf create mode 100644 terraform/21a-tagsetds/tagset.tf diff --git a/terraform/21a-tagsetds/config.tf b/terraform/21a-tagsetds/config.tf new file mode 100644 index 000000000..fc2ebf4b0 --- /dev/null +++ b/terraform/21a-tagsetds/config.tf @@ -0,0 +1,7 @@ +terraform { + required_providers { + octopusdeploy = { source = "OctopusDeployLabs/octopusdeploy", version = "0.14.3" } + // Use the option below when debugging + // octopusdeploy = { source = "octopus.com/com/octopusdeploy" } + } +} diff --git a/terraform/21a-tagsetds/provider.tf b/terraform/21a-tagsetds/provider.tf new file mode 100644 index 000000000..a04197720 --- /dev/null +++ b/terraform/21a-tagsetds/provider.tf @@ -0,0 +1,5 @@ +provider "octopusdeploy" { + address = "${var.octopus_server}" + api_key = "${var.octopus_apikey}" + space_id = "${var.octopus_space_id}" +} diff --git a/terraform/21a-tagsetds/provider_vars.tf b/terraform/21a-tagsetds/provider_vars.tf new file mode 100644 index 000000000..c7d93fe40 --- /dev/null +++ b/terraform/21a-tagsetds/provider_vars.tf @@ -0,0 +1,18 @@ +variable "octopus_server" { + type = string + nullable = false + sensitive = false + description = "The URL of the Octopus server e.g. https://myinstance.octopus.app." +} +variable "octopus_apikey" { + type = string + nullable = false + sensitive = true + description = "The API key used to access the Octopus server. See https://octopus.com/docs/octopus-rest-api/how-to-create-an-api-key for details on creating an API key." +} +variable "octopus_space_id" { + type = string + nullable = false + sensitive = false + description = "The space ID to populate" +} diff --git a/terraform/21a-tagsetds/space.tf b/terraform/21a-tagsetds/space.tf new file mode 100644 index 000000000..ee59bdc80 --- /dev/null +++ b/terraform/21a-tagsetds/space.tf @@ -0,0 +1,3 @@ +output "octopus_space_id" { + value = var.octopus_space_id +} diff --git a/terraform/21a-tagsetds/tagset.tf b/terraform/21a-tagsetds/tagset.tf new file mode 100644 index 000000000..5c6d4cb2b --- /dev/null +++ b/terraform/21a-tagsetds/tagset.tf @@ -0,0 +1,13 @@ +data "octopusdeploy_tag_sets" "data_lookup" { + partial_name = "Test tagset" + skip = 0 + take = 1 +} + +output "data_lookup" { + value = data.octopusdeploy_tag_sets.data_lookup.tag_sets[0].id +} + +output "tags" { + value = data.octopusdeploy_tag_sets.data_lookup.tag_sets[0].tags +} \ No newline at end of file From aa88b5f605db9ae1985a8c39b441377767b9f18b Mon Sep 17 00:00:00 2001 From: Hans Song Date: Fri, 5 Jan 2024 00:03:47 +1000 Subject: [PATCH 3/4] add terraform to tagset integration test --- integration_test.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/integration_test.go b/integration_test.go index 9786625ec..8d2a6925f 100644 --- a/integration_test.go +++ b/integration_test.go @@ -1508,6 +1508,12 @@ func TestTagSetResource(t *testing.T) { return err } + err = testFramework.TerraformInitAndApply(t, container, filepath.Join("./terraform", "21-tagset"), newSpaceId, []string{}) + + if err != nil { + return err + } + // Assert client, err := octoclient.CreateClient(container.URI, newSpaceId, test.ApiKey) query := tagsets.TagSetsQuery{ @@ -1557,6 +1563,17 @@ func TestTagSetResource(t *testing.T) { t.Fatal("Tag Set must have an tag called \"a\"") } + // Verify the environment data lookups work + lookup, err := testFramework.GetOutputVariable(t, filepath.Join("terraform", "21-tagset"), "data_lookup") + + if err != nil { + return err + } + + if lookup != resource.ID { + t.Fatal("The environment lookup did not succeed. Lookup value was \"" + lookup + "\" while the resource value was \"" + resource.ID + "\".") + } + return nil }) } From 45ecf0ccc6cf308db51f7007afb762c1a8dd4e16 Mon Sep 17 00:00:00 2001 From: Hans Song Date: Fri, 5 Jan 2024 00:06:19 +1000 Subject: [PATCH 4/4] update terraform mock path --- integration_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/integration_test.go b/integration_test.go index 8d2a6925f..272fc04c8 100644 --- a/integration_test.go +++ b/integration_test.go @@ -1508,7 +1508,7 @@ func TestTagSetResource(t *testing.T) { return err } - err = testFramework.TerraformInitAndApply(t, container, filepath.Join("./terraform", "21-tagset"), newSpaceId, []string{}) + err = testFramework.TerraformInitAndApply(t, container, filepath.Join("./terraform", "21a-tagsetds"), newSpaceId, []string{}) if err != nil { return err @@ -1564,7 +1564,7 @@ func TestTagSetResource(t *testing.T) { } // Verify the environment data lookups work - lookup, err := testFramework.GetOutputVariable(t, filepath.Join("terraform", "21-tagset"), "data_lookup") + lookup, err := testFramework.GetOutputVariable(t, filepath.Join("terraform", "21a-tagsetds"), "data_lookup") if err != nil { return err