Skip to content

Commit

Permalink
data-source/aws_ecs_service: Address PR #3617 feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
bflad committed May 31, 2018
1 parent c101bf5 commit 1349dc2
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 67 deletions.
43 changes: 22 additions & 21 deletions aws/data_source_aws_ecs_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ func dataSourceAwsEcsService() *schema.Resource {
},
"desired_count": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"launch_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"task_definition": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
}
Expand All @@ -47,9 +47,12 @@ func dataSourceAwsEcsService() *schema.Resource {
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(d.Get("cluster_arn").(string)),
Services: []*string{aws.String(d.Get("service_name").(string))},
Cluster: aws.String(clusterArn),
Services: []*string{aws.String(serviceName)},
}

log.Printf("[DEBUG] Reading ECS Service: %s", params)
Expand All @@ -59,25 +62,23 @@ func dataSourceAwsEcsServiceRead(d *schema.ResourceData, meta interface{}) error
return err
}

for _, service := range desc.Services {
if aws.StringValue(service.ClusterArn) != d.Get("cluster_arn").(string) {
continue
}
if aws.StringValue(service.ServiceName) != d.Get("service_name").(string) {
continue
}
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("task_definition", service.TaskDefinition)
if desc == nil || len(desc.Services) == 0 {
return fmt.Errorf("service with name %q in cluster %q not found", serviceName, clusterArn)
}

if d.Id() == "" {
return fmt.Errorf("service with name %q in cluster %q not found", d.Get("service_name").(string), d.Get("cluster_arn").(string))
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("task_definition", service.TaskDefinition)

return nil
}
33 changes: 18 additions & 15 deletions aws/data_source_aws_ecs_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,34 @@ import (
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccAWSEcsDataSource_ecsService(t *testing.T) {
func TestAccAWSEcsServiceDataSource_basic(t *testing.T) {
dataSourceName := "data.aws_ecs_service.test"
resourceName := "aws_ecs_service.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckAwsEcsServiceDataSourceConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.aws_ecs_service.default", "service_name", "mongodb"),
resource.TestCheckResourceAttr("data.aws_ecs_service.default", "desired_count", "1"),
resource.TestCheckResourceAttr("data.aws_ecs_service.default", "launch_type", "EC2"),
resource.TestCheckResourceAttrSet("data.aws_ecs_service.default", "arn"),
resource.TestCheckResourceAttrSet("data.aws_ecs_service.default", "task_definition"),
resource.TestCheckResourceAttrPair(resourceName, "id", dataSourceName, "arn"),
resource.TestCheckResourceAttrPair(resourceName, "desired_count", dataSourceName, "desired_count"),
resource.TestCheckResourceAttrPair(resourceName, "launch_type", dataSourceName, "launch_type"),
resource.TestCheckResourceAttrPair(resourceName, "name", dataSourceName, "service_name"),
resource.TestCheckResourceAttrPair(resourceName, "task_definition", dataSourceName, "task_definition"),
),
},
},
})
}

var testAccCheckAwsEcsServiceDataSourceConfig = fmt.Sprintf(`
resource "aws_ecs_cluster" "default" {
name = "default-%d"
resource "aws_ecs_cluster" "test" {
name = "tf-acc-%d"
}
resource "aws_ecs_task_definition" "mongo" {
resource "aws_ecs_task_definition" "test" {
family = "mongodb"
container_definitions = <<DEFINITION
[
Expand All @@ -48,15 +51,15 @@ resource "aws_ecs_task_definition" "mongo" {
DEFINITION
}
resource "aws_ecs_service" "mongo" {
resource "aws_ecs_service" "test" {
name = "mongodb"
cluster = "${aws_ecs_cluster.default.id}"
task_definition = "${aws_ecs_task_definition.mongo.arn}"
cluster = "${aws_ecs_cluster.test.id}"
task_definition = "${aws_ecs_task_definition.test.arn}"
desired_count = 1
}
data "aws_ecs_service" "default" {
service_name = "${aws_ecs_service.mongo.name}"
cluster_arn = "${aws_ecs_cluster.default.arn}"
data "aws_ecs_service" "test" {
service_name = "${aws_ecs_service.test.name}"
cluster_arn = "${aws_ecs_cluster.test.arn}"
}
`, acctest.RandInt())
3 changes: 3 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@
<li<%= sidebar_current("docs-aws-datasource-ecs-task-definition") %>>
<a href="/docs/providers/aws/d/ecs_task_definition.html">aws_ecs_task_definition</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-ecs-service") %>>
<a href="/docs/providers/aws/d/ecs_service.html">aws_ecs_service</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-efs-file-system") %>>
<a href="/docs/providers/aws/d/efs_file_system.html">aws_efs_file_system</a>
</li>
Expand Down
34 changes: 3 additions & 31 deletions website/docs/d/ecs_service.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,9 @@ Service within a AWS ECS Cluster.
## Example Usage

```hcl
data "aws_ecs_service" "ecs-mongo" {
service_name = "${aws_ecs_service.mongo.name}"
cluster_arn = "${aws_ecs_cluster.foo.arn}"
}
resource "aws_ecs_cluster" "foo" {
name = "foo"
}
resource "aws_ecs_task_definition" "mongo" {
family = "mongodb"
container_definitions = <<DEFINITION
[
{
"cpu": 128,
"essential": true,
"image": "mongo:latest",
"memory": 128,
"memoryReservation": 64,
"name": "mongodb"
}
]
DEFINITION
}
resource "aws_ecs_service" "mongo" {
name = "mongo"
cluster = "${aws_ecs_cluster.foo.id}"
desired_count = 2
task_definition = "${aws_ecs_task_definition.mongo.arn}"
data "aws_ecs_service" "example" {
service_name = "example"
cluster_arn = "${data.aws_ecs_cluster.example.arn}"
}
```

Expand Down

0 comments on commit 1349dc2

Please sign in to comment.