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