From 27574c8d7edf6b60e5c08615112cd8f36895482c Mon Sep 17 00:00:00 2001 From: Gaurav Mann Date: Fri, 15 Sep 2023 16:57:45 +0530 Subject: [PATCH 1/6] Add collection flag for postman ECS agent --- cmd/internal/ecs/ecs.go | 42 ++++++++++++++++------------------------- 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/cmd/internal/ecs/ecs.go b/cmd/internal/ecs/ecs.go index 5a69390..a574b98 100644 --- a/cmd/internal/ecs/ecs.go +++ b/cmd/internal/ecs/ecs.go @@ -1,6 +1,7 @@ package ecs import ( + "context" "fmt" "strings" @@ -13,8 +14,8 @@ import ( ) var ( - // Mandatory flag: Akita project name - projectFlag string + // Mandatory flag: Postman collection id + collectionId string // Any of these will be interactively prompted if not given on the command line. // On the other hand, to run non-interactively then all of them *must* be given. @@ -34,7 +35,7 @@ var ( var Cmd = &cobra.Command{ Use: "ecs", Short: "Add the Postman Live Collections Agent to AWS ECS.", - Long: "The CLI will collect information from you and add the Akita container to an ECS Task.", + Long: "The CLI will collect information from you and add the Postman live agent container to an ECS Task.", // N.B.: this is useless because the root command makes its own determination, // need to return AkitaErr to not show the usage. SilenceUsage: true, @@ -53,7 +54,7 @@ var AddToECSCmd = &cobra.Command{ var RemoveFromECSCmd = &cobra.Command{ Use: "remove", Short: "Remove the Postman Live Collections Agent from AWS ECS.", - Long: "Remove a previously installed Akita container from an ECS Task.", + Long: "Remove a previously installed Postman container from an ECS Task.", SilenceUsage: true, RunE: removeAgentFromECS, @@ -63,7 +64,7 @@ var RemoveFromECSCmd = &cobra.Command{ func init() { // TODO: add the ability to specify the credentials directly instead of via an AWS profile? - Cmd.PersistentFlags().StringVar(&projectFlag, "project", "", "Your Akita project.") + Cmd.PersistentFlags().StringVar(&collectionId, "collection", "", "Your postman collection ID") Cmd.PersistentFlags().StringVar(&awsProfileFlag, "profile", "", "Which of your AWS profiles to use to access ECS.") Cmd.PersistentFlags().StringVar(&awsRegionFlag, "region", "", "The AWS region in which your ECS cluster resides.") Cmd.PersistentFlags().StringVar(&ecsClusterFlag, "cluster", "", "The name or ARN of your ECS cluster.") @@ -91,34 +92,23 @@ func init() { func addAgentToECS(cmd *cobra.Command, args []string) error { // Check for API key - _, _, err := cmderr.RequireAkitaAPICredentials("The Postman Live Collections Agent must have an API key in order to capture traces.") + _, err := cmderr.RequirePostmanAPICredentials("The Postman Live Collections Agent must have an API key in order to capture traces.") if err != nil { return err } - // Check project's existence - if projectFlag == "" { - return errors.New("Must specify the name of your Akita project with the --project flag.") + // Check collecton Id's existence + if collectionId == "" { + return errors.New("Must specify the id of your collection with the --collection flag.") } frontClient := rest.NewFrontClient(rest.Domain, telemetry.GetClientID()) - _, err = util.GetServiceIDByName(frontClient, projectFlag) + _, err = util.GetServiceIDByPostmanCollectionID(frontClient, context.Background(), collectionId) if err != nil { - // TODO: we _could_ offer to create it, instead. - if strings.Contains(err.Error(), "cannot determine project ID") { - return cmderr.AkitaErr{ - Err: fmt.Errorf( - "Could not find the project %q in the Akita cloud. Please create it from the Akita web console before proceeding.", - projectFlag, - ), - } - } else { - return cmderr.AkitaErr{ - Err: errors.Wrapf( - err, - "Could not look up the project %q in the Akita cloud", - projectFlag, - ), - } + return cmderr.AkitaErr{ + Err: fmt.Errorf( + "Could not find the serviceId for %q", + collectionId, + ), } } From 3262830998811b4c961ef78b4c050436f3efeb98 Mon Sep 17 00:00:00 2001 From: Gaurav Mann Date: Fri, 15 Sep 2023 17:12:03 +0530 Subject: [PATCH 2/6] Replace akita by postman, fix errors --- cmd/internal/ecs/add.go | 13 +++++++------ cmd/internal/ecs/ecs.go | 5 ++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/cmd/internal/ecs/add.go b/cmd/internal/ecs/add.go index afb3508..f141afb 100644 --- a/cmd/internal/ecs/add.go +++ b/cmd/internal/ecs/add.go @@ -85,20 +85,21 @@ type AddWorkflow struct { const ( // Tag to use for objects created by the Akita CLI - akitaCreationTagKey = "akita.software:created_by" - akitaCreationTagValue = "Akita Software ECS integration" - akitaModificationTagKey = "akita.software:modified_by" - akitaModificationTagValue = "Akita Software ECS integration" + akitaCreationTagKey = "postman.software:created_by" + akitaCreationTagValue = "Postman Software ECS integration" + akitaModificationTagKey = "postman.software:modified_by" + akitaModificationTagValue = "Postman Software ECS integration" // Separate AWS secrets for the key ID and key secret // TODO: make these configurable - akitaSecretPrefix = "akita.software/" + akitaSecretPrefix = "postman.software/" defaultKeyIDName = akitaSecretPrefix + "api_key_id" defaultKeySecretName = akitaSecretPrefix + "api_key_secret" // Postman Live Collections Agent image locations akitaECRImage = "public.ecr.aws/akitasoftware/akita-cli" akitaDockerImage = "akitasoftware/cli" + // TODO: postmanDockerImage needed ? postmanECRImage = "docker.postman.com/postman-lc-agent" ) @@ -845,7 +846,7 @@ func modifyTaskState(wf *AddWorkflow) (nextState optionals.Optional[AddWorkflowS Name: aws.String("akita-agent"), // TODO: Cpu and Memory should be omitted for Fargate; they take their default values for EC2 if omitted. // For now we can leave the defaults in place, but they might be a bit large for EC2. - EntryPoint: []string{"/akita", "apidump", "--project", projectFlag}, + EntryPoint: []string{"/akita", "apidump", "--collection", collectionId}, Environment: []types.KeyValuePair{ {Name: aws.String("AKITA_API_KEY_ID"), Value: &apiKey}, {Name: aws.String("AKITA_API_KEY_SECRET"), Value: &apiSecret}, diff --git a/cmd/internal/ecs/ecs.go b/cmd/internal/ecs/ecs.go index a574b98..7da1a6c 100644 --- a/cmd/internal/ecs/ecs.go +++ b/cmd/internal/ecs/ecs.go @@ -3,7 +3,6 @@ package ecs import ( "context" "fmt" - "strings" "github.com/akitasoftware/akita-cli/cmd/internal/cmderr" "github.com/akitasoftware/akita-cli/rest" @@ -106,7 +105,7 @@ func addAgentToECS(cmd *cobra.Command, args []string) error { if err != nil { return cmderr.AkitaErr{ Err: fmt.Errorf( - "Could not find the serviceId for %q", + "could not find the serviceId for %q", collectionId, ), } @@ -116,5 +115,5 @@ func addAgentToECS(cmd *cobra.Command, args []string) error { } func removeAgentFromECS(cmd *cobra.Command, args []string) error { - return fmt.Errorf("This command is not yet implemented") + return fmt.Errorf("this command is not yet implemented") } From fb1acd2ea2fec1b49725d8fa8dd2791c88465bec Mon Sep 17 00:00:00 2001 From: Gaurav Mann Date: Mon, 18 Sep 2023 22:18:30 +0530 Subject: [PATCH 3/6] Change entrypoint, replace by postman --- cmd/internal/ecs/add.go | 23 +++++++++++------------ cmd/internal/ecs/ecs.go | 6 +++--- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/cmd/internal/ecs/add.go b/cmd/internal/ecs/add.go index f141afb..b1b1a59 100644 --- a/cmd/internal/ecs/add.go +++ b/cmd/internal/ecs/add.go @@ -85,22 +85,21 @@ type AddWorkflow struct { const ( // Tag to use for objects created by the Akita CLI - akitaCreationTagKey = "postman.software:created_by" - akitaCreationTagValue = "Postman Software ECS integration" - akitaModificationTagKey = "postman.software:modified_by" - akitaModificationTagValue = "Postman Software ECS integration" + akitaCreationTagKey = "postman:created_by" + akitaCreationTagValue = "Postman Live Insights ECS integration" + akitaModificationTagKey = "postman:modified_by" + akitaModificationTagValue = "Postman Live Insights ECS integration" // Separate AWS secrets for the key ID and key secret // TODO: make these configurable - akitaSecretPrefix = "postman.software/" + akitaSecretPrefix = "postman/" defaultKeyIDName = akitaSecretPrefix + "api_key_id" defaultKeySecretName = akitaSecretPrefix + "api_key_secret" // Postman Live Collections Agent image locations akitaECRImage = "public.ecr.aws/akitasoftware/akita-cli" akitaDockerImage = "akitasoftware/cli" - // TODO: postmanDockerImage needed ? - postmanECRImage = "docker.postman.com/postman-lc-agent" + postmanECRImage = "docker.postman.com/postman-lc-agent" ) // Run the "add to ECS" workflow until we complete or get an error. @@ -841,15 +840,15 @@ func modifyTaskState(wf *AddWorkflow) (nextState optionals.Optional[AddWorkflowS Value: aws.String(akitaCreationTagValue), }) - apiKey, apiSecret := cfg.GetAPIKeyAndSecret() + pKey, pEnv := cfg.GetPostmanAPIKeyAndEnvironment() input.ContainerDefinitions = append(input.ContainerDefinitions, types.ContainerDefinition{ - Name: aws.String("akita-agent"), + Name: aws.String("postman-lc-agent"), // TODO: Cpu and Memory should be omitted for Fargate; they take their default values for EC2 if omitted. // For now we can leave the defaults in place, but they might be a bit large for EC2. - EntryPoint: []string{"/akita", "apidump", "--collection", collectionId}, + EntryPoint: []string{"/postman-lc-agent", "apidump", "--collection", collectionId}, Environment: []types.KeyValuePair{ - {Name: aws.String("AKITA_API_KEY_ID"), Value: &apiKey}, - {Name: aws.String("AKITA_API_KEY_SECRET"), Value: &apiSecret}, + {Name: aws.String("POSTMAN_API_KEY"), Value: &pKey}, + {Name: aws.String("POSTMAN_ENV"), Value: &pEnv}, // Setting these environment variables will cause the traces to be tagged. {Name: aws.String("AKITA_AWS_REGION"), Value: &wf.awsRegion}, diff --git a/cmd/internal/ecs/ecs.go b/cmd/internal/ecs/ecs.go index 7da1a6c..87c2083 100644 --- a/cmd/internal/ecs/ecs.go +++ b/cmd/internal/ecs/ecs.go @@ -34,7 +34,7 @@ var ( var Cmd = &cobra.Command{ Use: "ecs", Short: "Add the Postman Live Collections Agent to AWS ECS.", - Long: "The CLI will collect information from you and add the Postman live agent container to an ECS Task.", + Long: "The CLI will collect information from you and add the Postman Live Collections Agent container to an ECS Task.", // N.B.: this is useless because the root command makes its own determination, // need to return AkitaErr to not show the usage. SilenceUsage: true, @@ -63,7 +63,7 @@ var RemoveFromECSCmd = &cobra.Command{ func init() { // TODO: add the ability to specify the credentials directly instead of via an AWS profile? - Cmd.PersistentFlags().StringVar(&collectionId, "collection", "", "Your postman collection ID") + Cmd.PersistentFlags().StringVar(&collectionId, "collection", "", "Your Postman collection ID") Cmd.PersistentFlags().StringVar(&awsProfileFlag, "profile", "", "Which of your AWS profiles to use to access ECS.") Cmd.PersistentFlags().StringVar(&awsRegionFlag, "region", "", "The AWS region in which your ECS cluster resides.") Cmd.PersistentFlags().StringVar(&ecsClusterFlag, "cluster", "", "The name or ARN of your ECS cluster.") @@ -98,7 +98,7 @@ func addAgentToECS(cmd *cobra.Command, args []string) error { // Check collecton Id's existence if collectionId == "" { - return errors.New("Must specify the id of your collection with the --collection flag.") + return errors.New("Must specify the ID of your collection with the --collection flag.") } frontClient := rest.NewFrontClient(rest.Domain, telemetry.GetClientID()) _, err = util.GetServiceIDByPostmanCollectionID(frontClient, context.Background(), collectionId) From 7eb233e61a9ee0ffa0edca00ba1a60384d7be4c1 Mon Sep 17 00:00:00 2001 From: Gaurav Mann Date: Tue, 19 Sep 2023 16:53:19 +0530 Subject: [PATCH 4/6] Replace getServiceId by getOrCreateServiceIdByCollectionId --- cmd/internal/ecs/ecs.go | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/cmd/internal/ecs/ecs.go b/cmd/internal/ecs/ecs.go index 87c2083..7c16909 100644 --- a/cmd/internal/ecs/ecs.go +++ b/cmd/internal/ecs/ecs.go @@ -101,14 +101,9 @@ func addAgentToECS(cmd *cobra.Command, args []string) error { return errors.New("Must specify the ID of your collection with the --collection flag.") } frontClient := rest.NewFrontClient(rest.Domain, telemetry.GetClientID()) - _, err = util.GetServiceIDByPostmanCollectionID(frontClient, context.Background(), collectionId) + _, err = util.GetOrCreateServiceIDByPostmanCollectionID(frontClient, collectionId) if err != nil { - return cmderr.AkitaErr{ - Err: fmt.Errorf( - "could not find the serviceId for %q", - collectionId, - ), - } + return err } return RunAddWorkflow() From a7988f01291e07a55da4b8a3b4fa01a36189f056 Mon Sep 17 00:00:00 2001 From: Gaurav Mann Date: Tue, 19 Sep 2023 17:35:40 +0530 Subject: [PATCH 5/6] Add postman-env only if present --- cmd/internal/ecs/add.go | 20 ++++++++++++-------- cmd/internal/ecs/ecs.go | 3 +-- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/cmd/internal/ecs/add.go b/cmd/internal/ecs/add.go index b1b1a59..7f31f75 100644 --- a/cmd/internal/ecs/add.go +++ b/cmd/internal/ecs/add.go @@ -99,7 +99,7 @@ const ( // Postman Live Collections Agent image locations akitaECRImage = "public.ecr.aws/akitasoftware/akita-cli" akitaDockerImage = "akitasoftware/cli" - postmanECRImage = "docker.postman.com/postman-lc-agent" + postmanECRImage = "docker.postman.com/postman-lc-agent" ) // Run the "add to ECS" workflow until we complete or get an error. @@ -841,20 +841,24 @@ func modifyTaskState(wf *AddWorkflow) (nextState optionals.Optional[AddWorkflowS }) pKey, pEnv := cfg.GetPostmanAPIKeyAndEnvironment() + envs := []types.KeyValuePair{} + if pEnv != "" { + envs = append(envs, []types.KeyValuePair{ + {Name: aws.String("POSTMAN_ENV"), Value: &pEnv}, + }...) + } input.ContainerDefinitions = append(input.ContainerDefinitions, types.ContainerDefinition{ Name: aws.String("postman-lc-agent"), // TODO: Cpu and Memory should be omitted for Fargate; they take their default values for EC2 if omitted. // For now we can leave the defaults in place, but they might be a bit large for EC2. EntryPoint: []string{"/postman-lc-agent", "apidump", "--collection", collectionId}, - Environment: []types.KeyValuePair{ + Environment: append(envs, []types.KeyValuePair{ {Name: aws.String("POSTMAN_API_KEY"), Value: &pKey}, - {Name: aws.String("POSTMAN_ENV"), Value: &pEnv}, - // Setting these environment variables will cause the traces to be tagged. - {Name: aws.String("AKITA_AWS_REGION"), Value: &wf.awsRegion}, - {Name: aws.String("AKITA_ECS_SERVICE"), Value: &wf.ecsService}, - {Name: aws.String("AKITA_ECS_TASK"), Value: &wf.ecsTaskDefinitionFamily}, - }, + {Name: aws.String("POSTMAN_AWS_REGION"), Value: &wf.awsRegion}, + {Name: aws.String("POSTMAN_ECS_SERVICE"), Value: &wf.ecsService}, + {Name: aws.String("POSTMAN_ECS_TASK"), Value: &wf.ecsTaskDefinitionFamily}, + }...), Essential: aws.Bool(false), Image: aws.String(postmanECRImage), }) diff --git a/cmd/internal/ecs/ecs.go b/cmd/internal/ecs/ecs.go index 7c16909..e5c67ca 100644 --- a/cmd/internal/ecs/ecs.go +++ b/cmd/internal/ecs/ecs.go @@ -1,7 +1,6 @@ package ecs import ( - "context" "fmt" "github.com/akitasoftware/akita-cli/cmd/internal/cmderr" @@ -41,7 +40,7 @@ var Cmd = &cobra.Command{ RunE: addAgentToECS, } -// 'akita ecs' should default to 'akita ecs add' +// 'postman-lc-agent ecs' should default to 'postman-lc-agent ecs add' var AddToECSCmd = &cobra.Command{ Use: "add", Short: Cmd.Short, From 5fbf1b668807d4495ad567fd7510ac188bc1d2f4 Mon Sep 17 00:00:00 2001 From: Gaurav Mann Date: Wed, 20 Sep 2023 16:44:21 +0530 Subject: [PATCH 6/6] Revert tags to AKITA, change deployment successful message --- cmd/internal/ecs/add.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cmd/internal/ecs/add.go b/cmd/internal/ecs/add.go index 7f31f75..9b9c68a 100644 --- a/cmd/internal/ecs/add.go +++ b/cmd/internal/ecs/add.go @@ -855,9 +855,9 @@ func modifyTaskState(wf *AddWorkflow) (nextState optionals.Optional[AddWorkflowS Environment: append(envs, []types.KeyValuePair{ {Name: aws.String("POSTMAN_API_KEY"), Value: &pKey}, // Setting these environment variables will cause the traces to be tagged. - {Name: aws.String("POSTMAN_AWS_REGION"), Value: &wf.awsRegion}, - {Name: aws.String("POSTMAN_ECS_SERVICE"), Value: &wf.ecsService}, - {Name: aws.String("POSTMAN_ECS_TASK"), Value: &wf.ecsTaskDefinitionFamily}, + {Name: aws.String("AKITA_AWS_REGION"), Value: &wf.awsRegion}, + {Name: aws.String("AKITA_ECS_SERVICE"), Value: &wf.ecsService}, + {Name: aws.String("AKITA_ECS_TASK"), Value: &wf.ecsTaskDefinitionFamily}, }...), Essential: aws.Bool(false), Image: aws.String(postmanECRImage), @@ -1002,6 +1002,6 @@ func waitForRestartState(wf *AddWorkflow) (nextState optionals.Optional[AddWorkf } reportStep("ECS Service Updated") - printer.Infof("Deployment successful! Please return to the Akita web console.\n") + printer.Infof("Deployment successful! Please return to the Postman Live collection you created.\n") return awf_done() }