github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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  					json, _ := normalizeJsonString(v)
    26  					return json
    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  			"tags": {
    62  				Type:     schema.TypeMap,
    63  				Computed: true,
    64  			},
    65  		},
    66  	}
    67  }
    68  
    69  func dataSourceAwsCloudFormationStackRead(d *schema.ResourceData, meta interface{}) error {
    70  	conn := meta.(*AWSClient).cfconn
    71  	name := d.Get("name").(string)
    72  	input := cloudformation.DescribeStacksInput{
    73  		StackName: aws.String(name),
    74  	}
    75  
    76  	out, err := conn.DescribeStacks(&input)
    77  	if err != nil {
    78  		return fmt.Errorf("Failed describing CloudFormation stack (%s): %s", name, err)
    79  	}
    80  	if l := len(out.Stacks); l != 1 {
    81  		return fmt.Errorf("Expected 1 CloudFormation stack (%s), found %d", name, l)
    82  	}
    83  	stack := out.Stacks[0]
    84  	d.SetId(*stack.StackId)
    85  
    86  	d.Set("description", stack.Description)
    87  	d.Set("disable_rollback", stack.DisableRollback)
    88  	d.Set("timeout_in_minutes", stack.TimeoutInMinutes)
    89  
    90  	if len(stack.NotificationARNs) > 0 {
    91  		d.Set("notification_arns", schema.NewSet(schema.HashString, flattenStringList(stack.NotificationARNs)))
    92  	}
    93  
    94  	d.Set("parameters", flattenAllCloudFormationParameters(stack.Parameters))
    95  	d.Set("tags", flattenCloudFormationTags(stack.Tags))
    96  	d.Set("outputs", flattenCloudFormationOutputs(stack.Outputs))
    97  
    98  	if len(stack.Capabilities) > 0 {
    99  		d.Set("capabilities", schema.NewSet(schema.HashString, flattenStringList(stack.Capabilities)))
   100  	}
   101  
   102  	tInput := cloudformation.GetTemplateInput{
   103  		StackName: aws.String(name),
   104  	}
   105  	tOut, err := conn.GetTemplate(&tInput)
   106  	if err != nil {
   107  		return err
   108  	}
   109  
   110  	template, err := normalizeJsonString(*tOut.TemplateBody)
   111  	if err != nil {
   112  		return errwrap.Wrapf("template body contains an invalid JSON: {{err}}", err)
   113  	}
   114  	d.Set("template_body", template)
   115  
   116  	return nil
   117  }