github.com/sathiyas/terraform@v0.6.9-0.20151210233947-3330da00b997/builtin/providers/aws/resource_aws_cloudformation_stack_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/service/cloudformation"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccAWSCloudFormation_basic(t *testing.T) {
    14  	var stack cloudformation.Stack
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckAWSCloudFormationDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccAWSCloudFormationConfig,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckCloudFormationStackExists("aws_cloudformation_stack.network", &stack),
    25  				),
    26  			},
    27  		},
    28  	})
    29  }
    30  
    31  func TestAccAWSCloudFormation_defaultParams(t *testing.T) {
    32  	var stack cloudformation.Stack
    33  
    34  	resource.Test(t, resource.TestCase{
    35  		PreCheck:     func() { testAccPreCheck(t) },
    36  		Providers:    testAccProviders,
    37  		CheckDestroy: testAccCheckAWSCloudFormationDestroy,
    38  		Steps: []resource.TestStep{
    39  			resource.TestStep{
    40  				Config: testAccAWSCloudFormationConfig_defaultParams,
    41  				Check: resource.ComposeTestCheckFunc(
    42  					testAccCheckCloudFormationStackExists("aws_cloudformation_stack.asg-demo", &stack),
    43  				),
    44  			},
    45  		},
    46  	})
    47  }
    48  
    49  func TestAccAWSCloudFormation_allAttributes(t *testing.T) {
    50  	var stack cloudformation.Stack
    51  
    52  	resource.Test(t, resource.TestCase{
    53  		PreCheck:     func() { testAccPreCheck(t) },
    54  		Providers:    testAccProviders,
    55  		CheckDestroy: testAccCheckAWSCloudFormationDestroy,
    56  		Steps: []resource.TestStep{
    57  			resource.TestStep{
    58  				Config: testAccAWSCloudFormationConfig_allAttributes,
    59  				Check: resource.ComposeTestCheckFunc(
    60  					testAccCheckCloudFormationStackExists("aws_cloudformation_stack.full", &stack),
    61  				),
    62  			},
    63  		},
    64  	})
    65  }
    66  
    67  func testAccCheckCloudFormationStackExists(n string, stack *cloudformation.Stack) resource.TestCheckFunc {
    68  	return func(s *terraform.State) error {
    69  		rs, ok := s.RootModule().Resources[n]
    70  		if !ok {
    71  			return fmt.Errorf("Not found: %s", n)
    72  		}
    73  
    74  		conn := testAccProvider.Meta().(*AWSClient).cfconn
    75  		params := &cloudformation.DescribeStacksInput{
    76  			StackName: aws.String(rs.Primary.ID),
    77  		}
    78  		resp, err := conn.DescribeStacks(params)
    79  		if err != nil {
    80  			return err
    81  		}
    82  		if len(resp.Stacks) == 0 {
    83  			return fmt.Errorf("CloudFormation stack not found")
    84  		}
    85  
    86  		return nil
    87  	}
    88  }
    89  
    90  func testAccCheckAWSCloudFormationDestroy(s *terraform.State) error {
    91  	conn := testAccProvider.Meta().(*AWSClient).cfconn
    92  
    93  	for _, rs := range s.RootModule().Resources {
    94  		if rs.Type != "aws_cloudformation_stack" {
    95  			continue
    96  		}
    97  
    98  		params := cloudformation.DescribeStacksInput{
    99  			StackName: aws.String(rs.Primary.ID),
   100  		}
   101  
   102  		resp, err := conn.DescribeStacks(&params)
   103  
   104  		if err == nil {
   105  			if len(resp.Stacks) != 0 &&
   106  				*resp.Stacks[0].StackId == rs.Primary.ID {
   107  				return fmt.Errorf("CloudFormation stack still exists: %q", rs.Primary.ID)
   108  			}
   109  		}
   110  	}
   111  
   112  	return nil
   113  }
   114  
   115  var testAccAWSCloudFormationConfig = `
   116  resource "aws_cloudformation_stack" "network" {
   117    name = "tf-networking-stack"
   118    template_body = <<STACK
   119  {
   120    "Resources" : {
   121      "MyVPC": {
   122        "Type" : "AWS::EC2::VPC",
   123        "Properties" : {
   124          "CidrBlock" : "10.0.0.0/16",
   125          "Tags" : [
   126            {"Key": "Name", "Value": "Primary_CF_VPC"}
   127          ]
   128        }
   129      }
   130    },
   131    "Outputs" : {
   132      "DefaultSgId" : {
   133        "Description": "The ID of default security group",
   134        "Value" : { "Fn::GetAtt" : [ "MyVPC", "DefaultSecurityGroup" ]}
   135      },
   136      "VpcID" : {
   137        "Description": "The VPC ID",
   138        "Value" : { "Ref" : "MyVPC" }
   139      }
   140    }
   141  }
   142  STACK
   143  }`
   144  
   145  var testAccAWSCloudFormationConfig_defaultParams = `
   146  resource "aws_cloudformation_stack" "asg-demo" {
   147    name = "tf-asg-demo-stack"
   148    template_body = <<BODY
   149  {
   150      "Parameters": {
   151          "TopicName": {
   152              "Type": "String"
   153          },
   154          "VPCCIDR": {
   155              "Type": "String",
   156              "Default": "10.10.0.0/16"
   157          }
   158      },
   159      "Resources": {
   160          "NotificationTopic": {
   161              "Type": "AWS::SNS::Topic",
   162              "Properties": {
   163                  "TopicName": {
   164                      "Ref": "TopicName"
   165                  }
   166              }
   167          },
   168          "MyVPC": {
   169              "Type": "AWS::EC2::VPC",
   170              "Properties": {
   171                  "CidrBlock": {
   172                      "Ref": "VPCCIDR"
   173                  },
   174                  "Tags": [
   175                      {
   176                          "Key": "Name",
   177                          "Value": "Primary_CF_VPC"
   178                      }
   179                  ]
   180              }
   181          }
   182      },
   183      "Outputs": {
   184          "VPCCIDR": {
   185              "Value": {
   186                  "Ref": "VPCCIDR"
   187              }
   188          }
   189      }
   190  }
   191  BODY
   192  
   193    parameters {
   194      TopicName = "ExampleTopic"
   195    }
   196  }
   197  `
   198  
   199  var testAccAWSCloudFormationConfig_allAttributes = `
   200  resource "aws_cloudformation_stack" "full" {
   201    name = "tf-full-stack"
   202    template_body = <<STACK
   203  {
   204    "Resources" : {
   205      "MyVPC": {
   206        "Type" : "AWS::EC2::VPC",
   207        "Properties" : {
   208          "CidrBlock" : "10.0.0.0/16",
   209          "Tags" : [
   210            {"Key": "Name", "Value": "Primary_CF_VPC"}
   211          ]
   212        }
   213      }
   214    }
   215  }
   216  STACK
   217  
   218    capabilities = ["CAPABILITY_IAM"]
   219    notification_arns = ["${aws_sns_topic.cf-updates.arn}"]
   220    on_failure = "DELETE"
   221    timeout_in_minutes = 1
   222  }
   223  
   224  resource "aws_sns_topic" "cf-updates" {
   225    name = "tf-cf-notifications"
   226  }
   227  `