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

[WIP] Add an aws_organizations_account_ids data source #4384

Closed
wants to merge 1 commit into from
Closed
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
65 changes: 65 additions & 0 deletions aws/data_source_aws_organizations_account_ids.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package aws

import (
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/organizations"
"github.com/hashicorp/terraform/helper/schema"
)

func dataSourceAwsOrganizationsAccountIds() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsOrganizationAccountIdsRead,

Schema: map[string]*schema.Schema{
"parent_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}

func dataSourceAwsOrganizationAccountIdsRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).organizationsconn

accountIds := make([]string, 0)

if parentId, ok := d.GetOk("parent_id"); ok {
input := &organizations.ListAccountsForParentInput{
ParentId: aws.String(parentId.(string)),
}
result, err := conn.ListAccountsForParent(input)

if err != nil {
return err
}

for _, account := range result.Accounts {
accountIds = append(accountIds, *account.Id)
}
} else {
input := &organizations.ListAccountsInput{}
result, err := conn.ListAccounts(input)

if err != nil {
return err
}

for _, account := range result.Accounts {
accountIds = append(accountIds, *account.Id)
}
}

d.SetId(time.Now().UTC().String())
d.Set("ids", accountIds)

return nil
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ func Provider() terraform.ResourceProvider {
"aws_mq_broker": dataSourceAwsMqBroker(),
"aws_nat_gateway": dataSourceAwsNatGateway(),
"aws_network_interface": dataSourceAwsNetworkInterface(),
"aws_organizations_account_ids": dataSourceAwsOrganizationsAccountIds(),
"aws_partition": dataSourceAwsPartition(),
"aws_prefix_list": dataSourceAwsPrefixList(),
"aws_rds_cluster": dataSourceAwsRdsCluster(),
Expand Down
3 changes: 3 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@
<li<%= sidebar_current("docs-aws-datasource-mq-broker") %>>
<a href="/docs/providers/aws/d/mq_broker.html">aws_mq_broker</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-organizations-account-ids") %>>
<a href="/docs/providers/aws/d/organizations_account_ids.html">aws_organizations_account_ids</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-partition") %>>
<a href="/docs/providers/aws/d/partition.html">aws_partition</a>
</li>
Expand Down
29 changes: 29 additions & 0 deletions website/docs/d/organizations_account_ids.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
layout: "aws"
page_title: "AWS: aws_organizations_account_ids"
sidebar_current: "docs-aws-datasource-organizations-account-ids"
description: |-
Provides a list of Account IDs in an Organization or Organizational Unit.Use this data source to get a list of Account IDs in an Organization or Organizational Unit
---

# Data Source: aws_organizations_account_ids

`aws_organizations_account_ids` provides a list of AccountIds in an [organization](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_manage_org.html) or [organizational unit](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_manage_ous.html).

Will give an error if Organizations aren't enabled - see `aws_organizations_organization`.

## Example Usage

```hcl
data "aws_organizations_account_ids" "master" {}
```

## Argument Reference

* `parent_id` - (Optional) The ID for the [parent root](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_getting-started_concepts.html#root) or [organizational unit](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_getting-started_concepts.html#organizationalunit) whose accounts you want to list. If you specify the root you get the list of all the accounts that are not in any organizational unit. If you specify an organizational unit, you get a list of all the accounts in only that organizational unit, and not any child organizational units.

## Attributes Reference

The following attributes are exported:

* `ids` - is set to a list Account IDs.