Skip to content

Commit

Permalink
Add 'aws_arn' data source.
Browse files Browse the repository at this point in the history
  • Loading branch information
ewbankkit committed Mar 30, 2018
1 parent 0095b64 commit 7b888ee
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 0 deletions.
59 changes: 59 additions & 0 deletions aws/data_source_aws_arn.go
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
}
48 changes: 48 additions & 0 deletions aws/data_source_aws_arn_test.go
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"
}
`
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ func Provider() terraform.ResourceProvider {
"aws_acm_certificate": dataSourceAwsAcmCertificate(),
"aws_ami": dataSourceAwsAmi(),
"aws_ami_ids": dataSourceAwsAmiIds(),
"aws_arn": dataSourceAwsArn(),
"aws_autoscaling_groups": dataSourceAwsAutoscalingGroups(),
"aws_availability_zone": dataSourceAwsAvailabilityZone(),
"aws_availability_zones": dataSourceAwsAvailabilityZones(),
Expand Down
3 changes: 3 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
<li<%= sidebar_current("docs-aws-datasource-ami-ids") %>>
<a href="/docs/providers/aws/d/ami_ids.html">aws_ami_ids</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-arn") %>>
<a href="/docs/providers/aws/d/arn.html">aws_arn</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-autoscaling-groups") %>>
<a href="/docs/providers/aws/d/autoscaling_groups.html">aws_autoscaling_groups</a>
</li>
Expand Down
41 changes: 41 additions & 0 deletions website/docs/d/arn.html.markdown
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.

0 comments on commit 7b888ee

Please sign in to comment.