From d6cd47fd5e0272fc7b3cb20fc507e9e4c264db58 Mon Sep 17 00:00:00 2001 From: Andy Chan Date: Fri, 11 Mar 2016 12:54:23 -0800 Subject: [PATCH 1/2] Reformat the kms alias test code --- .../providers/aws/resource_aws_kms_alias.go | 28 ++++++++++++- .../aws/resource_aws_kms_alias_test.go | 41 +++++++++++++++++++ 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/builtin/providers/aws/resource_aws_kms_alias.go b/builtin/providers/aws/resource_aws_kms_alias.go index ee9e2241c3ee..23bbf0b377a2 100644 --- a/builtin/providers/aws/resource_aws_kms_alias.go +++ b/builtin/providers/aws/resource_aws_kms_alias.go @@ -5,6 +5,7 @@ import ( "log" "regexp" + "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" "github.com/aws/aws-sdk-go/aws" @@ -24,8 +25,22 @@ func resourceAwsKmsAlias() *schema.Resource { Computed: true, }, "name": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + ConflictsWith: []string{"name_prefix"}, + ValidateFunc: func(v interface{}, k string) (ws []string, es []error) { + value := v.(string) + if !regexp.MustCompile(`^(alias\/)[a-zA-Z0-9:/_-]+$`).MatchString(value) { + es = append(es, fmt.Errorf( + "%q must begin with 'alias/' and be comprised of only [a-zA-Z0-9:/_-]", k)) + } + return + }, + }, + "name_prefix": &schema.Schema{ Type: schema.TypeString, - Required: true, + Optional: true, ForceNew: true, ValidateFunc: func(v interface{}, k string) (ws []string, es []error) { value := v.(string) @@ -46,7 +61,16 @@ func resourceAwsKmsAlias() *schema.Resource { func resourceAwsKmsAliasCreate(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).kmsconn - name := d.Get("name").(string) + + var name string + if v, ok := d.GetOk("name"); ok { + name = v.(string) + } else if v, ok := d.GetOk("name_prefix"); ok { + name = resource.PrefixedUniqueId(v.(string)) + } else { + name = resource.PrefixedUniqueId("alias/") + } + targetKeyId := d.Get("target_key_id").(string) log.Printf("[DEBUG] KMS alias create name: %s, target_key: %s", name, targetKeyId) diff --git a/builtin/providers/aws/resource_aws_kms_alias_test.go b/builtin/providers/aws/resource_aws_kms_alias_test.go index 7b19db5243c4..65349fda2232 100644 --- a/builtin/providers/aws/resource_aws_kms_alias_test.go +++ b/builtin/providers/aws/resource_aws_kms_alias_test.go @@ -31,6 +31,38 @@ func TestAccAWSKmsAlias_basic(t *testing.T) { }) } +func TestAccAWSKmsAlias_name_prefix(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSKmsAliasDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccAWSKmsSingleAlias, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSKmsAliasExists("aws_kms_alias.name_prefix"), + ), + }, + }, + }) +} + +func TestAccAWSKmsAlias_no_name(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSKmsAliasDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccAWSKmsSingleAlias, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSKmsAliasExists("aws_kms_alias.nothing"), + ), + }, + }, + }) +} + func TestAccAWSKmsAlias_multiple(t *testing.T) { resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -92,6 +124,15 @@ resource "aws_kms_key" "two" { deletion_window_in_days = 7 } +resource "aws_kms_alias" "name_prefix" { + name_prefix = "alias/tf-acc-key-alias" + target_key_id = "${aws_kms_key.one.key_id}" +} + +resource "aws_kms_alias" "nothing" { + target_key_id = "${aws_kms_key.one.key_id}" +} + resource "aws_kms_alias" "single" { name = "alias/tf-acc-key-alias" target_key_id = "${aws_kms_key.one.key_id}" From f7b85777d638210c4a3ad01ccc80a4b1b4a00dad Mon Sep 17 00:00:00 2001 From: Andy Chan Date: Fri, 11 Mar 2016 15:31:57 -0800 Subject: [PATCH 2/2] Added documentation for kms alias name_prefix --- website/source/docs/providers/aws/r/kms_alias.html.markdown | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/website/source/docs/providers/aws/r/kms_alias.html.markdown b/website/source/docs/providers/aws/r/kms_alias.html.markdown index 9efe4377ce29..3320b3db45a7 100644 --- a/website/source/docs/providers/aws/r/kms_alias.html.markdown +++ b/website/source/docs/providers/aws/r/kms_alias.html.markdown @@ -28,7 +28,10 @@ resource "aws_kms_alias" "a" { The following arguments are supported: -* `name` - (Required) The display name of the alias. The name must start with the word "alias" followed by a forward slash (alias/) + +* `name` - (Optional) The display name of the alias. The name must start with the word "alias" followed by a forward slash (alias/) +* `name_prefix` - (Optional) Creates an unique alias beginning with the specified prefix. +The name must start with the word "alias" followed by a forward slash (alias/). Conflicts with `name`. * `target_key_id` - (Required) Identifier for the key for which the alias is for, can be either an ARN or key_id. ## Attributes Reference