2015-12-12 00:35:22 +01:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
|
|
"github.com/aws/aws-sdk-go/service/opsworks"
|
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2017-02-10 15:53:50 +01:00
|
|
|
func TestAccAWSOpsworksInstance_importBasic(t *testing.T) {
|
|
|
|
stackName := fmt.Sprintf("tf-%d", acctest.RandInt())
|
|
|
|
resourceName := "aws_opsworks_instance.tf-acc"
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAwsOpsworksInstanceDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
|
|
|
Config: testAccAwsOpsworksInstanceConfigCreate(stackName),
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
ResourceName: resourceName,
|
|
|
|
ImportState: true,
|
|
|
|
ImportStateVerify: true,
|
|
|
|
ImportStateVerifyIgnore: []string{"state"}, //state is something we pass to the API and get back as status :(
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-12-12 00:35:22 +01:00
|
|
|
func TestAccAWSOpsworksInstance(t *testing.T) {
|
|
|
|
stackName := fmt.Sprintf("tf-%d", acctest.RandInt())
|
2016-03-24 21:46:45 +01:00
|
|
|
var opsinst opsworks.Instance
|
2015-12-12 00:35:22 +01:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAwsOpsworksInstanceDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2017-02-10 15:53:50 +01:00
|
|
|
{
|
2015-12-12 00:35:22 +01:00
|
|
|
Config: testAccAwsOpsworksInstanceConfigCreate(stackName),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2016-03-24 21:46:45 +01:00
|
|
|
testAccCheckAWSOpsworksInstanceExists(
|
|
|
|
"aws_opsworks_instance.tf-acc", &opsinst),
|
|
|
|
testAccCheckAWSOpsworksInstanceAttributes(&opsinst),
|
2015-12-12 00:35:22 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_opsworks_instance.tf-acc", "hostname", "tf-acc1",
|
|
|
|
),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_opsworks_instance.tf-acc", "instance_type", "t2.micro",
|
|
|
|
),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_opsworks_instance.tf-acc", "state", "stopped",
|
|
|
|
),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_opsworks_instance.tf-acc", "layer_ids.#", "1",
|
|
|
|
),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_opsworks_instance.tf-acc", "install_updates_on_boot", "true",
|
|
|
|
),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_opsworks_instance.tf-acc", "architecture", "x86_64",
|
|
|
|
),
|
2016-12-28 23:48:24 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_opsworks_instance.tf-acc", "tenancy", "default",
|
|
|
|
),
|
2015-12-12 00:35:22 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2017-03-15 15:17:53 +01:00
|
|
|
"aws_opsworks_instance.tf-acc", "os", "Amazon Linux 2016.09", // inherited from opsworks_stack_test
|
2015-12-12 00:35:22 +01:00
|
|
|
),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_opsworks_instance.tf-acc", "root_device_type", "ebs", // inherited from opsworks_stack_test
|
|
|
|
),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_opsworks_instance.tf-acc", "availability_zone", "us-west-2a", // inherited from opsworks_stack_test
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
2017-02-10 15:53:50 +01:00
|
|
|
{
|
2015-12-12 00:35:22 +01:00
|
|
|
Config: testAccAwsOpsworksInstanceConfigUpdate(stackName),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2016-03-24 21:46:45 +01:00
|
|
|
testAccCheckAWSOpsworksInstanceExists(
|
|
|
|
"aws_opsworks_instance.tf-acc", &opsinst),
|
|
|
|
testAccCheckAWSOpsworksInstanceAttributes(&opsinst),
|
2015-12-12 00:35:22 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_opsworks_instance.tf-acc", "hostname", "tf-acc1",
|
|
|
|
),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_opsworks_instance.tf-acc", "instance_type", "t2.small",
|
|
|
|
),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_opsworks_instance.tf-acc", "layer_ids.#", "2",
|
|
|
|
),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_opsworks_instance.tf-acc", "os", "Amazon Linux 2015.09",
|
|
|
|
),
|
2016-12-28 23:48:24 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_opsworks_instance.tf-acc", "tenancy", "default",
|
|
|
|
),
|
2015-12-12 00:35:22 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-03-24 21:46:45 +01:00
|
|
|
func testAccCheckAWSOpsworksInstanceExists(
|
|
|
|
n string, opsinst *opsworks.Instance) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No Opsworks Instance is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).opsworksconn
|
|
|
|
|
|
|
|
params := &opsworks.DescribeInstancesInput{
|
|
|
|
InstanceIds: []*string{&rs.Primary.ID},
|
|
|
|
}
|
|
|
|
resp, err := conn.DescribeInstances(params)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if v := len(resp.Instances); v != 1 {
|
|
|
|
return fmt.Errorf("Expected 1 request returned, got %d", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
*opsinst = *resp.Instances[0]
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckAWSOpsworksInstanceAttributes(
|
|
|
|
opsinst *opsworks.Instance) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
// Depending on the timing, the state could be requested or stopped
|
|
|
|
if *opsinst.Status != "stopped" && *opsinst.Status != "requested" {
|
|
|
|
return fmt.Errorf("Unexpected request status: %s", *opsinst.Status)
|
|
|
|
}
|
|
|
|
if *opsinst.AvailabilityZone != "us-west-2a" {
|
|
|
|
return fmt.Errorf("Unexpected availability zone: %s", *opsinst.AvailabilityZone)
|
|
|
|
}
|
|
|
|
if *opsinst.Architecture != "x86_64" {
|
|
|
|
return fmt.Errorf("Unexpected architecture: %s", *opsinst.Architecture)
|
|
|
|
}
|
2016-12-28 23:48:24 +01:00
|
|
|
if *opsinst.Tenancy != "default" {
|
|
|
|
return fmt.Errorf("Unexpected tenancy: %s", *opsinst.Tenancy)
|
|
|
|
}
|
2016-03-24 21:46:45 +01:00
|
|
|
if *opsinst.InfrastructureClass != "ec2" {
|
|
|
|
return fmt.Errorf("Unexpected infrastructure class: %s", *opsinst.InfrastructureClass)
|
|
|
|
}
|
|
|
|
if *opsinst.RootDeviceType != "ebs" {
|
|
|
|
return fmt.Errorf("Unexpected root device type: %s", *opsinst.RootDeviceType)
|
|
|
|
}
|
|
|
|
if *opsinst.VirtualizationType != "hvm" {
|
|
|
|
return fmt.Errorf("Unexpected virtualization type: %s", *opsinst.VirtualizationType)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-12 00:35:22 +01:00
|
|
|
func testAccCheckAwsOpsworksInstanceDestroy(s *terraform.State) error {
|
|
|
|
opsworksconn := testAccProvider.Meta().(*AWSClient).opsworksconn
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "aws_opsworks_instance" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
req := &opsworks.DescribeInstancesInput{
|
|
|
|
InstanceIds: []*string{
|
|
|
|
aws.String(rs.Primary.ID),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := opsworksconn.DescribeInstances(req)
|
|
|
|
if err != nil {
|
|
|
|
if awserr, ok := err.(awserr.Error); ok {
|
|
|
|
if awserr.Code() == "ResourceNotFoundException" {
|
|
|
|
// not found, good to go
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("Fall through error on OpsWorks instance test")
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccAwsOpsworksInstanceConfigCreate(name string) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "aws_security_group" "tf-ops-acc-web" {
|
|
|
|
name = "%s-web"
|
|
|
|
ingress {
|
|
|
|
from_port = 80
|
|
|
|
to_port = 80
|
|
|
|
protocol = "tcp"
|
|
|
|
cidr_blocks = ["0.0.0.0/0"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group" "tf-ops-acc-php" {
|
|
|
|
name = "%s-php"
|
|
|
|
ingress {
|
|
|
|
from_port = 8080
|
|
|
|
to_port = 8080
|
|
|
|
protocol = "tcp"
|
|
|
|
cidr_blocks = ["0.0.0.0/0"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_opsworks_static_web_layer" "tf-acc" {
|
|
|
|
stack_id = "${aws_opsworks_stack.tf-acc.id}"
|
|
|
|
|
|
|
|
custom_security_group_ids = [
|
|
|
|
"${aws_security_group.tf-ops-acc-web.id}",
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_opsworks_php_app_layer" "tf-acc" {
|
|
|
|
stack_id = "${aws_opsworks_stack.tf-acc.id}"
|
|
|
|
|
|
|
|
custom_security_group_ids = [
|
|
|
|
"${aws_security_group.tf-ops-acc-php.id}",
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_opsworks_instance" "tf-acc" {
|
|
|
|
stack_id = "${aws_opsworks_stack.tf-acc.id}"
|
|
|
|
layer_ids = [
|
|
|
|
"${aws_opsworks_static_web_layer.tf-acc.id}",
|
|
|
|
]
|
|
|
|
instance_type = "t2.micro"
|
|
|
|
state = "stopped"
|
|
|
|
hostname = "tf-acc1"
|
|
|
|
}
|
|
|
|
|
|
|
|
%s
|
|
|
|
|
|
|
|
`, name, name, testAccAwsOpsworksStackConfigVpcCreate(name))
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccAwsOpsworksInstanceConfigUpdate(name string) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "aws_security_group" "tf-ops-acc-web" {
|
|
|
|
name = "%s-web"
|
|
|
|
ingress {
|
|
|
|
from_port = 80
|
|
|
|
to_port = 80
|
|
|
|
protocol = "tcp"
|
|
|
|
cidr_blocks = ["0.0.0.0/0"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group" "tf-ops-acc-php" {
|
|
|
|
name = "%s-php"
|
|
|
|
ingress {
|
|
|
|
from_port = 8080
|
|
|
|
to_port = 8080
|
|
|
|
protocol = "tcp"
|
|
|
|
cidr_blocks = ["0.0.0.0/0"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_opsworks_static_web_layer" "tf-acc" {
|
|
|
|
stack_id = "${aws_opsworks_stack.tf-acc.id}"
|
|
|
|
|
|
|
|
custom_security_group_ids = [
|
|
|
|
"${aws_security_group.tf-ops-acc-web.id}",
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_opsworks_php_app_layer" "tf-acc" {
|
|
|
|
stack_id = "${aws_opsworks_stack.tf-acc.id}"
|
|
|
|
|
|
|
|
custom_security_group_ids = [
|
|
|
|
"${aws_security_group.tf-ops-acc-php.id}",
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_opsworks_instance" "tf-acc" {
|
|
|
|
stack_id = "${aws_opsworks_stack.tf-acc.id}"
|
|
|
|
layer_ids = [
|
|
|
|
"${aws_opsworks_static_web_layer.tf-acc.id}",
|
|
|
|
"${aws_opsworks_php_app_layer.tf-acc.id}",
|
|
|
|
]
|
|
|
|
instance_type = "t2.small"
|
|
|
|
state = "stopped"
|
|
|
|
hostname = "tf-acc1"
|
|
|
|
os = "Amazon Linux 2015.09"
|
|
|
|
}
|
|
|
|
|
|
|
|
%s
|
|
|
|
|
|
|
|
`, name, name, testAccAwsOpsworksStackConfigVpcCreate(name))
|
|
|
|
}
|