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

sagemaker/model_package_group_policy: Fix equivalent policy diffs #22259

Merged
merged 3 commits into from
Dec 16, 2021
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/22259.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_sagemaker_model_package_group_policy: Fix erroneous diffs in `resource_policy` when no changes made or policies are equivalent
```
24 changes: 21 additions & 3 deletions internal/service/sagemaker/model_package_group_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/aws/aws-sdk-go/service/sagemaker"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/structure"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
Expand Down Expand Up @@ -35,6 +36,10 @@ func ResourceModelPackageGroupPolicy() *schema.Resource {
Required: true,
ValidateFunc: validation.StringIsJSON,
DiffSuppressFunc: verify.SuppressEquivalentPolicyDiffs,
StateFunc: func(v interface{}) string {
json, _ := structure.NormalizeJsonString(v)
return json
},
},
},
}
Expand All @@ -43,13 +48,19 @@ func ResourceModelPackageGroupPolicy() *schema.Resource {
func resourceModelPackageGroupPolicyPut(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).SageMakerConn

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

if err != nil {
return fmt.Errorf("policy (%s) is invalid JSON: %w", d.Get("resource_policy").(string), err)
}

name := d.Get("model_package_group_name").(string)
input := &sagemaker.PutModelPackageGroupPolicyInput{
ModelPackageGroupName: aws.String(name),
ResourcePolicy: aws.String(d.Get("resource_policy").(string)),
ResourcePolicy: aws.String(policy),
}

_, err := conn.PutModelPackageGroupPolicy(input)
_, err = conn.PutModelPackageGroupPolicy(input)
if err != nil {
return fmt.Errorf("error creating SageMaker Model Package Group Policy %s: %w", name, err)
}
Expand All @@ -74,7 +85,14 @@ func resourceModelPackageGroupPolicyRead(d *schema.ResourceData, meta interface{
}

d.Set("model_package_group_name", d.Id())
d.Set("resource_policy", mpg.ResourcePolicy)

policyToSet, err := verify.PolicyToSet(d.Get("resource_policy").(string), aws.StringValue(mpg.ResourcePolicy))

if err != nil {
return err
}

d.Set("resource_policy", policyToSet)

return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,15 @@ func testAccCheckModelPackageGroupPolicyExists(n string, mpg *sagemaker.GetModel
func testAccModelPackageGroupPolicyBasicConfig(rName string) string {
return fmt.Sprintf(`
data "aws_caller_identity" "current" {}
data "aws_partition" "current" {}

data "aws_iam_policy_document" "test" {
statement {
sid = "AddPermModelPackageGroup"
actions = ["sagemaker:DescribeModelPackage", "sagemaker:ListModelPackages"]
resources = [aws_sagemaker_model_package_group.test.arn]
principals {
identifiers = [data.aws_caller_identity.current.account_id]
identifiers = ["arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:root"]
type = "AWS"
}
}
Expand Down