github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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 "memory_reservation": &schema.Schema{ 45 Type: schema.TypeInt, 46 Computed: true, 47 }, 48 "disable_networking": &schema.Schema{ 49 Type: schema.TypeBool, 50 Computed: true, 51 }, 52 "docker_labels": &schema.Schema{ 53 Type: schema.TypeMap, 54 Computed: true, 55 Elem: schema.TypeString, 56 }, 57 "environment": &schema.Schema{ 58 Type: schema.TypeMap, 59 Computed: true, 60 Elem: schema.TypeString, 61 }, 62 }, 63 } 64 } 65 66 func dataSourceAwsEcsContainerDefinitionRead(d *schema.ResourceData, meta interface{}) error { 67 conn := meta.(*AWSClient).ecsconn 68 69 desc, err := conn.DescribeTaskDefinition(&ecs.DescribeTaskDefinitionInput{ 70 TaskDefinition: aws.String(d.Get("task_definition").(string)), 71 }) 72 73 if err != nil { 74 return err 75 } 76 77 taskDefinition := *desc.TaskDefinition 78 for _, def := range taskDefinition.ContainerDefinitions { 79 if aws.StringValue(def.Name) != d.Get("container_name").(string) { 80 continue 81 } 82 83 d.SetId(fmt.Sprintf("%s/%s", aws.StringValue(taskDefinition.TaskDefinitionArn), d.Get("container_name").(string))) 84 d.Set("image", aws.StringValue(def.Image)) 85 image := aws.StringValue(def.Image) 86 if strings.Contains(image, ":") { 87 d.Set("image_digest", strings.Split(image, ":")[1]) 88 } 89 d.Set("cpu", aws.Int64Value(def.Cpu)) 90 d.Set("memory", aws.Int64Value(def.Memory)) 91 d.Set("memory_reservation", aws.Int64Value(def.MemoryReservation)) 92 d.Set("disable_networking", aws.BoolValue(def.DisableNetworking)) 93 d.Set("docker_labels", aws.StringValueMap(def.DockerLabels)) 94 95 var environment = map[string]string{} 96 for _, keyValuePair := range def.Environment { 97 environment[aws.StringValue(keyValuePair.Name)] = aws.StringValue(keyValuePair.Value) 98 } 99 d.Set("environment", environment) 100 } 101 102 if d.Id() == "" { 103 return fmt.Errorf("container with name %q not found in task definition %q", d.Get("container_name").(string), d.Get("task_definition").(string)) 104 } 105 106 return nil 107 }