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

secretsmanager: Improve diffs with policies #28863

Merged
merged 2 commits into from
Jan 12, 2023
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
7 changes: 7 additions & 0 deletions .changelog/28863.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:bug
resource/aws_secretsmanager_secret: Improve refresh to avoid unnecessary diffs in `policy`
```

```release-note:bug
resource/aws_secretsmanager_secret_policy: Improve refresh to avoid unnecessary diffs in `policy`
```
14 changes: 6 additions & 8 deletions internal/service/secretsmanager/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ func ResourceSecret() *schema.Resource {
ValidateFunc: validSecretNamePrefix,
},
"policy": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringIsJSON,
DiffSuppressFunc: verify.SuppressEquivalentPolicyDiffs,
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringIsJSON,
DiffSuppressFunc: verify.SuppressEquivalentPolicyDiffs,
DiffSuppressOnRefresh: true,
StateFunc: func(v interface{}) string {
json, _ := structure.NormalizeJsonString(v)
return json
Expand Down Expand Up @@ -203,7 +204,6 @@ func resourceSecretCreate(d *schema.ResourceData, meta interface{}) error {

if v, ok := d.GetOk("policy"); ok && v.(string) != "" && v.(string) != "{}" {
policy, err := structure.NormalizeJsonString(v.(string))

if err != nil {
return fmt.Errorf("policy (%s) is invalid JSON: %w", v.(string), err)
}
Expand Down Expand Up @@ -299,7 +299,6 @@ func resourceSecretRead(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("reading Secrets Manager Secret (%s) policy: %w", d.Id(), err)
} else if v := output.ResourcePolicy; v != nil {
policyToSet, err := verify.PolicyToSet(d.Get("policy").(string), aws.StringValue(v))

if err != nil {
return err
}
Expand Down Expand Up @@ -378,7 +377,6 @@ func resourceSecretUpdate(d *schema.ResourceData, meta interface{}) error {
if d.HasChange("policy") {
if v, ok := d.GetOk("policy"); ok && v.(string) != "" && v.(string) != "{}" {
policy, err := structure.NormalizeJsonString(v.(string))

if err != nil {
return fmt.Errorf("policy contains an invalid JSON: %w", err)
}
Expand Down
11 changes: 5 additions & 6 deletions internal/service/secretsmanager/secret_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ func ResourceSecretPolicy() *schema.Resource {
ValidateFunc: verify.ValidARN,
},
"policy": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsJSON,
DiffSuppressFunc: verify.SuppressEquivalentPolicyDiffs,
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsJSON,
DiffSuppressFunc: verify.SuppressEquivalentPolicyDiffs,
DiffSuppressOnRefresh: true,
StateFunc: func(v interface{}) string {
json, _ := structure.NormalizeJsonString(v)
return json
Expand All @@ -55,7 +56,6 @@ func resourceSecretPolicyCreate(d *schema.ResourceData, meta interface{}) error
conn := meta.(*conns.AWSClient).SecretsManagerConn()

policy, err := structure.NormalizeJsonString(d.Get("policy").(string))

if err != nil {
return fmt.Errorf("policy (%s) is invalid JSON: %w", d.Get("policy").(string), err)
}
Expand Down Expand Up @@ -125,7 +125,6 @@ func resourceSecretPolicyRead(d *schema.ResourceData, meta interface{}) error {

if output.ResourcePolicy != nil {
policyToSet, err := verify.PolicyToSet(d.Get("policy").(string), aws.StringValue(output.ResourcePolicy))

if err != nil {
return err
}
Expand Down