github.com/boyvanduuren/terraform@v0.7.0-rc2.0.20160805175930-de822d909c40/builtin/providers/aws/data_source_aws_ecs_container_definition.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/service/ecs"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  )
    11  
    12  func dataSourceAwsEcsContainerDefinition() *schema.Resource {
    13  	return &schema.Resource{
    14  		Read: dataSourceAwsEcsContainerDefinitionRead,
    15  
    16  		Schema: map[string]*schema.Schema{
    17  			"task_definition": &schema.Schema{
    18  				Type:     schema.TypeString,
    19  				Required: true,
    20  				ForceNew: true,
    21  			},
    22  			"container_name": &schema.Schema{
    23  				Type:     schema.TypeString,
    24  				Required: true,
    25  				ForceNew: true,
    26  			},
    27  			// Computed values.
    28  			"image": &schema.Schema{
    29  				Type:     schema.TypeString,
    30  				Computed: true,
    31  			},
    32  			"image_digest": &schema.Schema{
    33  				Type:     schema.TypeString,
    34  				Computed: true,
    35  			},
    36  			"cpu": &schema.Schema{
    37  				Type:     schema.TypeInt,
    38  				Computed: true,
    39  			},
    40  			"memory": &schema.Schema{
    41  				Type:     schema.TypeInt,
    42  				Computed: true,
    43  			},
    44  			"disable_networking": &schema.Schema{
    45  				Type:     schema.TypeBool,
    46  				Computed: true,
    47  			},
    48  			"docker_labels": &schema.Schema{
    49  				Type:     schema.TypeMap,
    50  				Computed: true,
    51  				Elem:     schema.TypeString,
    52  			},
    53  			"environment": &schema.Schema{
    54  				Type:     schema.TypeMap,
    55  				Computed: true,
    56  				Elem:     schema.TypeString,
    57  			},
    58  		},
    59  	}
    60  }
    61  
    62  func dataSourceAwsEcsContainerDefinitionRead(d *schema.ResourceData, meta interface{}) error {
    63  	conn := meta.(*AWSClient).ecsconn
    64  
    65  	desc, err := conn.DescribeTaskDefinition(&ecs.DescribeTaskDefinitionInput{
    66  		TaskDefinition: aws.String(d.Get("task_definition").(string)),
    67  	})
    68  	if err != nil {
    69  		return err
    70  	}
    71  
    72  	taskDefinition := *desc.TaskDefinition
    73  	for _, def := range taskDefinition.ContainerDefinitions {
    74  		if aws.StringValue(def.Name) != d.Get("container_name").(string) {
    75  			continue
    76  		}
    77  
    78  		d.SetId(fmt.Sprintf("%s/%s", aws.StringValue(taskDefinition.TaskDefinitionArn), d.Get("container_name").(string)))
    79  		d.Set("image", aws.StringValue(def.Image))
    80  		image := aws.StringValue(def.Image)
    81  		if strings.Contains(image, ":") {
    82  			d.Set("image_digest", strings.Split(image, ":")[1])
    83  		}
    84  		d.Set("cpu", aws.Int64Value(def.Cpu))
    85  		d.Set("memory", aws.Int64Value(def.Memory))
    86  		d.Set("disable_networking", aws.BoolValue(def.DisableNetworking))
    87  		d.Set("docker_labels", aws.StringValueMap(def.DockerLabels))
    88  
    89  		var environment = map[string]string{}
    90  		for _, keyValuePair := range def.Environment {
    91  			environment[aws.StringValue(keyValuePair.Name)] = aws.StringValue(keyValuePair.Value)
    92  		}
    93  		d.Set("environment", environment)
    94  	}
    95  
    96  	if d.Id() == "" {
    97  		return fmt.Errorf("container with name %q not found in task definition %q", d.Get("container_name").(string), d.Get("task_definition").(string))
    98  	}
    99  
   100  	return nil
   101  }