github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_opsworks_stack_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/terraform" 9 10 "github.com/aws/aws-sdk-go/aws" 11 "github.com/aws/aws-sdk-go/service/opsworks" 12 ) 13 14 ////////////////////////////////////////////////// 15 //// Helper configs for the necessary IAM objects 16 ////////////////////////////////////////////////// 17 18 var testAccAwsOpsworksStackIamConfig = ` 19 resource "aws_iam_role" "opsworks_service" { 20 name = "terraform_testacc_opsworks_service" 21 assume_role_policy = <<EOT 22 { 23 "Version": "2008-10-17", 24 "Statement": [ 25 { 26 "Sid": "", 27 "Effect": "Allow", 28 "Principal": { 29 "Service": "opsworks.amazonaws.com" 30 }, 31 "Action": "sts:AssumeRole" 32 } 33 ] 34 } 35 EOT 36 } 37 38 resource "aws_iam_role_policy" "opsworks_service" { 39 name = "terraform_testacc_opsworks_service" 40 role = "${aws_iam_role.opsworks_service.id}" 41 policy = <<EOT 42 { 43 "Statement": [ 44 { 45 "Action": [ 46 "ec2:*", 47 "iam:PassRole", 48 "cloudwatch:GetMetricStatistics", 49 "elasticloadbalancing:*", 50 "rds:*" 51 ], 52 "Effect": "Allow", 53 "Resource": ["*"] 54 } 55 ] 56 } 57 EOT 58 } 59 60 resource "aws_iam_role" "opsworks_instance" { 61 name = "terraform_testacc_opsworks_instance" 62 assume_role_policy = <<EOT 63 { 64 "Version": "2008-10-17", 65 "Statement": [ 66 { 67 "Sid": "", 68 "Effect": "Allow", 69 "Principal": { 70 "Service": "ec2.amazonaws.com" 71 }, 72 "Action": "sts:AssumeRole" 73 } 74 ] 75 } 76 EOT 77 } 78 79 resource "aws_iam_instance_profile" "opsworks_instance" { 80 name = "terraform_testacc_opsworks_instance" 81 roles = ["${aws_iam_role.opsworks_instance.name}"] 82 } 83 84 ` 85 86 /////////////////////////////// 87 //// Tests for the No-VPC case 88 /////////////////////////////// 89 90 var testAccAwsOpsworksStackConfigNoVpcCreate = testAccAwsOpsworksStackIamConfig + ` 91 resource "aws_opsworks_stack" "tf-acc" { 92 name = "tf-opsworks-acc" 93 region = "us-west-2" 94 service_role_arn = "${aws_iam_role.opsworks_service.arn}" 95 default_instance_profile_arn = "${aws_iam_instance_profile.opsworks_instance.arn}" 96 default_availability_zone = "us-west-2a" 97 default_os = "Amazon Linux 2014.09" 98 default_root_device_type = "ebs" 99 custom_json = "{\"key\": \"value\"}" 100 configuration_manager_version = "11.10" 101 use_opsworks_security_groups = false 102 } 103 ` 104 var testAccAWSOpsworksStackConfigNoVpcUpdate = testAccAwsOpsworksStackIamConfig + ` 105 resource "aws_opsworks_stack" "tf-acc" { 106 name = "tf-opsworks-acc" 107 region = "us-west-2" 108 service_role_arn = "${aws_iam_role.opsworks_service.arn}" 109 default_instance_profile_arn = "${aws_iam_instance_profile.opsworks_instance.arn}" 110 default_availability_zone = "us-west-2a" 111 default_os = "Amazon Linux 2014.09" 112 default_root_device_type = "ebs" 113 custom_json = "{\"key\": \"value\"}" 114 configuration_manager_version = "11.10" 115 use_opsworks_security_groups = false 116 use_custom_cookbooks = true 117 manage_berkshelf = true 118 custom_cookbooks_source { 119 type = "git" 120 revision = "master" 121 url = "https://github.com/aws/opsworks-example-cookbooks.git" 122 } 123 } 124 ` 125 126 func TestAccAwsOpsworksStackNoVpc(t *testing.T) { 127 resource.Test(t, resource.TestCase{ 128 PreCheck: func() { testAccPreCheck(t) }, 129 Providers: testAccProviders, 130 CheckDestroy: testAccCheckAwsOpsworksStackDestroy, 131 Steps: []resource.TestStep{ 132 resource.TestStep{ 133 Config: testAccAwsOpsworksStackConfigNoVpcCreate, 134 Check: testAccAwsOpsworksStackCheckResourceAttrsCreate, 135 }, 136 resource.TestStep{ 137 Config: testAccAWSOpsworksStackConfigNoVpcUpdate, 138 Check: testAccAwsOpsworksStackCheckResourceAttrsUpdate, 139 }, 140 }, 141 }) 142 } 143 144 //////////////////////////// 145 //// Tests for the VPC case 146 //////////////////////////// 147 148 var testAccAwsOpsworksStackConfigVpcCreate = testAccAwsOpsworksStackIamConfig + ` 149 resource "aws_vpc" "tf-acc" { 150 cidr_block = "10.3.5.0/24" 151 } 152 resource "aws_subnet" "tf-acc" { 153 vpc_id = "${aws_vpc.tf-acc.id}" 154 cidr_block = "${aws_vpc.tf-acc.cidr_block}" 155 availability_zone = "us-west-2a" 156 } 157 resource "aws_opsworks_stack" "tf-acc" { 158 name = "tf-opsworks-acc" 159 region = "us-west-2" 160 vpc_id = "${aws_vpc.tf-acc.id}" 161 default_subnet_id = "${aws_subnet.tf-acc.id}" 162 service_role_arn = "${aws_iam_role.opsworks_service.arn}" 163 default_instance_profile_arn = "${aws_iam_instance_profile.opsworks_instance.arn}" 164 default_os = "Amazon Linux 2014.09" 165 default_root_device_type = "ebs" 166 custom_json = "{\"key\": \"value\"}" 167 configuration_manager_version = "11.10" 168 use_opsworks_security_groups = false 169 } 170 ` 171 172 var testAccAWSOpsworksStackConfigVpcUpdate = testAccAwsOpsworksStackIamConfig + ` 173 resource "aws_vpc" "tf-acc" { 174 cidr_block = "10.3.5.0/24" 175 } 176 resource "aws_subnet" "tf-acc" { 177 vpc_id = "${aws_vpc.tf-acc.id}" 178 cidr_block = "${aws_vpc.tf-acc.cidr_block}" 179 availability_zone = "us-west-2a" 180 } 181 resource "aws_opsworks_stack" "tf-acc" { 182 name = "tf-opsworks-acc" 183 region = "us-west-2" 184 vpc_id = "${aws_vpc.tf-acc.id}" 185 default_subnet_id = "${aws_subnet.tf-acc.id}" 186 service_role_arn = "${aws_iam_role.opsworks_service.arn}" 187 default_instance_profile_arn = "${aws_iam_instance_profile.opsworks_instance.arn}" 188 default_os = "Amazon Linux 2014.09" 189 default_root_device_type = "ebs" 190 custom_json = "{\"key\": \"value\"}" 191 configuration_manager_version = "11.10" 192 use_opsworks_security_groups = false 193 use_custom_cookbooks = true 194 manage_berkshelf = true 195 custom_cookbooks_source { 196 type = "git" 197 revision = "master" 198 url = "https://github.com/aws/opsworks-example-cookbooks.git" 199 } 200 } 201 ` 202 203 func TestAccAwsOpsworksStackVpc(t *testing.T) { 204 resource.Test(t, resource.TestCase{ 205 PreCheck: func() { testAccPreCheck(t) }, 206 Providers: testAccProviders, 207 CheckDestroy: testAccCheckAwsOpsworksStackDestroy, 208 Steps: []resource.TestStep{ 209 resource.TestStep{ 210 Config: testAccAwsOpsworksStackConfigVpcCreate, 211 Check: testAccAwsOpsworksStackCheckResourceAttrsCreate, 212 }, 213 resource.TestStep{ 214 Config: testAccAWSOpsworksStackConfigVpcUpdate, 215 Check: resource.ComposeTestCheckFunc( 216 testAccAwsOpsworksStackCheckResourceAttrsUpdate, 217 testAccAwsOpsworksCheckVpc, 218 ), 219 }, 220 }, 221 }) 222 } 223 224 //////////////////////////// 225 //// Checkers and Utilities 226 //////////////////////////// 227 228 var testAccAwsOpsworksStackCheckResourceAttrsCreate = resource.ComposeTestCheckFunc( 229 resource.TestCheckResourceAttr( 230 "aws_opsworks_stack.tf-acc", 231 "name", 232 "tf-opsworks-acc", 233 ), 234 resource.TestCheckResourceAttr( 235 "aws_opsworks_stack.tf-acc", 236 "default_availability_zone", 237 "us-west-2a", 238 ), 239 resource.TestCheckResourceAttr( 240 "aws_opsworks_stack.tf-acc", 241 "default_os", 242 "Amazon Linux 2014.09", 243 ), 244 resource.TestCheckResourceAttr( 245 "aws_opsworks_stack.tf-acc", 246 "default_root_device_type", 247 "ebs", 248 ), 249 resource.TestCheckResourceAttr( 250 "aws_opsworks_stack.tf-acc", 251 "custom_json", 252 `{"key": "value"}`, 253 ), 254 resource.TestCheckResourceAttr( 255 "aws_opsworks_stack.tf-acc", 256 "configuration_manager_version", 257 "11.10", 258 ), 259 resource.TestCheckResourceAttr( 260 "aws_opsworks_stack.tf-acc", 261 "use_opsworks_security_groups", 262 "false", 263 ), 264 ) 265 266 var testAccAwsOpsworksStackCheckResourceAttrsUpdate = resource.ComposeTestCheckFunc( 267 resource.TestCheckResourceAttr( 268 "aws_opsworks_stack.tf-acc", 269 "name", 270 "tf-opsworks-acc", 271 ), 272 resource.TestCheckResourceAttr( 273 "aws_opsworks_stack.tf-acc", 274 "default_availability_zone", 275 "us-west-2a", 276 ), 277 resource.TestCheckResourceAttr( 278 "aws_opsworks_stack.tf-acc", 279 "default_os", 280 "Amazon Linux 2014.09", 281 ), 282 resource.TestCheckResourceAttr( 283 "aws_opsworks_stack.tf-acc", 284 "default_root_device_type", 285 "ebs", 286 ), 287 resource.TestCheckResourceAttr( 288 "aws_opsworks_stack.tf-acc", 289 "custom_json", 290 `{"key": "value"}`, 291 ), 292 resource.TestCheckResourceAttr( 293 "aws_opsworks_stack.tf-acc", 294 "configuration_manager_version", 295 "11.10", 296 ), 297 resource.TestCheckResourceAttr( 298 "aws_opsworks_stack.tf-acc", 299 "use_opsworks_security_groups", 300 "false", 301 ), 302 resource.TestCheckResourceAttr( 303 "aws_opsworks_stack.tf-acc", 304 "use_custom_cookbooks", 305 "true", 306 ), 307 resource.TestCheckResourceAttr( 308 "aws_opsworks_stack.tf-acc", 309 "manage_berkshelf", 310 "true", 311 ), 312 resource.TestCheckResourceAttr( 313 "aws_opsworks_stack.tf-acc", 314 "custom_cookbooks_source.0.type", 315 "git", 316 ), 317 resource.TestCheckResourceAttr( 318 "aws_opsworks_stack.tf-acc", 319 "custom_cookbooks_source.0.revision", 320 "master", 321 ), 322 resource.TestCheckResourceAttr( 323 "aws_opsworks_stack.tf-acc", 324 "custom_cookbooks_source.0.url", 325 "https://github.com/aws/opsworks-example-cookbooks.git", 326 ), 327 ) 328 329 func testAccAwsOpsworksCheckVpc(s *terraform.State) error { 330 rs, ok := s.RootModule().Resources["aws_opsworks_stack.tf-acc"] 331 if !ok { 332 return fmt.Errorf("Not found: %s", "aws_opsworks_stack.tf-acc") 333 } 334 if rs.Primary.ID == "" { 335 return fmt.Errorf("No ID is set") 336 } 337 338 p := rs.Primary 339 340 opsworksconn := testAccProvider.Meta().(*AWSClient).opsworksconn 341 describeOpts := &opsworks.DescribeStacksInput{ 342 StackIds: []*string{aws.String(p.ID)}, 343 } 344 resp, err := opsworksconn.DescribeStacks(describeOpts) 345 if err != nil { 346 return err 347 } 348 if len(resp.Stacks) == 0 { 349 return fmt.Errorf("No stack %s not found", p.ID) 350 } 351 if p.Attributes["vpc_id"] != *resp.Stacks[0].VpcId { 352 return fmt.Errorf("VPCID Got %s, expected %s", *resp.Stacks[0].VpcId, p.Attributes["vpc_id"]) 353 } 354 if p.Attributes["default_subnet_id"] != *resp.Stacks[0].DefaultSubnetId { 355 return fmt.Errorf("VPCID Got %s, expected %s", *resp.Stacks[0].DefaultSubnetId, p.Attributes["default_subnet_id"]) 356 } 357 return nil 358 } 359 360 func testAccCheckAwsOpsworksStackDestroy(s *terraform.State) error { 361 if len(s.RootModule().Resources) > 0 { 362 return fmt.Errorf("Expected all resources to be gone, but found: %#v", s.RootModule().Resources) 363 } 364 365 return nil 366 }