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