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

add master_account_name to oragnization's data source #36797

Merged
merged 3 commits into from
Apr 16, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changelog/36797.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:enhancement
data-source/aws_organizations_organization: Add `master_account_name` attribute
```

```release-note:enhancement
resource/aws_organizations_organization: Add `master_account_name` attribute
```
11 changes: 11 additions & 0 deletions internal/service/organizations/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ func ResourceOrganization() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"master_account_name": {
Type: schema.TypeString,
Computed: true,
},
"non_master_accounts": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -256,6 +260,12 @@ func resourceOrganizationRead(ctx context.Context, d *schema.ResourceData, meta
}

managementAccountID := aws.StringValue(org.MasterAccountId)
managementAccountName := ""
for _, v := range accounts {
if aws.StringValue(v.Id) == managementAccountID {
managementAccountName = aws.StringValue(v.Name)
}
}
nonManagementAccounts := tfslices.Filter(accounts, func(v *organizations.Account) bool {
return aws.StringValue(v.Id) != managementAccountID
})
Expand All @@ -274,6 +284,7 @@ func resourceOrganizationRead(ctx context.Context, d *schema.ResourceData, meta
d.Set("master_account_arn", org.MasterAccountArn)
d.Set("master_account_email", org.MasterAccountEmail)
d.Set("master_account_id", org.MasterAccountId)
d.Set("master_account_name", managementAccountName)
if err := d.Set("non_master_accounts", flattenAccounts(nonManagementAccounts)); err != nil {
return sdkdiag.AppendErrorf(diags, "setting non_master_accounts: %s", err)
}
Expand Down
12 changes: 12 additions & 0 deletions internal/service/organizations/organization_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ func DataSourceOrganization() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"master_account_name": {
Type: schema.TypeString,
Computed: true,
},
"non_master_accounts": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -172,6 +176,14 @@ func dataSourceOrganizationRead(ctx context.Context, d *schema.ResourceData, met
isDelegatedAdministrator := true
accounts, err := findAccounts(ctx, conn)

managementAccountName := ""
for _, v := range accounts {
if aws.StringValue(v.Id) == managementAccountID {
managementAccountName = aws.StringValue(v.Name)
}
}
d.Set("master_account_name", managementAccountName)

if err != nil {
if isManagementAccount || !tfawserr.ErrCodeEquals(err, organizations.ErrCodeAccessDeniedException) {
return sdkdiag.AppendErrorf(diags, "reading Organization (%s) accounts: %s", d.Id(), err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func testAccOrganizationDataSource_basic(t *testing.T) {
resource.TestCheckResourceAttrPair(resourceName, "master_account_arn", dataSourceName, "master_account_arn"),
resource.TestCheckResourceAttrPair(resourceName, "master_account_email", dataSourceName, "master_account_email"),
resource.TestCheckResourceAttrPair(resourceName, "master_account_id", dataSourceName, "master_account_id"),
resource.TestCheckResourceAttrPair(resourceName, "master_account_name", dataSourceName, "master_account_name"),
resource.TestCheckResourceAttrPair(resourceName, "non_master_accounts.#", dataSourceName, "non_master_accounts.#"),
resource.TestCheckResourceAttrPair(resourceName, "roots.#", dataSourceName, "roots.#"),
),
Expand Down
1 change: 1 addition & 0 deletions website/docs/d/organizations_organization.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ This data source exports the following attributes in addition to the arguments a
* `master_account_arn` - ARN of the account that is designated as the master account for the organization.
* `master_account_email` - The email address that is associated with the AWS account that is designated as the master account for the organization.
* `master_account_id` - Unique identifier (ID) of the master account of an organization.
* `master_account_name` - Name of the master account of an organization.

### Master Account or Delegated Administrator Attribute Reference

Expand Down
1 change: 1 addition & 0 deletions website/docs/r/organizations_organization.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ This resource exports the following attributes in addition to the arguments abov
* `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
* `master_account_name` - Name 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
Expand Down