-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
provider/aws: New DataSource: aws_partition (#11675)
- Loading branch information
Showing
5 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" { } | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
website/source/docs/providers/aws/d/partition.html.markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters