Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provider/aws: Add aws_cloudformation_stack #2636

Merged
merged 3 commits into from
Oct 28, 2015
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
provider/aws: Add acceptance tests for aws_cloudformation_stack
radeksimko committed Oct 13, 2015

Verified

This commit was signed with the committer’s verified signature.
tjanez Tadej Janež
commit 7088a0096e920501726c5769b941dfa85d4a38d6
228 changes: 228 additions & 0 deletions builtin/providers/aws/resource_aws_cloudformation_stack_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
package aws

import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccAWSCloudFormation_basic(t *testing.T) {
var stack cloudformation.Stack

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCloudFormationDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSCloudFormationConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudFormationStackExists("aws_cloudformation_stack.network", &stack),
),
},
},
})
}

func TestAccAWSCloudFormation_defaultParams(t *testing.T) {
var stack cloudformation.Stack

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCloudFormationDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSCloudFormationConfig_defaultParams,
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudFormationStackExists("aws_cloudformation_stack.asg-demo", &stack),
),
},
},
})
}

func TestAccAWSCloudFormation_allAttributes(t *testing.T) {
var stack cloudformation.Stack

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCloudFormationDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSCloudFormationConfig_allAttributes,
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudFormationStackExists("aws_cloudformation_stack.full", &stack),
),
},
},
})
}

func testAccCheckCloudFormationStackExists(n string, stack *cloudformation.Stack) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
rs = rs
return fmt.Errorf("Not found: %s", n)
}

conn := testAccProvider.Meta().(*AWSClient).cfconn
params := &cloudformation.DescribeStacksInput{
StackName: aws.String(rs.Primary.ID),
}
resp, err := conn.DescribeStacks(params)
if err != nil {
return err
}
if len(resp.Stacks) == 0 {
return fmt.Errorf("CloudFormation stack not found")
}

return nil
}
}

func testAccCheckAWSCloudFormationDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).cfconn

for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_cloudformation_stack" {
continue
}

params := cloudformation.DescribeStacksInput{
StackName: aws.String(rs.Primary.ID),
}

resp, err := conn.DescribeStacks(&params)

if err == nil {
if len(resp.Stacks) != 0 &&
*resp.Stacks[0].StackId == rs.Primary.ID {
return fmt.Errorf("CloudFormation stack still exists: %q", rs.Primary.ID)
}
}
}

return nil
}

var testAccAWSCloudFormationConfig = `
resource "aws_cloudformation_stack" "network" {
name = "tf-networking-stack"
template_body = <<STACK
{
"Resources" : {
"MyVPC": {
"Type" : "AWS::EC2::VPC",
"Properties" : {
"CidrBlock" : "10.0.0.0/16",
"Tags" : [
{"Key": "Name", "Value": "Primary_CF_VPC"}
]
}
}
},
"Outputs" : {
"DefaultSgId" : {
"Description": "The ID of default security group",
"Value" : { "Fn::GetAtt" : [ "MyVPC", "DefaultSecurityGroup" ]}
},
"VpcID" : {
"Description": "The VPC ID",
"Value" : { "Ref" : "MyVPC" }
}
}
}
STACK
}`

var testAccAWSCloudFormationConfig_defaultParams = `
resource "aws_cloudformation_stack" "asg-demo" {
name = "tf-asg-demo-stack"
template_body = <<BODY
{
"Parameters": {
"TopicName": {
"Type": "String"
},
"VPCCIDR": {
"Type": "String",
"Default": "10.10.0.0/16"
}
},
"Resources": {
"NotificationTopic": {
"Type": "AWS::SNS::Topic",
"Properties": {
"TopicName": {
"Ref": "TopicName"
}
}
},
"MyVPC": {
"Type": "AWS::EC2::VPC",
"Properties": {
"CidrBlock": {
"Ref": "VPCCIDR"
},
"Tags": [
{
"Key": "Name",
"Value": "Primary_CF_VPC"
}
]
}
}
},
"Outputs": {
"VPCCIDR": {
"Value": {
"Ref": "VPCCIDR"
}
}
}
}
BODY
parameters {
TopicName = "ExampleTopic"
}
}
`

var testAccAWSCloudFormationConfig_allAttributes = `
resource "aws_cloudformation_stack" "full" {
name = "tf-full-stack"
template_body = <<STACK
{
"Resources" : {
"MyVPC": {
"Type" : "AWS::EC2::VPC",
"Properties" : {
"CidrBlock" : "10.0.0.0/16",
"Tags" : [
{"Key": "Name", "Value": "Primary_CF_VPC"}
]
}
}
}
}
STACK
capabilities = ["CAPABILITY_IAM"]
notification_arns = ["${aws_sns_topic.cf-updates.arn}"]
on_failure = "DELETE"
timeout_in_minutes = 1
}
resource "aws_sns_topic" "cf-updates" {
name = "tf-cf-notifications"
}
`