Skip to content

Commit

Permalink
Merge pull request #34998 from hashicorp/f-finspace_kx_dataview-tags
Browse files Browse the repository at this point in the history
r/aws_finspace_kx_dataview: properly set arn, enabling tagging
  • Loading branch information
jar-b authored Dec 21, 2023
2 parents 217d2ce + f487eae commit 68c4405
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .changelog/34998.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_finspace_kx_dataview: Properly set `arn` attribute on read, resolving persistent differences when `tags` are configured
```
33 changes: 26 additions & 7 deletions internal/service/finspace/kx_dataview.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ package finspace
import (
"context"
"errors"
"fmt"
"log"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/arn"
"github.com/aws/aws-sdk-go-v2/service/finspace"
"github.com/aws/aws-sdk-go-v2/service/finspace/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -140,22 +142,25 @@ func resourceKxDataviewCreate(ctx context.Context, d *schema.ResourceData, meta
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).FinSpaceClient(ctx)

environmentID := d.Get("environment_id").(string)
databaseName := d.Get("database_name").(string)
name := d.Get("name").(string)

idParts := []string{
d.Get("environment_id").(string),
d.Get("database_name").(string),
d.Get("name").(string),
environmentID,
databaseName,
name,
}

rId, err := flex.FlattenResourceId(idParts, kxDataviewIdPartCount, false)
if err != nil {
return create.AppendDiagError(diags, names.FinSpace, create.ErrActionFlatteningResourceId, ResNameKxDataview, d.Get("name").(string), err)
}
d.SetId(rId)

in := &finspace.CreateKxDataviewInput{
DatabaseName: aws.String(d.Get("database_name").(string)),
DataviewName: aws.String(d.Get("name").(string)),
EnvironmentId: aws.String(d.Get("environment_id").(string)),
DatabaseName: aws.String(databaseName),
DataviewName: aws.String(name),
EnvironmentId: aws.String(environmentID),
AutoUpdate: d.Get("auto_update").(bool),
AzMode: types.KxAzMode(d.Get("az_mode").(string)),
ClientToken: aws.String(id.UniqueId()),
Expand Down Expand Up @@ -185,6 +190,7 @@ func resourceKxDataviewCreate(ctx context.Context, d *schema.ResourceData, meta
if out == nil || out.DataviewName == nil {
return create.AppendDiagError(diags, names.FinSpace, create.ErrActionCreating, ResNameKxDataview, d.Get("name").(string), errors.New("empty output"))
}

if _, err := waitKxDataviewCreated(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil {
return create.AppendDiagError(diags, names.FinSpace, create.ErrActionWaitingForCreation, ResNameKxDataview, d.Get("name").(string), err)
}
Expand Down Expand Up @@ -221,6 +227,19 @@ func resourceKxDataviewRead(ctx context.Context, d *schema.ResourceData, meta in
return create.AppendDiagError(diags, names.FinSpace, create.ErrActionReading, ResNameKxDataview, d.Id(), err)
}

// Manually construct the dataview ARN, which is not returned from the
// Create or Describe APIs.
//
// Ref: https://docs.aws.amazon.com/service-authorization/latest/reference/list_amazonfinspace.html#amazonfinspace-resources-for-iam-policies
dataviewARN := arn.ARN{
Partition: meta.(*conns.AWSClient).Partition,
Region: meta.(*conns.AWSClient).Region,
Service: names.FinSpace,
AccountID: meta.(*conns.AWSClient).AccountID,
Resource: fmt.Sprintf("kxEnvironment/%s/kxDatabase/%s/kxDataview/%s", aws.ToString(out.EnvironmentId), aws.ToString(out.DatabaseName), aws.ToString(out.DataviewName)),
}.String()
d.Set("arn", dataviewARN)

return diags
}

Expand Down
95 changes: 95 additions & 0 deletions internal/service/finspace/kx_dataview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,62 @@ func TestAccFinSpaceKxDataview_disappears(t *testing.T) {
})
}

func TestAccFinSpaceKxDataview_tags(t *testing.T) {
if testing.Short() {
t.Skip("Skipping test in short mode.")
}

ctx := acctest.Context(t)
var dataview finspace.GetKxDataviewOutput
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_finspace_kx_dataview.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acctest.PreCheck(ctx, t)
acctest.PreCheckPartitionHasService(t, finspace.ServiceID)
},
ErrorCheck: acctest.ErrorCheck(t, finspace.ServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckKxDataviewDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccKxDataviewConfig_tags1(rName, "key1", "value1"),
Check: resource.ComposeTestCheckFunc(
testAccCheckKxDataviewExists(ctx, resourceName, &dataview),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccKxDataviewConfig_tags2(rName, "key1", "value1updated", "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckKxDataviewExists(ctx, resourceName, &dataview),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1updated"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
{
Config: testAccKxDataviewConfig_tags1(rName, "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckKxDataviewExists(ctx, resourceName, &dataview),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
},
})
}

func TestAccFinSpaceKxDataview_withKxVolume(t *testing.T) {
if testing.Short() {
t.Skip("skipping long-running test in short mode")
Expand Down Expand Up @@ -201,6 +257,45 @@ resource "aws_finspace_kx_dataview" "test" {
`, rName))
}

func testAccKxDataviewConfig_tags1(rName, key1, value1 string) string {
return acctest.ConfigCompose(
testAccKxDataviewConfigBase(rName),
fmt.Sprintf(`
resource "aws_finspace_kx_dataview" "test" {
name = %[1]q
environment_id = aws_finspace_kx_environment.test.id
database_name = aws_finspace_kx_database.test.name
auto_update = true
az_mode = "SINGLE"
availability_zone_id = aws_finspace_kx_environment.test.availability_zones[0]
tags = {
%[2]q = %[3]q
}
}
`, rName, key1, value1))
}

func testAccKxDataviewConfig_tags2(rName, key1, value1, key2, value2 string) string {
return acctest.ConfigCompose(
testAccKxDataviewConfigBase(rName),
fmt.Sprintf(`
resource "aws_finspace_kx_dataview" "test" {
name = %[1]q
environment_id = aws_finspace_kx_environment.test.id
database_name = aws_finspace_kx_database.test.name
auto_update = true
az_mode = "SINGLE"
availability_zone_id = aws_finspace_kx_environment.test.availability_zones[0]
tags = {
%[2]q = %[3]q
%[4]q = %[5]q
}
}
`, rName, key1, value1, key2, value2))
}

func testAccKxDataviewConfig_withKxVolume(rName string) string {
return acctest.ConfigCompose(
testAccKxDataviewConfigBase(rName),
Expand Down
1 change: 1 addition & 0 deletions website/docs/index.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ In addition to [generic `provider` arguments](https://www.terraform.io/docs/conf
- [`aws_elasticache_cluster` resource](/docs/providers/aws/r/elasticache_cluster.html)
- [`aws_elb` data source](/docs/providers/aws/d/elb.html)
- [`aws_elb` resource](/docs/providers/aws/r/elb.html)
- [`aws_finspace_kx_dataview` resource](/docs/providers/aws/r/finspace_kx_dataview.html)
- [`aws_flow_log` resource](/docs/providers/aws/r/flow_log.html)
- [`aws_glue_catalog_database` resource](/docs/providers/aws/r/glue_catalog_database.html)
- [`aws_glue_catalog_table` resource](/docs/providers/aws/r/glue_catalog_table.html)
Expand Down

0 comments on commit 68c4405

Please sign in to comment.