From 97ac4bde16b8157d8f68211889cb42d9f593583d Mon Sep 17 00:00:00 2001 From: clint shryock Date: Thu, 8 Dec 2016 14:08:45 -0600 Subject: [PATCH] implement name_prefix --- .../aws/resource_aws_lightsail_key_pair.go | 21 +++++++++--- .../resource_aws_lightsail_key_pair_test.go | 33 +++++++++++++++++++ .../aws/r/lightsail_key_pair.html.markdown | 3 +- 3 files changed, 51 insertions(+), 6 deletions(-) diff --git a/builtin/providers/aws/resource_aws_lightsail_key_pair.go b/builtin/providers/aws/resource_aws_lightsail_key_pair.go index d2d073381..24138aaa9 100644 --- a/builtin/providers/aws/resource_aws_lightsail_key_pair.go +++ b/builtin/providers/aws/resource_aws_lightsail_key_pair.go @@ -21,9 +21,11 @@ func resourceAwsLightsailKeyPair() *schema.Resource { Schema: map[string]*schema.Schema{ "name": { - Type: schema.TypeString, - Required: true, - ForceNew: true, + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + ConflictsWith: []string{"name_prefix"}, }, "name_prefix": { Type: schema.TypeString, @@ -76,7 +78,15 @@ func resourceAwsLightsailKeyPair() *schema.Resource { func resourceAwsLightsailKeyPairCreate(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).lightsailconn - kName := d.Get("name").(string) + var kName string + if v, ok := d.GetOk("name"); ok { + kName = v.(string) + } else if v, ok := d.GetOk("name_prefix"); ok { + kName = resource.PrefixedUniqueId(v.(string)) + } else { + kName = resource.UniqueId() + } + var pubKey string var op *lightsail.Operation if pubKeyInterface, ok := d.GetOk("public_key"); ok { @@ -160,7 +170,7 @@ func resourceAwsLightsailKeyPairRead(d *schema.ResourceData, meta interface{}) e conn := meta.(*AWSClient).lightsailconn resp, err := conn.GetKeyPair(&lightsail.GetKeyPairInput{ - KeyPairName: aws.String(d.Get("name").(string)), + KeyPairName: aws.String(d.Id()), }) if err != nil { @@ -177,6 +187,7 @@ func resourceAwsLightsailKeyPairRead(d *schema.ResourceData, meta interface{}) e } d.Set("arn", resp.KeyPair.Arn) + d.Set("name", resp.KeyPair.Name) d.Set("fingerprint", resp.KeyPair.Fingerprint) return nil diff --git a/builtin/providers/aws/resource_aws_lightsail_key_pair_test.go b/builtin/providers/aws/resource_aws_lightsail_key_pair_test.go index ea70d9b46..e2ff906e8 100644 --- a/builtin/providers/aws/resource_aws_lightsail_key_pair_test.go +++ b/builtin/providers/aws/resource_aws_lightsail_key_pair_test.go @@ -86,6 +86,27 @@ func TestAccAWSLightsailKeyPair_encrypted(t *testing.T) { }) } +func TestAccAWSLightsailKeyPair_nameprefix(t *testing.T) { + var conf1, conf2 lightsail.KeyPair + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSLightsailKeyPairDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSLightsailKeyPairConfig_prefixed(), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSLightsailKeyPairExists("aws_lightsail_key_pair.lightsail_key_pair_test_omit", &conf1), + testAccCheckAWSLightsailKeyPairExists("aws_lightsail_key_pair.lightsail_key_pair_test_prefixed", &conf2), + resource.TestCheckResourceAttrSet("aws_lightsail_key_pair.lightsail_key_pair_test_omit", "name"), + resource.TestCheckResourceAttrSet("aws_lightsail_key_pair.lightsail_key_pair_test_prefixed", "name"), + ), + }, + }, + }) +} + func testAccCheckAWSLightsailKeyPairExists(n string, res *lightsail.KeyPair) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] @@ -185,6 +206,18 @@ EOF `, lightsailName, key) } +func testAccAWSLightsailKeyPairConfig_prefixed() string { + return fmt.Sprintf(` +provider "aws" { + region = "us-east-1" +} +resource "aws_lightsail_key_pair" "lightsail_key_pair_test_omit" {} +resource "aws_lightsail_key_pair" "lightsail_key_pair_test_prefixed" { + name_prefix = "cts" +} +`) +} + const lightsailPubKey = `ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com` const testLightsailKeyPairPubKey1 = `mQENBFXbjPUBCADjNjCUQwfxKL+RR2GA6pv/1K+zJZ8UWIF9S0lk7cVIEfJiprzzwiMwBS5cD0da rGin1FHvIWOZxujA7oW0O2TUuatqI3aAYDTfRYurh6iKLC+VS+F7H+/mhfFvKmgr0Y5kDCF1j0T/ diff --git a/website/source/docs/providers/aws/r/lightsail_key_pair.html.markdown b/website/source/docs/providers/aws/r/lightsail_key_pair.html.markdown index d713995f6..b60e5a2ec 100644 --- a/website/source/docs/providers/aws/r/lightsail_key_pair.html.markdown +++ b/website/source/docs/providers/aws/r/lightsail_key_pair.html.markdown @@ -45,7 +45,8 @@ resource "aws_lightsail_key_pair" "lg_key_pair" { The following arguments are supported: -* `name` - (Required) The name of the Lightsail Key Pair +* `name` - (Optional) The name of the Lightsail Key Pair. If omitted, a unique +name will be generated by Terraform * `pgp_key` – (Optional) An optional PGP key to encrypt the resulting private key material. Only used when creating a new key pair * `public_key` - (Required) The public key material. This public key will be