provider/aws: Add key_name_prefix argument to aws_key_pair resource (#9993)

* Added key_name_prefix to aws_key_pair resource schema.

* Added logic to prefix the aws_key_pair name on create.

* Added aws_key_pair test config for key_name_prefix case.

* Copied test cases from testAccAWSSecurityGroup namespace.

* Modified copied test case to suit aws_key_pair resource.

* Changed required flag to optional on key_name argument for aws_key_pair resource.

* Added documentation for key_name_prefix argument.

* Code style fix.

* Fixed undefined variable error in test.
This commit is contained in:
Nick Santamaria 2016-11-10 02:35:51 +11:00 committed by Paul Stack
parent 81e3157b62
commit 8949419bef
3 changed files with 71 additions and 5 deletions

View File

@ -27,10 +27,24 @@ func resourceAwsKeyPair() *schema.Resource {
Schema: map[string]*schema.Schema{
"key_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ConflictsWith: []string{"key_name_prefix"},
},
"key_name_prefix": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if len(value) > 100 {
errors = append(errors, fmt.Errorf(
"%q cannot be longer than 100 characters, name is limited to 255", k))
}
return
},
},
"public_key": &schema.Schema{
Type: schema.TypeString,
@ -56,8 +70,12 @@ func resourceAwsKeyPair() *schema.Resource {
func resourceAwsKeyPairCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
keyName := d.Get("key_name").(string)
if keyName == "" {
var keyName string
if v, ok := d.GetOk("key_name"); ok {
keyName = v.(string)
} else if v, ok := d.GetOk("key_name_prefix"); ok {
keyName = resource.PrefixedUniqueId(v.(string))
} else {
keyName = resource.UniqueId()
}

View File

@ -130,6 +130,46 @@ func testAccCheckAWSKeyPairExists(n string, res *ec2.KeyPairInfo) resource.TestC
}
}
func testAccCheckAWSKeyPair_namePrefix(t *testing.T) {
var conf ec2.KeyPairInfo
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
IDRefreshName: "aws_key_pair.a_key_pair",
IDRefreshIgnore: []string{"key_name_prefix"},
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSKeyPairDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCheckAWSKeyPairPrefixNameConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSKeyPairExists("aws_key_pair.a_key_pair", &conf),
testAccCheckAWSKeyPairGeneratedNamePrefix(
"aws_key_pair.a_key_pair", "baz-"),
),
},
},
})
}
func testAccCheckAWSKeyPairGeneratedNamePrefix(
resource, prefix string) resource.TestCheckFunc {
return func(s *terraform.State) error {
r, ok := s.RootModule().Resources[resource]
if !ok {
return fmt.Errorf("Resource not found")
}
name, ok := r.Primary.Attributes["name"]
if !ok {
return fmt.Errorf("Name attr not found: %#v", r.Primary.Attributes)
}
if !strings.HasPrefix(name, prefix) {
return fmt.Errorf("Name: %q, does not have prefix: %q", name, prefix)
}
return nil
}
}
const testAccAWSKeyPairConfig = `
resource "aws_key_pair" "a_key_pair" {
key_name = "tf-acc-key-pair"
@ -142,3 +182,10 @@ resource "aws_key_pair" "a_key_pair" {
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com"
}
`
const testAccCheckAWSKeyPairPrefixNameConfig = `
resource "aws_key_pair" "a_key_pair" {
key_name_prefix = "baz-"
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com"
}
`

View File

@ -31,8 +31,9 @@ resource "aws_key_pair" "deployer" {
The following arguments are supported:
* `public_key` - (Required) The public key material.
* `key_name` - (Optional) The name for the key pair.
* `key_name_prefix` - (Optional) Creates a unique name beginning with the specified prefix. Conflicts with `key_name`.
* `public_key` - (Required) The public key material.
## Attributes Reference
@ -47,4 +48,4 @@ Key Pairs can be imported using the `key_name`, e.g.
```
$ terraform import aws_key_pair.deployer deployer-key
```
```