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