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

resource/aws_organizations_organization: Add non_master_accounts attribute #8926

Merged
merged 1 commit into from
Jun 18, 2019
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
38 changes: 37 additions & 1 deletion aws/resource_aws_organizations_organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,30 @@ func resourceAwsOrganizationsOrganization() *schema.Resource {
},
},
},
"non_master_accounts": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"email": {
Type: schema.TypeString,
Computed: true,
},
"id": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"roots": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -208,8 +232,16 @@ func resourceAwsOrganizationsOrganizationRead(d *schema.ResourceData, meta inter

log.Printf("[INFO] Listing Accounts for Organization: %s", d.Id())
var accounts []*organizations.Account
var nonMasterAccounts []*organizations.Account
err = conn.ListAccountsPages(&organizations.ListAccountsInput{}, func(page *organizations.ListAccountsOutput, lastPage bool) bool {
accounts = append(accounts, page.Accounts...)
for _, account := range page.Accounts {
if aws.StringValue(account.Id) != aws.StringValue(org.Organization.MasterAccountId) {
nonMasterAccounts = append(nonMasterAccounts, account)
}

accounts = append(accounts, account)
}

return !lastPage
})
if err != nil {
Expand All @@ -236,6 +268,10 @@ func resourceAwsOrganizationsOrganizationRead(d *schema.ResourceData, meta inter
d.Set("master_account_email", org.Organization.MasterAccountEmail)
d.Set("master_account_id", org.Organization.MasterAccountId)

if err := d.Set("non_master_accounts", flattenOrganizationsAccounts(nonMasterAccounts)); err != nil {
return fmt.Errorf("error setting non_master_accounts: %s", err)
}

if err := d.Set("roots", flattenOrganizationsRoots(roots)); err != nil {
return fmt.Errorf("error setting roots: %s", err)
}
Expand Down
1 change: 1 addition & 0 deletions aws/resource_aws_organizations_organization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func testAccAwsOrganizationsOrganization_basic(t *testing.T) {
testAccMatchResourceAttrGlobalARN(resourceName, "master_account_arn", "organizations", regexp.MustCompile(`account/o-.+/.+`)),
resource.TestMatchResourceAttr(resourceName, "master_account_email", regexp.MustCompile(`.+@.+`)),
testAccCheckResourceAttrAccountID(resourceName, "master_account_id"),
resource.TestCheckResourceAttr(resourceName, "non_master_accounts.#", "0"),
resource.TestCheckResourceAttr(resourceName, "roots.#", "1"),
resource.TestMatchResourceAttr(resourceName, "roots.0.id", regexp.MustCompile(`r-[a-z0-9]{4,32}`)),
resource.TestCheckResourceAttrSet(resourceName, "roots.0.name"),
Expand Down
7 changes: 6 additions & 1 deletion website/docs/r/organizations_organization.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ The following arguments are supported:

In addition to all arguments above, the following attributes are exported:

* `accounts` - List of organization accounts (including the master account). All elements have these attributes:
* `accounts` - List of organization accounts including the master account. For a list excluding the master account, see the `non_master_accounts` attribute. All elements have these attributes:
* `arn` - ARN of the account
* `email` - Email of the account
* `id` - Identifier of the account
Expand All @@ -45,6 +45,11 @@ In addition to all arguments above, the following attributes are exported:
* `master_account_arn` - ARN of the master account
* `master_account_email` - Email address of the master account
* `master_account_id` - Identifier of the master account
* `non_master_accounts` - List of organization accounts excluding the master account. For a list including the master account, see the `accounts` attribute. All elements have these attributes:
* `arn` - ARN of the account
* `email` - Email of the account
* `id` - Identifier of the account
* `name` - Name of the account
* `roots` - List of organization roots. All elements have these attributes:
* `arn` - ARN of the root
* `id` - Identifier of the root
Expand Down