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
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
Expand Up @@ -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 {
Expand All @@ -36,6 +38,8 @@ func ResourceWorkspace() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
},
}
}
Expand All @@ -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()),
})
Expand All @@ -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
}

Expand All @@ -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))
}
Expand All @@ -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))
Expand Down
100 changes: 100 additions & 0 deletions internal/service/amp/workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
"github.com/aws/aws-sdk-go/service/prometheusservice"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
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"
Expand Down Expand Up @@ -74,6 +76,63 @@ func TestAccAMPWorkspace_disappears(t *testing.T) {
})
}

func TestAccAMPWorkspace_tagging(t *testing.T) {
rInt := sdkacctest.RandInt()
resourceName := "aws_prometheus_workspace.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, cloudwatchlogs.EndpointsID),
Providers: acctest.Providers,
CheckDestroy: testAccCheckAMPWorkspaceDestroy,
Steps: []resource.TestStep{
{
Config: testAccAMPWorkspaceWithTagsConfig(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckAMPWorkspaceExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.Environment", "production"),
resource.TestCheckResourceAttr(resourceName, "tags.Owner", "mh9"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAMPWorkspaceWithTagsAddedConfig(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckAMPWorkspaceExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "3"),
resource.TestCheckResourceAttr(resourceName, "tags.Environment", "production"),
resource.TestCheckResourceAttr(resourceName, "tags.Owner", "mh9"),
resource.TestCheckResourceAttr(resourceName, "tags.App", "foo42"),
),
},
{
Config: testAccAMPWorkspaceWithTagsUpdatedConfig(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckAMPWorkspaceExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "3"),
resource.TestCheckResourceAttr(resourceName, "tags.Environment", "production"),
resource.TestCheckResourceAttr(resourceName, "tags.Owner", "abhi"),
resource.TestCheckResourceAttr(resourceName, "tags.App", "foo42"),
),
},
{
Config: testAccAMPWorkspaceWithTagsConfig(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckAMPWorkspaceExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.Environment", "production"),
resource.TestCheckResourceAttr(resourceName, "tags.Owner", "mh9"),
),
},
},
})
}

func testAccCheckAMPWorkspaceExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -139,3 +198,44 @@ resource "aws_prometheus_workspace" "test" {
}
`
}

func testAccAMPWorkspaceWithTagsConfig(randInt int) string {
return fmt.Sprintf(`
resource "aws_prometheus_workspace" "test" {
alias = "test-%d"

tags = {
Environment = "production"
Owner = "mh9"
}
}
`, randInt)
}

func testAccAMPWorkspaceWithTagsAddedConfig(randInt int) string {
return fmt.Sprintf(`
resource "aws_prometheus_workspace" "test" {
alias = "test-%d"

tags = {
Environment = "production"
Owner = "mh9"
App = "foo42"
}
}
`, randInt)
}

func testAccAMPWorkspaceWithTagsUpdatedConfig(randInt int) string {
return fmt.Sprintf(`
resource "aws_prometheus_workspace" "test" {
alias = "test-%d"

tags = {
Environment = "production"
Owner = "abhi"
App = "foo42"
}
}
`, randInt)
}
9 changes: 8 additions & 1 deletion website/docs/r/prometheus_workspace.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ Manages an Amazon Managed Service for Prometheus (AMP) Workspace.
```terraform
resource "aws_prometheus_workspace" "demo" {
alias = "prometheus-test"

tags = {
Environment = "production"
Owner = "abhi"
}
}
```

Expand All @@ -25,6 +30,7 @@ resource "aws_prometheus_workspace" "demo" {
The following argument is supported:

* `alias` - (Optional) The alias of the prometheus workspace. See more [in AWS Docs](https://docs.aws.amazon.com/prometheus/latest/userguide/AMP-onboard-create-workspace.html).
* `tags` - (Optional) A map of tags to assign to the resource. If configured with a provider [`default_tags` configuration block](/docs/providers/aws/index.html#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level.

## Attributes Reference

Expand All @@ -33,11 +39,12 @@ In addition to all arguments above, the following attributes are exported:
* `arn` - Amazon Resource Name (ARN) of the workspace.
* `id` - Identifier of the workspace
* `prometheus_endpoint` - Prometheus endpoint available for this workspace.
* `tags_all` - A map of tags assigned to the resource, including those inherited from the provider [`default_tags` configuration block](/docs/providers/aws/index.html#default_tags-configuration-block).

## Import

AMP Workspaces can be imported using the identifier, e.g.,

```
$ terraform import aws_prometheus_workspace.demo ws-C6DCB907-F2D7-4D96-957B-66691F865D8B
```
```