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:
parent
81e3157b62
commit
8949419bef
|
@ -27,10 +27,24 @@ func resourceAwsKeyPair() *schema.Resource {
|
||||||
|
|
||||||
Schema: map[string]*schema.Schema{
|
Schema: map[string]*schema.Schema{
|
||||||
"key_name": &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,
|
Type: schema.TypeString,
|
||||||
Optional: true,
|
Optional: true,
|
||||||
Computed: true,
|
|
||||||
ForceNew: 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{
|
"public_key": &schema.Schema{
|
||||||
Type: schema.TypeString,
|
Type: schema.TypeString,
|
||||||
|
@ -56,8 +70,12 @@ func resourceAwsKeyPair() *schema.Resource {
|
||||||
func resourceAwsKeyPairCreate(d *schema.ResourceData, meta interface{}) error {
|
func resourceAwsKeyPairCreate(d *schema.ResourceData, meta interface{}) error {
|
||||||
conn := meta.(*AWSClient).ec2conn
|
conn := meta.(*AWSClient).ec2conn
|
||||||
|
|
||||||
keyName := d.Get("key_name").(string)
|
var keyName string
|
||||||
if keyName == "" {
|
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()
|
keyName = resource.UniqueId()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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 = `
|
const testAccAWSKeyPairConfig = `
|
||||||
resource "aws_key_pair" "a_key_pair" {
|
resource "aws_key_pair" "a_key_pair" {
|
||||||
key_name = "tf-acc-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"
|
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"
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
|
@ -31,8 +31,9 @@ resource "aws_key_pair" "deployer" {
|
||||||
|
|
||||||
The following arguments are supported:
|
The following arguments are supported:
|
||||||
|
|
||||||
* `public_key` - (Required) The public key material.
|
|
||||||
* `key_name` - (Optional) The name for the key pair.
|
* `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
|
## Attributes Reference
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue