github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/data_source_aws_cloudformation_stack.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 6 "github.com/aws/aws-sdk-go/aws" 7 "github.com/aws/aws-sdk-go/service/cloudformation" 8 "github.com/hashicorp/errwrap" 9 "github.com/hashicorp/terraform/helper/schema" 10 ) 11 12 func dataSourceAwsCloudFormationStack() *schema.Resource { 13 return &schema.Resource{ 14 Read: dataSourceAwsCloudFormationStackRead, 15 16 Schema: map[string]*schema.Schema{ 17 "name": { 18 Type: schema.TypeString, 19 Required: true, 20 }, 21 "template_body": { 22 Type: schema.TypeString, 23 Computed: true, 24 StateFunc: func(v interface{}) string { 25 template, _ := normalizeCloudFormationTemplate(v) 26 return template 27 }, 28 }, 29 "capabilities": { 30 Type: schema.TypeSet, 31 Computed: true, 32 Elem: &schema.Schema{Type: schema.TypeString}, 33 Set: schema.HashString, 34 }, 35 "description": { 36 Type: schema.TypeString, 37 Computed: true, 38 }, 39 "disable_rollback": { 40 Type: schema.TypeBool, 41 Computed: true, 42 }, 43 "notification_arns": { 44 Type: schema.TypeSet, 45 Computed: true, 46 Elem: &schema.Schema{Type: schema.TypeString}, 47 Set: schema.HashString, 48 }, 49 "parameters": { 50 Type: schema.TypeMap, 51 Computed: true, 52 }, 53 "outputs": { 54 Type: schema.TypeMap, 55 Computed: true, 56 }, 57 "timeout_in_minutes": { 58 Type: schema.TypeInt, 59 Computed: true, 60 }, 61 "iam_role_arn": { 62 Type: schema.TypeString, 63 Computed: true, 64 }, 65 "tags": { 66 Type: schema.TypeMap, 67 Computed: true, 68 }, 69 }, 70 } 71 } 72 73 func dataSourceAwsCloudFormationStackRead(d *schema.ResourceData, meta interface{}) error { 74 conn := meta.(*AWSClient).cfconn 75 name := d.Get("name").(string) 76 input := cloudformation.DescribeStacksInput{ 77 StackName: aws.String(name), 78 } 79 80 out, err := conn.DescribeStacks(&input) 81 if err != nil { 82 return fmt.Errorf("Failed describing CloudFormation stack (%s): %s", name, err) 83 } 84 if l := len(out.Stacks); l != 1 { 85 return fmt.Errorf("Expected 1 CloudFormation stack (%s), found %d", name, l) 86 } 87 stack := out.Stacks[0] 88 d.SetId(*stack.StackId) 89 90 d.Set("description", stack.Description) 91 d.Set("disable_rollback", stack.DisableRollback) 92 d.Set("timeout_in_minutes", stack.TimeoutInMinutes) 93 d.Set("iam_role_arn", stack.RoleARN) 94 95 if len(stack.NotificationARNs) > 0 { 96 d.Set("notification_arns", schema.NewSet(schema.HashString, flattenStringList(stack.NotificationARNs))) 97 } 98 99 d.Set("parameters", flattenAllCloudFormationParameters(stack.Parameters)) 100 d.Set("tags", flattenCloudFormationTags(stack.Tags)) 101 d.Set("outputs", flattenCloudFormationOutputs(stack.Outputs)) 102 103 if len(stack.Capabilities) > 0 { 104 d.Set("capabilities", schema.NewSet(schema.HashString, flattenStringList(stack.Capabilities))) 105 } 106 107 tInput := cloudformation.GetTemplateInput{ 108 StackName: aws.String(name), 109 } 110 tOut, err := conn.GetTemplate(&tInput) 111 if err != nil { 112 return err 113 } 114 115 template, err := normalizeCloudFormationTemplate(*tOut.TemplateBody) 116 if err != nil { 117 return errwrap.Wrapf("template body contains an invalid JSON or YAML: {{err}}", err) 118 } 119 d.Set("template_body", template) 120 121 return nil 122 }