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

Add tagging support for AMP workspaces #23202

Merged
merged 11 commits into from
Feb 15, 2022
Next Next commit
inits AMP workspace tagging
mhausenblas committed Feb 14, 2022

Verified

This commit was signed with the committer’s verified signature.
messense messense
commit 2c603d7d6cfd1dd07a009ecf99b11b9af22b1d77
4 changes: 4 additions & 0 deletions internal/service/amp/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//go:generate go run ../../generate/tags/main.go -ListTags -ListTagsInIDElem=ResourceArn -ServiceTagsMap -TagInIDElem=ResourceArn -UpdateTags
// ONLY generate directives and package declaration! Do not add anything else to this file.

package amp
75 changes: 75 additions & 0 deletions internal/service/amp/tags_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions internal/service/amp/workspace.go
Original file line number Diff line number Diff line change
@@ -10,7 +10,9 @@ import (
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/hashicorp/terraform-provider-aws/internal/conns"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
)

func ResourceWorkspace() *schema.Resource {
@@ -36,6 +38,8 @@ func ResourceWorkspace() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
},
}
}
@@ -44,6 +48,9 @@ func resourceWorkspaceRead(ctx context.Context, d *schema.ResourceData, meta int
log.Printf("[INFO] Reading AMP workspace %s", d.Id())
conn := meta.(*conns.AWSClient).AMPConn

defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

details, err := conn.DescribeWorkspaceWithContext(ctx, &prometheusservice.DescribeWorkspaceInput{
WorkspaceId: aws.String(d.Id()),
})
@@ -67,6 +74,22 @@ func resourceWorkspaceRead(ctx context.Context, d *schema.ResourceData, meta int
d.Set("arn", ws.Arn)
d.Set("prometheus_endpoint", ws.PrometheusEndpoint)

tags, err := ListTags(conn, *ws.Arn)

if err != nil {
return diag.FromErr(fmt.Errorf("error listing tags for Prometheus Workspace (%s): %s", d.Id(), err))
}

tags = tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig)

//lintignore:AWSR002
if err := d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil {
return diag.FromErr(fmt.Errorf("error setting tags: %w", err))
}

if err := d.Set("tags_all", tags.Map()); err != nil {
return diag.FromErr(fmt.Errorf("error setting tags_all: %w", err))
}
return nil
}

@@ -80,6 +103,22 @@ func resourceWorkspaceUpdate(ctx context.Context, d *schema.ResourceData, meta i
req.Alias = aws.String(v.(string))
}
conn := meta.(*conns.AWSClient).AMPConn

if d.HasChange("tags") {
o, n := d.GetChange("tags")
log.Printf("[DEBUG] Updating tags")
details, err := conn.DescribeWorkspaceWithContext(ctx, &prometheusservice.DescribeWorkspaceInput{
WorkspaceId: aws.String(d.Id()),
})
if err != nil {
return diag.FromErr(fmt.Errorf("error reading Prometheus Workspace (%s): %s", d.Id(), err))
}
ws := details.Workspace
if err := UpdateTags(conn, *ws.Arn, o, n); err != nil {
return diag.FromErr(fmt.Errorf("error updating Prometheus WorkSpace (%s) tags: %s", d.Id(), err))
}
}

if _, err := conn.UpdateWorkspaceAliasWithContext(ctx, req); err != nil {
return diag.FromErr(fmt.Errorf("error updating Prometheus WorkSpace (%s): %w", d.Id(), err))
}
@@ -90,12 +129,18 @@ func resourceWorkspaceUpdate(ctx context.Context, d *schema.ResourceData, meta i
func resourceWorkspaceCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
log.Printf("[INFO] Creating AMP workspace %s", d.Id())
conn := meta.(*conns.AWSClient).AMPConn
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(tftags.New(d.Get("tags").(map[string]interface{})))

req := &prometheusservice.CreateWorkspaceInput{}
if v, ok := d.GetOk("alias"); ok {
req.Alias = aws.String(v.(string))
}

if len(tags) > 0 {
req.Tags = Tags(tags.IgnoreAWS())
}

result, err := conn.CreateWorkspaceWithContext(ctx, req)
if err != nil {
return diag.FromErr(fmt.Errorf("error creating Prometheus WorkSpace (%s): %w", d.Id(), err))
14 changes: 14 additions & 0 deletions internal/service/amp/workspace_test.go
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ import (
sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
tfamp "github.com/hashicorp/terraform-provider-aws/internal/service/amp"
@@ -30,6 +31,7 @@ func TestAccAMPWorkspace_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckAMPWorkspaceExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "alias", workspaceAlias),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
),
},
{
@@ -139,3 +141,15 @@ resource "aws_prometheus_workspace" "test" {
}
`
}
func testAccAMPWorkspaceWithTagsConfig() string {
return `
resource "aws_prometheus_workspace" "tf-tag" {
alias = "tf-tag"

tags = {
Environment = "prod"
Owner = "mh9"
}
}
`
}