From 6b2821e226c99bedb9b06b09104b1a5a643450a3 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Sat, 21 Jan 2017 17:11:02 -0500 Subject: [PATCH] Add 'aws_canonical_user_id' data source. --- .../aws/data_source_aws_canonical_user_id.go | 48 +++++++++++++++++ .../data_source_aws_canonical_user_id_test.go | 52 +++++++++++++++++++ builtin/providers/aws/provider.go | 1 + .../aws/d/canonical_user_id.html.markdown | 35 +++++++++++++ website/source/layouts/aws.erb | 3 ++ 5 files changed, 139 insertions(+) create mode 100644 builtin/providers/aws/data_source_aws_canonical_user_id.go create mode 100644 builtin/providers/aws/data_source_aws_canonical_user_id_test.go create mode 100644 website/source/docs/providers/aws/d/canonical_user_id.html.markdown diff --git a/builtin/providers/aws/data_source_aws_canonical_user_id.go b/builtin/providers/aws/data_source_aws_canonical_user_id.go new file mode 100644 index 000000000000..ba6a0b09884a --- /dev/null +++ b/builtin/providers/aws/data_source_aws_canonical_user_id.go @@ -0,0 +1,48 @@ +package aws + +import ( + "fmt" + "log" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/s3" + "github.com/hashicorp/terraform/helper/schema" +) + +func dataSourceAwsCanonicalUserId() *schema.Resource { + return &schema.Resource{ + Read: dataSourceAwsCanonicalUserIdRead, + + Schema: map[string]*schema.Schema{ + "id": { + Type: schema.TypeString, + Computed: true, + }, + "display_name": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func dataSourceAwsCanonicalUserIdRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).s3conn + + log.Printf("[DEBUG] Listing S3 buckets.") + + req := &s3.ListBucketsInput{} + resp, err := conn.ListBuckets(req) + if err != nil { + return err + } + if resp == nil || resp.Owner == nil { + return fmt.Errorf("no canonical user ID found") + } + + d.SetId(aws.StringValue(resp.Owner.ID)) + d.Set("id", resp.Owner.ID) + d.Set("display_name", resp.Owner.DisplayName) + + return nil +} diff --git a/builtin/providers/aws/data_source_aws_canonical_user_id_test.go b/builtin/providers/aws/data_source_aws_canonical_user_id_test.go new file mode 100644 index 000000000000..c1bd6e5981c8 --- /dev/null +++ b/builtin/providers/aws/data_source_aws_canonical_user_id_test.go @@ -0,0 +1,52 @@ +// make testacc TEST=./builtin/providers/aws/ TESTARGS='-run=TestAccDataSourceAwsCanonicalUserId_' + +package aws + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccDataSourceAwsCanonicalUserId_basic(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceAwsCanonicalUserIdConfig, + Check: resource.ComposeTestCheckFunc( + testAccDataSourceAwsCanonicalUserIdCheckExists("data.aws_canonical_user_id.current"), + ), + }, + }, + }) +} + +func testAccDataSourceAwsCanonicalUserIdCheckExists(name string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[name] + if !ok { + return fmt.Errorf("Can't find Canonical User ID resource: %s", name) + } + + if rs.Primary.Attributes["id"] == "" { + return fmt.Errorf("Missing Canonical User ID") + } + if rs.Primary.Attributes["display_name"] == "" { + return fmt.Errorf("Missing Display Name") + } + + return nil + } +} + +const testAccDataSourceAwsCanonicalUserIdConfig = ` +provider "aws" { + region = "us-west-2" +} + +data "aws_canonical_user_id" "current" { } +` diff --git a/builtin/providers/aws/provider.go b/builtin/providers/aws/provider.go index 390d17e2c5eb..5f517d611f9b 100644 --- a/builtin/providers/aws/provider.go +++ b/builtin/providers/aws/provider.go @@ -152,6 +152,7 @@ func Provider() terraform.ResourceProvider { "aws_availability_zones": dataSourceAwsAvailabilityZones(), "aws_billing_service_account": dataSourceAwsBillingServiceAccount(), "aws_caller_identity": dataSourceAwsCallerIdentity(), + "aws_canonical_user_id": dataSourceAwsCanonicalUserId(), "aws_cloudformation_stack": dataSourceAwsCloudFormationStack(), "aws_ebs_snapshot": dataSourceAwsEbsSnapshot(), "aws_ebs_volume": dataSourceAwsEbsVolume(), diff --git a/website/source/docs/providers/aws/d/canonical_user_id.html.markdown b/website/source/docs/providers/aws/d/canonical_user_id.html.markdown new file mode 100644 index 000000000000..68620d8946fb --- /dev/null +++ b/website/source/docs/providers/aws/d/canonical_user_id.html.markdown @@ -0,0 +1,35 @@ +--- +layout: "aws" +page_title: "AWS: aws_canonical_user_id" +sidebar_current: "docs-aws-canonical-user-id" +description: |- + Provides the canonical user ID for the AWS account associated with the provider + connection to AWS. +--- + +# aws\_canonical\_user\_id + +The Canonical User ID data source allows access to the [canonical user ID](http://docs.aws.amazon.com/general/latest/gr/acct-identifiers.html) +for the effective account in which Terraform is working. + +## Example Usage + +``` +data "aws_canonical_user_id" "current" { } + +output "canonical_user_id" { + value = "${data.aws_canonical_user_id.current.id}" +} +``` + +## Argument Reference + +There are no arguments available for this data source. + +## Attributes Reference + +The following attributes are exported: + +* `id` - The canonical user ID associated with the AWS account. + +* `display_name` - The human-friendly name linked to the canonical user ID. diff --git a/website/source/layouts/aws.erb b/website/source/layouts/aws.erb index b769317c41c1..9adee3a6001e 100644 --- a/website/source/layouts/aws.erb +++ b/website/source/layouts/aws.erb @@ -38,6 +38,9 @@ > aws_caller_identity + > + aws_canonical_user_id + > aws_cloudformation_stack