Skip to content

Commit

Permalink
Updated tag and tag set implementations (and documentation)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbristowe committed Dec 11, 2020
1 parent d4a0e55 commit d51e93a
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 25 deletions.
9 changes: 6 additions & 3 deletions docs/data-sources/tag_sets.md
Expand Up @@ -30,12 +30,15 @@ Provides information about existing tag sets.

Read-only:

- **description** (String, Read-only) The description of this resource.
- **id** (String, Read-only) The unique ID for this resource.
- **name** (String, Read-only) The name of this resource.
- **tags** (List of Object, Read-only) (see [below for nested schema](#nestedatt--tag_set--tags))
- **sort_order** (Number, Read-only) The sort order associated with this resource.
- **space_id** (String, Read-only) The space ID associated with this resource.
- **tag** (List of Object, Read-only) A list of tags. (see [below for nested schema](#nestedatt--tag_set--tag))

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

- **canonical_tag_name** (String)
- **color** (String)
Expand Down
9 changes: 6 additions & 3 deletions docs/resources/tag_set.md
Expand Up @@ -19,11 +19,14 @@ This resource manages tag sets in Octopus Deploy.

### Optional

- **description** (String, Optional) The description of this resource.
- **id** (String, Optional) The unique ID for this resource.
- **tags** (Block List) (see [below for nested schema](#nestedblock--tags))
- **sort_order** (Number, Optional) The sort order associated with this resource.
- **space_id** (String, Optional) The space ID associated with this resource.
- **tag** (Block List) A list of tags. (see [below for nested schema](#nestedblock--tag))

<a id="nestedblock--tags"></a>
### Nested Schema for `tags`
<a id="nestedblock--tag"></a>
### Nested Schema for `tag`

Required:

Expand Down
14 changes: 14 additions & 0 deletions examples/resources/octopusdeploy_tagset/resource.tf
@@ -0,0 +1,14 @@
resource "octopusdeploy_tag_set" "example" {
description = "Provides tenants with access to certain early access programs."
name = "Early Access Program (EAP)"

tag {
color = "#00FF00"
name = "Alpha"
}

tag {
color = "#FF0000"
name = "Beta"
}
}
6 changes: 4 additions & 2 deletions octopusdeploy/resource_aws_account.go
Expand Up @@ -76,11 +76,13 @@ func resourceAmazonWebServicesAccountRead(ctx context.Context, d *schema.Resourc
return diag.FromErr(err)
}

if err := setAmazonWebServicesAccount(ctx, d, accountResource.(*octopusdeploy.AmazonWebServicesAccount)); err != nil {
amazonWebServicesAccount := accountResource.(*octopusdeploy.AmazonWebServicesAccount)

if err := setAmazonWebServicesAccount(ctx, d, amazonWebServicesAccount); err != nil {
return diag.FromErr(err)
}

log.Printf("[INFO] AWS account read: %#v", accountResource.(*octopusdeploy.AmazonWebServicesAccount))
log.Printf("[INFO] AWS account read: %#v", amazonWebServicesAccount)
return nil
}

Expand Down
30 changes: 23 additions & 7 deletions octopusdeploy/resource_tag_set.go
Expand Up @@ -2,6 +2,7 @@ package octopusdeploy

import (
"context"
"log"

"github.com/OctopusDeploy/go-octopusdeploy/octopusdeploy"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand All @@ -23,6 +24,8 @@ func resourceTagSet() *schema.Resource {
func resourceTagSetCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
tagSet := expandTagSet(d)

log.Printf("[INFO] creating tag set: %#v", tagSet)

client := m.(*octopusdeploy.Client)
tagSet, err := client.TagSets.Add(tagSet)
if err != nil {
Expand All @@ -31,49 +34,62 @@ func resourceTagSetCreate(ctx context.Context, d *schema.ResourceData, m interfa

d.SetId(tagSet.GetID())

log.Printf("[INFO] tag set created (%s)", d.Id())
return nil
}

func resourceTagSetDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Printf("[INFO] deleting tag set (%s)", d.Id())

client := m.(*octopusdeploy.Client)
err := client.TagSets.DeleteByID(d.Id())
if err != nil {
if err := client.TagSets.DeleteByID(d.Id()); err != nil {
return diag.FromErr(err)
}

d.SetId("")

log.Printf("[INFO] tag set deleted")
return nil
}

func resourceTagSetRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Printf("[INFO] reading tag set (%s)", d.Id())

client := m.(*octopusdeploy.Client)
tagSet, err := client.TagSets.GetByID(d.Id())
if err != nil {
apiError := err.(*octopusdeploy.APIError)
if apiError.StatusCode == 404 {
log.Printf("[INFO] tag set (%s) not found; deleting from state", d.Id())
d.SetId("")
return nil
}
return diag.FromErr(err)
}

d.Set("name", tagSet.Name)
d.SetId(tagSet.GetID())
if err := setTagSet(ctx, d, tagSet); err != nil {
return diag.FromErr(err)
}

log.Printf("[INFO] tag set read: %#v", tagSet)
return nil
}

func resourceTagSetUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
tagSet := expandTagSet(d)
tagSet.ID = d.Id()

log.Printf("[INFO] updating tag set: %#v", tagSet)

client := m.(*octopusdeploy.Client)
resource, err := client.TagSets.Update(tagSet)
updatedTagSet, err := client.TagSets.Update(tagSet)
if err != nil {
return diag.FromErr(err)
}

d.SetId(resource.GetID())
if err := setTagSet(ctx, d, updatedTagSet); err != nil {
return diag.FromErr(err)
}

log.Printf("[INFO] tag set updated (%s)", d.Id())
return nil
}
1 change: 1 addition & 0 deletions octopusdeploy/schema_tag.go
Expand Up @@ -10,6 +10,7 @@ func expandTag(tag map[string]interface{}) octopusdeploy.Tag {
CanonicalTagName: tag["canonical_tag_name"].(string),
Color: tag["color"].(string),
Description: tag["description"].(string),
ID: tag["id"].(string),
Name: tag["name"].(string),
SortOrder: tag["sort_order"].(int),
}
Expand Down
57 changes: 47 additions & 10 deletions octopusdeploy/schema_tag_set.go
@@ -1,6 +1,9 @@
package octopusdeploy

import (
"context"
"fmt"

"github.com/OctopusDeploy/go-octopusdeploy/octopusdeploy"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand All @@ -9,8 +12,21 @@ func expandTagSet(d *schema.ResourceData) *octopusdeploy.TagSet {
name := d.Get("name").(string)

var tagSet = octopusdeploy.NewTagSet(name)
tagSet.ID = d.Id()

if v, ok := d.GetOk("description"); ok {
tagSet.Description = v.(string)
}

if v, ok := d.GetOk("sort_order"); ok {
tagSet.SortOrder = int32(v.(int))
}

if v, ok := d.GetOk("space_id"); ok {
tagSet.SpaceID = v.(string)
}

if v, ok := d.GetOk("tags"); ok {
if v, ok := d.GetOk("tag"); ok {
tags := v.([]interface{})
for _, t := range tags {
tag := expandTag(t.(map[string]interface{}))
Expand All @@ -27,9 +43,12 @@ func flattenTagSet(tagSet *octopusdeploy.TagSet) map[string]interface{} {
}

return map[string]interface{}{
"id": tagSet.GetID(),
"name": tagSet.Name,
"tags": flattenTags(tagSet.Tags),
"description": tagSet.Description,
"id": tagSet.GetID(),
"name": tagSet.Name,
"sort_order": tagSet.SortOrder,
"space_id": tagSet.SpaceID,
"tag": flattenTags(tagSet.Tags),
}
}

Expand All @@ -54,12 +73,30 @@ func getTagSetDataSchema() map[string]*schema.Schema {

func getTagSetSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"id": getIDSchema(),
"name": getNameSchema(true),
"tags": {
Elem: &schema.Resource{Schema: getTagsSchema()},
Optional: true,
Type: schema.TypeList,
"description": getDescriptionSchema(),
"id": getIDSchema(),
"name": getNameSchema(true),
"sort_order": getSortOrderSchema(),
"space_id": getSpaceIDSchema(),
"tag": {
Description: "A list of tags.",
Elem: &schema.Resource{Schema: getTagsSchema()},
Optional: true,
Type: schema.TypeList,
},
}
}

func setTagSet(ctx context.Context, d *schema.ResourceData, tagSet *octopusdeploy.TagSet) error {
d.Set("description", tagSet.Description)
d.Set("id", tagSet.GetID())
d.Set("name", tagSet.Name)
d.Set("sort_order", tagSet.SortOrder)
d.Set("space_id", tagSet.SpaceID)

if err := d.Set("tag", flattenTags(tagSet.Tags)); err != nil {
return fmt.Errorf("error setting tag: %s", err)
}

return nil
}

0 comments on commit d51e93a

Please sign in to comment.