Skip to content

Commit

Permalink
provider/aws: New DataSource: aws_partition (#11675)
Browse files Browse the repository at this point in the history
  • Loading branch information
ValFadeev authored and stack72 committed Feb 4, 2017
1 parent 3b3bb30 commit cb8d85f
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 0 deletions.
33 changes: 33 additions & 0 deletions builtin/providers/aws/data_source_aws_partition.go
Original file line number Diff line number Diff line change
@@ -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
}
44 changes: 44 additions & 0 deletions builtin/providers/aws/data_source_aws_partition_test.go
Original file line number Diff line number Diff line change
@@ -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" { }
`
1 change: 1 addition & 0 deletions builtin/providers/aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
40 changes: 40 additions & 0 deletions website/source/docs/providers/aws/d/partition.html.markdown
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 3 additions & 0 deletions website/source/layouts/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@
<li<%= sidebar_current("docs-aws-datasource-kms-secret") %>>
<a href="/docs/providers/aws/d/kms_secret.html">aws_kms_secret</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-partition") %>>
<a href="/docs/providers/aws/d/partition.html">aws_partition</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-prefix-list") %>>
<a href="/docs/providers/aws/d/prefix_list.html">aws_prefix_list</a>
</li>
Expand Down

0 comments on commit cb8d85f

Please sign in to comment.