Skip to content

Commit dd0167f

Browse files
ValFadeevstack72
authored andcommitted
provider/aws: New DataSource: aws_partition (hashicorp#11675)
1 parent 0079f65 commit dd0167f

5 files changed

Lines changed: 121 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package aws
2+
3+
import (
4+
"log"
5+
"time"
6+
7+
"github.com/hashicorp/terraform/helper/schema"
8+
)
9+
10+
func dataSourceAwsPartition() *schema.Resource {
11+
return &schema.Resource{
12+
Read: dataSourceAwsPartitionRead,
13+
14+
Schema: map[string]*schema.Schema{
15+
"partition": {
16+
Type: schema.TypeString,
17+
Computed: true,
18+
},
19+
},
20+
}
21+
}
22+
23+
func dataSourceAwsPartitionRead(d *schema.ResourceData, meta interface{}) error {
24+
client := meta.(*AWSClient)
25+
26+
log.Printf("[DEBUG] Reading Partition.")
27+
d.SetId(time.Now().UTC().String())
28+
29+
log.Printf("[DEBUG] Setting AWS Partition to %s.", client.partition)
30+
d.Set("partition", meta.(*AWSClient).partition)
31+
32+
return nil
33+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package aws
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform/helper/resource"
8+
"github.com/hashicorp/terraform/terraform"
9+
)
10+
11+
func TestAccAWSPartition_basic(t *testing.T) {
12+
resource.Test(t, resource.TestCase{
13+
PreCheck: func() { testAccPreCheck(t) },
14+
Providers: testAccProviders,
15+
Steps: []resource.TestStep{
16+
{
17+
Config: testAccCheckAwsPartitionConfig_basic,
18+
Check: resource.ComposeTestCheckFunc(
19+
testAccCheckAwsPartition("data.aws_partition.current"),
20+
),
21+
},
22+
},
23+
})
24+
}
25+
26+
func testAccCheckAwsPartition(n string) resource.TestCheckFunc {
27+
return func(s *terraform.State) error {
28+
rs, ok := s.RootModule().Resources[n]
29+
if !ok {
30+
return fmt.Errorf("Can't find resource: %s", n)
31+
}
32+
33+
expected := testAccProvider.Meta().(*AWSClient).partition
34+
if rs.Primary.Attributes["partition"] != expected {
35+
return fmt.Errorf("Incorrect Partition: expected %q, got %q", expected, rs.Primary.Attributes["partition"])
36+
}
37+
38+
return nil
39+
}
40+
}
41+
42+
const testAccCheckAwsPartitionConfig_basic = `
43+
data "aws_partition" "current" { }
44+
`

builtin/providers/aws/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ func Provider() terraform.ResourceProvider {
174174
"aws_iam_server_certificate": dataSourceAwsIAMServerCertificate(),
175175
"aws_instance": dataSourceAwsInstance(),
176176
"aws_ip_ranges": dataSourceAwsIPRanges(),
177+
"aws_partition": dataSourceAwsPartition(),
177178
"aws_prefix_list": dataSourceAwsPrefixList(),
178179
"aws_redshift_service_account": dataSourceAwsRedshiftServiceAccount(),
179180
"aws_region": dataSourceAwsRegion(),
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
layout: "aws"
3+
page_title: "AWS: aws_partition"
4+
sidebar_current: "docs-aws-datasource-partition"
5+
description: |-
6+
Get AWS partition identifier
7+
---
8+
9+
# aws\_partition
10+
11+
Use this data source to lookup current AWS partition in which Terraform is working
12+
13+
## Example Usage
14+
15+
```
16+
data "aws_partition" "current" { }
17+
18+
data "aws_iam_policy_document" "s3_policy" {
19+
statement {
20+
sid = "1"
21+
22+
actions = [
23+
"s3:ListBucket",
24+
]
25+
26+
resources = [
27+
"arn:${data.aws_partition.current.partition}:s3:::my-bucket",
28+
]
29+
}
30+
}
31+
32+
```
33+
34+
## Argument Reference
35+
36+
There are no arguments available for this data source.
37+
38+
## Attributes Reference
39+
40+
`partition` is set to the identifier of the current partition.

website/source/layouts/aws.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@
8080
<li<%= sidebar_current("docs-aws-datasource-kms-secret") %>>
8181
<a href="/docs/providers/aws/d/kms_secret.html">aws_kms_secret</a>
8282
</li>
83+
<li<%= sidebar_current("docs-aws-datasource-partition") %>>
84+
<a href="/docs/providers/aws/d/partition.html">aws_partition</a>
85+
</li>
8386
<li<%= sidebar_current("docs-aws-datasource-prefix-list") %>>
8487
<a href="/docs/providers/aws/d/prefix_list.html">aws_prefix_list</a>
8588
</li>

0 commit comments

Comments
 (0)