github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_opsworks_application_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "reflect" 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/opsworks" 11 "github.com/hashicorp/terraform/helper/acctest" 12 "github.com/hashicorp/terraform/helper/resource" 13 "github.com/hashicorp/terraform/terraform" 14 ) 15 16 func TestAccAWSOpsworksApplication(t *testing.T) { 17 var opsapp opsworks.App 18 19 rInt := acctest.RandInt() 20 name := fmt.Sprintf("tf-ops-acc-application-%d", rInt) 21 22 resource.Test(t, resource.TestCase{ 23 PreCheck: func() { testAccPreCheck(t) }, 24 Providers: testAccProviders, 25 CheckDestroy: testAccCheckAwsOpsworksApplicationDestroy, 26 Steps: []resource.TestStep{ 27 { 28 Config: testAccAwsOpsworksApplicationCreate(name), 29 Check: resource.ComposeTestCheckFunc( 30 testAccCheckAWSOpsworksApplicationExists( 31 "aws_opsworks_application.tf-acc-app", &opsapp), 32 testAccCheckAWSOpsworksCreateAppAttributes(&opsapp), 33 resource.TestCheckResourceAttr( 34 "aws_opsworks_application.tf-acc-app", "name", name, 35 ), 36 resource.TestCheckResourceAttr( 37 "aws_opsworks_application.tf-acc-app", "type", "other", 38 ), 39 resource.TestCheckResourceAttr( 40 "aws_opsworks_application.tf-acc-app", "enable_ssl", "false", 41 ), 42 resource.TestCheckNoResourceAttr( 43 "aws_opsworks_application.tf-acc-app", "ssl_configuration", 44 ), 45 resource.TestCheckNoResourceAttr( 46 "aws_opsworks_application.tf-acc-app", "domains", 47 ), 48 resource.TestCheckNoResourceAttr( 49 "aws_opsworks_application.tf-acc-app", "app_source", 50 ), 51 resource.TestCheckResourceAttr( 52 "aws_opsworks_application.tf-acc-app", "environment.3077298702.key", "key1", 53 ), 54 resource.TestCheckResourceAttr( 55 "aws_opsworks_application.tf-acc-app", "environment.3077298702.value", "value1", 56 ), 57 resource.TestCheckNoResourceAttr( 58 "aws_opsworks_application.tf-acc-app", "environment.3077298702.secret", 59 ), 60 resource.TestCheckResourceAttr( 61 "aws_opsworks_application.tf-acc-app", "document_root", "foo", 62 ), 63 ), 64 }, 65 { 66 Config: testAccAwsOpsworksApplicationUpdate(name), 67 Check: resource.ComposeTestCheckFunc( 68 testAccCheckAWSOpsworksApplicationExists( 69 "aws_opsworks_application.tf-acc-app", &opsapp), 70 testAccCheckAWSOpsworksUpdateAppAttributes(&opsapp), 71 resource.TestCheckResourceAttr( 72 "aws_opsworks_application.tf-acc-app", "name", name, 73 ), 74 resource.TestCheckResourceAttr( 75 "aws_opsworks_application.tf-acc-app", "type", "rails", 76 ), 77 resource.TestCheckResourceAttr( 78 "aws_opsworks_application.tf-acc-app", "enable_ssl", "true", 79 ), 80 resource.TestCheckResourceAttr( 81 "aws_opsworks_application.tf-acc-app", "ssl_configuration.0.certificate", "-----BEGIN CERTIFICATE-----\nMIIBkDCB+gIJALoScFD0sJq3MA0GCSqGSIb3DQEBBQUAMA0xCzAJBgNVBAYTAkRF\nMB4XDTE1MTIxOTIwMzU1MVoXDTE2MDExODIwMzU1MVowDTELMAkGA1UEBhMCREUw\ngZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAKKQKbTTH/Julz16xY7ArYlzJYCP\nedTCx1bopuryCx/+d1gC94MtRdlPSpQl8mfc9iBdtXbJppp73Qh/DzLzO9Ns25xZ\n+kUQMhbIyLsaCBzuEGLgAaVdGpNvRBw++UoYtd0U7QczFAreTGLH8n8+FIzuI5Mc\n+MJ1TKbbt5gFfRSzAgMBAAEwDQYJKoZIhvcNAQEFBQADgYEALARo96wCDmaHKCaX\nS0IGLGnZCfiIUfCmBxOXBSJxDBwter95QHR0dMGxYIujee5n4vvavpVsqZnfMC3I\nOZWPlwiUJbNIpK+04Bg2vd5m/NMMrvi75RfmyeMtSfq/NrIX2Q3+nyWI7DLq7yZI\nV/YEvOqdAiy5NEWBztHx8HvB9G4=\n-----END CERTIFICATE-----", 82 ), 83 resource.TestCheckResourceAttr( 84 "aws_opsworks_application.tf-acc-app", "ssl_configuration.0.private_key", "-----BEGIN RSA PRIVATE KEY-----\nMIICXQIBAAKBgQCikCm00x/ybpc9esWOwK2JcyWAj3nUwsdW6Kbq8gsf/ndYAveD\nLUXZT0qUJfJn3PYgXbV2yaaae90Ifw8y8zvTbNucWfpFEDIWyMi7Gggc7hBi4AGl\nXRqTb0QcPvlKGLXdFO0HMxQK3kxix/J/PhSM7iOTHPjCdUym27eYBX0UswIDAQAB\nAoGBAIYcrvuqDboguI8U4TUjCkfSAgds1pLLWk79wu8jXkA329d1IyNKT0y3WIye\nPbyoEzmidZmZROQ/+ZsPz8c12Y0DrX73WSVzKNyJeP7XMk9HSzA1D9RX0U0S+5Kh\nFAMc2NEVVFIfQtVtoVmHdKDpnRYtOCHLW9rRpvqOOjd4mYk5AkEAzeiFr1mtlnsa\n67shMxzDaOTAFMchRz6G7aSovvCztxcB63ulFI/w9OTUMdTQ7ff7pet+lVihLc2W\nefIL0HvsjQJBAMocNTKaR/TnsV5GSk2kPAdR+zFP5sQy8sfMy0lEXTylc7zN4ajX\nMeHVoxp+GZgpfDcZ3ya808H1umyXh+xA1j8CQE9x9ZKQYT98RAjL7KVR5btk9w+N\nPTPF1j1+mHUDXfO4ds8qp6jlWKzEVXLcj7ghRADiebaZuaZ4eiSW1SQdjEkCQQC4\nwDhQ3X9RfEpCp3ZcqvjEqEg6t5N3XitYQPjDLN8eBRBbUsgpEy3iBuxl10eGNMX7\niIbYXlwkPYAArDPv3wT5AkAwp4vym+YKmDqh6gseKfRDuJqRiW9yD5A8VGr/w88k\n5rkuduVGP7tK3uIp00Its3aEyKF8mLGWYszVGeeLxAMH\n-----END RSA PRIVATE KEY-----", 85 ), 86 resource.TestCheckResourceAttr( 87 "aws_opsworks_application.tf-acc-app", "domains.0", "example.com", 88 ), 89 resource.TestCheckResourceAttr( 90 "aws_opsworks_application.tf-acc-app", "domains.1", "sub.example.com", 91 ), 92 resource.TestCheckResourceAttr( 93 "aws_opsworks_application.tf-acc-app", "app_source.0.password", "", 94 ), 95 resource.TestCheckResourceAttr( 96 "aws_opsworks_application.tf-acc-app", "app_source.0.revision", "master", 97 ), 98 resource.TestCheckResourceAttr( 99 "aws_opsworks_application.tf-acc-app", "app_source.0.ssh_key", "", 100 ), 101 resource.TestCheckResourceAttr( 102 "aws_opsworks_application.tf-acc-app", "app_source.0.type", "git", 103 ), 104 resource.TestCheckResourceAttr( 105 "aws_opsworks_application.tf-acc-app", "app_source.0.url", "https://github.com/aws/example.git", 106 ), 107 resource.TestCheckResourceAttr( 108 "aws_opsworks_application.tf-acc-app", "app_source.0.username", "", 109 ), 110 resource.TestCheckResourceAttr( 111 "aws_opsworks_application.tf-acc-app", "environment.2107898637.key", "key2", 112 ), 113 resource.TestCheckResourceAttr( 114 "aws_opsworks_application.tf-acc-app", "environment.2107898637.value", "value2", 115 ), 116 resource.TestCheckResourceAttr( 117 "aws_opsworks_application.tf-acc-app", "environment.2107898637.secure", "true", 118 ), 119 resource.TestCheckResourceAttr( 120 "aws_opsworks_application.tf-acc-app", "environment.3077298702.key", "key1", 121 ), 122 resource.TestCheckResourceAttr( 123 "aws_opsworks_application.tf-acc-app", "environment.3077298702.value", "value1", 124 ), 125 resource.TestCheckNoResourceAttr( 126 "aws_opsworks_application.tf-acc-app", "environment.3077298702.secret", 127 ), 128 resource.TestCheckResourceAttr( 129 "aws_opsworks_application.tf-acc-app", "document_root", "root", 130 ), 131 resource.TestCheckResourceAttr( 132 "aws_opsworks_application.tf-acc-app", "auto_bundle_on_deploy", "true", 133 ), 134 resource.TestCheckResourceAttr( 135 "aws_opsworks_application.tf-acc-app", "rails_env", "staging", 136 ), 137 ), 138 }, 139 }, 140 }) 141 } 142 143 func testAccCheckAWSOpsworksApplicationExists( 144 n string, opsapp *opsworks.App) resource.TestCheckFunc { 145 return func(s *terraform.State) error { 146 rs, ok := s.RootModule().Resources[n] 147 if !ok { 148 return fmt.Errorf("Not found: %s", n) 149 } 150 151 if rs.Primary.ID == "" { 152 return fmt.Errorf("No ID is set") 153 } 154 155 conn := testAccProvider.Meta().(*AWSClient).opsworksconn 156 157 params := &opsworks.DescribeAppsInput{ 158 AppIds: []*string{&rs.Primary.ID}, 159 } 160 resp, err := conn.DescribeApps(params) 161 162 if err != nil { 163 return err 164 } 165 166 if v := len(resp.Apps); v != 1 { 167 return fmt.Errorf("Expected 1 response returned, got %d", v) 168 } 169 170 *opsapp = *resp.Apps[0] 171 172 return nil 173 } 174 } 175 176 func testAccCheckAWSOpsworksCreateAppAttributes( 177 opsapp *opsworks.App) resource.TestCheckFunc { 178 return func(s *terraform.State) error { 179 if *opsapp.EnableSsl { 180 return fmt.Errorf("Unexpected enable ssl: %t", *opsapp.EnableSsl) 181 } 182 183 if *opsapp.Attributes["DocumentRoot"] != "foo" { 184 return fmt.Errorf("Unnexpected document root: %s", *opsapp.Attributes["DocumentRoot"]) 185 } 186 187 if *opsapp.Type != "other" { 188 return fmt.Errorf("Unnexpected type: %s", *opsapp.Type) 189 } 190 191 if *opsapp.AppSource.Type != "other" { 192 return fmt.Errorf("Unnexpected appsource type: %s", *opsapp.AppSource.Type) 193 } 194 195 expectedEnv := []*opsworks.EnvironmentVariable{ 196 { 197 Key: aws.String("key1"), 198 Value: aws.String("value1"), 199 Secure: aws.Bool(false), 200 }, 201 } 202 203 if !reflect.DeepEqual(expectedEnv, opsapp.Environment) { 204 return fmt.Errorf("Unnexpected environment: %s", opsapp.Environment) 205 } 206 207 if v := len(opsapp.Domains); v != 0 { 208 return fmt.Errorf("Expected 0 domains returned, got %d", v) 209 } 210 211 return nil 212 } 213 } 214 215 func testAccCheckAWSOpsworksUpdateAppAttributes( 216 opsapp *opsworks.App) resource.TestCheckFunc { 217 return func(s *terraform.State) error { 218 if *opsapp.Type != "rails" { 219 return fmt.Errorf("Unnexpected type: %s", *opsapp.Type) 220 } 221 222 if !*opsapp.EnableSsl { 223 return fmt.Errorf("Unexpected enable ssl: %t", *opsapp.EnableSsl) 224 } 225 226 if *opsapp.SslConfiguration.Certificate != "-----BEGIN CERTIFICATE-----\nMIIBkDCB+gIJALoScFD0sJq3MA0GCSqGSIb3DQEBBQUAMA0xCzAJBgNVBAYTAkRF\nMB4XDTE1MTIxOTIwMzU1MVoXDTE2MDExODIwMzU1MVowDTELMAkGA1UEBhMCREUw\ngZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAKKQKbTTH/Julz16xY7ArYlzJYCP\nedTCx1bopuryCx/+d1gC94MtRdlPSpQl8mfc9iBdtXbJppp73Qh/DzLzO9Ns25xZ\n+kUQMhbIyLsaCBzuEGLgAaVdGpNvRBw++UoYtd0U7QczFAreTGLH8n8+FIzuI5Mc\n+MJ1TKbbt5gFfRSzAgMBAAEwDQYJKoZIhvcNAQEFBQADgYEALARo96wCDmaHKCaX\nS0IGLGnZCfiIUfCmBxOXBSJxDBwter95QHR0dMGxYIujee5n4vvavpVsqZnfMC3I\nOZWPlwiUJbNIpK+04Bg2vd5m/NMMrvi75RfmyeMtSfq/NrIX2Q3+nyWI7DLq7yZI\nV/YEvOqdAiy5NEWBztHx8HvB9G4=\n-----END CERTIFICATE-----" { 227 return fmt.Errorf("Unexpected ssl configuration certificate: %s", *opsapp.SslConfiguration.Certificate) 228 } 229 230 if *opsapp.SslConfiguration.PrivateKey != "-----BEGIN RSA PRIVATE KEY-----\nMIICXQIBAAKBgQCikCm00x/ybpc9esWOwK2JcyWAj3nUwsdW6Kbq8gsf/ndYAveD\nLUXZT0qUJfJn3PYgXbV2yaaae90Ifw8y8zvTbNucWfpFEDIWyMi7Gggc7hBi4AGl\nXRqTb0QcPvlKGLXdFO0HMxQK3kxix/J/PhSM7iOTHPjCdUym27eYBX0UswIDAQAB\nAoGBAIYcrvuqDboguI8U4TUjCkfSAgds1pLLWk79wu8jXkA329d1IyNKT0y3WIye\nPbyoEzmidZmZROQ/+ZsPz8c12Y0DrX73WSVzKNyJeP7XMk9HSzA1D9RX0U0S+5Kh\nFAMc2NEVVFIfQtVtoVmHdKDpnRYtOCHLW9rRpvqOOjd4mYk5AkEAzeiFr1mtlnsa\n67shMxzDaOTAFMchRz6G7aSovvCztxcB63ulFI/w9OTUMdTQ7ff7pet+lVihLc2W\nefIL0HvsjQJBAMocNTKaR/TnsV5GSk2kPAdR+zFP5sQy8sfMy0lEXTylc7zN4ajX\nMeHVoxp+GZgpfDcZ3ya808H1umyXh+xA1j8CQE9x9ZKQYT98RAjL7KVR5btk9w+N\nPTPF1j1+mHUDXfO4ds8qp6jlWKzEVXLcj7ghRADiebaZuaZ4eiSW1SQdjEkCQQC4\nwDhQ3X9RfEpCp3ZcqvjEqEg6t5N3XitYQPjDLN8eBRBbUsgpEy3iBuxl10eGNMX7\niIbYXlwkPYAArDPv3wT5AkAwp4vym+YKmDqh6gseKfRDuJqRiW9yD5A8VGr/w88k\n5rkuduVGP7tK3uIp00Its3aEyKF8mLGWYszVGeeLxAMH\n-----END RSA PRIVATE KEY-----" { 231 return fmt.Errorf("Unexpected ssl configuration private key: %s", *opsapp.SslConfiguration.PrivateKey) 232 } 233 234 expectedAttrs := map[string]*string{ 235 "DocumentRoot": aws.String("root"), 236 "RailsEnv": aws.String("staging"), 237 "AutoBundleOnDeploy": aws.String("true"), 238 "AwsFlowRubySettings": nil, 239 } 240 241 if !reflect.DeepEqual(expectedAttrs, opsapp.Attributes) { 242 return fmt.Errorf("Unnexpected Attributes: %v", aws.StringValueMap(opsapp.Attributes)) 243 } 244 245 expectedAppSource := &opsworks.Source{ 246 Type: aws.String("git"), 247 Revision: aws.String("master"), 248 Url: aws.String("https://github.com/aws/example.git"), 249 } 250 251 if !reflect.DeepEqual(expectedAppSource, opsapp.AppSource) { 252 return fmt.Errorf("Unnexpected appsource: %s", opsapp.AppSource) 253 } 254 255 expectedEnv := []*opsworks.EnvironmentVariable{ 256 { 257 Key: aws.String("key2"), 258 Value: aws.String("*****FILTERED*****"), 259 Secure: aws.Bool(true), 260 }, 261 { 262 Key: aws.String("key1"), 263 Value: aws.String("value1"), 264 Secure: aws.Bool(false), 265 }, 266 } 267 268 if !reflect.DeepEqual(expectedEnv, opsapp.Environment) { 269 return fmt.Errorf("Unnexpected environment: %s", opsapp.Environment) 270 } 271 272 expectedDomains := []*string{ 273 aws.String("example.com"), 274 aws.String("sub.example.com"), 275 } 276 277 if !reflect.DeepEqual(expectedDomains, opsapp.Domains) { 278 return fmt.Errorf("Unnexpected Daomins : %v", aws.StringValueSlice(opsapp.Domains)) 279 } 280 281 return nil 282 } 283 } 284 285 func testAccCheckAwsOpsworksApplicationDestroy(s *terraform.State) error { 286 client := testAccProvider.Meta().(*AWSClient).opsworksconn 287 288 for _, rs := range s.RootModule().Resources { 289 if rs.Type != "aws_opsworks_application" { 290 continue 291 } 292 293 req := &opsworks.DescribeAppsInput{ 294 AppIds: []*string{ 295 aws.String(rs.Primary.ID), 296 }, 297 } 298 299 resp, err := client.DescribeApps(req) 300 if err == nil { 301 if len(resp.Apps) > 0 { 302 return fmt.Errorf("OpsWorks App still exist.") 303 } 304 } 305 306 if awserr, ok := err.(awserr.Error); ok { 307 if awserr.Code() != "ResourceNotFoundException" { 308 return err 309 } 310 } 311 } 312 313 return nil 314 } 315 316 func testAccAwsOpsworksApplicationCreate(name string) string { 317 return testAccAwsOpsworksStackConfigVpcCreate(name) + 318 fmt.Sprintf(` 319 resource "aws_opsworks_application" "tf-acc-app" { 320 stack_id = "${aws_opsworks_stack.tf-acc.id}" 321 name = "%s" 322 type = "other" 323 enable_ssl = false 324 app_source ={ 325 type = "other" 326 } 327 environment = { key = "key1" value = "value1" secure = false} 328 document_root = "foo" 329 } 330 `, name) 331 } 332 333 func testAccAwsOpsworksApplicationUpdate(name string) string { 334 return testAccAwsOpsworksStackConfigVpcCreate(name) + 335 fmt.Sprintf(` 336 resource "aws_opsworks_application" "tf-acc-app" { 337 stack_id = "${aws_opsworks_stack.tf-acc.id}" 338 name = "%s" 339 type = "rails" 340 domains = ["example.com", "sub.example.com"] 341 enable_ssl = true 342 ssl_configuration = { 343 private_key = <<EOS 344 -----BEGIN RSA PRIVATE KEY----- 345 MIICXQIBAAKBgQCikCm00x/ybpc9esWOwK2JcyWAj3nUwsdW6Kbq8gsf/ndYAveD 346 LUXZT0qUJfJn3PYgXbV2yaaae90Ifw8y8zvTbNucWfpFEDIWyMi7Gggc7hBi4AGl 347 XRqTb0QcPvlKGLXdFO0HMxQK3kxix/J/PhSM7iOTHPjCdUym27eYBX0UswIDAQAB 348 AoGBAIYcrvuqDboguI8U4TUjCkfSAgds1pLLWk79wu8jXkA329d1IyNKT0y3WIye 349 PbyoEzmidZmZROQ/+ZsPz8c12Y0DrX73WSVzKNyJeP7XMk9HSzA1D9RX0U0S+5Kh 350 FAMc2NEVVFIfQtVtoVmHdKDpnRYtOCHLW9rRpvqOOjd4mYk5AkEAzeiFr1mtlnsa 351 67shMxzDaOTAFMchRz6G7aSovvCztxcB63ulFI/w9OTUMdTQ7ff7pet+lVihLc2W 352 efIL0HvsjQJBAMocNTKaR/TnsV5GSk2kPAdR+zFP5sQy8sfMy0lEXTylc7zN4ajX 353 MeHVoxp+GZgpfDcZ3ya808H1umyXh+xA1j8CQE9x9ZKQYT98RAjL7KVR5btk9w+N 354 PTPF1j1+mHUDXfO4ds8qp6jlWKzEVXLcj7ghRADiebaZuaZ4eiSW1SQdjEkCQQC4 355 wDhQ3X9RfEpCp3ZcqvjEqEg6t5N3XitYQPjDLN8eBRBbUsgpEy3iBuxl10eGNMX7 356 iIbYXlwkPYAArDPv3wT5AkAwp4vym+YKmDqh6gseKfRDuJqRiW9yD5A8VGr/w88k 357 5rkuduVGP7tK3uIp00Its3aEyKF8mLGWYszVGeeLxAMH 358 -----END RSA PRIVATE KEY----- 359 EOS 360 certificate = <<EOS 361 -----BEGIN CERTIFICATE----- 362 MIIBkDCB+gIJALoScFD0sJq3MA0GCSqGSIb3DQEBBQUAMA0xCzAJBgNVBAYTAkRF 363 MB4XDTE1MTIxOTIwMzU1MVoXDTE2MDExODIwMzU1MVowDTELMAkGA1UEBhMCREUw 364 gZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAKKQKbTTH/Julz16xY7ArYlzJYCP 365 edTCx1bopuryCx/+d1gC94MtRdlPSpQl8mfc9iBdtXbJppp73Qh/DzLzO9Ns25xZ 366 +kUQMhbIyLsaCBzuEGLgAaVdGpNvRBw++UoYtd0U7QczFAreTGLH8n8+FIzuI5Mc 367 +MJ1TKbbt5gFfRSzAgMBAAEwDQYJKoZIhvcNAQEFBQADgYEALARo96wCDmaHKCaX 368 S0IGLGnZCfiIUfCmBxOXBSJxDBwter95QHR0dMGxYIujee5n4vvavpVsqZnfMC3I 369 OZWPlwiUJbNIpK+04Bg2vd5m/NMMrvi75RfmyeMtSfq/NrIX2Q3+nyWI7DLq7yZI 370 V/YEvOqdAiy5NEWBztHx8HvB9G4= 371 -----END CERTIFICATE----- 372 EOS 373 } 374 app_source = { 375 type = "git" 376 revision = "master" 377 url = "https://github.com/aws/example.git" 378 } 379 environment = { key = "key1" value = "value1" secure = false} 380 environment = { key = "key2" value = "value2" } 381 document_root = "root" 382 auto_bundle_on_deploy = "true" 383 rails_env = "staging" 384 } 385 `, name) 386 }