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 support for tags for IAM Policy #17924

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions aws/resource_aws_iam_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func resourceAwsIamPolicy() *schema.Resource {
Expand Down Expand Up @@ -67,6 +68,7 @@ func resourceAwsIamPolicy() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchema(),
},
}
}
Expand Down Expand Up @@ -96,6 +98,9 @@ func resourceAwsIamPolicyCreate(d *schema.ResourceData, meta interface{}) error
}

d.SetId(aws.StringValue(response.Policy.Arn))
if v := d.Get("tags").(map[string]interface{}); len(v) > 0 {
request.Tags = keyvaluetags.New(v).IgnoreAws().IamTags()
}

return resourceAwsIamPolicyRead(d, meta)
}
Expand Down Expand Up @@ -147,6 +152,7 @@ func resourceAwsIamPolicyRead(d *schema.ResourceData, meta interface{}) error {
d.Set("description", getPolicyResponse.Policy.Description)
d.Set("name", getPolicyResponse.Policy.PolicyName)
d.Set("path", getPolicyResponse.Policy.Path)
d.Set("tags", getPolicyResponse.Policy.Tags)

// Retrieve policy

Expand Down
36 changes: 36 additions & 0 deletions aws/resource_aws_iam_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,3 +443,39 @@ resource "aws_iam_policy" "test" {
}
`, rName, policy)
}

func testAccAWSIAMPolicyConfigTags(t *testing.T) {
var role iam.GetRoleOutput
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_iam_policy.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSUserDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSIAMRoleConfig_tags(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSRoleExists(resourceName, &role),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.tag1", "test-value1"),
resource.TestCheckResourceAttr(resourceName, "tags.tag2", "test-value2"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAWSIAMRoleConfig_tagsUpdate(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSRoleExists(resourceName, &role),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.tag2", "test-value"),
),
},
},
})
}
2 changes: 1 addition & 1 deletion awsproviderlint/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/terraform-providers/terraform-provider-aws/awsproviderlint
go 1.16

require (
github.com/aws/aws-sdk-go v1.37.4
github.com/aws/aws-sdk-go v1.37.10
github.com/bflad/tfproviderlint v0.22.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.4.3
golang.org/x/tools v0.0.0-20200928201943-a0ef9b62deab
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/terraform-providers/terraform-provider-aws
go 1.16

require (
github.com/aws/aws-sdk-go v1.37.4
github.com/aws/aws-sdk-go v1.37.23
github.com/beevik/etree v1.1.0
github.com/fatih/color v1.9.0 // indirect
github.com/hashicorp/aws-sdk-go-base v0.7.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ github.com/aws/aws-sdk-go v1.25.3/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN
github.com/aws/aws-sdk-go v1.31.9/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZveU8YkpAk0=
github.com/aws/aws-sdk-go v1.37.4 h1:tWxrpMK/oRSXVnjUzhGeCWLR00fW0WF4V4sycYPPrJ8=
github.com/aws/aws-sdk-go v1.37.4/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro=
github.com/aws/aws-sdk-go v1.37.23 h1:bO80NcSmRv52w+GFpBegoLdlP/Z0OwUqQ9bbeCLCy/0=
github.com/aws/aws-sdk-go v1.37.23/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro=
github.com/beevik/etree v1.1.0 h1:T0xke/WvNtMoCqgzPhkX2r4rjY3GDZFi+FjpRZY2Jbs=
github.com/beevik/etree v1.1.0/go.mod h1:r8Aw8JqVegEf0w2fDnATrX9VpkMcyFeM0FhwO62wh+A=
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d h1:xDfNPAt8lFiC1UJrqV3uuy861HCTo708pDMbjHHdCas=
Expand Down