implement name_prefix

This commit is contained in:
clint shryock 2016-12-08 14:08:45 -06:00
parent 86bb969087
commit 97ac4bde16
3 changed files with 51 additions and 6 deletions

View File

@ -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

View File

@ -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/

View File

@ -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