Skip to content

Commit

Permalink
aws: Fix bug w/ changing ECS service LB association
Browse files Browse the repository at this point in the history
 - fixes #3444
 - fixes #4227
  • Loading branch information
radeksimko committed Dec 22, 2015
1 parent 7d6b980 commit b95c8fa
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
5 changes: 5 additions & 0 deletions builtin/providers/aws/resource_aws_ecs_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,32 @@ func resourceAwsEcsService() *schema.Resource {

"iam_role": &schema.Schema{
Type: schema.TypeString,
ForceNew: true,
Optional: true,
},

"load_balancer": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"elb_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"container_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"container_port": &schema.Schema{
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},
},
},
Expand Down
43 changes: 34 additions & 9 deletions builtin/providers/aws/resource_aws_ecs_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ func TestAccAWSEcsService_withIamRole(t *testing.T) {
testAccCheckAWSEcsServiceExists("aws_ecs_service.ghost"),
),
},
resource.TestStep{
Config: testAccAWSEcsService_withIamRole_modified,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEcsServiceExists("aws_ecs_service.ghost"),
),
},
},
})
}
Expand Down Expand Up @@ -209,6 +215,7 @@ func testAccCheckAWSEcsServiceDestroy(s *terraform.State) error {

out, err := conn.DescribeServices(&ecs.DescribeServicesInput{
Services: []*string{aws.String(rs.Primary.ID)},
Cluster: aws.String(rs.Primary.Attributes["cluster"]),
})

if awserr, ok := err.(awserr.Error); ok && awserr.Code() == "ClusterNotFoundException" {
Expand All @@ -217,8 +224,19 @@ func testAccCheckAWSEcsServiceDestroy(s *terraform.State) error {

if err == nil {
if len(out.Services) > 0 {
return fmt.Errorf("ECS service still exists:\n%#v", out.Services)
var activeServices []*ecs.Service
for _, svc := range out.Services {
if *svc.Status != "INACTIVE" {
activeServices = append(activeServices, svc)
}
}
if len(activeServices) == 0 {
return nil
}

return fmt.Errorf("ECS service still exists:\n%#v", activeServices)
}
return nil
}

return err
Expand Down Expand Up @@ -294,7 +312,7 @@ resource "aws_ecs_service" "mongo" {
}
`

var testAccAWSEcsService_withIamRole = `
var tpl_testAccAWSEcsService_withIamRole = `
resource "aws_ecs_cluster" "main" {
name = "terraformecstest11"
}
Expand All @@ -306,13 +324,13 @@ resource "aws_ecs_task_definition" "ghost" {
{
"cpu": 128,
"essential": true,
"image": "ghost:latest",
"image": "%s",
"memory": 128,
"name": "ghost",
"name": "%s",
"portMappings": [
{
"containerPort": 2368,
"hostPort": 8080
"containerPort": %d,
"hostPort": %d
}
]
}
Expand Down Expand Up @@ -364,7 +382,7 @@ resource "aws_elb" "main" {
availability_zones = ["us-west-2a"]
listener {
instance_port = 8080
instance_port = %d
instance_protocol = "http"
lb_port = 80
lb_protocol = "http"
Expand All @@ -380,14 +398,21 @@ resource "aws_ecs_service" "ghost" {
load_balancer {
elb_name = "${aws_elb.main.id}"
container_name = "ghost"
container_port = "2368"
container_name = "%s"
container_port = "%d"
}
depends_on = ["aws_iam_role_policy.ecs_service"]
}
`

var testAccAWSEcsService_withIamRole = fmt.Sprintf(
tpl_testAccAWSEcsService_withIamRole,
"ghost:latest", "ghost", 2368, 8080, 8080, "ghost", 2368)
var testAccAWSEcsService_withIamRole_modified = fmt.Sprintf(
tpl_testAccAWSEcsService_withIamRole,
"nginx:latest", "nginx", 80, 8080, 8080, "nginx", 80)

var testAccAWSEcsServiceWithFamilyAndRevision = `
resource "aws_ecs_cluster" "default" {
name = "terraformecstest2"
Expand Down

0 comments on commit b95c8fa

Please sign in to comment.