From 7ae22ca1a3c9e03d8abb1bea72725dbaace0bf60 Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Thu, 23 Apr 2020 18:37:47 +0300 Subject: [PATCH 1/2] key pair tag on create --- aws/resource_aws_key_pair.go | 28 ++-------------------------- aws/resource_aws_key_pair_test.go | 30 ++++++++++++------------------ 2 files changed, 14 insertions(+), 44 deletions(-) diff --git a/aws/resource_aws_key_pair.go b/aws/resource_aws_key_pair.go index afeff33d8f4..deb1a7307f3 100644 --- a/aws/resource_aws_key_pair.go +++ b/aws/resource_aws_key_pair.go @@ -10,7 +10,6 @@ import ( "github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags" "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/service/ec2" ) @@ -87,6 +86,7 @@ func resourceAwsKeyPairCreate(d *schema.ResourceData, meta interface{}) error { req := &ec2.ImportKeyPairInput{ KeyName: aws.String(keyName), PublicKeyMaterial: []byte(publicKey), + TagSpecifications: ec2TagSpecificationsFromMap(d.Get("tags").(map[string]interface{}), ec2.ResourceTypeKeyPair), } resp, err := conn.ImportKeyPair(req) if err != nil { @@ -95,29 +95,6 @@ func resourceAwsKeyPairCreate(d *schema.ResourceData, meta interface{}) error { d.SetId(*resp.KeyName) - if v := d.Get("tags").(map[string]interface{}); len(v) > 0 { - readReq := &ec2.DescribeKeyPairsInput{ - KeyNames: []*string{aws.String(d.Id())}, - } - readResp, err := conn.DescribeKeyPairs(readReq) - if err != nil { - awsErr, ok := err.(awserr.Error) - if ok && awsErr.Code() == "InvalidKeyPair.NotFound" { - d.SetId("") - return nil - } - return fmt.Errorf("Error retrieving KeyPair: %s", err) - } - - for _, keyPair := range readResp.KeyPairs { - if *keyPair.KeyName == d.Id() { - if err := keyvaluetags.Ec2CreateTags(conn, aws.StringValue(keyPair.KeyPairId), v); err != nil { - return fmt.Errorf("error adding tags: %s", err) - } - } - } - - } return resourceAwsKeyPairRead(d, meta) } @@ -128,8 +105,7 @@ func resourceAwsKeyPairRead(d *schema.ResourceData, meta interface{}) error { } resp, err := conn.DescribeKeyPairs(req) if err != nil { - awsErr, ok := err.(awserr.Error) - if ok && awsErr.Code() == "InvalidKeyPair.NotFound" { + if isAWSErr(err, "InvalidKeyPair.NotFound", "") { d.SetId("") return nil } diff --git a/aws/resource_aws_key_pair_test.go b/aws/resource_aws_key_pair_test.go index 143f3734a62..9ef05cdc3fc 100644 --- a/aws/resource_aws_key_pair_test.go +++ b/aws/resource_aws_key_pair_test.go @@ -8,7 +8,6 @@ import ( "testing" "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/service/ec2" "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/helper/resource" @@ -61,7 +60,7 @@ func testSweepKeyPairs(region string) error { func TestAccAWSKeyPair_basic(t *testing.T) { var keyPair ec2.KeyPairInfo fingerprint := "d7:ff:a6:63:18:64:9c:57:a1:ee:ca:a4:ad:c2:81:62" - resourceName := "aws_key_pair.a_key_pair" + resourceName := "aws_key_pair.test" rName := acctest.RandomWithPrefix("tf-acc-test") resource.ParallelTest(t, resource.TestCase{ @@ -90,7 +89,7 @@ func TestAccAWSKeyPair_basic(t *testing.T) { func TestAccAWSKeyPair_tags(t *testing.T) { var keyPair ec2.KeyPairInfo - resourceName := "aws_key_pair.a_key_pair" + resourceName := "aws_key_pair.test" rName := acctest.RandomWithPrefix("tf-acc-test") resource.ParallelTest(t, resource.TestCase{ @@ -135,7 +134,7 @@ func TestAccAWSKeyPair_tags(t *testing.T) { func TestAccAWSKeyPair_generatedName(t *testing.T) { var keyPair ec2.KeyPairInfo - resourceName := "aws_key_pair.a_key_pair" + resourceName := "aws_key_pair.test" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -162,11 +161,11 @@ func TestAccAWSKeyPair_generatedName(t *testing.T) { func TestAccAWSKeyPair_namePrefix(t *testing.T) { var keyPair ec2.KeyPairInfo - resourceName := "aws_key_pair.a_key_pair" + resourceName := "aws_key_pair.test" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, - IDRefreshName: "aws_key_pair.a_key_pair", + IDRefreshName: resourceName, IDRefreshIgnore: []string{"key_name_prefix"}, Providers: testAccProviders, CheckDestroy: testAccCheckAWSKeyPairDestroy, @@ -191,7 +190,7 @@ func TestAccAWSKeyPair_namePrefix(t *testing.T) { func TestAccAWSKeyPair_disappears(t *testing.T) { var keyPair ec2.KeyPairInfo - resourceName := "aws_key_pair.a_key_pair" + resourceName := "aws_key_pair.test" rName := acctest.RandomWithPrefix("tf-acc-test") resource.ParallelTest(t, resource.TestCase{ @@ -230,12 +229,7 @@ func testAccCheckAWSKeyPairDestroy(s *terraform.State) error { return nil } - // Verify the error is what we want - ec2err, ok := err.(awserr.Error) - if !ok { - return err - } - if ec2err.Code() != "InvalidKeyPair.NotFound" { + if !isAWSErr(err, "InvalidKeyPair.NotFound", "") { return err } } @@ -308,7 +302,7 @@ func testAccCheckAWSKeyPairExists(n string, res *ec2.KeyPairInfo) resource.TestC func testAccAWSKeyPairConfig(rName string) string { return fmt.Sprintf(` -resource "aws_key_pair" "a_key_pair" { +resource "aws_key_pair" "test" { key_name = %[1]q public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com" } @@ -317,7 +311,7 @@ resource "aws_key_pair" "a_key_pair" { func testAccAWSKeyPairConfigTags1(rName, tagKey1, tagValue1 string) string { return fmt.Sprintf(` -resource "aws_key_pair" "a_key_pair" { +resource "aws_key_pair" "test" { key_name = %[1]q public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com" @@ -330,7 +324,7 @@ resource "aws_key_pair" "a_key_pair" { func testAccAWSKeyPairConfigTags2(rName, tagKey1, tagValue1, tagKey2, tagValue2 string) string { return fmt.Sprintf(` -resource "aws_key_pair" "a_key_pair" { +resource "aws_key_pair" "test" { key_name = %[1]q public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com" @@ -343,13 +337,13 @@ resource "aws_key_pair" "a_key_pair" { } const testAccAWSKeyPairConfig_generatedName = ` -resource "aws_key_pair" "a_key_pair" { +resource "aws_key_pair" "test" { 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" { +resource "aws_key_pair" "test" { key_name_prefix = "baz-" public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com" } From 609e3f23bc96ba131683c7e09e48e61f0bacfe46 Mon Sep 17 00:00:00 2001 From: DrFaust92 Date: Thu, 23 Apr 2020 18:38:43 +0300 Subject: [PATCH 2/2] order imports --- aws/resource_aws_key_pair.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/aws/resource_aws_key_pair.go b/aws/resource_aws_key_pair.go index deb1a7307f3..c5a84240ce1 100644 --- a/aws/resource_aws_key_pair.go +++ b/aws/resource_aws_key_pair.go @@ -4,13 +4,12 @@ import ( "fmt" "strings" + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/ec2" "github.com/hashicorp/terraform-plugin-sdk/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/helper/validation" "github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags" - - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/ec2" ) func resourceAwsKeyPair() *schema.Resource {