github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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/terraform/helper/schema"
     9  )
    10  
    11  func dataSourceAwsCloudFormationStack() *schema.Resource {
    12  	return &schema.Resource{
    13  		Read: dataSourceAwsCloudFormationStackRead,
    14  
    15  		Schema: map[string]*schema.Schema{
    16  			"name": {
    17  				Type:     schema.TypeString,
    18  				Required: true,
    19  			},
    20  			"template_body": {
    21  				Type:      schema.TypeString,
    22  				Computed:  true,
    23  				StateFunc: normalizeJson,
    24  			},
    25  			"capabilities": {
    26  				Type:     schema.TypeSet,
    27  				Computed: true,
    28  				Elem:     &schema.Schema{Type: schema.TypeString},
    29  				Set:      schema.HashString,
    30  			},
    31  			"description": {
    32  				Type:     schema.TypeString,
    33  				Computed: true,
    34  			},
    35  			"disable_rollback": {
    36  				Type:     schema.TypeBool,
    37  				Computed: true,
    38  			},
    39  			"notification_arns": {
    40  				Type:     schema.TypeSet,
    41  				Computed: true,
    42  				Elem:     &schema.Schema{Type: schema.TypeString},
    43  				Set:      schema.HashString,
    44  			},
    45  			"parameters": {
    46  				Type:     schema.TypeMap,
    47  				Computed: true,
    48  			},
    49  			"outputs": {
    50  				Type:     schema.TypeMap,
    51  				Computed: true,
    52  			},
    53  			"timeout_in_minutes": {
    54  				Type:     schema.TypeInt,
    55  				Computed: true,
    56  			},
    57  			"tags": {
    58  				Type:     schema.TypeMap,
    59  				Computed: true,
    60  			},
    61  		},
    62  	}
    63  }
    64  
    65  func dataSourceAwsCloudFormationStackRead(d *schema.ResourceData, meta interface{}) error {
    66  	conn := meta.(*AWSClient).cfconn
    67  	name := d.Get("name").(string)
    68  	input := cloudformation.DescribeStacksInput{
    69  		StackName: aws.String(name),
    70  	}
    71  
    72  	out, err := conn.DescribeStacks(&input)
    73  	if err != nil {
    74  		return fmt.Errorf("Failed describing CloudFormation stack (%s): %s", name, err)
    75  	}
    76  	if l := len(out.Stacks); l != 1 {
    77  		return fmt.Errorf("Expected 1 CloudFormation stack (%s), found %d", name, l)
    78  	}
    79  	stack := out.Stacks[0]
    80  	d.SetId(*stack.StackId)
    81  
    82  	d.Set("description", stack.Description)
    83  	d.Set("disable_rollback", stack.DisableRollback)
    84  	d.Set("timeout_in_minutes", stack.TimeoutInMinutes)
    85  
    86  	if len(stack.NotificationARNs) > 0 {
    87  		d.Set("notification_arns", schema.NewSet(schema.HashString, flattenStringList(stack.NotificationARNs)))
    88  	}
    89  
    90  	d.Set("parameters", flattenAllCloudFormationParameters(stack.Parameters))
    91  	d.Set("tags", flattenCloudFormationTags(stack.Tags))
    92  	d.Set("outputs", flattenCloudFormationOutputs(stack.Outputs))
    93  
    94  	if len(stack.Capabilities) > 0 {
    95  		d.Set("capabilities", schema.NewSet(schema.HashString, flattenStringList(stack.Capabilities)))
    96  	}
    97  
    98  	tInput := cloudformation.GetTemplateInput{
    99  		StackName: aws.String(name),
   100  	}
   101  	tOut, err := conn.GetTemplate(&tInput)
   102  	if err != nil {
   103  		return err
   104  	}
   105  
   106  	d.Set("template_body", normalizeJson(*tOut.TemplateBody))
   107  
   108  	return nil
   109  }