Skip to content

Commit ba38f48

Browse files
committed
Implemented support for tagging ECS Cluster, and EC2 resources for the ecs-cli up command
1 parent 021e104 commit ba38f48

File tree

11 files changed

+268
-106
lines changed

11 files changed

+268
-106
lines changed

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

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,12 @@ import (
3131
ecsclient "github.com/aws/amazon-ecs-cli/ecs-cli/modules/clients/aws/ecs"
3232
"github.com/aws/amazon-ecs-cli/ecs-cli/modules/commands/flags"
3333
"github.com/aws/amazon-ecs-cli/ecs-cli/modules/config"
34+
"github.com/aws/amazon-ecs-cli/ecs-cli/modules/utils"
3435
"github.com/aws/aws-sdk-go/aws"
36+
sdkCFN "github.com/aws/aws-sdk-go/service/cloudformation"
37+
"github.com/aws/aws-sdk-go/service/ecs"
3538
"github.com/docker/libcompose/project"
39+
"github.com/pkg/errors"
3640
"github.com/sirupsen/logrus"
3741
"github.com/urfave/cli"
3842
)
@@ -238,6 +242,7 @@ func createCluster(context *cli.Context, awsClients *AWSClients, commandConfig *
238242
if err != nil {
239243
return err
240244
}
245+
241246
cfnParams.Add(ParameterKeyCluster, commandConfig.Cluster)
242247
if context.Bool(flags.NoAutoAssignPublicIPAddressFlag) {
243248
cfnParams.Add(ParameterKeyAssociatePublicIPAddress, "false")
@@ -282,6 +287,14 @@ func createCluster(context *cli.Context, awsClients *AWSClients, commandConfig *
282287
return fmt.Errorf("You have selected subnets. Please specify a VPC with the '--%s' flag", flags.VpcIdFlag)
283288
}
284289

290+
tags := make([]*ecs.Tag, 0)
291+
if tagVal := context.String(flags.ResourceTagsFlag); tagVal != "" {
292+
tags, err = utils.GetTags(tagVal, tags)
293+
if err != nil {
294+
return err
295+
}
296+
}
297+
285298
if launchType == config.LaunchTypeEC2 {
286299
architecture, err := determineArchitecture(cfnParams)
287300
if err != nil {
@@ -306,7 +319,7 @@ func createCluster(context *cli.Context, awsClients *AWSClients, commandConfig *
306319
}
307320

308321
// Create ECS cluster
309-
if _, err := ecsClient.CreateCluster(commandConfig.Cluster); err != nil {
322+
if _, err := ecsClient.CreateCluster(commandConfig.Cluster, tags); err != nil {
310323
return err
311324
}
312325

@@ -321,9 +334,12 @@ func createCluster(context *cli.Context, awsClients *AWSClients, commandConfig *
321334
}
322335
}
323336
// Create cfn stack
324-
template := cloudformation.GetClusterTemplate()
337+
template, err := cloudformation.GetClusterTemplate(tags, stackName)
338+
if err != nil {
339+
return errors.Wrapf(err, "Error building cloudformation template")
340+
}
325341

326-
if _, err := cfnClient.CreateStack(template, stackName, true, cfnParams); err != nil {
342+
if _, err := cfnClient.CreateStack(template, stackName, true, cfnParams, convertToCFNTags(tags)); err != nil {
327343
return err
328344
}
329345

@@ -354,6 +370,18 @@ func determineArchitecture(cfnParams *cloudformation.CfnStackParams) (string, er
354370
return architecture, nil
355371
}
356372

373+
// unfortunately go SDK lacks a unified Tag type
374+
func convertToCFNTags(tags []*ecs.Tag) []*sdkCFN.Tag {
375+
var cfnTags []*sdkCFN.Tag
376+
for _, tag := range tags {
377+
cfnTags = append(cfnTags, &sdkCFN.Tag{
378+
Key: tag.Key,
379+
Value: tag.Value,
380+
})
381+
}
382+
return cfnTags
383+
}
384+
357385
var newCommandConfig = func(context *cli.Context, rdwr config.ReadWriter) (*config.CommandConfig, error) {
358386
return config.NewCommandConfig(context, rdwr)
359387
}
@@ -378,7 +406,16 @@ func createEmptyCluster(context *cli.Context, ecsClient ecsclient.ECSClient, cfn
378406
return fmt.Errorf("A CloudFormation stack already exists for the cluster '%s'.", commandConfig.Cluster)
379407
}
380408

381-
if _, err := ecsClient.CreateCluster(commandConfig.Cluster); err != nil {
409+
tags := make([]*ecs.Tag, 0)
410+
var err error
411+
if tagVal := context.String(flags.ResourceTagsFlag); tagVal != "" {
412+
tags, err = utils.GetTags(tagVal, tags)
413+
if err != nil {
414+
return err
415+
}
416+
}
417+
418+
if _, err := ecsClient.CreateCluster(commandConfig.Cluster, tags); err != nil {
382419
return err
383420
}
384421

0 commit comments

Comments
 (0)