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

f-policy_data_source: adding support for attachment count #36759

Merged
Merged
6 changes: 6 additions & 0 deletions .changelog/36759.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
```release-note:enhancement
data-source/aws_iam_policy: Add `attachment_count` attribute
```
```release-note:enhancement
resource/aws_iam_policy: Add `attachment_count` attribute
```
5 changes: 5 additions & 0 deletions internal/service/iam/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ func resourcePolicy() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"attachment_count": {
Type: schema.TypeInt,
Computed: true,
},
"description": {
Type: schema.TypeString,
ForceNew: true,
Expand Down Expand Up @@ -194,6 +198,7 @@ func resourcePolicyRead(ctx context.Context, d *schema.ResourceData, meta interf
policy := output.policy

d.Set("arn", policy.Arn)
d.Set("attachment_count", policy.AttachmentCount)
d.Set("description", policy.Description)
d.Set("name", policy.PolicyName)
d.Set("name_prefix", create.NamePrefixFromName(aws.ToString(policy.PolicyName)))
Expand Down
5 changes: 5 additions & 0 deletions internal/service/iam/policy_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ func dataSourcePolicy() *schema.Resource {
ValidateFunc: verify.ValidARN,
ConflictsWith: []string{"name", "path_prefix"},
},
"attachment_count": {
Type: schema.TypeInt,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -97,6 +101,7 @@ func dataSourcePolicyRead(ctx context.Context, d *schema.ResourceData, meta inte

d.SetId(arn)
d.Set("arn", arn)
d.Set("attachment_count", policy.AttachmentCount)
d.Set("description", policy.Description)
d.Set("name", policy.PolicyName)
d.Set("path", policy.Path)
Expand Down
6 changes: 6 additions & 0 deletions internal/service/iam/policy_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func TestAccIAMPolicyDataSource_arn(t *testing.T) {
resource.TestCheckResourceAttrPair(datasourceName, "policy", resourceName, "policy"),
resource.TestCheckResourceAttrPair(datasourceName, "policy_id", resourceName, "policy_id"),
resource.TestCheckResourceAttrPair(datasourceName, "arn", resourceName, "arn"),
resource.TestCheckResourceAttrPair(datasourceName, "attachment_count", resourceName, "attachment_count"),
resource.TestCheckResourceAttrPair(datasourceName, "tags.%", resourceName, "tags.%"),
),
},
Expand Down Expand Up @@ -61,6 +62,7 @@ func TestAccIAMPolicyDataSource_arnTags(t *testing.T) {
resource.TestCheckResourceAttrPair(datasourceName, "policy", resourceName, "policy"),
resource.TestCheckResourceAttrPair(datasourceName, "policy_id", resourceName, "policy_id"),
resource.TestCheckResourceAttrPair(datasourceName, "arn", resourceName, "arn"),
resource.TestCheckResourceAttrPair(datasourceName, "attachment_count", resourceName, "attachment_count"),
resource.TestCheckResourceAttrPair(datasourceName, "tags.%", resourceName, "tags.%"),
resource.TestCheckResourceAttrPair(datasourceName, "tags.key", resourceName, "tags.key"),
),
Expand Down Expand Up @@ -89,6 +91,7 @@ func TestAccIAMPolicyDataSource_name(t *testing.T) {
resource.TestCheckResourceAttrPair(datasourceName, "policy", resourceName, "policy"),
resource.TestCheckResourceAttrPair(datasourceName, "policy_id", resourceName, "policy_id"),
resource.TestCheckResourceAttrPair(datasourceName, "arn", resourceName, "arn"),
resource.TestCheckResourceAttrPair(datasourceName, "attachment_count", resourceName, "attachment_count"),
resource.TestCheckResourceAttrPair(datasourceName, "tags.%", resourceName, "tags.%"),
),
},
Expand Down Expand Up @@ -116,6 +119,7 @@ func TestAccIAMPolicyDataSource_nameTags(t *testing.T) {
resource.TestCheckResourceAttrPair(datasourceName, "policy", resourceName, "policy"),
resource.TestCheckResourceAttrPair(datasourceName, "policy_id", resourceName, "policy_id"),
resource.TestCheckResourceAttrPair(datasourceName, "arn", resourceName, "arn"),
resource.TestCheckResourceAttrPair(datasourceName, "attachment_count", resourceName, "attachment_count"),
resource.TestCheckResourceAttrPair(datasourceName, "tags.%", resourceName, "tags.%"),
resource.TestCheckResourceAttrPair(datasourceName, "tags.key", resourceName, "tags.key"),
),
Expand Down Expand Up @@ -146,6 +150,7 @@ func TestAccIAMPolicyDataSource_nameAndPathPrefix(t *testing.T) {
resource.TestCheckResourceAttrPair(datasourceName, "policy", resourceName, "policy"),
resource.TestCheckResourceAttrPair(datasourceName, "policy_id", resourceName, "policy_id"),
resource.TestCheckResourceAttrPair(datasourceName, "arn", resourceName, "arn"),
resource.TestCheckResourceAttrPair(datasourceName, "attachment_count", resourceName, "attachment_count"),
resource.TestCheckResourceAttrPair(datasourceName, "tags.%", resourceName, "tags.%"),
),
},
Expand Down Expand Up @@ -175,6 +180,7 @@ func TestAccIAMPolicyDataSource_nameAndPathPrefixTags(t *testing.T) {
resource.TestCheckResourceAttrPair(datasourceName, "policy", resourceName, "policy"),
resource.TestCheckResourceAttrPair(datasourceName, "policy_id", resourceName, "policy_id"),
resource.TestCheckResourceAttrPair(datasourceName, "arn", resourceName, "arn"),
resource.TestCheckResourceAttrPair(datasourceName, "attachment_count", resourceName, "attachment_count"),
resource.TestCheckResourceAttrPair(datasourceName, "tags.%", resourceName, "tags.%"),
resource.TestCheckResourceAttrPair(datasourceName, "tags.key", resourceName, "tags.key"),
),
Expand Down
1 change: 1 addition & 0 deletions internal/service/iam/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func TestAccIAMPolicy_basic(t *testing.T) {
testAccCheckPolicyExists(ctx, resourceName, &out),
acctest.CheckResourceAttrGlobalARN(resourceName, "arn", "iam", fmt.Sprintf("policy/%s", rName)),
resource.TestCheckResourceAttr(resourceName, "description", ""),
resource.TestCheckResourceAttr(resourceName, "attachment_count", "0"),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "path", "/"),
resource.TestCheckResourceAttr(resourceName, "policy", expectedPolicyText),
Expand Down
1 change: 1 addition & 0 deletions website/docs/d/iam_policy.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ data "aws_iam_policy" "example" {
This data source exports the following attributes in addition to the arguments above:

* `arn` - ARN of the policy.
* `attachment_count` - Number of entities (users, groups, and roles) that the policy is attached to.
* `path` - Path to the policy.
* `description` - Description of the policy.
* `policy` - Policy document of the policy.
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/iam_policy.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ This resource supports the following arguments:
This resource exports the following attributes in addition to the arguments above:

* `arn` - ARN assigned by AWS to this policy.
* `attachment_count` - Number of entities (users, groups, and roles) that the policy is attached to.
* `id` - ARN assigned by AWS to this policy.
* `policy_id` - Policy's ID.
* `tags_all` - A map of tags assigned to the resource, including those inherited from the provider [`default_tags` configuration block](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#default_tags-configuration-block).
Expand Down
Loading