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

r/aws_redshiftserverless_namespace - fix user/pass update #28125

Merged
merged 3 commits into from
Dec 1, 2022
Merged
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
3 changes: 3 additions & 0 deletions .changelog/28125.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_redshiftserverless_namespace: Fix updating `admin_username` and `admin_user_password`
```
95 changes: 47 additions & 48 deletions internal/service/redshiftserverless/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ func ResourceNamespace() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"admin_user_password": {
Type: schema.TypeString,
Optional: true,
Expand All @@ -44,6 +40,10 @@ func ResourceNamespace() *schema.Resource {
Sensitive: true,
Computed: true,
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
"db_name": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -100,8 +100,10 @@ func resourceNamespaceCreate(d *schema.ResourceData, meta interface{}) error {
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(tftags.New(d.Get("tags").(map[string]interface{})))

input := redshiftserverless.CreateNamespaceInput{
NamespaceName: aws.String(d.Get("namespace_name").(string)),
name := d.Get("namespace_name").(string)
input := &redshiftserverless.CreateNamespaceInput{
NamespaceName: aws.String(name),
Tags: Tags(tags.IgnoreAWS()),
}

if v, ok := d.GetOk("admin_user_password"); ok {
Expand All @@ -120,27 +122,25 @@ func resourceNamespaceCreate(d *schema.ResourceData, meta interface{}) error {
input.DefaultIamRoleArn = aws.String(v.(string))
}

if v, ok := d.GetOk("kms_key_id"); ok {
input.KmsKeyId = aws.String(v.(string))
}

if v, ok := d.GetOk("iam_roles"); ok && v.(*schema.Set).Len() > 0 {
input.IamRoles = flex.ExpandStringSet(v.(*schema.Set))
}

if v, ok := d.GetOk("kms_key_id"); ok {
input.KmsKeyId = aws.String(v.(string))
}

if v, ok := d.GetOk("log_exports"); ok && v.(*schema.Set).Len() > 0 {
input.LogExports = flex.ExpandStringSet(v.(*schema.Set))
}

input.Tags = Tags(tags.IgnoreAWS())

out, err := conn.CreateNamespace(&input)
output, err := conn.CreateNamespace(input)

if err != nil {
return fmt.Errorf("error creating Redshift Serverless Namespace : %w", err)
return fmt.Errorf("creating Redshift Serverless Namespace (%s): %w", name, err)
}

d.SetId(aws.StringValue(out.Namespace.NamespaceName))
d.SetId(aws.StringValue(output.Namespace.NamespaceName))

return resourceNamespaceRead(d, meta)
}
Expand All @@ -150,47 +150,48 @@ func resourceNamespaceRead(d *schema.ResourceData, meta interface{}) error {
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

out, err := FindNamespaceByName(conn, d.Id())
output, err := FindNamespaceByName(conn, d.Id())

if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] Redshift Serverless Namespace (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
return fmt.Errorf("error reading Redshift Serverless Namespace (%s): %w", d.Id(), err)
return fmt.Errorf("reading Redshift Serverless Namespace (%s): %w", d.Id(), err)
}

arn := aws.StringValue(out.NamespaceArn)
arn := aws.StringValue(output.NamespaceArn)
d.Set("admin_username", output.AdminUsername)
d.Set("arn", arn)
d.Set("namespace_name", out.NamespaceName)
d.Set("namespace_id", out.NamespaceId)
d.Set("kms_key_id", out.KmsKeyId)
d.Set("admin_username", out.AdminUsername)
d.Set("db_name", out.DbName)
d.Set("default_iam_role_arn", out.DefaultIamRoleArn)
d.Set("log_exports", flex.FlattenStringSet(out.LogExports))
d.Set("iam_roles", flex.FlattenStringSet(out.IamRoles))
d.Set("db_name", output.DbName)
d.Set("default_iam_role_arn", output.DefaultIamRoleArn)
d.Set("iam_roles", aws.StringValueSlice(output.IamRoles))
d.Set("kms_key_id", output.KmsKeyId)
d.Set("log_exports", aws.StringValueSlice(output.LogExports))
d.Set("namespace_id", output.NamespaceId)
d.Set("namespace_name", output.NamespaceName)

tags, err := ListTags(conn, arn)

if err != nil {
if tfawserr.ErrCodeEquals(err, "UnknownOperationException") {
return nil
}
if tfawserr.ErrCodeEquals(err, "UnknownOperationException") {
return nil
}

return fmt.Errorf("error listing tags for edshift Serverless Namespace (%s): %w", arn, err)
if err != nil {
return fmt.Errorf("listing tags for Redshift Serverless Namespace (%s): %w", arn, err)
}

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

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

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

return nil
Expand All @@ -204,45 +205,43 @@ func resourceNamespaceUpdate(d *schema.ResourceData, meta interface{}) error {
NamespaceName: aws.String(d.Id()),
}

if d.HasChange("kms_key_id") {
input.KmsKeyId = aws.String(d.Get("kms_key_id").(string))
if d.HasChanges("admin_username", "admin_user_password") {
input.AdminUsername = aws.String(d.Get("admin_username").(string))
input.AdminUserPassword = aws.String(d.Get("admin_user_password").(string))
}

if d.HasChange("default_iam_role_arn") {
input.DefaultIamRoleArn = aws.String(d.Get("default_iam_role_arn").(string))
}

if d.HasChange("admin_username") {
input.AdminUserPassword = aws.String(d.Get("admin_username").(string))
if d.HasChange("iam_roles") {
input.IamRoles = flex.ExpandStringSet(d.Get("iam_roles").(*schema.Set))
}

if d.HasChange("admin_user_password") {
input.AdminUsername = aws.String(d.Get("admin_user_password").(string))
if d.HasChange("kms_key_id") {
input.KmsKeyId = aws.String(d.Get("kms_key_id").(string))
}

if d.HasChange("log_exports") {
input.LogExports = flex.ExpandStringSet(d.Get("log_exports").(*schema.Set))
}

if d.HasChange("iam_roles") {
input.IamRoles = flex.ExpandStringSet(d.Get("iam_roles").(*schema.Set))
}

_, err := conn.UpdateNamespace(input)

if err != nil {
return fmt.Errorf("error updating Redshift Serverless Namespace (%s): %w", d.Id(), err)
return fmt.Errorf("updating Redshift Serverless Namespace (%s): %w", d.Id(), err)
}

if _, err := waitNamespaceUpdated(conn, d.Id()); err != nil {
return fmt.Errorf("error waiting for Redshift Serverless Namespace (%s) to be updated: %w", d.Id(), err)
return fmt.Errorf("waiting for Redshift Serverless Namespace (%s) update: %w", d.Id(), err)
}
}

if d.HasChange("tags_all") {
o, n := d.GetChange("tags_all")

if err := UpdateTags(conn, d.Get("arn").(string), o, n); err != nil {
return fmt.Errorf("error updating Redshift Serverless Namespace (%s) tags: %w", d.Get("arn").(string), err)
return fmt.Errorf("updating Redshift Serverless Namespace (%s) tags: %w", d.Get("arn").(string), err)
}
}

Expand All @@ -267,11 +266,11 @@ func resourceNamespaceDelete(d *schema.ResourceData, meta interface{}) error {
}

if err != nil {
return fmt.Errorf("error deleting Redshift Serverless Namespace (%s): %w", d.Id(), err)
return fmt.Errorf("deleting Redshift Serverless Namespace (%s): %w", d.Id(), err)
}

if _, err := waitNamespaceDeleted(conn, d.Id()); err != nil {
return fmt.Errorf("error waiting for Redshift Serverless Namespace (%s) delete: %w", d.Id(), err)
return fmt.Errorf("waiting for Redshift Serverless Namespace (%s) delete: %w", d.Id(), err)
}

return nil
Expand Down
41 changes: 41 additions & 0 deletions internal/service/redshiftserverless/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,38 @@ func TestAccRedshiftServerlessNamespace_basic(t *testing.T) {
})
}

func TestAccRedshiftServerlessNamespace_user(t *testing.T) {
resourceName := "aws_redshiftserverless_namespace.test"
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, redshiftserverless.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckNamespaceDestroy,
Steps: []resource.TestStep{
{
Config: testAccNamespaceConfig_basic(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckNamespaceExists(resourceName),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccNamespaceConfig_user(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckNamespaceExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "admin_user_password", "Password123"),
),
},
},
})
}

func TestAccRedshiftServerlessNamespace_tags(t *testing.T) {
resourceName := "aws_redshiftserverless_namespace.test"
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
Expand Down Expand Up @@ -174,6 +206,15 @@ resource "aws_redshiftserverless_namespace" "test" {
`, rName)
}

func testAccNamespaceConfig_user(rName string) string {
return fmt.Sprintf(`
resource "aws_redshiftserverless_namespace" "test" {
namespace_name = %[1]q
admin_user_password = "Password123"
}
`, rName)
}

func testAccNamespaceConfig_updated(rName string) string {
return fmt.Sprintf(`
resource "aws_iam_role" "test" {
Expand Down