forked from hashicorp/terraform-provider-aws
/
data_source_aws_ecs_service.go
89 lines (75 loc) · 2.03 KB
/
data_source_aws_ecs_service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package aws
import (
"fmt"
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceAwsEcsService() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsEcsServiceRead,
Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
"cluster_arn": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"desired_count": {
Type: schema.TypeInt,
Computed: true,
},
"launch_type": {
Type: schema.TypeString,
Computed: true,
},
"scheduling_strategy": {
Type: schema.TypeString,
Computed: true,
},
"task_definition": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceAwsEcsServiceRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ecsconn
clusterArn := d.Get("cluster_arn").(string)
serviceName := d.Get("service_name").(string)
params := &ecs.DescribeServicesInput{
Cluster: aws.String(clusterArn),
Services: []*string{aws.String(serviceName)},
}
log.Printf("[DEBUG] Reading ECS Service: %s", params)
desc, err := conn.DescribeServices(params)
if err != nil {
return err
}
if desc == nil || len(desc.Services) == 0 {
return fmt.Errorf("service with name %q in cluster %q not found", serviceName, clusterArn)
}
if len(desc.Services) > 1 {
return fmt.Errorf("multiple services with name %q found in cluster %q", serviceName, clusterArn)
}
service := desc.Services[0]
d.SetId(aws.StringValue(service.ServiceArn))
d.Set("service_name", service.ServiceName)
d.Set("arn", service.ServiceArn)
d.Set("cluster_arn", service.ClusterArn)
d.Set("desired_count", service.DesiredCount)
d.Set("launch_type", service.LaunchType)
d.Set("scheduling_strategy", service.SchedulingStrategy)
d.Set("task_definition", service.TaskDefinition)
return nil
}