Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
Add collection flag for postman ECS agent (#234)
Browse files Browse the repository at this point in the history
- Adds collection flag
  • Loading branch information
gmann42 authored Sep 20, 2023
1 parent e4b857e commit b54c18c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 44 deletions.
32 changes: 18 additions & 14 deletions cmd/internal/ecs/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ 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: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 = "akita.software/"
akitaSecretPrefix = "postman/"
defaultKeyIDName = akitaSecretPrefix + "api_key_id"
defaultKeySecretName = akitaSecretPrefix + "api_key_secret"

Expand Down Expand Up @@ -840,21 +840,25 @@ func modifyTaskState(wf *AddWorkflow) (nextState optionals.Optional[AddWorkflowS
Value: aws.String(akitaCreationTagValue),
})

apiKey, apiSecret := cfg.GetAPIKeyAndSecret()
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("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", "--project", projectFlag},
Environment: []types.KeyValuePair{
{Name: aws.String("AKITA_API_KEY_ID"), Value: &apiKey},
{Name: aws.String("AKITA_API_KEY_SECRET"), Value: &apiSecret},

EntryPoint: []string{"/postman-lc-agent", "apidump", "--collection", collectionId},
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("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),
})
Expand Down Expand Up @@ -998,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()
}
43 changes: 13 additions & 30 deletions cmd/internal/ecs/ecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ecs

import (
"fmt"
"strings"

"github.com/akitasoftware/akita-cli/cmd/internal/cmderr"
"github.com/akitasoftware/akita-cli/rest"
Expand All @@ -13,8 +12,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.
Expand All @@ -34,14 +33,14 @@ 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 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,
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,
Expand All @@ -53,7 +52,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,

Expand All @@ -63,7 +62,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.")
Expand Down Expand Up @@ -91,40 +90,24 @@ 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.GetOrCreateServiceIDByPostmanCollectionID(frontClient, 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 err
}

return RunAddWorkflow()
}

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")
}

0 comments on commit b54c18c

Please sign in to comment.