Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose tags in tagset data source #591

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/data-sources/tag_sets.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))

<a id="nestedatt--tag_sets--tags"></a>
### Nested Schema for `tag_sets.tags`

Read-Only:

- `canonical_tag_name` (String)
- `color` (String)
- `description` (String)
- `name` (String)
- `sort_order` (Number)


17 changes: 17 additions & 0 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
})
}
Expand Down
11 changes: 11 additions & 0 deletions octopusdeploy/schema_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 26 additions & 0 deletions octopusdeploy/schema_tag_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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{
Expand Down
7 changes: 7 additions & 0 deletions terraform/21a-tagsetds/config.tf
Original file line number Diff line number Diff line change
@@ -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" }
}
}
5 changes: 5 additions & 0 deletions terraform/21a-tagsetds/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
provider "octopusdeploy" {
address = "${var.octopus_server}"
api_key = "${var.octopus_apikey}"
space_id = "${var.octopus_space_id}"
}
18 changes: 18 additions & 0 deletions terraform/21a-tagsetds/provider_vars.tf
Original file line number Diff line number Diff line change
@@ -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"
}
3 changes: 3 additions & 0 deletions terraform/21a-tagsetds/space.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "octopus_space_id" {
value = var.octopus_space_id
}
13 changes: 13 additions & 0 deletions terraform/21a-tagsetds/tagset.tf
Original file line number Diff line number Diff line change
@@ -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
}