github.com/gwilym/terraform@v0.3.8-0.20151231151641-c7573de75b19/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  // Regression for https://github.com/hashicorp/terraform/issues/4332
    68  func TestAccAWSCloudFormation_withParams(t *testing.T) {
    69  	var stack cloudformation.Stack
    70  
    71  	resource.Test(t, resource.TestCase{
    72  		PreCheck:     func() { testAccPreCheck(t) },
    73  		Providers:    testAccProviders,
    74  		CheckDestroy: testAccCheckAWSCloudFormationDestroy,
    75  		Steps: []resource.TestStep{
    76  			resource.TestStep{
    77  				Config: testAccAWSCloudFormationConfig_withParams,
    78  				Check: resource.ComposeTestCheckFunc(
    79  					testAccCheckCloudFormationStackExists("aws_cloudformation_stack.with_params", &stack),
    80  				),
    81  			},
    82  			resource.TestStep{
    83  				Config: testAccAWSCloudFormationConfig_withParams_modified,
    84  				Check: resource.ComposeTestCheckFunc(
    85  					testAccCheckCloudFormationStackExists("aws_cloudformation_stack.with_params", &stack),
    86  				),
    87  			},
    88  		},
    89  	})
    90  }
    91  
    92  func testAccCheckCloudFormationStackExists(n string, stack *cloudformation.Stack) resource.TestCheckFunc {
    93  	return func(s *terraform.State) error {
    94  		rs, ok := s.RootModule().Resources[n]
    95  		if !ok {
    96  			return fmt.Errorf("Not found: %s", n)
    97  		}
    98  
    99  		conn := testAccProvider.Meta().(*AWSClient).cfconn
   100  		params := &cloudformation.DescribeStacksInput{
   101  			StackName: aws.String(rs.Primary.ID),
   102  		}
   103  		resp, err := conn.DescribeStacks(params)
   104  		if err != nil {
   105  			return err
   106  		}
   107  		if len(resp.Stacks) == 0 {
   108  			return fmt.Errorf("CloudFormation stack not found")
   109  		}
   110  
   111  		return nil
   112  	}
   113  }
   114  
   115  func testAccCheckAWSCloudFormationDestroy(s *terraform.State) error {
   116  	conn := testAccProvider.Meta().(*AWSClient).cfconn
   117  
   118  	for _, rs := range s.RootModule().Resources {
   119  		if rs.Type != "aws_cloudformation_stack" {
   120  			continue
   121  		}
   122  
   123  		params := cloudformation.DescribeStacksInput{
   124  			StackName: aws.String(rs.Primary.ID),
   125  		}
   126  
   127  		resp, err := conn.DescribeStacks(&params)
   128  
   129  		if err != nil {
   130  			return err
   131  		}
   132  
   133  		for _, s := range resp.Stacks {
   134  			if *s.StackId == rs.Primary.ID && *s.StackStatus != "DELETE_COMPLETE" {
   135  				return fmt.Errorf("CloudFormation stack still exists: %q", rs.Primary.ID)
   136  			}
   137  		}
   138  	}
   139  
   140  	return nil
   141  }
   142  
   143  var testAccAWSCloudFormationConfig = `
   144  resource "aws_cloudformation_stack" "network" {
   145    name = "tf-networking-stack"
   146    template_body = <<STACK
   147  {
   148    "Resources" : {
   149      "MyVPC": {
   150        "Type" : "AWS::EC2::VPC",
   151        "Properties" : {
   152          "CidrBlock" : "10.0.0.0/16",
   153          "Tags" : [
   154            {"Key": "Name", "Value": "Primary_CF_VPC"}
   155          ]
   156        }
   157      }
   158    },
   159    "Outputs" : {
   160      "DefaultSgId" : {
   161        "Description": "The ID of default security group",
   162        "Value" : { "Fn::GetAtt" : [ "MyVPC", "DefaultSecurityGroup" ]}
   163      },
   164      "VpcID" : {
   165        "Description": "The VPC ID",
   166        "Value" : { "Ref" : "MyVPC" }
   167      }
   168    }
   169  }
   170  STACK
   171  }`
   172  
   173  var testAccAWSCloudFormationConfig_defaultParams = `
   174  resource "aws_cloudformation_stack" "asg-demo" {
   175    name = "tf-asg-demo-stack"
   176    template_body = <<BODY
   177  {
   178      "Parameters": {
   179          "TopicName": {
   180              "Type": "String"
   181          },
   182          "VPCCIDR": {
   183              "Type": "String",
   184              "Default": "10.10.0.0/16"
   185          }
   186      },
   187      "Resources": {
   188          "NotificationTopic": {
   189              "Type": "AWS::SNS::Topic",
   190              "Properties": {
   191                  "TopicName": {
   192                      "Ref": "TopicName"
   193                  }
   194              }
   195          },
   196          "MyVPC": {
   197              "Type": "AWS::EC2::VPC",
   198              "Properties": {
   199                  "CidrBlock": {
   200                      "Ref": "VPCCIDR"
   201                  },
   202                  "Tags": [
   203                      {
   204                          "Key": "Name",
   205                          "Value": "Primary_CF_VPC"
   206                      }
   207                  ]
   208              }
   209          }
   210      },
   211      "Outputs": {
   212          "VPCCIDR": {
   213              "Value": {
   214                  "Ref": "VPCCIDR"
   215              }
   216          }
   217      }
   218  }
   219  BODY
   220  
   221    parameters {
   222      TopicName = "ExampleTopic"
   223    }
   224  }
   225  `
   226  
   227  var testAccAWSCloudFormationConfig_allAttributes = `
   228  resource "aws_cloudformation_stack" "full" {
   229    name = "tf-full-stack"
   230    template_body = <<STACK
   231  {
   232    "Resources" : {
   233      "MyVPC": {
   234        "Type" : "AWS::EC2::VPC",
   235        "Properties" : {
   236          "CidrBlock" : "10.0.0.0/16",
   237          "Tags" : [
   238            {"Key": "Name", "Value": "Primary_CF_VPC"}
   239          ]
   240        }
   241      }
   242    }
   243  }
   244  STACK
   245  
   246    capabilities = ["CAPABILITY_IAM"]
   247    notification_arns = ["${aws_sns_topic.cf-updates.arn}"]
   248    on_failure = "DELETE"
   249    timeout_in_minutes = 1
   250  }
   251  
   252  resource "aws_sns_topic" "cf-updates" {
   253    name = "tf-cf-notifications"
   254  }
   255  `
   256  
   257  var tpl_testAccAWSCloudFormationConfig_withParams = `
   258  resource "aws_cloudformation_stack" "with_params" {
   259    name = "tf-stack-with-params"
   260    parameters {
   261      VpcCIDR = "%s"
   262    }
   263    template_body = <<STACK
   264  {
   265    "Parameters" : {
   266      "VpcCIDR" : {
   267        "Description" : "CIDR to be used for the VPC",
   268        "Type" : "String"
   269      }
   270    },
   271    "Resources" : {
   272      "MyVPC": {
   273        "Type" : "AWS::EC2::VPC",
   274        "Properties" : {
   275          "CidrBlock" : {"Ref": "VpcCIDR"},
   276          "Tags" : [
   277            {"Key": "Name", "Value": "Primary_CF_VPC"}
   278          ]
   279        }
   280      }
   281    }
   282  }
   283  STACK
   284  
   285    on_failure = "DELETE"
   286    timeout_in_minutes = 1
   287  }
   288  `
   289  
   290  var testAccAWSCloudFormationConfig_withParams = fmt.Sprintf(
   291  	tpl_testAccAWSCloudFormationConfig_withParams,
   292  	"10.0.0.0/16")
   293  var testAccAWSCloudFormationConfig_withParams_modified = fmt.Sprintf(
   294  	tpl_testAccAWSCloudFormationConfig_withParams,
   295  	"12.0.0.0/16")