From 74587baa4ae1d3d7ee7be5c66e635b4a2596c58d Mon Sep 17 00:00:00 2001 From: Steve Hoeksema Date: Wed, 7 Sep 2016 17:43:45 +1200 Subject: [PATCH 1/4] Add AWS Billing & Cost Management service account This adds a very simple data source for the AWS Billing account ID magic number. Used to allow AWS to dump detailed billing reports into an S3 bucket you control. http://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/billing-getting-started.html#step-2 --- ...data_source_aws_billing_service_account.go | 31 ++++++++++ ...source_aws_billing_service_account_test.go | 27 +++++++++ builtin/providers/aws/provider.go | 1 + .../aws/d/billing_service_account.markdown | 60 +++++++++++++++++++ 4 files changed, 119 insertions(+) create mode 100644 builtin/providers/aws/data_source_aws_billing_service_account.go create mode 100644 builtin/providers/aws/data_source_aws_billing_service_account_test.go create mode 100644 website/source/docs/providers/aws/d/billing_service_account.markdown diff --git a/builtin/providers/aws/data_source_aws_billing_service_account.go b/builtin/providers/aws/data_source_aws_billing_service_account.go new file mode 100644 index 000000000000..75ba32f94b9e --- /dev/null +++ b/builtin/providers/aws/data_source_aws_billing_service_account.go @@ -0,0 +1,31 @@ +package aws + +import ( + "fmt" + + "github.com/hashicorp/terraform/helper/schema" +) + +// See http://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/billing-getting-started.html#step-2 +var billingAccountId = "386209384616" + +func dataSourceAwsBillingServiceAccount() *schema.Resource { + return &schema.Resource{ + Read: dataSourceAwsBillingServiceAccountRead, + + Schema: map[string]*schema.Schema{ + "arn": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func dataSourceAwsBillingServiceAccountRead(d *schema.ResourceData, meta interface{}) error { + d.SetId(billingAccountId) + + d.Set("arn", "arn:aws:iam::"+billingAccountId+":root") + + return nil +} diff --git a/builtin/providers/aws/data_source_aws_billing_service_account_test.go b/builtin/providers/aws/data_source_aws_billing_service_account_test.go new file mode 100644 index 000000000000..5273e425f4a0 --- /dev/null +++ b/builtin/providers/aws/data_source_aws_billing_service_account_test.go @@ -0,0 +1,27 @@ +package aws + +import ( + "testing" + + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccAWSBillingServiceAccount_basic(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccCheckAwsBillingServiceAccountConfig, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.aws_billing_service_account.main", "id", "386209384616"), + resource.TestCheckResourceAttr("data.aws_billing_service_account.main", "arn", "arn:aws:iam::386209384616:root"), + ), + } + }, + }) +} + +const testAccCheckAwsBillingServiceAccountConfig = ` +data "aws_billing_service_account" "main" { } +` diff --git a/builtin/providers/aws/provider.go b/builtin/providers/aws/provider.go index e7b23fd615aa..860602bf55da 100644 --- a/builtin/providers/aws/provider.go +++ b/builtin/providers/aws/provider.go @@ -145,6 +145,7 @@ func Provider() terraform.ResourceProvider { DataSourcesMap: map[string]*schema.Resource{ "aws_ami": dataSourceAwsAmi(), "aws_availability_zones": dataSourceAwsAvailabilityZones(), + "aws_billing_service_account": dataSourceAwsBillingServiceAccount(), "aws_caller_identity": dataSourceAwsCallerIdentity(), "aws_cloudformation_stack": dataSourceAwsCloudFormationStack(), "aws_ecs_container_definition": dataSourceAwsEcsContainerDefinition(), diff --git a/website/source/docs/providers/aws/d/billing_service_account.markdown b/website/source/docs/providers/aws/d/billing_service_account.markdown new file mode 100644 index 000000000000..6594ea4655e3 --- /dev/null +++ b/website/source/docs/providers/aws/d/billing_service_account.markdown @@ -0,0 +1,60 @@ +--- +layout: "aws" +page_title: "AWS: aws_billing_service_account" +sidebar_current: "docs-aws-datasource-billing-service-account" +description: |- + Get AWS Billing Service Account +--- + +# aws\_billing\_service\_account + +Use this data source to get the Account ID of the [AWS Billing and Cost Management Service Account](http://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/billing-getting-started.html#step-2) for the purpose of whitelisting in S3 bucket policy. + +## Example Usage + +``` +data "aws_billing_service_account" "main" { } + +resource "aws_s3_bucket" "billing_logs" { + bucket = "my-billing-tf-test-bucket" + acl = "private" + policy = < Date: Thu, 8 Sep 2016 10:58:13 +1200 Subject: [PATCH 2/4] Fix formatting --- ...data_source_aws_billing_service_account.go | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/builtin/providers/aws/data_source_aws_billing_service_account.go b/builtin/providers/aws/data_source_aws_billing_service_account.go index 75ba32f94b9e..8353261994c1 100644 --- a/builtin/providers/aws/data_source_aws_billing_service_account.go +++ b/builtin/providers/aws/data_source_aws_billing_service_account.go @@ -1,31 +1,31 @@ package aws import ( - "fmt" + "fmt" - "github.com/hashicorp/terraform/helper/schema" + "github.com/hashicorp/terraform/helper/schema" ) // See http://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/billing-getting-started.html#step-2 var billingAccountId = "386209384616" func dataSourceAwsBillingServiceAccount() *schema.Resource { - return &schema.Resource{ - Read: dataSourceAwsBillingServiceAccountRead, - - Schema: map[string]*schema.Schema{ - "arn": &schema.Schema{ - Type: schema.TypeString, - Computed: true, - }, - }, - } + return &schema.Resource{ + Read: dataSourceAwsBillingServiceAccountRead, + + Schema: map[string]*schema.Schema{ + "arn": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + }, + } } func dataSourceAwsBillingServiceAccountRead(d *schema.ResourceData, meta interface{}) error { - d.SetId(billingAccountId) + d.SetId(billingAccountId) - d.Set("arn", "arn:aws:iam::"+billingAccountId+":root") + d.Set("arn", "arn:aws:iam::"+billingAccountId+":root") - return nil + return nil } From e65be3d25634d3f7618d35e30075909c764956ed Mon Sep 17 00:00:00 2001 From: Steve Hoeksema Date: Thu, 8 Sep 2016 15:51:31 +1200 Subject: [PATCH 3/4] Format with tabs, add missing comma --- ...source_aws_billing_service_account_test.go | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/builtin/providers/aws/data_source_aws_billing_service_account_test.go b/builtin/providers/aws/data_source_aws_billing_service_account_test.go index 5273e425f4a0..adfee84f85b0 100644 --- a/builtin/providers/aws/data_source_aws_billing_service_account_test.go +++ b/builtin/providers/aws/data_source_aws_billing_service_account_test.go @@ -1,25 +1,25 @@ package aws import ( - "testing" + "testing" - "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/helper/resource" ) func TestAccAWSBillingServiceAccount_basic(t *testing.T) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - resource.TestStep{ - Config: testAccCheckAwsBillingServiceAccountConfig, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("data.aws_billing_service_account.main", "id", "386209384616"), - resource.TestCheckResourceAttr("data.aws_billing_service_account.main", "arn", "arn:aws:iam::386209384616:root"), - ), - } - }, - }) + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccCheckAwsBillingServiceAccountConfig, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.aws_billing_service_account.main", "id", "386209384616"), + resource.TestCheckResourceAttr("data.aws_billing_service_account.main", "arn", "arn:aws:iam::386209384616:root"), + ), + }, + }, + }) } const testAccCheckAwsBillingServiceAccountConfig = ` From 0cf330000e661d8f6aa076cc28ee60998405ce00 Mon Sep 17 00:00:00 2001 From: Steve Hoeksema Date: Fri, 9 Sep 2016 09:00:48 +1200 Subject: [PATCH 4/4] Don't import fmt --- .../providers/aws/data_source_aws_billing_service_account.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/builtin/providers/aws/data_source_aws_billing_service_account.go b/builtin/providers/aws/data_source_aws_billing_service_account.go index 8353261994c1..f5bfb838727e 100644 --- a/builtin/providers/aws/data_source_aws_billing_service_account.go +++ b/builtin/providers/aws/data_source_aws_billing_service_account.go @@ -1,8 +1,6 @@ package aws import ( - "fmt" - "github.com/hashicorp/terraform/helper/schema" )