github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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 rs = rs 72 return fmt.Errorf("Not found: %s", n) 73 } 74 75 conn := testAccProvider.Meta().(*AWSClient).cfconn 76 params := &cloudformation.DescribeStacksInput{ 77 StackName: aws.String(rs.Primary.ID), 78 } 79 resp, err := conn.DescribeStacks(params) 80 if err != nil { 81 return err 82 } 83 if len(resp.Stacks) == 0 { 84 return fmt.Errorf("CloudFormation stack not found") 85 } 86 87 return nil 88 } 89 } 90 91 func testAccCheckAWSCloudFormationDestroy(s *terraform.State) error { 92 conn := testAccProvider.Meta().(*AWSClient).cfconn 93 94 for _, rs := range s.RootModule().Resources { 95 if rs.Type != "aws_cloudformation_stack" { 96 continue 97 } 98 99 params := cloudformation.DescribeStacksInput{ 100 StackName: aws.String(rs.Primary.ID), 101 } 102 103 resp, err := conn.DescribeStacks(¶ms) 104 105 if err == nil { 106 if len(resp.Stacks) != 0 && 107 *resp.Stacks[0].StackId == rs.Primary.ID { 108 return fmt.Errorf("CloudFormation stack still exists: %q", rs.Primary.ID) 109 } 110 } 111 } 112 113 return nil 114 } 115 116 var testAccAWSCloudFormationConfig = ` 117 resource "aws_cloudformation_stack" "network" { 118 name = "tf-networking-stack" 119 template_body = <<STACK 120 { 121 "Resources" : { 122 "MyVPC": { 123 "Type" : "AWS::EC2::VPC", 124 "Properties" : { 125 "CidrBlock" : "10.0.0.0/16", 126 "Tags" : [ 127 {"Key": "Name", "Value": "Primary_CF_VPC"} 128 ] 129 } 130 } 131 }, 132 "Outputs" : { 133 "DefaultSgId" : { 134 "Description": "The ID of default security group", 135 "Value" : { "Fn::GetAtt" : [ "MyVPC", "DefaultSecurityGroup" ]} 136 }, 137 "VpcID" : { 138 "Description": "The VPC ID", 139 "Value" : { "Ref" : "MyVPC" } 140 } 141 } 142 } 143 STACK 144 }` 145 146 var testAccAWSCloudFormationConfig_defaultParams = ` 147 resource "aws_cloudformation_stack" "asg-demo" { 148 name = "tf-asg-demo-stack" 149 template_body = <<BODY 150 { 151 "Parameters": { 152 "TopicName": { 153 "Type": "String" 154 }, 155 "VPCCIDR": { 156 "Type": "String", 157 "Default": "10.10.0.0/16" 158 } 159 }, 160 "Resources": { 161 "NotificationTopic": { 162 "Type": "AWS::SNS::Topic", 163 "Properties": { 164 "TopicName": { 165 "Ref": "TopicName" 166 } 167 } 168 }, 169 "MyVPC": { 170 "Type": "AWS::EC2::VPC", 171 "Properties": { 172 "CidrBlock": { 173 "Ref": "VPCCIDR" 174 }, 175 "Tags": [ 176 { 177 "Key": "Name", 178 "Value": "Primary_CF_VPC" 179 } 180 ] 181 } 182 } 183 }, 184 "Outputs": { 185 "VPCCIDR": { 186 "Value": { 187 "Ref": "VPCCIDR" 188 } 189 } 190 } 191 } 192 BODY 193 194 parameters { 195 TopicName = "ExampleTopic" 196 } 197 } 198 ` 199 200 var testAccAWSCloudFormationConfig_allAttributes = ` 201 resource "aws_cloudformation_stack" "full" { 202 name = "tf-full-stack" 203 template_body = <<STACK 204 { 205 "Resources" : { 206 "MyVPC": { 207 "Type" : "AWS::EC2::VPC", 208 "Properties" : { 209 "CidrBlock" : "10.0.0.0/16", 210 "Tags" : [ 211 {"Key": "Name", "Value": "Primary_CF_VPC"} 212 ] 213 } 214 } 215 } 216 } 217 STACK 218 219 capabilities = ["CAPABILITY_IAM"] 220 notification_arns = ["${aws_sns_topic.cf-updates.arn}"] 221 on_failure = "DELETE" 222 timeout_in_minutes = 1 223 } 224 225 resource "aws_sns_topic" "cf-updates" { 226 name = "tf-cf-notifications" 227 } 228 `