github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/aws/resource_aws_lightsail_key_pair_test.go (about) 1 package aws 2 3 import ( 4 "errors" 5 "fmt" 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/lightsail" 11 "github.com/hashicorp/terraform/helper/acctest" 12 "github.com/hashicorp/terraform/helper/resource" 13 "github.com/hashicorp/terraform/terraform" 14 ) 15 16 func TestAccAWSLightsailKeyPair_basic(t *testing.T) { 17 var conf lightsail.KeyPair 18 lightsailName := fmt.Sprintf("tf-test-lightsail-%d", acctest.RandInt()) 19 20 resource.Test(t, resource.TestCase{ 21 PreCheck: func() { testAccPreCheck(t) }, 22 Providers: testAccProviders, 23 CheckDestroy: testAccCheckAWSLightsailKeyPairDestroy, 24 Steps: []resource.TestStep{ 25 { 26 Config: testAccAWSLightsailKeyPairConfig_basic(lightsailName), 27 Check: resource.ComposeAggregateTestCheckFunc( 28 testAccCheckAWSLightsailKeyPairExists("aws_lightsail_key_pair.lightsail_key_pair_test", &conf), 29 resource.TestCheckResourceAttrSet("aws_lightsail_key_pair.lightsail_key_pair_test", "arn"), 30 resource.TestCheckResourceAttrSet("aws_lightsail_key_pair.lightsail_key_pair_test", "fingerprint"), 31 resource.TestCheckResourceAttrSet("aws_lightsail_key_pair.lightsail_key_pair_test", "public_key"), 32 resource.TestCheckResourceAttrSet("aws_lightsail_key_pair.lightsail_key_pair_test", "private_key"), 33 ), 34 }, 35 }, 36 }) 37 } 38 39 func TestAccAWSLightsailKeyPair_imported(t *testing.T) { 40 var conf lightsail.KeyPair 41 lightsailName := fmt.Sprintf("tf-test-lightsail-%d", acctest.RandInt()) 42 43 resource.Test(t, resource.TestCase{ 44 PreCheck: func() { testAccPreCheck(t) }, 45 Providers: testAccProviders, 46 CheckDestroy: testAccCheckAWSLightsailKeyPairDestroy, 47 Steps: []resource.TestStep{ 48 { 49 Config: testAccAWSLightsailKeyPairConfig_imported(lightsailName, testLightsailKeyPairPubKey1), 50 Check: resource.ComposeAggregateTestCheckFunc( 51 testAccCheckAWSLightsailKeyPairExists("aws_lightsail_key_pair.lightsail_key_pair_test", &conf), 52 resource.TestCheckResourceAttrSet("aws_lightsail_key_pair.lightsail_key_pair_test", "arn"), 53 resource.TestCheckResourceAttrSet("aws_lightsail_key_pair.lightsail_key_pair_test", "fingerprint"), 54 resource.TestCheckResourceAttrSet("aws_lightsail_key_pair.lightsail_key_pair_test", "public_key"), 55 resource.TestCheckResourceAttr("aws_lightsail_key_pair.lightsail_key_pair_test", "encrypted_fingerprint", ""), 56 resource.TestCheckResourceAttr("aws_lightsail_key_pair.lightsail_key_pair_test", "encrypted_private_key", ""), 57 resource.TestCheckResourceAttr("aws_lightsail_key_pair.lightsail_key_pair_test", "private_key", ""), 58 ), 59 }, 60 }, 61 }) 62 } 63 64 func TestAccAWSLightsailKeyPair_encrypted(t *testing.T) { 65 var conf lightsail.KeyPair 66 lightsailName := fmt.Sprintf("tf-test-lightsail-%d", acctest.RandInt()) 67 68 resource.Test(t, resource.TestCase{ 69 PreCheck: func() { testAccPreCheck(t) }, 70 Providers: testAccProviders, 71 CheckDestroy: testAccCheckAWSLightsailKeyPairDestroy, 72 Steps: []resource.TestStep{ 73 { 74 Config: testAccAWSLightsailKeyPairConfig_encrypted(lightsailName, testLightsailKeyPairPubKey1), 75 Check: resource.ComposeAggregateTestCheckFunc( 76 testAccCheckAWSLightsailKeyPairExists("aws_lightsail_key_pair.lightsail_key_pair_test", &conf), 77 resource.TestCheckResourceAttrSet("aws_lightsail_key_pair.lightsail_key_pair_test", "arn"), 78 resource.TestCheckResourceAttrSet("aws_lightsail_key_pair.lightsail_key_pair_test", "fingerprint"), 79 resource.TestCheckResourceAttrSet("aws_lightsail_key_pair.lightsail_key_pair_test", "encrypted_fingerprint"), 80 resource.TestCheckResourceAttrSet("aws_lightsail_key_pair.lightsail_key_pair_test", "encrypted_private_key"), 81 resource.TestCheckResourceAttrSet("aws_lightsail_key_pair.lightsail_key_pair_test", "public_key"), 82 resource.TestCheckResourceAttr("aws_lightsail_key_pair.lightsail_key_pair_test", "private_key", ""), 83 ), 84 }, 85 }, 86 }) 87 } 88 89 func TestAccAWSLightsailKeyPair_nameprefix(t *testing.T) { 90 var conf1, conf2 lightsail.KeyPair 91 92 resource.Test(t, resource.TestCase{ 93 PreCheck: func() { testAccPreCheck(t) }, 94 Providers: testAccProviders, 95 CheckDestroy: testAccCheckAWSLightsailKeyPairDestroy, 96 Steps: []resource.TestStep{ 97 { 98 Config: testAccAWSLightsailKeyPairConfig_prefixed(), 99 Check: resource.ComposeAggregateTestCheckFunc( 100 testAccCheckAWSLightsailKeyPairExists("aws_lightsail_key_pair.lightsail_key_pair_test_omit", &conf1), 101 testAccCheckAWSLightsailKeyPairExists("aws_lightsail_key_pair.lightsail_key_pair_test_prefixed", &conf2), 102 resource.TestCheckResourceAttrSet("aws_lightsail_key_pair.lightsail_key_pair_test_omit", "name"), 103 resource.TestCheckResourceAttrSet("aws_lightsail_key_pair.lightsail_key_pair_test_prefixed", "name"), 104 ), 105 }, 106 }, 107 }) 108 } 109 110 func testAccCheckAWSLightsailKeyPairExists(n string, res *lightsail.KeyPair) resource.TestCheckFunc { 111 return func(s *terraform.State) error { 112 rs, ok := s.RootModule().Resources[n] 113 if !ok { 114 return fmt.Errorf("Not found: %s", n) 115 } 116 117 if rs.Primary.ID == "" { 118 return errors.New("No LightsailKeyPair set") 119 } 120 121 conn := testAccProvider.Meta().(*AWSClient).lightsailconn 122 123 respKeyPair, err := conn.GetKeyPair(&lightsail.GetKeyPairInput{ 124 KeyPairName: aws.String(rs.Primary.Attributes["name"]), 125 }) 126 127 if err != nil { 128 return err 129 } 130 131 if respKeyPair == nil || respKeyPair.KeyPair == nil { 132 return fmt.Errorf("KeyPair (%s) not found", rs.Primary.Attributes["name"]) 133 } 134 *res = *respKeyPair.KeyPair 135 return nil 136 } 137 } 138 139 func testAccCheckAWSLightsailKeyPairDestroy(s *terraform.State) error { 140 141 for _, rs := range s.RootModule().Resources { 142 if rs.Type != "aws_lightsail_key_pair" { 143 continue 144 } 145 146 conn := testAccProvider.Meta().(*AWSClient).lightsailconn 147 148 respKeyPair, err := conn.GetKeyPair(&lightsail.GetKeyPairInput{ 149 KeyPairName: aws.String(rs.Primary.Attributes["name"]), 150 }) 151 152 if err == nil { 153 if respKeyPair.KeyPair != nil { 154 return fmt.Errorf("LightsailKeyPair %q still exists", rs.Primary.ID) 155 } 156 } 157 158 // Verify the error 159 if awsErr, ok := err.(awserr.Error); ok { 160 if awsErr.Code() == "NotFoundException" { 161 return nil 162 } 163 } 164 return err 165 } 166 167 return nil 168 } 169 170 func testAccAWSLightsailKeyPairConfig_basic(lightsailName string) string { 171 return fmt.Sprintf(` 172 provider "aws" { 173 region = "us-east-1" 174 } 175 resource "aws_lightsail_key_pair" "lightsail_key_pair_test" { 176 name = "%s" 177 } 178 `, lightsailName) 179 } 180 181 func testAccAWSLightsailKeyPairConfig_imported(lightsailName, key string) string { 182 return fmt.Sprintf(` 183 provider "aws" { 184 region = "us-east-1" 185 } 186 resource "aws_lightsail_key_pair" "lightsail_key_pair_test" { 187 name = "%s" 188 189 public_key = "%s" 190 } 191 `, lightsailName, lightsailPubKey) 192 } 193 194 func testAccAWSLightsailKeyPairConfig_encrypted(lightsailName, key string) string { 195 return fmt.Sprintf(` 196 provider "aws" { 197 region = "us-east-1" 198 } 199 resource "aws_lightsail_key_pair" "lightsail_key_pair_test" { 200 name = "%s" 201 202 pgp_key = <<EOF 203 %s 204 EOF 205 } 206 `, lightsailName, key) 207 } 208 209 func testAccAWSLightsailKeyPairConfig_prefixed() string { 210 return fmt.Sprintf(` 211 provider "aws" { 212 region = "us-east-1" 213 } 214 resource "aws_lightsail_key_pair" "lightsail_key_pair_test_omit" {} 215 resource "aws_lightsail_key_pair" "lightsail_key_pair_test_prefixed" { 216 name_prefix = "cts" 217 } 218 `) 219 } 220 221 const lightsailPubKey = `ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com` 222 const testLightsailKeyPairPubKey1 = `mQENBFXbjPUBCADjNjCUQwfxKL+RR2GA6pv/1K+zJZ8UWIF9S0lk7cVIEfJiprzzwiMwBS5cD0da 223 rGin1FHvIWOZxujA7oW0O2TUuatqI3aAYDTfRYurh6iKLC+VS+F7H+/mhfFvKmgr0Y5kDCF1j0T/ 224 063QZ84IRGucR/X43IY7kAtmxGXH0dYOCzOe5UBX1fTn3mXGe2ImCDWBH7gOViynXmb6XNvXkP0f 225 sF5St9jhO7mbZU9EFkv9O3t3EaURfHopsCVDOlCkFCw5ArY+DUORHRzoMX0PnkyQb5OzibkChzpg 226 8hQssKeVGpuskTdz5Q7PtdW71jXd4fFVzoNH8fYwRpziD2xNvi6HABEBAAG0EFZhdWx0IFRlc3Qg 227 S2V5IDGJATgEEwECACIFAlXbjPUCGy8GCwkIBwMCBhUIAgkKCwQWAgMBAh4BAheAAAoJEOfLr44B 228 HbeTo+sH/i7bapIgPnZsJ81hmxPj4W12uvunksGJiC7d4hIHsG7kmJRTJfjECi+AuTGeDwBy84TD 229 cRaOB6e79fj65Fg6HgSahDUtKJbGxj/lWzmaBuTzlN3CEe8cMwIPqPT2kajJVdOyrvkyuFOdPFOE 230 A7bdCH0MqgIdM2SdF8t40k/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