2014-12-18 04:27:57 +01:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-06-06 17:34:17 +02:00
|
|
|
"log"
|
2015-04-30 14:26:17 +02:00
|
|
|
"strings"
|
2014-12-18 04:27:57 +01:00
|
|
|
"testing"
|
|
|
|
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2014-12-18 04:27:57 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2017-06-06 17:34:17 +02:00
|
|
|
func init() {
|
|
|
|
resource.AddTestSweepers("aws_key_pair", &resource.Sweeper{
|
|
|
|
Name: "aws_key_pair",
|
|
|
|
F: testSweepKeyPairs,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSweepKeyPairs(region string) error {
|
|
|
|
client, err := sharedClientForRegion(region)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error getting client: %s", err)
|
|
|
|
}
|
|
|
|
ec2conn := client.(*AWSClient).ec2conn
|
|
|
|
|
|
|
|
log.Printf("Destroying the tmp keys in (%s)", client.(*AWSClient).region)
|
|
|
|
|
|
|
|
resp, err := ec2conn.DescribeKeyPairs(&ec2.DescribeKeyPairsInput{
|
|
|
|
Filters: []*ec2.Filter{
|
|
|
|
&ec2.Filter{
|
|
|
|
Name: aws.String("key-name"),
|
|
|
|
Values: []*string{aws.String("tmp-key*")},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error describing key pairs in Sweeper: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
keyPairs := resp.KeyPairs
|
|
|
|
for _, d := range keyPairs {
|
|
|
|
_, err := ec2conn.DeleteKeyPair(&ec2.DeleteKeyPairInput{
|
|
|
|
KeyName: d.KeyName,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting key pairs in Sweeper: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-08 01:04:38 +02:00
|
|
|
func TestAccAWSKeyPair_basic(t *testing.T) {
|
2015-03-04 22:17:23 +01:00
|
|
|
var conf ec2.KeyPairInfo
|
2014-12-18 04:27:57 +01:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSKeyPairDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAWSKeyPairConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSKeyPairExists("aws_key_pair.a_key_pair", &conf),
|
|
|
|
testAccCheckAWSKeyPairFingerprint("d7:ff:a6:63:18:64:9c:57:a1:ee:ca:a4:ad:c2:81:62", &conf),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-30 14:26:17 +02:00
|
|
|
func TestAccAWSKeyPair_generatedName(t *testing.T) {
|
|
|
|
var conf ec2.KeyPairInfo
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSKeyPairDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAWSKeyPairConfig_generatedName,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSKeyPairExists("aws_key_pair.a_key_pair", &conf),
|
|
|
|
testAccCheckAWSKeyPairFingerprint("d7:ff:a6:63:18:64:9c:57:a1:ee:ca:a4:ad:c2:81:62", &conf),
|
|
|
|
func(s *terraform.State) error {
|
|
|
|
if conf.KeyName == nil {
|
|
|
|
return fmt.Errorf("bad: No SG name")
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(*conf.KeyName, "terraform-") {
|
|
|
|
return fmt.Errorf("No terraform- prefix: %s", *conf.KeyName)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-12-18 04:27:57 +01:00
|
|
|
func testAccCheckAWSKeyPairDestroy(s *terraform.State) error {
|
2015-04-16 22:05:55 +02:00
|
|
|
ec2conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
2014-12-18 04:27:57 +01:00
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "aws_key_pair" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to find key pair
|
2015-04-16 22:05:55 +02:00
|
|
|
resp, err := ec2conn.DescribeKeyPairs(&ec2.DescribeKeyPairsInput{
|
2015-04-06 22:13:29 +02:00
|
|
|
KeyNames: []*string{aws.String(rs.Primary.ID)},
|
2015-03-04 22:17:23 +01:00
|
|
|
})
|
2014-12-18 04:27:57 +01:00
|
|
|
if err == nil {
|
2015-03-04 22:17:23 +01:00
|
|
|
if len(resp.KeyPairs) > 0 {
|
2014-12-18 04:27:57 +01:00
|
|
|
return fmt.Errorf("still exist.")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the error is what we want
|
2015-05-20 13:21:23 +02:00
|
|
|
ec2err, ok := err.(awserr.Error)
|
2014-12-18 04:27:57 +01:00
|
|
|
if !ok {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-20 13:21:23 +02:00
|
|
|
if ec2err.Code() != "InvalidKeyPair.NotFound" {
|
2014-12-18 04:27:57 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-04 22:17:23 +01:00
|
|
|
func testAccCheckAWSKeyPairFingerprint(expectedFingerprint string, conf *ec2.KeyPairInfo) resource.TestCheckFunc {
|
2014-12-18 04:27:57 +01:00
|
|
|
return func(s *terraform.State) error {
|
2015-03-04 22:17:23 +01:00
|
|
|
if *conf.KeyFingerprint != expectedFingerprint {
|
|
|
|
return fmt.Errorf("incorrect fingerprint. expected %s, got %s", expectedFingerprint, *conf.KeyFingerprint)
|
2014-12-18 04:27:57 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-04 22:17:23 +01:00
|
|
|
func testAccCheckAWSKeyPairExists(n string, res *ec2.KeyPairInfo) resource.TestCheckFunc {
|
2014-12-18 04:27:57 +01:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No KeyPair name is set")
|
|
|
|
}
|
|
|
|
|
2015-04-16 22:05:55 +02:00
|
|
|
ec2conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
2014-12-18 04:27:57 +01:00
|
|
|
|
2015-04-16 22:05:55 +02:00
|
|
|
resp, err := ec2conn.DescribeKeyPairs(&ec2.DescribeKeyPairsInput{
|
2015-04-06 22:13:29 +02:00
|
|
|
KeyNames: []*string{aws.String(rs.Primary.ID)},
|
2015-03-04 22:17:23 +01:00
|
|
|
})
|
2014-12-18 04:27:57 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-04 22:17:23 +01:00
|
|
|
if len(resp.KeyPairs) != 1 ||
|
|
|
|
*resp.KeyPairs[0].KeyName != rs.Primary.ID {
|
2014-12-18 04:27:57 +01:00
|
|
|
return fmt.Errorf("KeyPair not found")
|
|
|
|
}
|
2015-03-04 22:17:23 +01:00
|
|
|
|
2015-04-06 22:13:29 +02:00
|
|
|
*res = *resp.KeyPairs[0]
|
2014-12-18 04:27:57 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-09 16:35:51 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-18 04:27:57 +01:00
|
|
|
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 phodgson@thoughtworks.com"
|
|
|
|
}
|
|
|
|
`
|
2015-04-30 14:26:17 +02:00
|
|
|
|
|
|
|
const testAccAWSKeyPairConfig_generatedName = `
|
|
|
|
resource "aws_key_pair" "a_key_pair" {
|
|
|
|
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com"
|
|
|
|
}
|
|
|
|
`
|
2016-11-09 16:35:51 +01:00
|
|
|
|
|
|
|
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"
|
|
|
|
}
|
|
|
|
`
|