github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/aws/resource_aws_key_pair_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "log" 6 "strings" 7 "testing" 8 9 "github.com/aws/aws-sdk-go/aws" 10 "github.com/aws/aws-sdk-go/aws/awserr" 11 "github.com/aws/aws-sdk-go/service/ec2" 12 "github.com/hashicorp/terraform/helper/resource" 13 "github.com/hashicorp/terraform/terraform" 14 ) 15 16 func init() { 17 resource.AddTestSweepers("aws_key_pair", &resource.Sweeper{ 18 Name: "aws_key_pair", 19 F: testSweepKeyPairs, 20 }) 21 } 22 23 func testSweepKeyPairs(region string) error { 24 client, err := sharedClientForRegion(region) 25 if err != nil { 26 return fmt.Errorf("error getting client: %s", err) 27 } 28 ec2conn := client.(*AWSClient).ec2conn 29 30 log.Printf("Destroying the tmp keys in (%s)", client.(*AWSClient).region) 31 32 resp, err := ec2conn.DescribeKeyPairs(&ec2.DescribeKeyPairsInput{ 33 Filters: []*ec2.Filter{ 34 &ec2.Filter{ 35 Name: aws.String("key-name"), 36 Values: []*string{aws.String("tmp-key*")}, 37 }, 38 }, 39 }) 40 if err != nil { 41 return fmt.Errorf("Error describing key pairs in Sweeper: %s", err) 42 } 43 44 keyPairs := resp.KeyPairs 45 for _, d := range keyPairs { 46 _, err := ec2conn.DeleteKeyPair(&ec2.DeleteKeyPairInput{ 47 KeyName: d.KeyName, 48 }) 49 50 if err != nil { 51 return fmt.Errorf("Error deleting key pairs in Sweeper: %s", err) 52 } 53 } 54 return nil 55 } 56 57 func TestAccAWSKeyPair_basic(t *testing.T) { 58 var conf ec2.KeyPairInfo 59 60 resource.Test(t, resource.TestCase{ 61 PreCheck: func() { testAccPreCheck(t) }, 62 Providers: testAccProviders, 63 CheckDestroy: testAccCheckAWSKeyPairDestroy, 64 Steps: []resource.TestStep{ 65 resource.TestStep{ 66 Config: testAccAWSKeyPairConfig, 67 Check: resource.ComposeTestCheckFunc( 68 testAccCheckAWSKeyPairExists("aws_key_pair.a_key_pair", &conf), 69 testAccCheckAWSKeyPairFingerprint("d7:ff:a6:63:18:64:9c:57:a1:ee:ca:a4:ad:c2:81:62", &conf), 70 ), 71 }, 72 }, 73 }) 74 } 75 76 func TestAccAWSKeyPair_generatedName(t *testing.T) { 77 var conf ec2.KeyPairInfo 78 79 resource.Test(t, resource.TestCase{ 80 PreCheck: func() { testAccPreCheck(t) }, 81 Providers: testAccProviders, 82 CheckDestroy: testAccCheckAWSKeyPairDestroy, 83 Steps: []resource.TestStep{ 84 resource.TestStep{ 85 Config: testAccAWSKeyPairConfig_generatedName, 86 Check: resource.ComposeTestCheckFunc( 87 testAccCheckAWSKeyPairExists("aws_key_pair.a_key_pair", &conf), 88 testAccCheckAWSKeyPairFingerprint("d7:ff:a6:63:18:64:9c:57:a1:ee:ca:a4:ad:c2:81:62", &conf), 89 func(s *terraform.State) error { 90 if conf.KeyName == nil { 91 return fmt.Errorf("bad: No SG name") 92 } 93 if !strings.HasPrefix(*conf.KeyName, "terraform-") { 94 return fmt.Errorf("No terraform- prefix: %s", *conf.KeyName) 95 } 96 return nil 97 }, 98 ), 99 }, 100 }, 101 }) 102 } 103 104 func testAccCheckAWSKeyPairDestroy(s *terraform.State) error { 105 ec2conn := testAccProvider.Meta().(*AWSClient).ec2conn 106 107 for _, rs := range s.RootModule().Resources { 108 if rs.Type != "aws_key_pair" { 109 continue 110 } 111 112 // Try to find key pair 113 resp, err := ec2conn.DescribeKeyPairs(&ec2.DescribeKeyPairsInput{ 114 KeyNames: []*string{aws.String(rs.Primary.ID)}, 115 }) 116 if err == nil { 117 if len(resp.KeyPairs) > 0 { 118 return fmt.Errorf("still exist.") 119 } 120 return nil 121 } 122 123 // Verify the error is what we want 124 ec2err, ok := err.(awserr.Error) 125 if !ok { 126 return err 127 } 128 if ec2err.Code() != "InvalidKeyPair.NotFound" { 129 return err 130 } 131 } 132 133 return nil 134 } 135 136 func testAccCheckAWSKeyPairFingerprint(expectedFingerprint string, conf *ec2.KeyPairInfo) resource.TestCheckFunc { 137 return func(s *terraform.State) error { 138 if *conf.KeyFingerprint != expectedFingerprint { 139 return fmt.Errorf("incorrect fingerprint. expected %s, got %s", expectedFingerprint, *conf.KeyFingerprint) 140 } 141 return nil 142 } 143 } 144 145 func testAccCheckAWSKeyPairExists(n string, res *ec2.KeyPairInfo) resource.TestCheckFunc { 146 return func(s *terraform.State) error { 147 rs, ok := s.RootModule().Resources[n] 148 if !ok { 149 return fmt.Errorf("Not found: %s", n) 150 } 151 152 if rs.Primary.ID == "" { 153 return fmt.Errorf("No KeyPair name is set") 154 } 155 156 ec2conn := testAccProvider.Meta().(*AWSClient).ec2conn 157 158 resp, err := ec2conn.DescribeKeyPairs(&ec2.DescribeKeyPairsInput{ 159 KeyNames: []*string{aws.String(rs.Primary.ID)}, 160 }) 161 if err != nil { 162 return err 163 } 164 if len(resp.KeyPairs) != 1 || 165 *resp.KeyPairs[0].KeyName != rs.Primary.ID { 166 return fmt.Errorf("KeyPair not found") 167 } 168 169 *res = *resp.KeyPairs[0] 170 171 return nil 172 } 173 } 174 175 func testAccCheckAWSKeyPair_namePrefix(t *testing.T) { 176 var conf ec2.KeyPairInfo 177 178 resource.Test(t, resource.TestCase{ 179 PreCheck: func() { testAccPreCheck(t) }, 180 IDRefreshName: "aws_key_pair.a_key_pair", 181 IDRefreshIgnore: []string{"key_name_prefix"}, 182 Providers: testAccProviders, 183 CheckDestroy: testAccCheckAWSKeyPairDestroy, 184 Steps: []resource.TestStep{ 185 resource.TestStep{ 186 Config: testAccCheckAWSKeyPairPrefixNameConfig, 187 Check: resource.ComposeTestCheckFunc( 188 testAccCheckAWSKeyPairExists("aws_key_pair.a_key_pair", &conf), 189 testAccCheckAWSKeyPairGeneratedNamePrefix( 190 "aws_key_pair.a_key_pair", "baz-"), 191 ), 192 }, 193 }, 194 }) 195 } 196 197 func testAccCheckAWSKeyPairGeneratedNamePrefix( 198 resource, prefix string) resource.TestCheckFunc { 199 return func(s *terraform.State) error { 200 r, ok := s.RootModule().Resources[resource] 201 if !ok { 202 return fmt.Errorf("Resource not found") 203 } 204 name, ok := r.Primary.Attributes["name"] 205 if !ok { 206 return fmt.Errorf("Name attr not found: %#v", r.Primary.Attributes) 207 } 208 if !strings.HasPrefix(name, prefix) { 209 return fmt.Errorf("Name: %q, does not have prefix: %q", name, prefix) 210 } 211 return nil 212 } 213 } 214 215 const testAccAWSKeyPairConfig = ` 216 resource "aws_key_pair" "a_key_pair" { 217 key_name = "tf-acc-key-pair" 218 public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com" 219 } 220 ` 221 222 const testAccAWSKeyPairConfig_generatedName = ` 223 resource "aws_key_pair" "a_key_pair" { 224 public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com" 225 } 226 ` 227 228 const testAccCheckAWSKeyPairPrefixNameConfig = ` 229 resource "aws_key_pair" "a_key_pair" { 230 key_name_prefix = "baz-" 231 public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com" 232 } 233 `