Skip to content

Commit 558399a

Browse files
committed
Refactor ECSClient#RunTask
See cd9894 TODO: Move RunTask tests in client to task_test.go
1 parent 681a036 commit 558399a

File tree

4 files changed

+84
-39
lines changed

4 files changed

+84
-39
lines changed

ecs-cli/modules/cli/compose/entity/task/task.go

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -240,30 +240,26 @@ func (t *Task) stopTasks(ecsTasks []*ecs.Task) error {
240240
}
241241

242242
// runTasks issues run task request to ECS Service in chunks of count=10
243-
func (t *Task) runTasks(taskDefinitionId string, totalCount int) ([]*ecs.Task, error) {
244-
networkConfig, err := composeutils.ConvertToECSNetworkConfiguration(t.ecsContext.ECSParams)
245-
if err != nil {
246-
return nil, err
247-
}
248-
243+
func (t *Task) runTasks(taskDefinition string, totalCount int) ([]*ecs.Task, error) {
249244
result := []*ecs.Task{}
250245
chunkSize := 10 // can issue only upto 10 tasks in a RunTask Call
251-
launchType := t.Context().CommandConfig.LaunchType
252-
253-
if err := entity.ValidateFargateParams(t.Context().ECSParams, launchType); err != nil {
254-
return nil, err
255-
}
256246

257247
for i := 0; i < totalCount; i += chunkSize {
258248
count := chunkSize
259249
if i+chunkSize > totalCount {
260250
count = totalCount - i
261251
}
262252

263-
ecsTasks, err := t.Context().ECSClient.RunTask(taskDefinitionId, entity.GetTaskGroup(t), count, networkConfig, launchType)
253+
runTaskInput, err := t.buildRunTaskInput(taskDefinition, count)
264254
if err != nil {
265255
return nil, err
266256
}
257+
258+
ecsTasks, err := t.Context().ECSClient.RunTask(runTaskInput)
259+
if err != nil {
260+
return nil, err
261+
}
262+
267263
for _, failure := range ecsTasks.Failures {
268264
log.WithFields(log.Fields{
269265
"reason": aws.StringValue(failure.Reason),
@@ -275,6 +271,39 @@ func (t *Task) runTasks(taskDefinitionId string, totalCount int) ([]*ecs.Task, e
275271
return result, nil
276272
}
277273

274+
func (t *Task) buildRunTaskInput(taskDefinition string, count int) (*ecs.RunTaskInput, error) {
275+
cluster := t.Context().CommandConfig.Cluster
276+
launchType := t.Context().CommandConfig.LaunchType
277+
group := entity.GetTaskGroup(t)
278+
279+
ecsParams := t.ecsContext.ECSParams
280+
networkConfig, err := composeutils.ConvertToECSNetworkConfiguration(ecsParams)
281+
if err != nil {
282+
return nil, err
283+
}
284+
285+
if err := entity.ValidateFargateParams(ecsParams, launchType); err != nil {
286+
return nil, err
287+
}
288+
289+
runTaskInput := &ecs.RunTaskInput{
290+
Cluster: aws.String(cluster),
291+
TaskDefinition: aws.String(taskDefinition),
292+
Group: aws.String(group),
293+
Count: aws.Int64(int64(count)),
294+
}
295+
296+
if networkConfig != nil {
297+
runTaskInput.NetworkConfiguration = networkConfig
298+
}
299+
300+
if launchType != "" {
301+
runTaskInput.LaunchType = aws.String(launchType)
302+
}
303+
304+
return runTaskInput, nil
305+
}
306+
278307
// createOne issues run task with count=1 and waits for it to get to running state
279308
func (t *Task) createOne() error {
280309
ecsTask, err := t.runTasks(aws.StringValue(t.TaskDefinition().TaskDefinitionArn), 1)

ecs-cli/modules/clients/aws/ecs/client.go

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ type ECSClient interface {
5454

5555
// Tasks related
5656
GetTasksPages(listTasksInput *ecs.ListTasksInput, fn ProcessTasksAction) error
57-
RunTask(taskDefinition, taskGroup string, count int, networkConfig *ecs.NetworkConfiguration, launchType string) (*ecs.RunTaskOutput, error)
57+
RunTask(runTaskInput *ecs.RunTaskInput) (*ecs.RunTaskOutput, error)
5858
RunTaskWithOverrides(taskDefinition, taskGroup string, count int, overrides map[string][]string) (*ecs.RunTaskOutput, error)
5959
StopTask(taskID string) error
6060
DescribeTasks(taskIds []*string) ([]*ecs.Task, error)
@@ -368,27 +368,12 @@ func (c *ecsClient) DescribeTasks(taskArns []*string) ([]*ecs.Task, error) {
368368
}
369369

370370
// RunTask issues a run task request for the input task definition
371-
func (c *ecsClient) RunTask(taskDefinition, group string, count int, networkConfig *ecs.NetworkConfiguration, launchType string) (*ecs.RunTaskOutput, error) {
372-
runTaskInput := &ecs.RunTaskInput{
373-
Cluster: aws.String(c.config.Cluster),
374-
TaskDefinition: aws.String(taskDefinition),
375-
Group: aws.String(group),
376-
Count: aws.Int64(int64(count)),
377-
}
378-
379-
if networkConfig != nil {
380-
runTaskInput.NetworkConfiguration = networkConfig
381-
}
382-
383-
if launchType != "" {
384-
runTaskInput.LaunchType = aws.String(launchType)
385-
}
386-
387-
resp, err := c.client.RunTask(runTaskInput)
371+
func (c *ecsClient) RunTask(input *ecs.RunTaskInput) (*ecs.RunTaskOutput, error) {
372+
resp, err := c.client.RunTask(input)
388373

389374
if err != nil {
390375
log.WithFields(log.Fields{
391-
"task definition": taskDefinition,
376+
"task definition": input.TaskDefinition,
392377
"error": err,
393378
}).Error("Error running tasks")
394379
}

ecs-cli/modules/clients/aws/ecs/client_test.go

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,14 @@ func TestRunTask(t *testing.T) {
448448
assert.Nil(t, req.LaunchType, "Expected Launch Type to be nil.")
449449
}).Return(&ecs.RunTaskOutput{}, nil)
450450

451-
_, err := client.RunTask(td, group, count, nil, "")
451+
runTaskInput := &ecs.RunTaskInput{
452+
Cluster: aws.String(clusterName),
453+
TaskDefinition: aws.String(td),
454+
Group: aws.String(group),
455+
Count: aws.Int64(int64(count)),
456+
}
457+
458+
_, err := client.RunTask(runTaskInput)
452459
assert.NoError(t, err, "Unexpected error when calling RunTask")
453460
}
454461

@@ -470,7 +477,14 @@ func TestRunTaskWithLaunchTypeEC2(t *testing.T) {
470477
assert.Nil(t, req.NetworkConfiguration, "Expected Network Config to be nil.")
471478
}).Return(&ecs.RunTaskOutput{}, nil)
472479

473-
_, err := client.RunTask(td, group, count, nil, "EC2")
480+
runTaskInput := &ecs.RunTaskInput{
481+
Cluster: aws.String(clusterName),
482+
TaskDefinition: aws.String(td),
483+
Group: aws.String(group),
484+
Count: aws.Int64(int64(count)),
485+
LaunchType: aws.String("EC2"),
486+
}
487+
_, err := client.RunTask(runTaskInput)
474488
assert.NoError(t, err, "Unexpected error when calling RunTask")
475489
}
476490

@@ -503,7 +517,16 @@ func TestRunTaskWithLaunchTypeFargate(t *testing.T) {
503517
assert.NotNil(t, req.NetworkConfiguration, "Expected Network Config to not be nil.")
504518
}).Return(&ecs.RunTaskOutput{}, nil)
505519

506-
_, err := client.RunTask(td, group, count, networkConfig, "FARGATE")
520+
runTaskInput := &ecs.RunTaskInput{
521+
Cluster: aws.String(clusterName),
522+
TaskDefinition: aws.String(td),
523+
Group: aws.String(group),
524+
Count: aws.Int64(int64(count)),
525+
LaunchType: aws.String("FARGATE"),
526+
NetworkConfiguration: networkConfig,
527+
}
528+
529+
_, err := client.RunTask(runTaskInput)
507530
assert.NoError(t, err, "Unexpected error when calling RunTask")
508531
}
509532

@@ -534,7 +557,15 @@ func TestRunTask_WithTaskNetworking(t *testing.T) {
534557
assert.Equal(t, networkConfig, req.NetworkConfiguration, "Expected networkConfiguration to match")
535558
}).Return(&ecs.RunTaskOutput{}, nil)
536559

537-
_, err := client.RunTask(td, group, count, networkConfig, "")
560+
runTaskInput := &ecs.RunTaskInput{
561+
Cluster: aws.String(clusterName),
562+
TaskDefinition: aws.String(td),
563+
Group: aws.String(group),
564+
Count: aws.Int64(int64(count)),
565+
LaunchType: aws.String("EC2"),
566+
NetworkConfiguration: networkConfig,
567+
}
568+
_, err := client.RunTask(runTaskInput)
538569
assert.NoError(t, err, "Unexpected error when calling RunTask")
539570
}
540571

ecs-cli/modules/clients/aws/ecs/mock/client.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)