-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
152 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,59 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/aws/aws-sdk-go/aws/arn" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func dataSourceAwsArn() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceAwsArnRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"arn": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validateArn, | ||
}, | ||
"partition": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"service": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"region": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"account": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"resource": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceAwsArnRead(d *schema.ResourceData, meta interface{}) error { | ||
v := d.Get("arn").(string) | ||
arn, err := arn.Parse(v) | ||
if err != nil { | ||
return fmt.Errorf("Error parsing '%s': %s", v, err.Error()) | ||
} | ||
|
||
d.SetId(arn.String()) | ||
d.Set("partition", arn.Partition) | ||
d.Set("service", arn.Service) | ||
d.Set("region", arn.Region) | ||
d.Set("account", arn.AccountID) | ||
d.Set("resource", arn.Resource) | ||
|
||
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,48 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestAccDataSourceAwsArn_basic(t *testing.T) { | ||
resourceName := "data.aws_arn.test" | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccDataSourceAwsArnConfig, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccDataSourceAwsArn(resourceName), | ||
resource.TestCheckResourceAttr(resourceName, "partition", "aws"), | ||
resource.TestCheckResourceAttr(resourceName, "service", "rds"), | ||
resource.TestCheckResourceAttr(resourceName, "region", "eu-west-1"), | ||
resource.TestCheckResourceAttr(resourceName, "account", "123456789012"), | ||
resource.TestCheckResourceAttr(resourceName, "resource", "db:mysql-db"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceAwsArn(name string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
_, ok := s.RootModule().Resources[name] | ||
if !ok { | ||
return fmt.Errorf("root module has no resource called %s", name) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
const testAccDataSourceAwsArnConfig = ` | ||
data "aws_arn" "test" { | ||
arn = "arn:aws:rds:eu-west-1:123456789012:db:mysql-db" | ||
} | ||
` |
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
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
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,41 @@ | ||
--- | ||
layout: "aws" | ||
page_title: "AWS: aws_arn" | ||
sidebar_current: "docs-aws-datasource-arn" | ||
description: |- | ||
Parses an ARN into its constituent parts. | ||
--- | ||
|
||
# Data Source: aws_arn | ||
|
||
Parses an Amazon Resource Name (ARN) into its constituent parts. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "aws_arn" "db_instance" { | ||
arn = "arn:aws:rds:eu-west-1:123456789012:db:mysql-db" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `arn` - (Required) The ARN to parse. | ||
|
||
## Attributes Reference | ||
|
||
The following attributes are exported: | ||
|
||
* `partition` - The partition that the resource is in. | ||
|
||
* `service` - The [service namespace](https://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html#genref-aws-service-namespaces) that identifies the AWS product. | ||
|
||
* `region` - The region the resource resides in. | ||
Note that the ARNs for some resources do not require a region, so this component might be omitted. | ||
|
||
* `account` - The [ID](https://docs.aws.amazon.com/general/latest/gr/acct-identifiers.html) of the AWS account that owns the resource, without the hyphens. | ||
|
||
* `resource` - The content of this part of the ARN varies by service. | ||
It often includes an indicator of the type of resource—for example, an IAM user or Amazon RDS database —followed by a slash (/) or a colon (:), followed by the resource name itself. |