github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/resource_aws_opsworks_custom_layer_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  // These tests assume the existence of predefined Opsworks IAM roles named `aws-opsworks-ec2-role`
    16  // and `aws-opsworks-service-role`.
    17  
    18  func TestAccAWSOpsworksCustomLayer(t *testing.T) {
    19  	stackName := fmt.Sprintf("tf-%d", acctest.RandInt())
    20  	resource.Test(t, resource.TestCase{
    21  		PreCheck:     func() { testAccPreCheck(t) },
    22  		Providers:    testAccProviders,
    23  		CheckDestroy: testAccCheckAwsOpsworksCustomLayerDestroy,
    24  		Steps: []resource.TestStep{
    25  			resource.TestStep{
    26  				Config: testAccAwsOpsworksCustomLayerConfigNoVpcCreate(stackName),
    27  				Check: resource.ComposeTestCheckFunc(
    28  					resource.TestCheckResourceAttr(
    29  						"aws_opsworks_custom_layer.tf-acc", "name", stackName,
    30  					),
    31  					resource.TestCheckResourceAttr(
    32  						"aws_opsworks_custom_layer.tf-acc", "auto_assign_elastic_ips", "false",
    33  					),
    34  					resource.TestCheckResourceAttr(
    35  						"aws_opsworks_custom_layer.tf-acc", "auto_healing", "true",
    36  					),
    37  					resource.TestCheckResourceAttr(
    38  						"aws_opsworks_custom_layer.tf-acc", "drain_elb_on_shutdown", "true",
    39  					),
    40  					resource.TestCheckResourceAttr(
    41  						"aws_opsworks_custom_layer.tf-acc", "instance_shutdown_timeout", "300",
    42  					),
    43  					resource.TestCheckResourceAttr(
    44  						"aws_opsworks_custom_layer.tf-acc", "custom_security_group_ids.#", "2",
    45  					),
    46  					resource.TestCheckResourceAttr(
    47  						"aws_opsworks_custom_layer.tf-acc", "system_packages.#", "2",
    48  					),
    49  					resource.TestCheckResourceAttr(
    50  						"aws_opsworks_custom_layer.tf-acc", "system_packages.1368285564", "git",
    51  					),
    52  					resource.TestCheckResourceAttr(
    53  						"aws_opsworks_custom_layer.tf-acc", "system_packages.2937857443", "golang",
    54  					),
    55  					resource.TestCheckResourceAttr(
    56  						"aws_opsworks_custom_layer.tf-acc", "ebs_volume.#", "1",
    57  					),
    58  					resource.TestCheckResourceAttr(
    59  						"aws_opsworks_custom_layer.tf-acc", "ebs_volume.3575749636.type", "gp2",
    60  					),
    61  					resource.TestCheckResourceAttr(
    62  						"aws_opsworks_custom_layer.tf-acc", "ebs_volume.3575749636.number_of_disks", "2",
    63  					),
    64  					resource.TestCheckResourceAttr(
    65  						"aws_opsworks_custom_layer.tf-acc", "ebs_volume.3575749636.mount_point", "/home",
    66  					),
    67  					resource.TestCheckResourceAttr(
    68  						"aws_opsworks_custom_layer.tf-acc", "ebs_volume.3575749636.size", "100",
    69  					),
    70  				),
    71  			},
    72  			resource.TestStep{
    73  				Config: testAccAwsOpsworksCustomLayerConfigUpdate(stackName),
    74  				Check: resource.ComposeTestCheckFunc(
    75  					resource.TestCheckResourceAttr(
    76  						"aws_opsworks_custom_layer.tf-acc", "name", stackName,
    77  					),
    78  					resource.TestCheckResourceAttr(
    79  						"aws_opsworks_custom_layer.tf-acc", "drain_elb_on_shutdown", "false",
    80  					),
    81  					resource.TestCheckResourceAttr(
    82  						"aws_opsworks_custom_layer.tf-acc", "instance_shutdown_timeout", "120",
    83  					),
    84  					resource.TestCheckResourceAttr(
    85  						"aws_opsworks_custom_layer.tf-acc", "custom_security_group_ids.#", "3",
    86  					),
    87  					resource.TestCheckResourceAttr(
    88  						"aws_opsworks_custom_layer.tf-acc", "system_packages.#", "3",
    89  					),
    90  					resource.TestCheckResourceAttr(
    91  						"aws_opsworks_custom_layer.tf-acc", "system_packages.1368285564", "git",
    92  					),
    93  					resource.TestCheckResourceAttr(
    94  						"aws_opsworks_custom_layer.tf-acc", "system_packages.2937857443", "golang",
    95  					),
    96  					resource.TestCheckResourceAttr(
    97  						"aws_opsworks_custom_layer.tf-acc", "system_packages.4101929740", "subversion",
    98  					),
    99  					resource.TestCheckResourceAttr(
   100  						"aws_opsworks_custom_layer.tf-acc", "ebs_volume.#", "2",
   101  					),
   102  					resource.TestCheckResourceAttr(
   103  						"aws_opsworks_custom_layer.tf-acc", "ebs_volume.3575749636.type", "gp2",
   104  					),
   105  					resource.TestCheckResourceAttr(
   106  						"aws_opsworks_custom_layer.tf-acc", "ebs_volume.3575749636.number_of_disks", "2",
   107  					),
   108  					resource.TestCheckResourceAttr(
   109  						"aws_opsworks_custom_layer.tf-acc", "ebs_volume.3575749636.mount_point", "/home",
   110  					),
   111  					resource.TestCheckResourceAttr(
   112  						"aws_opsworks_custom_layer.tf-acc", "ebs_volume.3575749636.size", "100",
   113  					),
   114  					resource.TestCheckResourceAttr(
   115  						"aws_opsworks_custom_layer.tf-acc", "ebs_volume.1266957920.type", "io1",
   116  					),
   117  					resource.TestCheckResourceAttr(
   118  						"aws_opsworks_custom_layer.tf-acc", "ebs_volume.1266957920.number_of_disks", "4",
   119  					),
   120  					resource.TestCheckResourceAttr(
   121  						"aws_opsworks_custom_layer.tf-acc", "ebs_volume.1266957920.mount_point", "/var",
   122  					),
   123  					resource.TestCheckResourceAttr(
   124  						"aws_opsworks_custom_layer.tf-acc", "ebs_volume.1266957920.size", "100",
   125  					),
   126  					resource.TestCheckResourceAttr(
   127  						"aws_opsworks_custom_layer.tf-acc", "ebs_volume.1266957920.raid_level", "1",
   128  					),
   129  					resource.TestCheckResourceAttr(
   130  						"aws_opsworks_custom_layer.tf-acc", "ebs_volume.1266957920.iops", "3000",
   131  					),
   132  					resource.TestCheckResourceAttr(
   133  						"aws_opsworks_custom_layer.tf-acc", "custom_json", `{"layer_key":"layer_value2"}`,
   134  					),
   135  				),
   136  			},
   137  		},
   138  	})
   139  }
   140  
   141  func testAccCheckAwsOpsworksCustomLayerDestroy(s *terraform.State) error {
   142  	opsworksconn := testAccProvider.Meta().(*AWSClient).opsworksconn
   143  	for _, rs := range s.RootModule().Resources {
   144  		if rs.Type != "aws_opsworks_custom_layer" {
   145  			continue
   146  		}
   147  		req := &opsworks.DescribeLayersInput{
   148  			LayerIds: []*string{
   149  				aws.String(rs.Primary.ID),
   150  			},
   151  		}
   152  
   153  		_, err := opsworksconn.DescribeLayers(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 custom layer test")
   166  }
   167  
   168  func testAccAwsOpsworksCustomLayerSecurityGroups(name string) string {
   169  	return fmt.Sprintf(`
   170  resource "aws_security_group" "tf-ops-acc-layer1" {
   171    name = "%s-layer1"
   172    ingress {
   173      from_port = 8
   174      to_port = -1
   175      protocol = "icmp"
   176      cidr_blocks = ["0.0.0.0/0"]
   177    }
   178  }
   179  resource "aws_security_group" "tf-ops-acc-layer2" {
   180    name = "%s-layer2"
   181    ingress {
   182      from_port = 8
   183      to_port = -1
   184      protocol = "icmp"
   185      cidr_blocks = ["0.0.0.0/0"]
   186    }
   187  }`, name, name)
   188  }
   189  
   190  func testAccAwsOpsworksCustomLayerConfigNoVpcCreate(name string) string {
   191  	return fmt.Sprintf(`
   192  provider "aws" {
   193  	region = "us-east-1"
   194  }
   195  
   196  resource "aws_opsworks_custom_layer" "tf-acc" {
   197    stack_id = "${aws_opsworks_stack.tf-acc.id}"
   198    name = "%s"
   199    short_name = "tf-ops-acc-custom-layer"
   200    auto_assign_public_ips = true
   201    custom_security_group_ids = [
   202      "${aws_security_group.tf-ops-acc-layer1.id}",
   203      "${aws_security_group.tf-ops-acc-layer2.id}",
   204    ]
   205    drain_elb_on_shutdown = true
   206    instance_shutdown_timeout = 300
   207    system_packages = [
   208      "git",
   209      "golang",
   210    ]
   211    ebs_volume {
   212      type = "gp2"
   213      number_of_disks = 2
   214      mount_point = "/home"
   215      size = 100
   216      raid_level = 0
   217    }
   218  }
   219  
   220  %s
   221  
   222  %s 
   223  
   224  `, name, testAccAwsOpsworksStackConfigNoVpcCreate(name), testAccAwsOpsworksCustomLayerSecurityGroups(name))
   225  }
   226  
   227  func testAccAwsOpsworksCustomLayerConfigVpcCreate(name string) string {
   228  	return fmt.Sprintf(`
   229  provider "aws" {
   230  	region = "us-west-2"
   231  }
   232  
   233  resource "aws_opsworks_custom_layer" "tf-acc" {
   234    stack_id = "${aws_opsworks_stack.tf-acc.id}"
   235    name = "%s"
   236    short_name = "tf-ops-acc-custom-layer"
   237    auto_assign_public_ips = false
   238    custom_security_group_ids = [
   239      "${aws_security_group.tf-ops-acc-layer1.id}",
   240      "${aws_security_group.tf-ops-acc-layer2.id}",
   241    ]
   242    drain_elb_on_shutdown = true
   243    instance_shutdown_timeout = 300
   244    system_packages = [
   245      "git",
   246      "golang",
   247    ]
   248    ebs_volume {
   249      type = "gp2"
   250      number_of_disks = 2
   251      mount_point = "/home"
   252      size = 100
   253      raid_level = 0
   254    }
   255  }
   256  
   257  %s
   258  
   259  %s
   260  
   261  `, name, testAccAwsOpsworksStackConfigVpcCreate(name), testAccAwsOpsworksCustomLayerSecurityGroups(name))
   262  }
   263  
   264  func testAccAwsOpsworksCustomLayerConfigUpdate(name string) string {
   265  	return fmt.Sprintf(`
   266  provider "aws" {
   267    region = "us-east-1"
   268  }
   269  
   270  resource "aws_security_group" "tf-ops-acc-layer3" {
   271    name = "tf-ops-acc-layer3"
   272    ingress {
   273      from_port = 8
   274      to_port = -1
   275      protocol = "icmp"
   276      cidr_blocks = ["0.0.0.0/0"]
   277    }
   278  }
   279  resource "aws_opsworks_custom_layer" "tf-acc" {
   280    stack_id = "${aws_opsworks_stack.tf-acc.id}"
   281    name = "%s"
   282    short_name = "tf-ops-acc-custom-layer"
   283    auto_assign_public_ips = true
   284    custom_security_group_ids = [
   285      "${aws_security_group.tf-ops-acc-layer1.id}",
   286      "${aws_security_group.tf-ops-acc-layer2.id}",
   287      "${aws_security_group.tf-ops-acc-layer3.id}",
   288    ]
   289    drain_elb_on_shutdown = false
   290    instance_shutdown_timeout = 120
   291    system_packages = [
   292      "git",
   293      "golang",
   294      "subversion",
   295    ]
   296    ebs_volume {
   297      type = "gp2"
   298      number_of_disks = 2
   299      mount_point = "/home"
   300      size = 100
   301      raid_level = 0
   302    }
   303    ebs_volume {
   304      type = "io1"
   305      number_of_disks = 4
   306      mount_point = "/var"
   307      size = 100
   308      raid_level = 1
   309      iops = 3000
   310    }
   311    custom_json = "{\"layer_key\": \"layer_value2\"}"
   312  }
   313  
   314  %s
   315  
   316  %s 
   317  
   318  `, name, testAccAwsOpsworksStackConfigNoVpcCreate(name), testAccAwsOpsworksCustomLayerSecurityGroups(name))
   319  }