github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/resource_aws_opsworks_instance_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/aws/aws-sdk-go/aws/awserr" 9 "github.com/aws/aws-sdk-go/service/opsworks" 10 "github.com/hashicorp/terraform/helper/acctest" 11 "github.com/hashicorp/terraform/helper/resource" 12 "github.com/hashicorp/terraform/terraform" 13 ) 14 15 func TestAccAWSOpsworksInstance_importBasic(t *testing.T) { 16 stackName := fmt.Sprintf("tf-%d", acctest.RandInt()) 17 resourceName := "aws_opsworks_instance.tf-acc" 18 19 resource.Test(t, resource.TestCase{ 20 PreCheck: func() { testAccPreCheck(t) }, 21 Providers: testAccProviders, 22 CheckDestroy: testAccCheckAwsOpsworksInstanceDestroy, 23 Steps: []resource.TestStep{ 24 { 25 Config: testAccAwsOpsworksInstanceConfigCreate(stackName), 26 }, 27 28 { 29 ResourceName: resourceName, 30 ImportState: true, 31 ImportStateVerify: true, 32 ImportStateVerifyIgnore: []string{"state"}, //state is something we pass to the API and get back as status :( 33 }, 34 }, 35 }) 36 } 37 38 func TestAccAWSOpsworksInstance(t *testing.T) { 39 stackName := fmt.Sprintf("tf-%d", acctest.RandInt()) 40 var opsinst opsworks.Instance 41 resource.Test(t, resource.TestCase{ 42 PreCheck: func() { testAccPreCheck(t) }, 43 Providers: testAccProviders, 44 CheckDestroy: testAccCheckAwsOpsworksInstanceDestroy, 45 Steps: []resource.TestStep{ 46 { 47 Config: testAccAwsOpsworksInstanceConfigCreate(stackName), 48 Check: resource.ComposeTestCheckFunc( 49 testAccCheckAWSOpsworksInstanceExists( 50 "aws_opsworks_instance.tf-acc", &opsinst), 51 testAccCheckAWSOpsworksInstanceAttributes(&opsinst), 52 resource.TestCheckResourceAttr( 53 "aws_opsworks_instance.tf-acc", "hostname", "tf-acc1", 54 ), 55 resource.TestCheckResourceAttr( 56 "aws_opsworks_instance.tf-acc", "instance_type", "t2.micro", 57 ), 58 resource.TestCheckResourceAttr( 59 "aws_opsworks_instance.tf-acc", "state", "stopped", 60 ), 61 resource.TestCheckResourceAttr( 62 "aws_opsworks_instance.tf-acc", "layer_ids.#", "1", 63 ), 64 resource.TestCheckResourceAttr( 65 "aws_opsworks_instance.tf-acc", "install_updates_on_boot", "true", 66 ), 67 resource.TestCheckResourceAttr( 68 "aws_opsworks_instance.tf-acc", "architecture", "x86_64", 69 ), 70 resource.TestCheckResourceAttr( 71 "aws_opsworks_instance.tf-acc", "tenancy", "default", 72 ), 73 resource.TestCheckResourceAttr( 74 "aws_opsworks_instance.tf-acc", "os", "Amazon Linux 2014.09", // inherited from opsworks_stack_test 75 ), 76 resource.TestCheckResourceAttr( 77 "aws_opsworks_instance.tf-acc", "root_device_type", "ebs", // inherited from opsworks_stack_test 78 ), 79 resource.TestCheckResourceAttr( 80 "aws_opsworks_instance.tf-acc", "availability_zone", "us-west-2a", // inherited from opsworks_stack_test 81 ), 82 ), 83 }, 84 { 85 Config: testAccAwsOpsworksInstanceConfigUpdate(stackName), 86 Check: resource.ComposeTestCheckFunc( 87 testAccCheckAWSOpsworksInstanceExists( 88 "aws_opsworks_instance.tf-acc", &opsinst), 89 testAccCheckAWSOpsworksInstanceAttributes(&opsinst), 90 resource.TestCheckResourceAttr( 91 "aws_opsworks_instance.tf-acc", "hostname", "tf-acc1", 92 ), 93 resource.TestCheckResourceAttr( 94 "aws_opsworks_instance.tf-acc", "instance_type", "t2.small", 95 ), 96 resource.TestCheckResourceAttr( 97 "aws_opsworks_instance.tf-acc", "layer_ids.#", "2", 98 ), 99 resource.TestCheckResourceAttr( 100 "aws_opsworks_instance.tf-acc", "os", "Amazon Linux 2015.09", 101 ), 102 resource.TestCheckResourceAttr( 103 "aws_opsworks_instance.tf-acc", "tenancy", "default", 104 ), 105 ), 106 }, 107 }, 108 }) 109 } 110 111 func testAccCheckAWSOpsworksInstanceExists( 112 n string, opsinst *opsworks.Instance) resource.TestCheckFunc { 113 return func(s *terraform.State) error { 114 rs, ok := s.RootModule().Resources[n] 115 if !ok { 116 return fmt.Errorf("Not found: %s", n) 117 } 118 119 if rs.Primary.ID == "" { 120 return fmt.Errorf("No Opsworks Instance is set") 121 } 122 123 conn := testAccProvider.Meta().(*AWSClient).opsworksconn 124 125 params := &opsworks.DescribeInstancesInput{ 126 InstanceIds: []*string{&rs.Primary.ID}, 127 } 128 resp, err := conn.DescribeInstances(params) 129 130 if err != nil { 131 return err 132 } 133 134 if v := len(resp.Instances); v != 1 { 135 return fmt.Errorf("Expected 1 request returned, got %d", v) 136 } 137 138 *opsinst = *resp.Instances[0] 139 140 return nil 141 } 142 } 143 144 func testAccCheckAWSOpsworksInstanceAttributes( 145 opsinst *opsworks.Instance) resource.TestCheckFunc { 146 return func(s *terraform.State) error { 147 // Depending on the timing, the state could be requested or stopped 148 if *opsinst.Status != "stopped" && *opsinst.Status != "requested" { 149 return fmt.Errorf("Unexpected request status: %s", *opsinst.Status) 150 } 151 if *opsinst.AvailabilityZone != "us-west-2a" { 152 return fmt.Errorf("Unexpected availability zone: %s", *opsinst.AvailabilityZone) 153 } 154 if *opsinst.Architecture != "x86_64" { 155 return fmt.Errorf("Unexpected architecture: %s", *opsinst.Architecture) 156 } 157 if *opsinst.Tenancy != "default" { 158 return fmt.Errorf("Unexpected tenancy: %s", *opsinst.Tenancy) 159 } 160 if *opsinst.InfrastructureClass != "ec2" { 161 return fmt.Errorf("Unexpected infrastructure class: %s", *opsinst.InfrastructureClass) 162 } 163 if *opsinst.RootDeviceType != "ebs" { 164 return fmt.Errorf("Unexpected root device type: %s", *opsinst.RootDeviceType) 165 } 166 if *opsinst.VirtualizationType != "hvm" { 167 return fmt.Errorf("Unexpected virtualization type: %s", *opsinst.VirtualizationType) 168 } 169 return nil 170 } 171 } 172 173 func testAccCheckAwsOpsworksInstanceDestroy(s *terraform.State) error { 174 opsworksconn := testAccProvider.Meta().(*AWSClient).opsworksconn 175 for _, rs := range s.RootModule().Resources { 176 if rs.Type != "aws_opsworks_instance" { 177 continue 178 } 179 req := &opsworks.DescribeInstancesInput{ 180 InstanceIds: []*string{ 181 aws.String(rs.Primary.ID), 182 }, 183 } 184 185 _, err := opsworksconn.DescribeInstances(req) 186 if err != nil { 187 if awserr, ok := err.(awserr.Error); ok { 188 if awserr.Code() == "ResourceNotFoundException" { 189 // not found, good to go 190 return nil 191 } 192 } 193 return err 194 } 195 } 196 197 return fmt.Errorf("Fall through error on OpsWorks instance test") 198 } 199 200 func testAccAwsOpsworksInstanceConfigCreate(name string) string { 201 return fmt.Sprintf(` 202 resource "aws_security_group" "tf-ops-acc-web" { 203 name = "%s-web" 204 ingress { 205 from_port = 80 206 to_port = 80 207 protocol = "tcp" 208 cidr_blocks = ["0.0.0.0/0"] 209 } 210 } 211 212 resource "aws_security_group" "tf-ops-acc-php" { 213 name = "%s-php" 214 ingress { 215 from_port = 8080 216 to_port = 8080 217 protocol = "tcp" 218 cidr_blocks = ["0.0.0.0/0"] 219 } 220 } 221 222 resource "aws_opsworks_static_web_layer" "tf-acc" { 223 stack_id = "${aws_opsworks_stack.tf-acc.id}" 224 225 custom_security_group_ids = [ 226 "${aws_security_group.tf-ops-acc-web.id}", 227 ] 228 } 229 230 resource "aws_opsworks_php_app_layer" "tf-acc" { 231 stack_id = "${aws_opsworks_stack.tf-acc.id}" 232 233 custom_security_group_ids = [ 234 "${aws_security_group.tf-ops-acc-php.id}", 235 ] 236 } 237 238 resource "aws_opsworks_instance" "tf-acc" { 239 stack_id = "${aws_opsworks_stack.tf-acc.id}" 240 layer_ids = [ 241 "${aws_opsworks_static_web_layer.tf-acc.id}", 242 ] 243 instance_type = "t2.micro" 244 state = "stopped" 245 hostname = "tf-acc1" 246 } 247 248 %s 249 250 `, name, name, testAccAwsOpsworksStackConfigVpcCreate(name)) 251 } 252 253 func testAccAwsOpsworksInstanceConfigUpdate(name string) string { 254 return fmt.Sprintf(` 255 resource "aws_security_group" "tf-ops-acc-web" { 256 name = "%s-web" 257 ingress { 258 from_port = 80 259 to_port = 80 260 protocol = "tcp" 261 cidr_blocks = ["0.0.0.0/0"] 262 } 263 } 264 265 resource "aws_security_group" "tf-ops-acc-php" { 266 name = "%s-php" 267 ingress { 268 from_port = 8080 269 to_port = 8080 270 protocol = "tcp" 271 cidr_blocks = ["0.0.0.0/0"] 272 } 273 } 274 275 resource "aws_opsworks_static_web_layer" "tf-acc" { 276 stack_id = "${aws_opsworks_stack.tf-acc.id}" 277 278 custom_security_group_ids = [ 279 "${aws_security_group.tf-ops-acc-web.id}", 280 ] 281 } 282 283 resource "aws_opsworks_php_app_layer" "tf-acc" { 284 stack_id = "${aws_opsworks_stack.tf-acc.id}" 285 286 custom_security_group_ids = [ 287 "${aws_security_group.tf-ops-acc-php.id}", 288 ] 289 } 290 291 resource "aws_opsworks_instance" "tf-acc" { 292 stack_id = "${aws_opsworks_stack.tf-acc.id}" 293 layer_ids = [ 294 "${aws_opsworks_static_web_layer.tf-acc.id}", 295 "${aws_opsworks_php_app_layer.tf-acc.id}", 296 ] 297 instance_type = "t2.small" 298 state = "stopped" 299 hostname = "tf-acc1" 300 os = "Amazon Linux 2015.09" 301 } 302 303 %s 304 305 `, name, name, testAccAwsOpsworksStackConfigVpcCreate(name)) 306 }