-
Notifications
You must be signed in to change notification settings - Fork 9.2k
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
resource/aws_organizations_organization: Add enabled_policy_types argument #8588
Merged
bflad
merged 3 commits into
master
from
f-aws_organizations_organization-enable-policies
May 10, 2019
+341
−11
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,13 +3,17 @@ package aws | |
import ( | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/organizations" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/hashicorp/terraform/helper/validation" | ||
) | ||
|
||
const organizationsPolicyTypeStatusDisabled = "DISABLED" | ||
|
||
func resourceAwsOrganizationsOrganization() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsOrganizationsOrganizationCreate, | ||
|
@@ -78,6 +82,16 @@ func resourceAwsOrganizationsOrganization() *schema.Resource { | |
}, | ||
}, | ||
}, | ||
"enabled_policy_types": { | ||
Type: schema.TypeSet, | ||
Optional: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
ValidateFunc: validation.StringInSlice([]string{ | ||
organizations.PolicyTypeServiceControlPolicy, | ||
}, false), | ||
}, | ||
}, | ||
"feature_set": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
|
@@ -123,6 +137,32 @@ func resourceAwsOrganizationsOrganizationCreate(d *schema.ResourceData, meta int | |
} | ||
} | ||
|
||
enabledPolicyTypes := d.Get("enabled_policy_types").(*schema.Set).List() | ||
|
||
if len(enabledPolicyTypes) > 0 { | ||
defaultRoot, err := getOrganizationDefaultRoot(conn) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error getting AWS Organization (%s) default root: %s", d.Id(), err) | ||
} | ||
|
||
for _, v := range enabledPolicyTypes { | ||
enabledPolicyType := v.(string) | ||
input := &organizations.EnablePolicyTypeInput{ | ||
PolicyType: aws.String(enabledPolicyType), | ||
RootId: defaultRoot.Id, | ||
} | ||
|
||
if _, err := conn.EnablePolicyType(input); err != nil { | ||
return fmt.Errorf("error enabling policy type (%s) in Organization (%s): %s", enabledPolicyType, d.Id(), err) | ||
} | ||
|
||
if err := waitForOrganizationDefaultRootPolicyTypeEnable(conn, enabledPolicyType); err != nil { | ||
return fmt.Errorf("error waiting for policy type (%s) enabling in Organization (%s): %s", enabledPolicyType, d.Id(), err) | ||
} | ||
} | ||
} | ||
|
||
return resourceAwsOrganizationsOrganizationRead(d, meta) | ||
} | ||
|
||
|
@@ -181,6 +221,18 @@ func resourceAwsOrganizationsOrganizationRead(d *schema.ResourceData, meta inter | |
return fmt.Errorf("error setting aws_service_access_principals: %s", err) | ||
} | ||
|
||
enabledPolicyTypes := make([]string, 0) | ||
|
||
for _, policyType := range roots[0].PolicyTypes { | ||
if aws.StringValue(policyType.Status) == organizations.PolicyTypeStatusEnabled || aws.StringValue(policyType.Status) == organizations.PolicyTypeStatusPendingEnable { | ||
enabledPolicyTypes = append(enabledPolicyTypes, aws.StringValue(policyType.Type)) | ||
} | ||
} | ||
|
||
if err := d.Set("enabled_policy_types", enabledPolicyTypes); err != nil { | ||
return fmt.Errorf("error setting enabled_policy_types: %s", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
|
@@ -221,6 +273,47 @@ func resourceAwsOrganizationsOrganizationUpdate(d *schema.ResourceData, meta int | |
} | ||
} | ||
|
||
if d.HasChange("enabled_policy_types") { | ||
defaultRootID := d.Get("roots.0.id").(string) | ||
o, n := d.GetChange("enabled_policy_types") | ||
oldSet := o.(*schema.Set) | ||
newSet := n.(*schema.Set) | ||
|
||
for _, v := range oldSet.Difference(newSet).List() { | ||
policyType := v.(string) | ||
input := &organizations.DisablePolicyTypeInput{ | ||
PolicyType: aws.String(policyType), | ||
RootId: aws.String(defaultRootID), | ||
} | ||
|
||
log.Printf("[DEBUG] Disabling Policy Type in Organization: %s", input) | ||
if _, err := conn.DisablePolicyType(input); err != nil { | ||
return fmt.Errorf("error disabling policy type (%s) in Organization (%s) Root (%s): %s", policyType, d.Id(), defaultRootID, err) | ||
} | ||
|
||
if err := waitForOrganizationDefaultRootPolicyTypeDisable(conn, policyType); err != nil { | ||
return fmt.Errorf("error waiting for policy type (%s) disabling in Organization (%s) Root (%s): %s", policyType, d.Id(), defaultRootID, err) | ||
} | ||
} | ||
|
||
for _, v := range newSet.Difference(oldSet).List() { | ||
policyType := v.(string) | ||
input := &organizations.EnablePolicyTypeInput{ | ||
PolicyType: aws.String(policyType), | ||
RootId: aws.String(d.Get("roots.0.id").(string)), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor we can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes we can! Thanks! |
||
} | ||
|
||
log.Printf("[DEBUG] Enabling Policy Type in Organization: %s", input) | ||
if _, err := conn.EnablePolicyType(input); err != nil { | ||
return fmt.Errorf("error enabling policy type (%s) in Organization (%s) Root (%s): %s", policyType, d.Id(), defaultRootID, err) | ||
} | ||
|
||
if err := waitForOrganizationDefaultRootPolicyTypeEnable(conn, policyType); err != nil { | ||
return fmt.Errorf("error waiting for policy type (%s) enabling in Organization (%s) Root (%s): %s", policyType, d.Id(), defaultRootID, err) | ||
} | ||
} | ||
} | ||
|
||
return resourceAwsOrganizationsOrganizationRead(d, meta) | ||
} | ||
|
||
|
@@ -266,3 +359,73 @@ func flattenOrganizationsRootPolicyTypeSummaries(summaries []*organizations.Poli | |
} | ||
return result | ||
} | ||
|
||
func getOrganizationDefaultRoot(conn *organizations.Organizations) (*organizations.Root, error) { | ||
var roots []*organizations.Root | ||
|
||
err := conn.ListRootsPages(&organizations.ListRootsInput{}, func(page *organizations.ListRootsOutput, lastPage bool) bool { | ||
roots = append(roots, page.Roots...) | ||
|
||
return !lastPage | ||
}) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(roots) == 0 { | ||
return nil, fmt.Errorf("no roots found") | ||
} | ||
|
||
return roots[0], nil | ||
} | ||
|
||
func getOrganizationDefaultRootPolicyTypeRefreshFunc(conn *organizations.Organizations, policyType string) resource.StateRefreshFunc { | ||
return func() (interface{}, string, error) { | ||
defaultRoot, err := getOrganizationDefaultRoot(conn) | ||
|
||
if err != nil { | ||
return nil, "", fmt.Errorf("error getting default root: %s", err) | ||
} | ||
|
||
for _, pt := range defaultRoot.PolicyTypes { | ||
if aws.StringValue(pt.Type) == policyType { | ||
return pt, aws.StringValue(pt.Status), nil | ||
} | ||
} | ||
|
||
return &organizations.PolicyTypeSummary{}, organizationsPolicyTypeStatusDisabled, nil | ||
} | ||
} | ||
|
||
func waitForOrganizationDefaultRootPolicyTypeDisable(conn *organizations.Organizations, policyType string) error { | ||
stateConf := &resource.StateChangeConf{ | ||
Pending: []string{ | ||
organizations.PolicyTypeStatusEnabled, | ||
organizations.PolicyTypeStatusPendingDisable, | ||
}, | ||
Target: []string{organizationsPolicyTypeStatusDisabled}, | ||
Refresh: getOrganizationDefaultRootPolicyTypeRefreshFunc(conn, policyType), | ||
Timeout: 5 * time.Minute, | ||
} | ||
|
||
_, err := stateConf.WaitForState() | ||
|
||
return err | ||
} | ||
|
||
func waitForOrganizationDefaultRootPolicyTypeEnable(conn *organizations.Organizations, policyType string) error { | ||
stateConf := &resource.StateChangeConf{ | ||
Pending: []string{ | ||
organizationsPolicyTypeStatusDisabled, | ||
organizations.PolicyTypeStatusPendingEnable, | ||
}, | ||
Target: []string{organizations.PolicyTypeStatusEnabled}, | ||
Refresh: getOrganizationDefaultRootPolicyTypeRefreshFunc(conn, policyType), | ||
Timeout: 5 * time.Minute, | ||
} | ||
|
||
_, err := stateConf.WaitForState() | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we be returning that it's active when it's still potentially Pending here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A lot of the AWS resources currently treat pending statuses similar to their end state during read to account for scenarios where changes might have occurred outside Terraform or for eventual consistency reasons. This one was a holdout before I implemented the waiter functions as during my local testing of this it didn't appear to display any eventual consistency issues. I'll remove it and verify everything still looks okay. 👍