Skip to content

Commit

Permalink
d/aws_kms_alias: Add target_key_arn attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
bflad committed Jan 18, 2018
1 parent fae04df commit 0008ef1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
19 changes: 19 additions & 0 deletions aws/data_source_aws_kms_alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"time"

"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/kms"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/schema"
Expand All @@ -23,6 +24,10 @@ func dataSourceAwsKmsAlias() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"target_key_arn": {
Type: schema.TypeString,
Computed: true,
},
"target_key_id": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -58,6 +63,20 @@ func dataSourceAwsKmsAliasRead(d *schema.ResourceData, meta interface{}) error {

d.SetId(time.Now().UTC().String())
d.Set("arn", alias.AliasArn)

aliasARN, err := arn.Parse(*alias.AliasArn)
if err != nil {
return err
}
targetKeyARN := arn.ARN{
Partition: aliasARN.Partition,
Service: aliasARN.Service,
Region: aliasARN.Region,
AccountID: aliasARN.AccountID,
Resource: fmt.Sprintf("key/%s", *alias.TargetKeyId),
}
d.Set("target_key_arn", targetKeyARN.String())

d.Set("target_key_id", alias.TargetKeyId)

return nil
Expand Down
10 changes: 10 additions & 0 deletions aws/data_source_aws_kms_alias_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aws

import (
"fmt"
"strings"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
Expand Down Expand Up @@ -47,6 +48,15 @@ func testAccDataSourceAwsKmsAliasCheck(name string) resource.TestCheckFunc {
)
}

expectedTargetKeyArnSuffix := fmt.Sprintf("key/%s", kmsKeyRs.Primary.Attributes["target_key_id"])
if !strings.HasSuffix(attr["target_key_arn"], expectedTargetKeyArnSuffix) {
return fmt.Errorf(
"target_key_arn is %s; want suffix %s",
attr["target_key_arn"],
expectedTargetKeyArnSuffix,
)
}

if attr["target_key_id"] != kmsKeyRs.Primary.Attributes["target_key_id"] {
return fmt.Errorf(
"target_key_id is %s; want %s",
Expand Down
1 change: 1 addition & 0 deletions website/docs/d/kms_alias.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ data "aws_kms_alias" "s3" {

* `arn` - The Amazon Resource Name(ARN) of the key alias.
* `target_key_id` - Key identifier pointed to by the alias.
* `target_key_arn` - ARN pointed to by the alias.

0 comments on commit 0008ef1

Please sign in to comment.