Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/key_pair - tag on create #12962

Merged
merged 2 commits into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 4 additions & 29 deletions aws/resource_aws_key_pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +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/aws/awserr"
"github.com/aws/aws-sdk-go/service/ec2"
)

func resourceAwsKeyPair() *schema.Resource {
Expand Down Expand Up @@ -87,6 +85,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 {
Expand All @@ -95,29 +94,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)
}

Expand All @@ -128,8 +104,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
}
Expand Down
30 changes: 12 additions & 18 deletions aws/resource_aws_key_pair_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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) },
Expand All @@ -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,
Expand All @@ -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{
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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"
}
Expand All @@ -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"

Expand All @@ -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"

Expand All @@ -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"
}
Expand Down