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

Enable organization tag policies with attachment #11612

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
1 change: 1 addition & 0 deletions aws/resource_aws_organizations_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func resourceAwsOrganizationsPolicy() *schema.Resource {
Default: organizations.PolicyTypeServiceControlPolicy,
ValidateFunc: validation.StringInSlice([]string{
organizations.PolicyTypeServiceControlPolicy,
organizations.PolicyTypeTagPolicy,
}, false),
},
},
Expand Down
14 changes: 7 additions & 7 deletions aws/resource_aws_organizations_policy_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,16 @@ func resourceAwsOrganizationsPolicyAttachmentRead(d *schema.ResourceData, meta i
return err
}

input := &organizations.ListPoliciesForTargetInput{
Filter: aws.String(organizations.PolicyTypeServiceControlPolicy),
TargetId: aws.String(targetID),
input := &organizations.ListTargetsForPolicyInput{
PolicyId: aws.String(policyID),
}

log.Printf("[DEBUG] Listing Organizations Policies for Target: %s", input)
var output *organizations.PolicySummary
err = conn.ListPoliciesForTargetPages(input, func(page *organizations.ListPoliciesForTargetOutput, lastPage bool) bool {
for _, policySummary := range page.Policies {
if aws.StringValue(policySummary.Id) == policyID {
var output *organizations.PolicyTargetSummary

err = conn.ListTargetsForPolicyPages(input, func(page *organizations.ListTargetsForPolicyOutput, lastPage bool) bool {
for _, policySummary := range page.Targets {
if aws.StringValue(policySummary.TargetId) == targetID {
output = policySummary
return true
}
Expand Down
36 changes: 24 additions & 12 deletions aws/resource_aws_organizations_policy_attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"fmt"
"log"
"strconv"
"testing"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -18,13 +19,24 @@ func testAccAwsOrganizationsPolicyAttachment_Account(t *testing.T) {
policyIdResourceName := "aws_organizations_policy.test"
targetIdResourceName := "aws_organizations_organization.test"

serviceControlPolicyContent := `{"Version": "2012-10-17", "Statement": { "Effect": "Allow", "Action": "*", "Resource": "*"}}`
tagPolicyContent := `{ "tags": { "Product": { "tag_key": { "@@assign": "Product" }, "enforced_for": { "@@assign": [ "ec2:instance" ] } } } }`

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccOrganizationsAccountPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAwsOrganizationsPolicyAttachmentDestroy,
Steps: []resource.TestStep{
{
Config: testAccAwsOrganizationsPolicyAttachmentConfig_Account(rName),
Config: testAccAwsOrganizationsPolicyAttachmentConfig_Account(rName, organizations.PolicyTypeServiceControlPolicy, serviceControlPolicyContent),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsOrganizationsPolicyAttachmentExists(resourceName),
resource.TestCheckResourceAttrPair(resourceName, "policy_id", policyIdResourceName, "id"),
resource.TestCheckResourceAttrPair(resourceName, "target_id", targetIdResourceName, "master_account_id"),
),
},
{
Config: testAccAwsOrganizationsPolicyAttachmentConfig_Account(rName, organizations.PolicyTypeTagPolicy, tagPolicyContent),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsOrganizationsPolicyAttachmentExists(resourceName),
resource.TestCheckResourceAttrPair(resourceName, "policy_id", policyIdResourceName, "id"),
Expand Down Expand Up @@ -163,16 +175,15 @@ func testAccCheckAwsOrganizationsPolicyAttachmentExists(resourceName string) res
return err
}

input := &organizations.ListPoliciesForTargetInput{
Filter: aws.String(organizations.PolicyTypeServiceControlPolicy),
TargetId: aws.String(targetID),
input := &organizations.ListTargetsForPolicyInput{
PolicyId: aws.String(policyID),
}

log.Printf("[DEBUG] Listing Organizations Policies for Target: %s", input)
var output *organizations.PolicySummary
err = conn.ListPoliciesForTargetPages(input, func(page *organizations.ListPoliciesForTargetOutput, lastPage bool) bool {
for _, policySummary := range page.Policies {
if aws.StringValue(policySummary.Id) == policyID {
var output *organizations.PolicyTargetSummary
err = conn.ListTargetsForPolicyPages(input, func(page *organizations.ListTargetsForPolicyOutput, lastPage bool) bool {
for _, policySummary := range page.Targets {
if aws.StringValue(policySummary.TargetId) == targetID {
output = policySummary
return true
}
Expand All @@ -192,24 +203,25 @@ func testAccCheckAwsOrganizationsPolicyAttachmentExists(resourceName string) res
}
}

func testAccAwsOrganizationsPolicyAttachmentConfig_Account(rName string) string {
func testAccAwsOrganizationsPolicyAttachmentConfig_Account(rName, policyType, policyContent string) string {
return fmt.Sprintf(`
resource "aws_organizations_organization" "test" {
enabled_policy_types = ["SERVICE_CONTROL_POLICY"]
enabled_policy_types = ["SERVICE_CONTROL_POLICY", "TAG_POLICY"]
}

resource "aws_organizations_policy" "test" {
depends_on = ["aws_organizations_organization.test"]

content = "{\"Version\": \"2012-10-17\", \"Statement\": { \"Effect\": \"Allow\", \"Action\": \"*\", \"Resource\": \"*\"}}"
name = "%s"
type = "%s"
content = %s
}

resource "aws_organizations_policy_attachment" "test" {
policy_id = "${aws_organizations_policy.test.id}"
target_id = "${aws_organizations_organization.test.master_account_id}"
}
`, rName)
`, rName, policyType, strconv.Quote(policyContent))
}

func testAccAwsOrganizationsPolicyAttachmentConfig_OrganizationalUnit(rName string) string {
Expand Down
57 changes: 57 additions & 0 deletions aws/resource_aws_organizations_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,49 @@ func testAccAwsOrganizationsPolicy_description(t *testing.T) {
})
}

func testAccAwsOrganizationsPolicy_type(t *testing.T) {
var policy organizations.Policy
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_organizations_policy.test"

serviceControlPolicyContent := `{"Version": "2012-10-17", "Statement": { "Effect": "Allow", "Action": "*", "Resource": "*"}}`
tagPolicyContent := `{ "tags": { "Product": { "tag_key": { "@@assign": "Product" }, "enforced_for": { "@@assign": [ "ec2:instance" ] } } } }`

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAwsOrganizationsPolicyDestroy,
Steps: []resource.TestStep{
{
Config: testAccAwsOrganizationsPolicyConfig_Type(rName, serviceControlPolicyContent, organizations.PolicyTypeServiceControlPolicy),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsOrganizationsPolicyExists(resourceName, &policy),
resource.TestCheckResourceAttr(resourceName, "type", organizations.PolicyTypeServiceControlPolicy),
),
},
{
Config: testAccAwsOrganizationsPolicyConfig_Type(rName, tagPolicyContent, organizations.PolicyTypeTagPolicy),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsOrganizationsPolicyExists(resourceName, &policy),
resource.TestCheckResourceAttr(resourceName, "type", organizations.PolicyTypeTagPolicy),
),
},
{
Config: testAccAwsOrganizationsPolicyConfig_Required(rName, serviceControlPolicyContent),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsOrganizationsPolicyExists(resourceName, &policy),
resource.TestCheckResourceAttr(resourceName, "type", organizations.PolicyTypeServiceControlPolicy),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckAwsOrganizationsPolicyDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).organizationsconn

Expand Down Expand Up @@ -243,3 +286,17 @@ resource "aws_organizations_policy" "test5" {
}
`, rName)
}

func testAccAwsOrganizationsPolicyConfig_Type(rName, content, policyType string) string {
return fmt.Sprintf(`
resource "aws_organizations_organization" "test" {}

resource "aws_organizations_policy" "test" {
content = %s
name = "%s"
type = "%s"

depends_on = ["aws_organizations_organization.test"]
}
`, strconv.Quote(content), rName, policyType)
}
1 change: 1 addition & 0 deletions aws/resource_aws_organizations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func TestAccAWSOrganizations(t *testing.T) {
"basic": testAccAwsOrganizationsPolicy_basic,
"concurrent": testAccAwsOrganizationsPolicy_concurrent,
"Description": testAccAwsOrganizationsPolicy_description,
"Type": testAccAwsOrganizationsPolicy_type,
},
"PolicyAttachment": {
"Account": testAccAwsOrganizationsPolicyAttachment_Account,
Expand Down
4 changes: 2 additions & 2 deletions website/docs/r/organizations_policy.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ CONTENT

The following arguments are supported:

* `content` - (Required) The policy content to add to the new policy. For example, if you create a [service control policy (SCP)](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_manage_policies_scp.html), this string must be JSON text that specifies the permissions that admins in attached accounts can delegate to their users, groups, and roles. For more information about the SCP syntax, see the [Service Control Policy Syntax documentation](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_reference_scp-syntax.html).
* `content` - (Required) The policy content to add to the new policy. For example, if you create a [service control policy (SCP)](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_manage_policies_scp.html), this string must be JSON text that specifies the permissions that admins in attached accounts can delegate to their users, groups, and roles. For more information about the SCP syntax, see the [Service Control Policy Syntax documentation](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_reference_scp-syntax.html) and for more information on the Tag Policy syntax, see the [Tag Policy Syntax documentation](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_manage_policies_example-tag-policies.html).
* `name` - (Required) The friendly name to assign to the policy.
* `description` - (Optional) A description to assign to the policy.
* `type` - (Optional) The type of policy to create. Currently, the only valid value is `SERVICE_CONTROL_POLICY` (SCP).
* `type` - (Optional) The type of policy to create. Currently, the only valid values are `SERVICE_CONTROL_POLICY` (SCP) and `TAG_POLICY`. Defaults to `SERVICE_CONTROL_POLICY`.

## Attribute Reference

Expand Down