Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource/aws_kms_alias: Add target_key_arn attribute #3096

Merged
merged 1 commit into from
Jan 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions aws/resource_aws_kms_alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/hashicorp/terraform/helper/schema"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/kms"
)

Expand All @@ -25,18 +26,18 @@ func resourceAwsKmsAlias() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"arn": &schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"name": &schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"name_prefix"},
ValidateFunc: validateAwsKmsName,
},
"name_prefix": &schema.Schema{
"name_prefix": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Expand All @@ -49,10 +50,14 @@ func resourceAwsKmsAlias() *schema.Resource {
return
},
},
"target_key_id": &schema.Schema{
"target_key_id": {
Type: schema.TypeString,
Required: true,
},
"target_key_arn": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -113,6 +118,19 @@ func resourceAwsKmsAliasRead(d *schema.ResourceData, meta interface{}) error {
d.Set("arn", alias.AliasArn)
d.Set("target_key_id", alias.TargetKeyId)

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())

return nil
}

Expand All @@ -124,6 +142,7 @@ func resourceAwsKmsAliasUpdate(d *schema.ResourceData, meta interface{}) error {
if err != nil {
return err
}
return resourceAwsKmsAliasRead(d, meta)
}
return nil
}
Expand Down
5 changes: 5 additions & 0 deletions aws/resource_aws_kms_alias_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestAccAWSKmsAlias_basic(t *testing.T) {
Config: testAccAWSKmsSingleAlias(rInt, kmsAliasTimestamp),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSKmsAliasExists("aws_kms_alias.single"),
resource.TestCheckResourceAttrSet("aws_kms_alias.single", "target_key_arn"),
),
},
{
Expand All @@ -46,6 +47,7 @@ func TestAccAWSKmsAlias_name_prefix(t *testing.T) {
Config: testAccAWSKmsSingleAlias(rInt, kmsAliasTimestamp),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSKmsAliasExists("aws_kms_alias.name_prefix"),
resource.TestCheckResourceAttrSet("aws_kms_alias.name_prefix", "target_key_arn"),
),
},
},
Expand All @@ -64,6 +66,7 @@ func TestAccAWSKmsAlias_no_name(t *testing.T) {
Config: testAccAWSKmsSingleAlias(rInt, kmsAliasTimestamp),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSKmsAliasExists("aws_kms_alias.nothing"),
resource.TestCheckResourceAttrSet("aws_kms_alias.nothing", "target_key_arn"),
),
},
},
Expand All @@ -82,7 +85,9 @@ func TestAccAWSKmsAlias_multiple(t *testing.T) {
Config: testAccAWSKmsMultipleAliases(rInt, kmsAliasTimestamp),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSKmsAliasExists("aws_kms_alias.one"),
resource.TestCheckResourceAttrSet("aws_kms_alias.one", "target_key_arn"),
testAccCheckAWSKmsAliasExists("aws_kms_alias.two"),
resource.TestCheckResourceAttrSet("aws_kms_alias.two", "target_key_arn"),
),
},
},
Expand Down
3 changes: 2 additions & 1 deletion website/docs/r/kms_alias.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ The name must start with the word "alias" followed by a forward slash (alias/).

## Attributes Reference

The following attributes are exported:
The following additional attributes are exported:

* `arn` - The Amazon Resource Name (ARN) of the key alias.
* `target_key_arn` - The Amazon Resource Name (ARN) of the target key identifier.

## Import

Expand Down