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

New Data Source: aws_iam_policy #1999

Merged
merged 2 commits into from
Jan 31, 2018
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
40 changes: 40 additions & 0 deletions aws/data_source_aws_iam_policy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package aws

import (
"github.com/hashicorp/terraform/helper/schema"
)

func dataSourceAwsIAMPolicy() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsIAMPolicyRead,

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Optional: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would define as Required: true instead

},
"name": {
Type: schema.TypeString,
Computed: true,
},
"policy": {
Type: schema.TypeString,
Computed: true,
},
"path": {
Type: schema.TypeString,
Computed: true,
},

"description": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceAwsIAMPolicyRead(d *schema.ResourceData, meta interface{}) error {
d.SetId(d.Get("arn").(string))
return resourceAwsIamPolicyRead(d, meta)
}
61 changes: 61 additions & 0 deletions aws/data_source_aws_iam_policy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package aws

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccAWSDataSourceIAMPolicy_basic(t *testing.T) {
policyName := fmt.Sprintf("test-policy-%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccAwsDataSourceIamPolicyConfig(policyName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.aws_iam_policy.test", "name", policyName),
resource.TestCheckResourceAttr("data.aws_iam_policy.test", "description", "My test policy"),
resource.TestCheckResourceAttr("data.aws_iam_policy.test", "path", "/"),
resource.TestCheckResourceAttrSet("data.aws_iam_policy.test", "policy"),
resource.TestMatchResourceAttr("data.aws_iam_policy.test", "arn",
regexp.MustCompile(`^arn:[\w-]+:([a-zA-Z0-9\-])+:([a-z]{2}-(gov-)?[a-z]+-\d{1})?:(\d{12})?:(.*)$`)),
),
},
},
})

}

func testAccAwsDataSourceIamPolicyConfig(policyName string) string {
return fmt.Sprintf(`
resource "aws_iam_policy" "test_policy" {
name = "%s"
path = "/"
description = "My test policy"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"ec2:Describe*"
],
"Effect": "Allow",
"Resource": "*"
}
]
}
EOF
}

data "aws_iam_policy" "test" {
arn = "${aws_iam_policy.test_policy.arn}"
}
`, policyName)
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ func Provider() terraform.ResourceProvider {
"aws_iam_account_alias": dataSourceAwsIamAccountAlias(),
"aws_iam_group": dataSourceAwsIAMGroup(),
"aws_iam_instance_profile": dataSourceAwsIAMInstanceProfile(),
"aws_iam_policy": dataSourceAwsIAMPolicy(),
"aws_iam_policy_document": dataSourceAwsIamPolicyDocument(),
"aws_iam_role": dataSourceAwsIAMRole(),
"aws_iam_server_certificate": dataSourceAwsIAMServerCertificate(),
Expand Down
33 changes: 33 additions & 0 deletions website/docs/d/iam_policy.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
layout: "aws"
page_title: "AWS: aws_iam_policy"
sidebar_current: "docs-aws-datasource-iam-policy"
description: |-
Get information on a Amazon IAM policy
---

# aws_iam_policy

This data source can be used to fetch information about a specific
IAM policy.

## Example Usage

```hcl
data "aws_iam_policy" "example" {
arn = "arn:aws:iam::123456789012:policy/UsersManageOwnCredentials"
}
```

## Argument Reference

* `arn` - (Required) ARN of the IAM policy.

## Attributes Reference

* `name` - The name of the IAM policy.
* `arn` - The Amazon Resource Name (ARN) specifying the policy.
* `path` - The path to the policy.
* `description` - The description of the policy.
* `policy` - The policy document of the policy.