diff --git a/aws/data_source_aws_organizations_account_ids.go b/aws/data_source_aws_organizations_account_ids.go new file mode 100644 index 00000000000..90cf1142c0f --- /dev/null +++ b/aws/data_source_aws_organizations_account_ids.go @@ -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 +} diff --git a/aws/provider.go b/aws/provider.go index 1891e6418ba..e76951f8b36 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -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(), diff --git a/website/aws.erb b/website/aws.erb index 616605551f5..8b680a31d18 100644 --- a/website/aws.erb +++ b/website/aws.erb @@ -220,6 +220,9 @@