Skip to content

Commit

Permalink
Merge pull request #6316 from terraform-providers/b-aws_ecs_service-m…
Browse files Browse the repository at this point in the history
…in-0

resource/aws_ecs_service: Continue supporting replica deployment_minimum_healthy_percent = 0 and deployment_maximum_percent = 100
  • Loading branch information
bflad authored Nov 2, 2018
2 parents 2ca6afd + 08b6ee1 commit 77ef086
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 46 deletions.
85 changes: 40 additions & 45 deletions aws/resource_aws_ecs_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,10 @@ func resourceAwsEcsService() *schema.Resource {
"deployment_maximum_percent": {
Type: schema.TypeInt,
Optional: true,
Default: 200,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if d.Get("scheduling_strategy").(string) == ecs.SchedulingStrategyDaemon {
if d.Get("scheduling_strategy").(string) == ecs.SchedulingStrategyDaemon && new == "200" {
return true
} else { // must be SchedulingStrategyReplica
if !d.IsNewResource() && new == "0" {
return true
}
}
return false
},
Expand All @@ -108,8 +105,9 @@ func resourceAwsEcsService() *schema.Resource {
"deployment_minimum_healthy_percent": {
Type: schema.TypeInt,
Optional: true,
Default: 100,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if !d.IsNewResource() && new == "0" {
if d.Get("scheduling_strategy").(string) == ecs.SchedulingStrategyDaemon && new == "100" {
return true
}
return false
Expand Down Expand Up @@ -329,25 +327,28 @@ func resourceAwsEcsServiceImport(d *schema.ResourceData, meta interface{}) ([]*s
func resourceAwsEcsServiceCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ecsconn

input := ecs.CreateServiceInput{
ServiceName: aws.String(d.Get("name").(string)),
TaskDefinition: aws.String(d.Get("task_definition").(string)),
DesiredCount: aws.Int64(int64(d.Get("desired_count").(int))),
ClientToken: aws.String(resource.UniqueId()),
}

deploymentConfiguration := &ecs.DeploymentConfiguration{}
deploymentMinimumHealthyPercent := d.Get("deployment_minimum_healthy_percent").(int)
schedulingStrategy := d.Get("scheduling_strategy").(string)

if v, ok := d.GetOk("deployment_maximum_percent"); ok {
deploymentConfiguration.MaximumPercent = aws.Int64(int64(v.(int)))
input := ecs.CreateServiceInput{
ClientToken: aws.String(resource.UniqueId()),
SchedulingStrategy: aws.String(schedulingStrategy),
ServiceName: aws.String(d.Get("name").(string)),
TaskDefinition: aws.String(d.Get("task_definition").(string)),
}

if v, ok := d.GetOk("deployment_minimum_healthy_percent"); ok {
deploymentConfiguration.MinimumHealthyPercent = aws.Int64(int64(v.(int)))
if schedulingStrategy == ecs.SchedulingStrategyDaemon && deploymentMinimumHealthyPercent != 100 {
input.DeploymentConfiguration = &ecs.DeploymentConfiguration{
MinimumHealthyPercent: aws.Int64(int64(deploymentMinimumHealthyPercent)),
}
} else if schedulingStrategy == ecs.SchedulingStrategyReplica {
input.DeploymentConfiguration = &ecs.DeploymentConfiguration{
MaximumPercent: aws.Int64(int64(d.Get("deployment_maximum_percent").(int))),
MinimumHealthyPercent: aws.Int64(int64(deploymentMinimumHealthyPercent)),
}
input.DesiredCount = aws.Int64(int64(d.Get("desired_count").(int)))
}

input.DeploymentConfiguration = deploymentConfiguration

if v, ok := d.GetOk("cluster"); ok {
input.Cluster = aws.String(v.(string))
}
Expand All @@ -360,14 +361,6 @@ func resourceAwsEcsServiceCreate(d *schema.ResourceData, meta interface{}) error
input.LaunchType = aws.String(v.(string))
}

schedulingStrategy := d.Get("scheduling_strategy").(string)
input.SchedulingStrategy = aws.String(schedulingStrategy)
if schedulingStrategy == ecs.SchedulingStrategyDaemon {
// unset these if DAEMON
input.DeploymentConfiguration.MaximumPercent = nil
input.DesiredCount = nil
}

loadBalancers := expandEcsLoadBalancers(d.Get("load_balancer").(*schema.Set).List())
if len(loadBalancers) > 0 {
log.Printf("[DEBUG] Adding ECS load balancers: %s", loadBalancers)
Expand Down Expand Up @@ -777,11 +770,26 @@ func resourceAwsEcsServiceUpdate(d *schema.ResourceData, meta interface{}) error
}

schedulingStrategy := d.Get("scheduling_strategy").(string)
// Automatically ignore desired count if DAEMON
if schedulingStrategy != ecs.SchedulingStrategyDaemon && d.HasChange("desired_count") {
_, n := d.GetChange("desired_count")
input.DesiredCount = aws.Int64(int64(n.(int)))

if schedulingStrategy == ecs.SchedulingStrategyDaemon {
if d.HasChange("deployment_minimum_healthy_percent") {
input.DeploymentConfiguration = &ecs.DeploymentConfiguration{
MinimumHealthyPercent: aws.Int64(int64(d.Get("deployment_minimum_healthy_percent").(int))),
}
}
} else if schedulingStrategy == ecs.SchedulingStrategyReplica {
if d.HasChange("desired_count") {
input.DesiredCount = aws.Int64(int64(d.Get("desired_count").(int)))
}

if d.HasChange("deployment_maximum_percent") || d.HasChange("deployment_minimum_healthy_percent") {
input.DeploymentConfiguration = &ecs.DeploymentConfiguration{
MaximumPercent: aws.Int64(int64(d.Get("deployment_maximum_percent").(int))),
MinimumHealthyPercent: aws.Int64(int64(d.Get("deployment_minimum_healthy_percent").(int))),
}
}
}

if d.HasChange("health_check_grace_period_seconds") {
_, n := d.GetChange("health_check_grace_period_seconds")
input.HealthCheckGracePeriodSeconds = aws.Int64(int64(n.(int)))
Expand All @@ -791,19 +799,6 @@ func resourceAwsEcsServiceUpdate(d *schema.ResourceData, meta interface{}) error
input.TaskDefinition = aws.String(n.(string))
}

if schedulingStrategy == ecs.SchedulingStrategyReplica && (d.HasChange("deployment_maximum_percent") || d.HasChange("deployment_minimum_healthy_percent")) {
input.DeploymentConfiguration = &ecs.DeploymentConfiguration{
MaximumPercent: aws.Int64(int64(d.Get("deployment_maximum_percent").(int))),
MinimumHealthyPercent: aws.Int64(int64(d.Get("deployment_minimum_healthy_percent").(int))),
}
}

if schedulingStrategy == ecs.SchedulingStrategyDaemon && d.HasChange("deployment_minimum_healthy_percent") {
input.DeploymentConfiguration = &ecs.DeploymentConfiguration{
MinimumHealthyPercent: aws.Int64(int64(d.Get("deployment_minimum_healthy_percent").(int))),
}
}

if d.HasChange("network_configuration") {
input.NetworkConfiguration = expandEcsNetworkConfiguration(d.Get("network_configuration").([]interface{}))
}
Expand Down
58 changes: 57 additions & 1 deletion aws/resource_aws_ecs_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,29 @@ func TestAccAWSEcsService_withDeploymentValues(t *testing.T) {
})
}

// Regression for https://github.com/terraform-providers/terraform-provider-aws/issues/6315
func TestAccAWSEcsService_withDeploymentMinimumZeroMaximumOneHundred(t *testing.T) {
var service ecs.Service
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_ecs_service.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEcsServiceDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSEcsServiceConfigDeploymentPercents(rName, 0, 100),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEcsServiceExists(resourceName, &service),
resource.TestCheckResourceAttr(resourceName, "deployment_maximum_percent", "100"),
resource.TestCheckResourceAttr(resourceName, "deployment_minimum_healthy_percent", "0"),
),
},
},
})
}

// Regression for https://github.com/hashicorp/terraform/issues/3444
func TestAccAWSEcsService_withLbChanges(t *testing.T) {
var service ecs.Service
Expand Down Expand Up @@ -692,7 +715,7 @@ func TestAccAWSEcsService_withDaemonSchedulingStrategySetDeploymentMinimum(t *te
tdName := fmt.Sprintf("tf-acc-td-svc-w-ss-daemon-%s", rString)
svcName := fmt.Sprintf("tf-acc-svc-w-ss-daemon-%s", rString)

resource.Test(t, resource.TestCase{
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEcsServiceDestroy,
Expand Down Expand Up @@ -2191,6 +2214,39 @@ resource "aws_ecs_service" "ghost" {
`, clusterName, tdName, svcName)
}

func testAccAWSEcsServiceConfigDeploymentPercents(rName string, deploymentMinimumHealthyPercent, deploymentMaximumPercent int) string {
return fmt.Sprintf(`
resource "aws_ecs_cluster" "test" {
name = %q
}
resource "aws_ecs_task_definition" "test" {
family = %q
container_definitions = <<DEFINITION
[
{
"cpu": 128,
"essential": true,
"image": "mongo:latest",
"memory": 128,
"name": "mongodb"
}
]
DEFINITION
}
resource "aws_ecs_service" "test" {
cluster = "${aws_ecs_cluster.test.id}"
deployment_maximum_percent = %d
deployment_minimum_healthy_percent = %d
desired_count = 1
name = %q
task_definition = "${aws_ecs_task_definition.test.arn}"
}
`, rName, rName, deploymentMaximumPercent, deploymentMinimumHealthyPercent, rName)
}

func testAccAWSEcsServiceWithReplicaSchedulingStrategy(clusterName, tdName, svcName string) string {
return fmt.Sprintf(`
resource "aws_ecs_cluster" "default" {
Expand Down

0 comments on commit 77ef086

Please sign in to comment.