diff --git a/docs/data-sources/tag_sets.md b/docs/data-sources/tag_sets.md index 8061cfb1c..454f1f2bf 100644 --- a/docs/data-sources/tag_sets.md +++ b/docs/data-sources/tag_sets.md @@ -38,3 +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/integration_test.go b/integration_test.go index 9786625ec..272fc04c8 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", "21a-tagsetds"), 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", "21a-tagsetds"), "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 }) } 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{ 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