Skip to content

Commit c8d6ad6

Browse files
committed
Rename context.Context to context.ECSContext
1 parent 459704c commit c8d6ad6

File tree

15 files changed

+126
-120
lines changed

15 files changed

+126
-120
lines changed

ecs-cli/modules/cli/cluster/cluster_app.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"strings"
2222

2323
"github.com/aws/amazon-ecs-cli/ecs-cli/modules/cli/compose/container"
24-
composecontext "github.com/aws/amazon-ecs-cli/ecs-cli/modules/cli/compose/context"
24+
ecscontext "github.com/aws/amazon-ecs-cli/ecs-cli/modules/cli/compose/context"
2525
"github.com/aws/amazon-ecs-cli/ecs-cli/modules/cli/compose/entity/task"
2626
"github.com/aws/amazon-ecs-cli/ecs-cli/modules/clients/aws/cloudformation"
2727
ec2client "github.com/aws/amazon-ecs-cli/ecs-cli/modules/clients/aws/ec2"
@@ -441,7 +441,7 @@ func clusterPS(context *cli.Context, rdwr config.ReadWriter, ecsClient ecsclient
441441
}
442442
ec2Client := ec2client.NewEC2Client(cliParams)
443443

444-
ecsContext := &composecontext.Context{ECSClient: ecsClient, EC2Client: ec2Client}
444+
ecsContext := &ecscontext.ECSContext{ECSClient: ecsClient, EC2Client: ec2Client}
445445
task := task.NewTask(ecsContext)
446446
return task.Info(false)
447447
}

ecs-cli/modules/cli/compose/context/context.go renamed to ecs-cli/modules/cli/compose/context/ecs_context.go

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,24 @@ import (
1919
"path/filepath"
2020
"strings"
2121

22-
"github.com/sirupsen/logrus"
2322
ec2client "github.com/aws/amazon-ecs-cli/ecs-cli/modules/clients/aws/ec2"
2423
ecsclient "github.com/aws/amazon-ecs-cli/ecs-cli/modules/clients/aws/ecs"
2524
"github.com/aws/amazon-ecs-cli/ecs-cli/modules/commands/flags"
2625
"github.com/aws/amazon-ecs-cli/ecs-cli/modules/config"
2726
"github.com/aws/amazon-ecs-cli/ecs-cli/modules/utils/compose"
2827
"github.com/docker/libcompose/project"
28+
"github.com/sirupsen/logrus"
2929
"github.com/urfave/cli"
3030
)
3131

32-
// Context is a wrapper around libcompose.project.Context
33-
type Context struct {
32+
// ECSContext is a wrapper around libcompose.project.Context
33+
type ECSContext struct {
3434
project.Context
3535

3636
CLIContext *cli.Context
37-
CLIParams *config.CLIParams
37+
38+
CLIParams *config.CLIParams
39+
3840
// NOTE: Ideally, would like to only store the non-TaskDef related fields here (e.g. "DeploymentConfig")
3941
ECSParams *utils.ECSParams
4042

@@ -46,13 +48,13 @@ type Context struct {
4648
IsService bool
4749
}
4850

49-
// Open populates the context with new ECS and EC2 Clients
50-
func (context *Context) Open() error {
51-
// setup aws service clients
52-
context.ECSClient = ecsclient.NewECSClient()
53-
context.ECSClient.Initialize(context.CLIParams)
51+
// Open populates the ECSContext with new ECS and EC2 Clients
52+
func (ecsContext *ECSContext) Open() error {
53+
// setup AWS service clients
54+
ecsContext.ECSClient = ecsclient.NewECSClient()
55+
ecsContext.ECSClient.Initialize(ecsContext.CLIParams)
5456

55-
context.EC2Client = ec2client.NewEC2Client(context.CLIParams)
57+
ecsContext.EC2Client = ec2client.NewEC2Client(ecsContext.CLIParams)
5658

5759
return nil
5860
}
@@ -61,27 +63,27 @@ func (context *Context) Open() error {
6163
// 1. Command line option
6264
// 2. Environment variable
6365
// 3. Current working directory
64-
func (context *Context) SetProjectName() error {
65-
projectName := context.CLIContext.GlobalString(flags.ProjectNameFlag)
66+
func (ecsContext *ECSContext) SetProjectName() error {
67+
projectName := ecsContext.CLIContext.GlobalString(flags.ProjectNameFlag)
6668
if projectName != "" {
67-
context.ProjectName = projectName
69+
ecsContext.ProjectName = projectName
6870
return nil
6971
}
70-
projectName, err := context.lookupProjectName()
72+
projectName, err := ecsContext.lookupProjectName()
7173
if err != nil {
7274
return err
7375
}
74-
context.ProjectName = projectName
76+
ecsContext.ProjectName = projectName
7577
return nil
7678
}
7779

7880
// This following is derived from Docker's Libcompose project, Copyright 2015 Docker, Inc.
7981
// The original code may be found :
8082
// https://github.com/docker/libcompose/blob/master/project/context.go
81-
func (context *Context) lookupProjectName() (string, error) {
83+
func (ecsContext *ECSContext) lookupProjectName() (string, error) {
8284
file := "."
83-
if len(context.ComposeFiles) > 0 {
84-
file = context.ComposeFiles[0]
85+
if len(ecsContext.ComposeFiles) > 0 {
86+
file = ecsContext.ComposeFiles[0]
8587
}
8688

8789
f, err := filepath.Abs(file)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type ProjectEntity interface {
3535
Down() error
3636

3737
LoadContext() error
38-
Context() *context.Context
38+
Context() *context.ECSContext
3939
Sleeper() *utils.TimeSleeper
4040
TaskDefinition() *ecs.TaskDefinition
4141
TaskDefinitionCache() cache.Cache

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func TestGetContainersForTasksWithTaskNetworkingFargateENIDeleted(t *testing.T)
155155
mockEcs := mock_ecs.NewMockECSClient(ctrl)
156156
mockEc2 := mock_ec2.NewMockEC2Client(ctrl)
157157
mockProjectEntity := mock_entity.NewMockProjectEntity(ctrl)
158-
mockContext := &context.Context{
158+
mockContext := &context.ECSContext{
159159
ECSClient: mockEcs,
160160
EC2Client: mockEc2,
161161
}
@@ -184,7 +184,7 @@ func TestGetContainersForTasksWithTaskNetworkingFargateTaskStopped(t *testing.T)
184184
mockEcs := mock_ecs.NewMockECSClient(ctrl)
185185
mockEc2 := mock_ec2.NewMockEC2Client(ctrl)
186186
mockProjectEntity := mock_entity.NewMockProjectEntity(ctrl)
187-
mockContext := &context.Context{
187+
mockContext := &context.ECSContext{
188188
ECSClient: mockEcs,
189189
EC2Client: mockEc2,
190190
}
@@ -213,7 +213,7 @@ func TestGetContainersForTasksWithTaskNetworkingEC2TaskStopped(t *testing.T) {
213213
mockEcs := mock_ecs.NewMockECSClient(ctrl)
214214
mockEc2 := mock_ec2.NewMockEC2Client(ctrl)
215215
mockProjectEntity := mock_entity.NewMockProjectEntity(ctrl)
216-
mockContext := &context.Context{
216+
mockContext := &context.ECSContext{
217217
ECSClient: mockEcs,
218218
EC2Client: mockEc2,
219219
}
@@ -244,7 +244,7 @@ func TestGetContainersForTasksWithTaskNetworkingEC2(t *testing.T) {
244244
mockEcs := mock_ecs.NewMockECSClient(ctrl)
245245
mockEc2 := mock_ec2.NewMockEC2Client(ctrl)
246246
mockProjectEntity := mock_entity.NewMockProjectEntity(ctrl)
247-
mockContext := &context.Context{
247+
mockContext := &context.ECSContext{
248248
ECSClient: mockEcs,
249249
EC2Client: mockEc2,
250250
}
@@ -281,7 +281,7 @@ func TestGetContainersForTasksWithTaskNetworkingFargate(t *testing.T) {
281281
mockEcs := mock_ecs.NewMockECSClient(ctrl)
282282
mockEc2 := mock_ec2.NewMockEC2Client(ctrl)
283283
mockProjectEntity := mock_entity.NewMockProjectEntity(ctrl)
284-
mockContext := &context.Context{
284+
mockContext := &context.ECSContext{
285285
ECSClient: mockEcs,
286286
EC2Client: mockEc2,
287287
}
@@ -316,7 +316,7 @@ func TestGetContainersForTasksWithTaskNetworkingFargateENIDescribeFails(t *testi
316316
mockEcs := mock_ecs.NewMockECSClient(ctrl)
317317
mockEc2 := mock_ec2.NewMockEC2Client(ctrl)
318318
mockProjectEntity := mock_entity.NewMockProjectEntity(ctrl)
319-
mockContext := &context.Context{
319+
mockContext := &context.ECSContext{
320320
ECSClient: mockEcs,
321321
EC2Client: mockEc2,
322322
}
@@ -355,7 +355,7 @@ func TestGetContainersForTasksWithTaskNetworkingFargateENIWithoutPublicIP(t *tes
355355
mockEcs := mock_ecs.NewMockECSClient(ctrl)
356356
mockEc2 := mock_ec2.NewMockEC2Client(ctrl)
357357
mockProjectEntity := mock_entity.NewMockProjectEntity(ctrl)
358-
mockContext := &context.Context{
358+
mockContext := &context.ECSContext{
359359
ECSClient: mockEcs,
360360
EC2Client: mockEc2,
361361
}
@@ -455,7 +455,7 @@ func TestGetContainersForTasksErrorCases(t *testing.T) {
455455
containerInstances[containerInstanceArn] = ec2InstanceID
456456

457457
mockEc2, mockEcs, mockProjectEntity := setupTest(t)
458-
mockContext := &context.Context{
458+
mockContext := &context.ECSContext{
459459
ECSClient: mockEcs,
460460
EC2Client: mockEc2,
461461
}
@@ -495,7 +495,7 @@ func setupMocks(t *testing.T, getEc2InstanceIDsRequest []*string, getEc2Instance
495495

496496
mockEc2, mockEcs, mockProjectEntity := setupTest(t)
497497

498-
mockContext := &context.Context{
498+
mockContext := &context.ECSContext{
499499
ECSClient: mockEcs,
500500
EC2Client: mockEc2,
501501
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
)
3232

3333
type validateListTasksInput func(*ecs.ListTasksInput, string, *testing.T)
34-
type setupEntityForTestInfo func(*context.Context) ProjectEntity
34+
type setupEntityForTestInfo func(*context.ECSContext) ProjectEntity
3535

3636
// TestInfo tests ps commands
3737
func TestInfo(setupEntity setupEntityForTestInfo, validateFunc validateListTasksInput, t *testing.T, filterLocal bool) {
@@ -103,7 +103,7 @@ func TestInfo(setupEntity setupEntityForTestInfo, validateFunc validateListTasks
103103
mockEc2.EXPECT().DescribeInstances([]*string{&ec2InstanceID}).Return(ec2InstancesMap, nil),
104104
)
105105

106-
context := &context.Context{
106+
context := &context.ECSContext{
107107
ECSClient: mockEcs,
108108
EC2Client: mockEc2,
109109
CLIParams: &config.CLIParams{},

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ func (_m *MockProjectEntity) EXPECT() *_MockProjectEntityRecorder {
4747
return _m.recorder
4848
}
4949

50-
func (_m *MockProjectEntity) Context() *context.Context {
50+
func (_m *MockProjectEntity) Context() *context.ECSContext {
5151
ret := _m.ctrl.Call(_m, "Context")
52-
ret0, _ := ret[0].(*context.Context)
52+
ret0, _ := ret[0].(*context.ECSContext)
5353
return ret0
5454
}
5555

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"strconv"
1919
"strings"
2020

21-
log "github.com/sirupsen/logrus"
2221
"github.com/aws/amazon-ecs-cli/ecs-cli/modules/cli/compose/context"
2322
"github.com/aws/amazon-ecs-cli/ecs-cli/modules/cli/compose/entity"
2423
"github.com/aws/amazon-ecs-cli/ecs-cli/modules/cli/compose/entity/types"
@@ -30,14 +29,15 @@ import (
3029
"github.com/aws/aws-sdk-go/service/ecs"
3130
"github.com/docker/libcompose/project"
3231
"github.com/pkg/errors"
32+
log "github.com/sirupsen/logrus"
3333
)
3434

3535
// Service type is placeholder for a single task definition and its cache
3636
// and it performs operations on ECS Service level
3737
type Service struct {
3838
taskDef *ecs.TaskDefinition
3939
cache cache.Cache
40-
projectContext *context.Context
40+
ecsContext *context.ECSContext
4141
timeSleeper *utils.TimeSleeper
4242
deploymentConfig *ecs.DeploymentConfiguration
4343
loadBalancer *ecs.LoadBalancer
@@ -51,15 +51,17 @@ const (
5151
)
5252

5353
// NewService creates an instance of a Service and also sets up a cache for task definition
54-
func NewService(context *context.Context) entity.ProjectEntity {
54+
func NewService(ecsContext *context.ECSContext) entity.ProjectEntity {
5555
return &Service{
56-
cache: entity.SetupTaskDefinitionCache(),
57-
projectContext: context,
58-
timeSleeper: &utils.TimeSleeper{},
56+
cache: entity.SetupTaskDefinitionCache(),
57+
ecsContext: ecsContext,
58+
timeSleeper: &utils.TimeSleeper{},
5959
}
6060
}
6161

62-
// LoadContext reads the context set in NewService and loads DeploymentConfiguration and LoadBalancer
62+
// LoadContext reads the ECS context set in NewService and loads DeploymentConfiguration and LoadBalancer
63+
// TODO: refactor to memoize s.Context().CLIContext, since that's the only
64+
// thing that LoadContext seems to care about? (even in getInt64FromCLIContext)
6365
func (s *Service) LoadContext() error {
6466
maxPercent, err := getInt64FromCLIContext(s.Context(), flags.DeploymentMaxPercentFlag)
6567
if err != nil {
@@ -114,7 +116,7 @@ func (s *Service) LoadContext() error {
114116
}
115117

116118
// getInt64FromCLIContext reads the flag from the cli context and typecasts into *int64
117-
func getInt64FromCLIContext(context *context.Context, flag string) (*int64, error) {
119+
func getInt64FromCLIContext(context *context.ECSContext, flag string) (*int64, error) {
118120
value := context.CLIContext.String(flag)
119121
if value == "" {
120122
return nil, nil
@@ -132,8 +134,8 @@ func (s *Service) SetTaskDefinition(taskDefinition *ecs.TaskDefinition) {
132134
}
133135

134136
// Context returs the context of this project
135-
func (s *Service) Context() *context.Context {
136-
return s.projectContext
137+
func (s *Service) Context() *context.ECSContext {
138+
return s.ecsContext
137139
}
138140

139141
// Sleeper returs an instance of TimeSleeper used to wait until Service has gone to a stable state
@@ -261,7 +263,7 @@ func (s *Service) Up() error {
261263
// if the task definitions were different, updateService with new task definition
262264
// this creates a deployment in ECS and slowly takes down the containers with old ones and starts new ones
263265

264-
networkConfig, err := composeutils.ConvertToECSNetworkConfiguration(s.projectContext.ECSParams)
266+
networkConfig, err := composeutils.ConvertToECSNetworkConfiguration(s.ecsContext.ECSParams)
265267
if err != nil {
266268
return err
267269
}
@@ -361,7 +363,7 @@ func (s *Service) createService() error {
361363
taskDefinitionID := entity.GetIdFromArn(s.TaskDefinition().TaskDefinitionArn)
362364
launchType := s.Context().CLIParams.LaunchType
363365

364-
networkConfig, err := composeutils.ConvertToECSNetworkConfiguration(s.projectContext.ECSParams)
366+
networkConfig, err := composeutils.ConvertToECSNetworkConfiguration(s.ecsContext.ECSParams)
365367
if err != nil {
366368
return err
367369
}
@@ -425,7 +427,7 @@ func (s *Service) startService(ecsService *ecs.Service) error {
425427
func (s *Service) updateService(count int64) error {
426428
serviceName := entity.GetServiceName(s)
427429
deploymentConfig := s.DeploymentConfig()
428-
networkConfig, err := composeutils.ConvertToECSNetworkConfiguration(s.projectContext.ECSParams)
430+
networkConfig, err := composeutils.ConvertToECSNetworkConfiguration(s.ecsContext.ECSParams)
429431
forceDeployment := s.Context().CLIContext.Bool(flags.ForceDeploymentFlag)
430432

431433
if err != nil {

0 commit comments

Comments
 (0)