github.com/ndarilek/terraform@v0.3.8-0.20150320140257-d3135c1b2bac/builtin/providers/aws/resource_aws_key_pair_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/aws-sdk-go/aws" 8 "github.com/hashicorp/aws-sdk-go/gen/ec2" 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 ) 12 13 func TestAccAWSKeyPair_normal(t *testing.T) { 14 var conf ec2.KeyPairInfo 15 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { testAccPreCheck(t) }, 18 Providers: testAccProviders, 19 CheckDestroy: testAccCheckAWSKeyPairDestroy, 20 Steps: []resource.TestStep{ 21 resource.TestStep{ 22 Config: testAccAWSKeyPairConfig, 23 Check: resource.ComposeTestCheckFunc( 24 testAccCheckAWSKeyPairExists("aws_key_pair.a_key_pair", &conf), 25 testAccCheckAWSKeyPairFingerprint("d7:ff:a6:63:18:64:9c:57:a1:ee:ca:a4:ad:c2:81:62", &conf), 26 ), 27 }, 28 }, 29 }) 30 } 31 32 func testAccCheckAWSKeyPairDestroy(s *terraform.State) error { 33 ec2conn := testAccProvider.Meta().(*AWSClient).ec2conn 34 35 for _, rs := range s.RootModule().Resources { 36 if rs.Type != "aws_key_pair" { 37 continue 38 } 39 40 // Try to find key pair 41 resp, err := ec2conn.DescribeKeyPairs(&ec2.DescribeKeyPairsRequest{ 42 KeyNames: []string{rs.Primary.ID}, 43 }) 44 if err == nil { 45 if len(resp.KeyPairs) > 0 { 46 return fmt.Errorf("still exist.") 47 } 48 return nil 49 } 50 51 // Verify the error is what we want 52 ec2err, ok := err.(aws.APIError) 53 if !ok { 54 return err 55 } 56 if ec2err.Code != "InvalidKeyPair.NotFound" { 57 return err 58 } 59 } 60 61 return nil 62 } 63 64 func testAccCheckAWSKeyPairFingerprint(expectedFingerprint string, conf *ec2.KeyPairInfo) resource.TestCheckFunc { 65 return func(s *terraform.State) error { 66 if *conf.KeyFingerprint != expectedFingerprint { 67 return fmt.Errorf("incorrect fingerprint. expected %s, got %s", expectedFingerprint, *conf.KeyFingerprint) 68 } 69 return nil 70 } 71 } 72 73 func testAccCheckAWSKeyPairExists(n string, res *ec2.KeyPairInfo) resource.TestCheckFunc { 74 return func(s *terraform.State) error { 75 rs, ok := s.RootModule().Resources[n] 76 if !ok { 77 return fmt.Errorf("Not found: %s", n) 78 } 79 80 if rs.Primary.ID == "" { 81 return fmt.Errorf("No KeyPair name is set") 82 } 83 84 ec2conn := testAccProvider.Meta().(*AWSClient).ec2conn 85 86 resp, err := ec2conn.DescribeKeyPairs(&ec2.DescribeKeyPairsRequest{ 87 KeyNames: []string{rs.Primary.ID}, 88 }) 89 if err != nil { 90 return err 91 } 92 if len(resp.KeyPairs) != 1 || 93 *resp.KeyPairs[0].KeyName != rs.Primary.ID { 94 return fmt.Errorf("KeyPair not found") 95 } 96 97 *res = resp.KeyPairs[0] 98 99 return nil 100 } 101 } 102 103 const testAccAWSKeyPairConfig = ` 104 resource "aws_key_pair" "a_key_pair" { 105 key_name = "tf-acc-key-pair" 106 public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com" 107 } 108 `