Skip to content

Commit

Permalink
Merge pull request hashicorp#3707 from loivis/validatefunc/key_pair
Browse files Browse the repository at this point in the history
resource/key_pair: drop custom ValidateFunc for name_prefix
  • Loading branch information
bflad authored Mar 16, 2018
2 parents e9de732 + 9a7d309 commit 405fd79
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 28 deletions.
24 changes: 9 additions & 15 deletions aws/resource_aws_key_pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,21 @@ func resourceAwsKeyPair() *schema.Resource {
MigrateState: resourceAwsKeyPairMigrateState,

Schema: map[string]*schema.Schema{
"key_name": &schema.Schema{
"key_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ConflictsWith: []string{"key_name_prefix"},
ValidateFunc: validateMaxLength(255),
},
"key_name_prefix": &schema.Schema{
Type: schema.TypeString,
Optional: 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
},
"key_name_prefix": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validateMaxLength(255 - resource.UniqueIDSuffixLength),
},
"public_key": &schema.Schema{
"public_key": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Expand All @@ -59,7 +53,7 @@ func resourceAwsKeyPair() *schema.Resource {
}
},
},
"fingerprint": &schema.Schema{
"fingerprint": {
Type: schema.TypeString,
Computed: true,
},
Expand Down
24 changes: 11 additions & 13 deletions aws/resource_aws_key_pair_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func testSweepKeyPairs(region string) error {

resp, err := ec2conn.DescribeKeyPairs(&ec2.DescribeKeyPairsInput{
Filters: []*ec2.Filter{
&ec2.Filter{
{
Name: aws.String("key-name"),
Values: []*string{aws.String("tmp-key*")},
},
Expand Down Expand Up @@ -62,7 +62,7 @@ func TestAccAWSKeyPair_basic(t *testing.T) {
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSKeyPairDestroy,
Steps: []resource.TestStep{
resource.TestStep{
{
Config: testAccAWSKeyPairConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSKeyPairExists("aws_key_pair.a_key_pair", &conf),
Expand All @@ -81,7 +81,7 @@ func TestAccAWSKeyPair_generatedName(t *testing.T) {
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSKeyPairDestroy,
Steps: []resource.TestStep{
resource.TestStep{
{
Config: testAccAWSKeyPairConfig_generatedName,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSKeyPairExists("aws_key_pair.a_key_pair", &conf),
Expand Down Expand Up @@ -182,20 +182,18 @@ func testAccCheckAWSKeyPair_namePrefix(t *testing.T) {
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-"),
testAccCheckAWSKeyPairGeneratedNamePrefix("aws_key_pair.a_key_pair", "baz-"),
),
},
},
})
}

func testAccCheckAWSKeyPairGeneratedNamePrefix(
resource, prefix string) resource.TestCheckFunc {
func testAccCheckAWSKeyPairGeneratedNamePrefix(resource, prefix string) resource.TestCheckFunc {
return func(s *terraform.State) error {
r, ok := s.RootModule().Resources[resource]
if !ok {
Expand All @@ -214,20 +212,20 @@ func testAccCheckAWSKeyPairGeneratedNamePrefix(

const testAccAWSKeyPairConfig = `
resource "aws_key_pair" "a_key_pair" {
key_name = "tf-acc-key-pair"
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 [email protected]"
key_name = "tf-acc-key-pair"
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 [email protected]"
}
`

const testAccAWSKeyPairConfig_generatedName = `
resource "aws_key_pair" "a_key_pair" {
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 [email protected]"
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 [email protected]"
}
`

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 [email protected]"
key_name_prefix = "baz-"
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 [email protected]"
}
`

0 comments on commit 405fd79

Please sign in to comment.