From cb8d85fc5003713375f462ffd1da6ed24175bf17 Mon Sep 17 00:00:00 2001 From: ValFadeev Date: Sat, 4 Feb 2017 11:54:45 +0000 Subject: [PATCH] provider/aws: New DataSource: aws_partition (#11675) --- .../aws/data_source_aws_partition.go | 33 ++++++++++++++ .../aws/data_source_aws_partition_test.go | 44 +++++++++++++++++++ builtin/providers/aws/provider.go | 1 + .../providers/aws/d/partition.html.markdown | 40 +++++++++++++++++ website/source/layouts/aws.erb | 3 ++ 5 files changed, 121 insertions(+) create mode 100644 builtin/providers/aws/data_source_aws_partition.go create mode 100644 builtin/providers/aws/data_source_aws_partition_test.go create mode 100644 website/source/docs/providers/aws/d/partition.html.markdown diff --git a/builtin/providers/aws/data_source_aws_partition.go b/builtin/providers/aws/data_source_aws_partition.go new file mode 100644 index 000000000..d52f7ee47 --- /dev/null +++ b/builtin/providers/aws/data_source_aws_partition.go @@ -0,0 +1,33 @@ +package aws + +import ( + "log" + "time" + + "github.com/hashicorp/terraform/helper/schema" +) + +func dataSourceAwsPartition() *schema.Resource { + return &schema.Resource{ + Read: dataSourceAwsPartitionRead, + + Schema: map[string]*schema.Schema{ + "partition": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func dataSourceAwsPartitionRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*AWSClient) + + log.Printf("[DEBUG] Reading Partition.") + d.SetId(time.Now().UTC().String()) + + log.Printf("[DEBUG] Setting AWS Partition to %s.", client.partition) + d.Set("partition", meta.(*AWSClient).partition) + + return nil +} diff --git a/builtin/providers/aws/data_source_aws_partition_test.go b/builtin/providers/aws/data_source_aws_partition_test.go new file mode 100644 index 000000000..5610a0b96 --- /dev/null +++ b/builtin/providers/aws/data_source_aws_partition_test.go @@ -0,0 +1,44 @@ +package aws + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccAWSPartition_basic(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccCheckAwsPartitionConfig_basic, + Check: resource.ComposeTestCheckFunc( + testAccCheckAwsPartition("data.aws_partition.current"), + ), + }, + }, + }) +} + +func testAccCheckAwsPartition(n string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Can't find resource: %s", n) + } + + expected := testAccProvider.Meta().(*AWSClient).partition + if rs.Primary.Attributes["partition"] != expected { + return fmt.Errorf("Incorrect Partition: expected %q, got %q", expected, rs.Primary.Attributes["partition"]) + } + + return nil + } +} + +const testAccCheckAwsPartitionConfig_basic = ` +data "aws_partition" "current" { } +` diff --git a/builtin/providers/aws/provider.go b/builtin/providers/aws/provider.go index fcdcf5ca5..f71009f9b 100644 --- a/builtin/providers/aws/provider.go +++ b/builtin/providers/aws/provider.go @@ -174,6 +174,7 @@ func Provider() terraform.ResourceProvider { "aws_iam_server_certificate": dataSourceAwsIAMServerCertificate(), "aws_instance": dataSourceAwsInstance(), "aws_ip_ranges": dataSourceAwsIPRanges(), + "aws_partition": dataSourceAwsPartition(), "aws_prefix_list": dataSourceAwsPrefixList(), "aws_redshift_service_account": dataSourceAwsRedshiftServiceAccount(), "aws_region": dataSourceAwsRegion(), diff --git a/website/source/docs/providers/aws/d/partition.html.markdown b/website/source/docs/providers/aws/d/partition.html.markdown new file mode 100644 index 000000000..79e70cc2f --- /dev/null +++ b/website/source/docs/providers/aws/d/partition.html.markdown @@ -0,0 +1,40 @@ +--- +layout: "aws" +page_title: "AWS: aws_partition" +sidebar_current: "docs-aws-datasource-partition" +description: |- + Get AWS partition identifier +--- + +# aws\_partition + +Use this data source to lookup current AWS partition in which Terraform is working + +## Example Usage + +``` +data "aws_partition" "current" { } + +data "aws_iam_policy_document" "s3_policy" { + statement { + sid = "1" + + actions = [ + "s3:ListBucket", + ] + + resources = [ + "arn:${data.aws_partition.current.partition}:s3:::my-bucket", + ] + } +} + +``` + +## Argument Reference + +There are no arguments available for this data source. + +## Attributes Reference + +`partition` is set to the identifier of the current partition. diff --git a/website/source/layouts/aws.erb b/website/source/layouts/aws.erb index 35e1f8b6c..6c275c6c5 100644 --- a/website/source/layouts/aws.erb +++ b/website/source/layouts/aws.erb @@ -80,6 +80,9 @@ > aws_kms_secret + > + aws_partition + > aws_prefix_list