diff --git a/docs/stackit_logs.md b/docs/stackit_logs.md index 212cf8c4e..f01dc7d0d 100644 --- a/docs/stackit_logs.md +++ b/docs/stackit_logs.md @@ -30,5 +30,6 @@ stackit logs [flags] ### SEE ALSO * [stackit](./stackit.md) - Manage STACKIT resources using the command line +* [stackit logs access-token](./stackit_logs_access-token.md) - Provides functionality for Logs access-tokens * [stackit logs instance](./stackit_logs_instance.md) - Provides functionality for Logs instances diff --git a/docs/stackit_logs_access-token.md b/docs/stackit_logs_access-token.md new file mode 100644 index 000000000..a42baa613 --- /dev/null +++ b/docs/stackit_logs_access-token.md @@ -0,0 +1,40 @@ +## stackit logs access-token + +Provides functionality for Logs access-tokens + +### Synopsis + +Provides functionality for Logs access-tokens. + +``` +stackit logs access-token [flags] +``` + +### Options + +``` + -h, --help Help for "stackit logs access-token" +``` + +### Options inherited from parent commands + +``` + -y, --assume-yes If set, skips all confirmation prompts + --async If set, runs the command asynchronously + -o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"] + -p, --project-id string Project ID + --region string Target region for region-specific requests + --verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info") +``` + +### SEE ALSO + +* [stackit logs](./stackit_logs.md) - Provides functionality for Logs +* [stackit logs access-token create](./stackit_logs_access-token_create.md) - Creates a Logs access token +* [stackit logs access-token delete](./stackit_logs_access-token_delete.md) - Deletes a Logs access token +* [stackit logs access-token delete-all](./stackit_logs_access-token_delete-all.md) - Deletes all Logs access token +* [stackit logs access-token delete-all-expired](./stackit_logs_access-token_delete-all-expired.md) - Deletes all expired Logs access token +* [stackit logs access-token describe](./stackit_logs_access-token_describe.md) - Shows details of a Logs access token +* [stackit logs access-token list](./stackit_logs_access-token_list.md) - Lists all Logs access tokens of a project +* [stackit logs access-token update](./stackit_logs_access-token_update.md) - Updates a Logs access token + diff --git a/docs/stackit_logs_access-token_create.md b/docs/stackit_logs_access-token_create.md new file mode 100644 index 000000000..947260d03 --- /dev/null +++ b/docs/stackit_logs_access-token_create.md @@ -0,0 +1,51 @@ +## stackit logs access-token create + +Creates a Logs access token + +### Synopsis + +Creates a Logs access token. + +``` +stackit logs access-token create [flags] +``` + +### Examples + +``` + Create a access token with the display name "access-token-1" for the instance "xxx" with read and write permissions + $ stackit logs access-token create --display-name access-token-1 --instance-id xxx --permissions read,write + + Create a write only access token with a description + $ stackit logs access-token create --display-name access-token-2 --instance-id xxx --permissions write --description "Access token for service" + + Create a read only access token which expires in 30 days + $ stackit logs access-token create --display-name access-token-3 --instance-id xxx --permissions read --lifetime 30 +``` + +### Options + +``` + --description string Description of the access token + --display-name string Display name for the access token + -h, --help Help for "stackit logs access-token create" + --instance-id string ID of the Logs instance + --lifetime int Lifetime of the access token in days [1 - 180] + --permissions strings Permissions of the access token ["read" "write"] +``` + +### Options inherited from parent commands + +``` + -y, --assume-yes If set, skips all confirmation prompts + --async If set, runs the command asynchronously + -o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"] + -p, --project-id string Project ID + --region string Target region for region-specific requests + --verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info") +``` + +### SEE ALSO + +* [stackit logs access-token](./stackit_logs_access-token.md) - Provides functionality for Logs access-tokens + diff --git a/docs/stackit_logs_access-token_delete-all-expired.md b/docs/stackit_logs_access-token_delete-all-expired.md new file mode 100644 index 000000000..b63792e63 --- /dev/null +++ b/docs/stackit_logs_access-token_delete-all-expired.md @@ -0,0 +1,41 @@ +## stackit logs access-token delete-all-expired + +Deletes all expired Logs access token + +### Synopsis + +Deletes all expired Logs access token. + +``` +stackit logs access-token delete-all-expired [flags] +``` + +### Examples + +``` + Delete all expired access tokens in instance "xxx" + $ stackit logs access-token delete-all-expired --instance-id xxx +``` + +### Options + +``` + -h, --help Help for "stackit logs access-token delete-all-expired" + --instance-id string ID of the Logs instance +``` + +### Options inherited from parent commands + +``` + -y, --assume-yes If set, skips all confirmation prompts + --async If set, runs the command asynchronously + -o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"] + -p, --project-id string Project ID + --region string Target region for region-specific requests + --verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info") +``` + +### SEE ALSO + +* [stackit logs access-token](./stackit_logs_access-token.md) - Provides functionality for Logs access-tokens + diff --git a/docs/stackit_logs_access-token_delete-all.md b/docs/stackit_logs_access-token_delete-all.md new file mode 100644 index 000000000..de546fdef --- /dev/null +++ b/docs/stackit_logs_access-token_delete-all.md @@ -0,0 +1,41 @@ +## stackit logs access-token delete-all + +Deletes all Logs access token + +### Synopsis + +Deletes all Logs access token. + +``` +stackit logs access-token delete-all [flags] +``` + +### Examples + +``` + Delete all access tokens in instance "xxx" + $ stackit logs access-token delete-all --instance-id xxx +``` + +### Options + +``` + -h, --help Help for "stackit logs access-token delete-all" + --instance-id string ID of the Logs instance +``` + +### Options inherited from parent commands + +``` + -y, --assume-yes If set, skips all confirmation prompts + --async If set, runs the command asynchronously + -o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"] + -p, --project-id string Project ID + --region string Target region for region-specific requests + --verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info") +``` + +### SEE ALSO + +* [stackit logs access-token](./stackit_logs_access-token.md) - Provides functionality for Logs access-tokens + diff --git a/docs/stackit_logs_access-token_delete.md b/docs/stackit_logs_access-token_delete.md new file mode 100644 index 000000000..99d700c81 --- /dev/null +++ b/docs/stackit_logs_access-token_delete.md @@ -0,0 +1,41 @@ +## stackit logs access-token delete + +Deletes a Logs access token + +### Synopsis + +Deletes a Logs access token. + +``` +stackit logs access-token delete ACCESS_TOKEN_ID [flags] +``` + +### Examples + +``` + Delete access token with ID "xxx" in instance "yyy" + $ stackit logs access-token delete xxx --instance-id yyy +``` + +### Options + +``` + -h, --help Help for "stackit logs access-token delete" + --instance-id string ID of the Logs instance +``` + +### Options inherited from parent commands + +``` + -y, --assume-yes If set, skips all confirmation prompts + --async If set, runs the command asynchronously + -o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"] + -p, --project-id string Project ID + --region string Target region for region-specific requests + --verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info") +``` + +### SEE ALSO + +* [stackit logs access-token](./stackit_logs_access-token.md) - Provides functionality for Logs access-tokens + diff --git a/docs/stackit_logs_access-token_describe.md b/docs/stackit_logs_access-token_describe.md new file mode 100644 index 000000000..1e690bcda --- /dev/null +++ b/docs/stackit_logs_access-token_describe.md @@ -0,0 +1,44 @@ +## stackit logs access-token describe + +Shows details of a Logs access token + +### Synopsis + +Shows details of a Logs access token. + +``` +stackit logs access-token describe ACCESS_TOKEN_ID [flags] +``` + +### Examples + +``` + Show details of a Logs access token with ID "xxx" + $ stackit logs access-token describe xxx + + Show details of a Logs access token with ID "xxx" in JSON format + $ stackit logs access-token describe xxx --output-format json +``` + +### Options + +``` + -h, --help Help for "stackit logs access-token describe" + --instance-id string ID of the Logs instance +``` + +### Options inherited from parent commands + +``` + -y, --assume-yes If set, skips all confirmation prompts + --async If set, runs the command asynchronously + -o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"] + -p, --project-id string Project ID + --region string Target region for region-specific requests + --verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info") +``` + +### SEE ALSO + +* [stackit logs access-token](./stackit_logs_access-token.md) - Provides functionality for Logs access-tokens + diff --git a/docs/stackit_logs_access-token_list.md b/docs/stackit_logs_access-token_list.md new file mode 100644 index 000000000..0fc6b899e --- /dev/null +++ b/docs/stackit_logs_access-token_list.md @@ -0,0 +1,48 @@ +## stackit logs access-token list + +Lists all Logs access tokens of a project + +### Synopsis + +Lists all access tokens of a project. + +``` +stackit logs access-token list [flags] +``` + +### Examples + +``` + Lists all access tokens of the instance "xxx" + $ stackit logs access-token list --instance-id xxx + + Lists all access tokens in JSON format + $ stackit logs access-token list --instance-id xxx --output-format json + + Lists up to 10 access-token + $ stackit logs access-token list --instance-id xxx --limit 10 +``` + +### Options + +``` + -h, --help Help for "stackit logs access-token list" + --instance-id string ID of the Logs instance + --limit int Maximum number of entries to list +``` + +### Options inherited from parent commands + +``` + -y, --assume-yes If set, skips all confirmation prompts + --async If set, runs the command asynchronously + -o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"] + -p, --project-id string Project ID + --region string Target region for region-specific requests + --verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info") +``` + +### SEE ALSO + +* [stackit logs access-token](./stackit_logs_access-token.md) - Provides functionality for Logs access-tokens + diff --git a/docs/stackit_logs_access-token_update.md b/docs/stackit_logs_access-token_update.md new file mode 100644 index 000000000..10bc62701 --- /dev/null +++ b/docs/stackit_logs_access-token_update.md @@ -0,0 +1,46 @@ +## stackit logs access-token update + +Updates a Logs access token + +### Synopsis + +Updates a access token. + +``` +stackit logs access-token update ACCESS_TOKEN_ID [flags] +``` + +### Examples + +``` + Update access token with ID "xxx" with new name "access-token-1" + $ stackit logs access-token update xxx --instance-id yyy --display-name access-token-1 + + Update access token with ID "xxx" with new description "Access token for Service XY" + $ stackit logs access-token update xxx --instance-id yyy --description "Access token for Service XY" +``` + +### Options + +``` + --description string Description of the access token + --display-name string Display name for the access token + -h, --help Help for "stackit logs access-token update" + --instance-id string ID of the Logs instance +``` + +### Options inherited from parent commands + +``` + -y, --assume-yes If set, skips all confirmation prompts + --async If set, runs the command asynchronously + -o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"] + -p, --project-id string Project ID + --region string Target region for region-specific requests + --verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info") +``` + +### SEE ALSO + +* [stackit logs access-token](./stackit_logs_access-token.md) - Provides functionality for Logs access-tokens + diff --git a/internal/cmd/logs/access_token/access_token.go b/internal/cmd/logs/access_token/access_token.go new file mode 100644 index 000000000..1013dfe77 --- /dev/null +++ b/internal/cmd/logs/access_token/access_token.go @@ -0,0 +1,38 @@ +package access_token + +import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/logs/access_token/create" + "github.com/stackitcloud/stackit-cli/internal/cmd/logs/access_token/delete" + "github.com/stackitcloud/stackit-cli/internal/cmd/logs/access_token/delete_all" + "github.com/stackitcloud/stackit-cli/internal/cmd/logs/access_token/delete_all_expired" + "github.com/stackitcloud/stackit-cli/internal/cmd/logs/access_token/describe" + "github.com/stackitcloud/stackit-cli/internal/cmd/logs/access_token/list" + "github.com/stackitcloud/stackit-cli/internal/cmd/logs/access_token/update" + "github.com/stackitcloud/stackit-cli/internal/pkg/args" + "github.com/stackitcloud/stackit-cli/internal/pkg/types" + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" + + "github.com/spf13/cobra" +) + +func NewCmd(params *types.CmdParams) *cobra.Command { + cmd := &cobra.Command{ + Use: "access-token", + Short: "Provides functionality for Logs access-tokens", + Long: "Provides functionality for Logs access-tokens.", + Args: args.NoArgs, + Run: utils.CmdHelp, + } + addSubcommands(cmd, params) + return cmd +} + +func addSubcommands(cmd *cobra.Command, params *types.CmdParams) { + cmd.AddCommand(create.NewCmd(params)) + cmd.AddCommand(delete.NewCmd(params)) + cmd.AddCommand(delete_all.NewCmd(params)) + cmd.AddCommand(delete_all_expired.NewCmd(params)) + cmd.AddCommand(describe.NewCmd(params)) + cmd.AddCommand(list.NewCmd(params)) + cmd.AddCommand(update.NewCmd(params)) +} diff --git a/internal/cmd/logs/access_token/create/create.go b/internal/cmd/logs/access_token/create/create.go new file mode 100644 index 000000000..452342b3d --- /dev/null +++ b/internal/cmd/logs/access_token/create/create.go @@ -0,0 +1,157 @@ +package create + +import ( + "context" + "fmt" + + "github.com/stackitcloud/stackit-cli/internal/pkg/types" + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" + + "github.com/stackitcloud/stackit-cli/internal/pkg/args" + cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" + "github.com/stackitcloud/stackit-cli/internal/pkg/examples" + "github.com/stackitcloud/stackit-cli/internal/pkg/flags" + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" + "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" + "github.com/stackitcloud/stackit-cli/internal/pkg/services/logs/client" + logsUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/logs/utils" + "github.com/stackitcloud/stackit-sdk-go/services/logs" + + "github.com/spf13/cobra" +) + +const ( + displayNameFlag = "display-name" + instanceIdFlag = "instance-id" + lifetimeFlag = "lifetime" + descriptionFlag = "description" + permissionsFlag = "permissions" +) + +type inputModel struct { + *globalflags.GlobalFlagModel + InstanceId string + Description *string + DisplayName string + Lifetime *int64 + Permissions []string +} + +func NewCmd(params *types.CmdParams) *cobra.Command { + cmd := &cobra.Command{ + Use: "create", + Short: "Creates a Logs access token", + Long: "Creates a Logs access token.", + Args: args.NoArgs, + Example: examples.Build( + examples.NewExample( + `Create a access token with the display name "access-token-1" for the instance "xxx" with read and write permissions`, + `$ stackit logs access-token create --display-name access-token-1 --instance-id xxx --permissions read,write`, + ), + examples.NewExample( + `Create a write only access token with a description`, + `$ stackit logs access-token create --display-name access-token-2 --instance-id xxx --permissions write --description "Access token for service"`, + ), + examples.NewExample( + `Create a read only access token which expires in 30 days`, + `$ stackit logs access-token create --display-name access-token-3 --instance-id xxx --permissions read --lifetime 30`, + ), + ), + RunE: func(cmd *cobra.Command, args []string) error { + ctx := context.Background() + model, err := parseInput(params.Printer, cmd, args) + if err != nil { + return err + } + + // Configure API client + apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion) + if err != nil { + return err + } + + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd) + if err != nil { + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) + projectLabel = model.ProjectId + } else if projectLabel == "" { + projectLabel = model.ProjectId + } + + instanceLabel, err := logsUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.Region, model.InstanceId) + if err != nil { + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) + instanceLabel = model.InstanceId + } + + prompt := fmt.Sprintf("Are you sure you want to create a access token for the Logs instance %q in the project %q?", instanceLabel, projectLabel) + err = params.Printer.PromptForConfirmation(prompt) + if err != nil { + return err + } + + // Call API + req := buildRequest(ctx, model, apiClient) + resp, err := req.Execute() + if err != nil { + return fmt.Errorf("create Logs access-token : %w", err) + } + + return outputResult(params.Printer, model.OutputFormat, instanceLabel, resp) + }, + } + configureFlags(cmd) + return cmd +} + +func configureFlags(cmd *cobra.Command) { + cmd.Flags().Var(flags.UUIDFlag(), instanceIdFlag, "ID of the Logs instance") + cmd.Flags().String(displayNameFlag, "", "Display name for the access token") + cmd.Flags().String(descriptionFlag, "", "Description of the access token") + cmd.Flags().Int64(lifetimeFlag, 0, "Lifetime of the access token in days [1 - 180]") + cmd.Flags().StringSlice(permissionsFlag, []string{}, `Permissions of the access token ["read" "write"]`) + + err := flags.MarkFlagsRequired(cmd, instanceIdFlag, displayNameFlag, permissionsFlag) + cobra.CheckErr(err) +} + +func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel, error) { + globalFlags := globalflags.Parse(p, cmd) + if globalFlags.ProjectId == "" { + return nil, &cliErr.ProjectIdError{} + } + + model := inputModel{ + GlobalFlagModel: globalFlags, + DisplayName: flags.FlagToStringValue(p, cmd, displayNameFlag), + InstanceId: flags.FlagToStringValue(p, cmd, instanceIdFlag), + Description: flags.FlagToStringPointer(p, cmd, descriptionFlag), + Lifetime: flags.FlagToInt64Pointer(p, cmd, lifetimeFlag), + Permissions: flags.FlagToStringSliceValue(p, cmd, permissionsFlag), + } + + p.DebugInputModel(model) + return &model, nil +} + +func buildRequest(ctx context.Context, model *inputModel, apiClient *logs.APIClient) logs.ApiCreateAccessTokenRequest { + req := apiClient.CreateAccessToken(ctx, model.ProjectId, model.Region, model.InstanceId) + + return req.CreateAccessTokenPayload(logs.CreateAccessTokenPayload{ + Description: model.Description, + DisplayName: &model.DisplayName, + Lifetime: model.Lifetime, + Permissions: &model.Permissions, + }) +} + +func outputResult(p *print.Printer, outputFormat, instanceLabel string, accessToken *logs.AccessToken) error { + if accessToken == nil { + return fmt.Errorf("access token cannot be nil") + } + return p.OutputResult(outputFormat, accessToken, func() error { + p.Outputf("Created access token for Logs instance %q.\n\nID: %s\nToken: %s\n", instanceLabel, utils.PtrValue(accessToken.Id), utils.PtrValue(accessToken.AccessToken)) + return nil + }) +} diff --git a/internal/cmd/logs/access_token/create/create_test.go b/internal/cmd/logs/access_token/create/create_test.go new file mode 100644 index 000000000..e29fc864e --- /dev/null +++ b/internal/cmd/logs/access_token/create/create_test.go @@ -0,0 +1,282 @@ +package create + +import ( + "context" + "testing" + + "github.com/stackitcloud/stackit-cli/internal/pkg/types" + + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" + "github.com/stackitcloud/stackit-cli/internal/pkg/testutils" + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" + + "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" + "github.com/google/uuid" + "github.com/stackitcloud/stackit-sdk-go/services/logs" +) + +const ( + testRegion = "eu01" + + testDisplayName = "display-name" + testDescription = "description" +) + +type testCtxKey struct{} + +var ( + testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo") + testClient = &logs.APIClient{} + testProjectId = uuid.NewString() + testInstanceId = uuid.NewString() +) + +func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string { + flagValues := map[string]string{ + globalflags.ProjectIdFlag: testProjectId, + globalflags.RegionFlag: testRegion, + + instanceIdFlag: testInstanceId, + displayNameFlag: testDisplayName, + descriptionFlag: testDescription, + permissionsFlag: "read,write", + lifetimeFlag: "0", + } + for _, mod := range mods { + mod(flagValues) + } + return flagValues +} + +func fixtureInputModel(mods ...func(model *inputModel)) *inputModel { + model := &inputModel{ + GlobalFlagModel: &globalflags.GlobalFlagModel{ + Verbosity: globalflags.VerbosityDefault, + ProjectId: testProjectId, + Region: testRegion, + }, + + InstanceId: testInstanceId, + Description: utils.Ptr(testDescription), + DisplayName: testDisplayName, + Lifetime: utils.Ptr(int64(0)), + Permissions: []string{ + "read", + "write", + }, + } + for _, mod := range mods { + mod(model) + } + return model +} + +func fixtureRequest(mods ...func(request *logs.ApiCreateAccessTokenRequest)) logs.ApiCreateAccessTokenRequest { + request := testClient.CreateAccessToken(testCtx, testProjectId, testRegion, testInstanceId) + request = request.CreateAccessTokenPayload(fixturePayload()) + for _, mod := range mods { + mod(&request) + } + return request +} + +func fixturePayload(mods ...func(payload *logs.CreateAccessTokenPayload)) logs.CreateAccessTokenPayload { + payload := logs.CreateAccessTokenPayload{ + DisplayName: utils.Ptr(testDisplayName), + Description: utils.Ptr(testDescription), + Lifetime: utils.Ptr(int64(0)), + Permissions: utils.Ptr([]string{ + "read", + "write", + }), + } + for _, mod := range mods { + mod(&payload) + } + return payload +} + +func TestParseInput(t *testing.T) { + tests := []struct { + description string + argValues []string + flagValues map[string]string + isValid bool + expectedModel *inputModel + }{ + { + description: "base", + flagValues: fixtureFlagValues(), + isValid: true, + expectedModel: fixtureInputModel(), + }, + { + description: "only required flags", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + delete(flagValues, lifetimeFlag) + delete(flagValues, descriptionFlag) + }), + isValid: true, + expectedModel: fixtureInputModel(func(model *inputModel) { + model.Lifetime = nil + model.Description = nil + }), + }, + { + description: "one permission", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues[permissionsFlag] = "read" + }), + isValid: true, + expectedModel: fixtureInputModel(func(model *inputModel) { + model.Permissions = []string{ + "read", + } + }), + }, + { + description: "no values", + flagValues: map[string]string{}, + isValid: false, + }, + { + description: "project id missing", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + delete(flagValues, globalflags.ProjectIdFlag) + }), + isValid: false, + }, + { + description: "project id invalid 1", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues[globalflags.ProjectIdFlag] = "" + }), + isValid: false, + }, + { + description: "project id invalid 2", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues[globalflags.ProjectIdFlag] = "invalid-uuid" + }), + isValid: false, + }, + { + description: "instance id missing", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + delete(flagValues, instanceIdFlag) + }), + isValid: false, + }, + { + description: "instance id invalid 1", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues[instanceIdFlag] = "" + }), + isValid: false, + }, + { + description: "instance id invalid 2", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues[instanceIdFlag] = "invalid-uuid" + }), + isValid: false, + }, + { + description: "lifetime invalid", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues[lifetimeFlag] = "invalid-integer" + }), + isValid: false, + }, + } + + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + testutils.TestParseInput(t, NewCmd, parseInput, tt.expectedModel, tt.argValues, tt.flagValues, tt.isValid) + }) + } +} + +func TestBuildRequest(t *testing.T) { + var tests = []struct { + description string + model *inputModel + expectedRequest logs.ApiCreateAccessTokenRequest + }{ + { + description: "base", + model: fixtureInputModel(), + expectedRequest: fixtureRequest(), + }, + } + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + request := buildRequest(testCtx, tt.model, testClient) + + diff := cmp.Diff(tt.expectedRequest, request, + cmp.AllowUnexported(tt.expectedRequest), + cmpopts.EquateComparable(testCtx), + ) + if diff != "" { + t.Fatalf("Data does not match: %s", diff) + } + }) + } +} + +func TestOutputResult(t *testing.T) { + type args struct { + outputFormat string + instanceLabel string + accessToken *logs.AccessToken + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "base", + args: args{ + instanceLabel: "", + accessToken: utils.Ptr(logs.AccessToken{ + Id: utils.Ptr(uuid.NewString()), + Permissions: utils.Ptr([]string{ + "read", + "write", + }), + DisplayName: utils.Ptr("Token"), + AccessToken: utils.Ptr("Secret access token"), + Creator: utils.Ptr(uuid.NewString()), + Expires: utils.Ptr(false), + Status: utils.Ptr(logs.ACCESSTOKENSTATUS_ACTIVE), + }), + }, + wantErr: false, + }, + { + name: "empty access token", + args: args{ + instanceLabel: "", + accessToken: utils.Ptr(logs.AccessToken{}), + }, + wantErr: false, + }, + { + name: "empty", + args: args{}, + wantErr: true, + }, + } + p := print.NewPrinter() + p.Cmd = NewCmd(&types.CmdParams{Printer: p}) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := outputResult(p, tt.args.outputFormat, tt.args.instanceLabel, tt.args.accessToken); (err != nil) != tt.wantErr { + t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} diff --git a/internal/cmd/logs/access_token/delete/delete.go b/internal/cmd/logs/access_token/delete/delete.go new file mode 100644 index 000000000..77b4777dc --- /dev/null +++ b/internal/cmd/logs/access_token/delete/delete.go @@ -0,0 +1,116 @@ +package delete + +import ( + "context" + "fmt" + + "github.com/stackitcloud/stackit-cli/internal/pkg/flags" + "github.com/stackitcloud/stackit-cli/internal/pkg/types" + "github.com/stackitcloud/stackit-sdk-go/services/logs" + + "github.com/stackitcloud/stackit-cli/internal/pkg/args" + "github.com/stackitcloud/stackit-cli/internal/pkg/errors" + "github.com/stackitcloud/stackit-cli/internal/pkg/examples" + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" + "github.com/stackitcloud/stackit-cli/internal/pkg/services/logs/client" + logUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/logs/utils" + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" + + "github.com/spf13/cobra" +) + +const ( + instanceIdFlag = "instance-id" + accessTokenIdArg = "ACCESS_TOKEN_ID" +) + +type inputModel struct { + *globalflags.GlobalFlagModel + InstanceId string + AccessTokenId string +} + +func NewCmd(params *types.CmdParams) *cobra.Command { + cmd := &cobra.Command{ + Use: fmt.Sprintf("delete %s", accessTokenIdArg), + Short: "Deletes a Logs access token", + Long: "Deletes a Logs access token.", + Args: args.SingleArg(accessTokenIdArg, utils.ValidateUUID), + Example: examples.Build( + examples.NewExample( + `Delete access token with ID "xxx" in instance "yyy"`, + "$ stackit logs access-token delete xxx --instance-id yyy", + ), + ), + RunE: func(cmd *cobra.Command, args []string) error { + ctx := context.Background() + model, err := parseInput(params.Printer, cmd, args) + if err != nil { + return err + } + + // Configure API client + apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion) + if err != nil { + return err + } + + // Get the display name for confirmation + accessTokenLabel, err := logUtils.GetAccessTokenName(ctx, apiClient, model.ProjectId, model.Region, model.InstanceId, model.AccessTokenId) + if err != nil { + params.Printer.Debug(print.ErrorLevel, "get access token: %v", err) + } + if accessTokenLabel == "" { + accessTokenLabel = model.AccessTokenId + } + + prompt := fmt.Sprintf("Are you sure you want to delete access token %q?", accessTokenLabel) + err = params.Printer.PromptForConfirmation(prompt) + if err != nil { + return err + } + + // Call API + req := buildRequest(ctx, model, apiClient) + err = req.Execute() + if err != nil { + return fmt.Errorf("delete access token: %w", err) + } + + params.Printer.Outputf("Deleted access token %q\n", accessTokenLabel) + return nil + }, + } + configureFlags(cmd) + return cmd +} + +func configureFlags(cmd *cobra.Command) { + cmd.Flags().Var(flags.UUIDFlag(), instanceIdFlag, "ID of the Logs instance") + + err := flags.MarkFlagsRequired(cmd, instanceIdFlag) + cobra.CheckErr(err) +} + +func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) { + accessTokenId := inputArgs[0] + + globalFlags := globalflags.Parse(p, cmd) + if globalFlags.ProjectId == "" { + return nil, &errors.ProjectIdError{} + } + + model := inputModel{ + GlobalFlagModel: globalFlags, + AccessTokenId: accessTokenId, + InstanceId: flags.FlagToStringValue(p, cmd, instanceIdFlag), + } + + p.DebugInputModel(model) + return &model, nil +} + +func buildRequest(ctx context.Context, model *inputModel, apiClient *logs.APIClient) logs.ApiDeleteAccessTokenRequest { + return apiClient.DeleteAccessToken(ctx, model.ProjectId, model.Region, model.InstanceId, model.AccessTokenId) +} diff --git a/internal/cmd/logs/access_token/delete/delete_test.go b/internal/cmd/logs/access_token/delete/delete_test.go new file mode 100644 index 000000000..23bbb5464 --- /dev/null +++ b/internal/cmd/logs/access_token/delete/delete_test.go @@ -0,0 +1,207 @@ +package delete + +import ( + "context" + "testing" + + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/testutils" + + "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" + "github.com/google/uuid" + "github.com/stackitcloud/stackit-sdk-go/services/logs" +) + +const ( + testRegion = "eu01" +) + +type testCtxKey struct{} + +var ( + testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo") + testClient = &logs.APIClient{} + + testProjectId = uuid.NewString() + testInstanceId = uuid.NewString() + testAccessTokenId = uuid.NewString() +) + +func fixtureArgValues(mods ...func(argValues []string)) []string { + argValues := []string{ + testAccessTokenId, + } + for _, mod := range mods { + mod(argValues) + } + return argValues +} + +func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string { + flagValues := map[string]string{ + globalflags.ProjectIdFlag: testProjectId, + globalflags.RegionFlag: testRegion, + + instanceIdFlag: testInstanceId, + } + for _, mod := range mods { + mod(flagValues) + } + return flagValues +} + +func fixtureInputModel(mods ...func(model *inputModel)) *inputModel { + model := &inputModel{ + GlobalFlagModel: &globalflags.GlobalFlagModel{ + Verbosity: globalflags.VerbosityDefault, + ProjectId: testProjectId, + Region: testRegion, + }, + + InstanceId: testInstanceId, + AccessTokenId: testAccessTokenId, + } + for _, mod := range mods { + mod(model) + } + return model +} + +func fixtureRequest(mods ...func(request *logs.ApiDeleteAccessTokenRequest)) logs.ApiDeleteAccessTokenRequest { + request := testClient.DeleteAccessToken(testCtx, testProjectId, testRegion, testInstanceId, testAccessTokenId) + for _, mod := range mods { + mod(&request) + } + return request +} + +func TestParseInput(t *testing.T) { + tests := []struct { + description string + argValues []string + flagValues map[string]string + isValid bool + expectedModel *inputModel + }{ + { + description: "base", + argValues: fixtureArgValues(), + flagValues: fixtureFlagValues(), + isValid: true, + expectedModel: fixtureInputModel(), + }, + { + description: "no values", + argValues: []string{}, + flagValues: map[string]string{}, + isValid: false, + }, + { + description: "no arg values", + argValues: []string{}, + flagValues: fixtureFlagValues(), + isValid: false, + }, + { + description: "no flag values", + argValues: fixtureArgValues(), + flagValues: map[string]string{}, + isValid: false, + }, + { + description: "project id missing", + argValues: fixtureArgValues(), + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + delete(flagValues, globalflags.ProjectIdFlag) + }), + isValid: false, + }, + { + description: "project id invalid 1", + argValues: fixtureArgValues(), + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues[globalflags.ProjectIdFlag] = "" + }), + isValid: false, + }, + { + description: "project id invalid 2", + argValues: fixtureArgValues(), + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues[globalflags.ProjectIdFlag] = "invalid-uuid" + }), + isValid: false, + }, + { + description: "instance id missing", + argValues: fixtureArgValues(), + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + delete(flagValues, instanceIdFlag) + }), + isValid: false, + }, + { + description: "instance id invalid 1", + argValues: fixtureArgValues(), + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues[instanceIdFlag] = "" + }), + isValid: false, + }, + { + description: "instance id invalid 2", + argValues: fixtureArgValues(), + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues[instanceIdFlag] = "invalid-uuid" + }), + isValid: false, + }, + { + description: "access token id invalid 1", + argValues: []string{""}, + flagValues: fixtureFlagValues(), + isValid: false, + }, + { + description: "access token id invalid 2", + argValues: []string{"invalid-uuid"}, + flagValues: fixtureFlagValues(), + isValid: false, + }, + } + + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + testutils.TestParseInput(t, NewCmd, parseInput, tt.expectedModel, tt.argValues, tt.flagValues, tt.isValid) + }) + } +} + +func TestBuildRequest(t *testing.T) { + tests := []struct { + description string + model *inputModel + expectedRequest logs.ApiDeleteAccessTokenRequest + }{ + { + description: "base", + model: fixtureInputModel(), + expectedRequest: fixtureRequest(), + }, + } + + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + request := buildRequest(testCtx, tt.model, testClient) + + diff := cmp.Diff(request, tt.expectedRequest, + cmp.AllowUnexported(tt.expectedRequest), + cmpopts.EquateComparable(testCtx), + ) + if diff != "" { + t.Fatalf("Data does not match: %s", diff) + } + }) + } +} diff --git a/internal/cmd/logs/access_token/delete_all/delete_all.go b/internal/cmd/logs/access_token/delete_all/delete_all.go new file mode 100644 index 000000000..337010be8 --- /dev/null +++ b/internal/cmd/logs/access_token/delete_all/delete_all.go @@ -0,0 +1,111 @@ +package delete_all + +import ( + "context" + "fmt" + + "github.com/stackitcloud/stackit-cli/internal/pkg/args" + "github.com/stackitcloud/stackit-cli/internal/pkg/flags" + "github.com/stackitcloud/stackit-cli/internal/pkg/types" + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" + "github.com/stackitcloud/stackit-sdk-go/services/logs" + + "github.com/stackitcloud/stackit-cli/internal/pkg/errors" + "github.com/stackitcloud/stackit-cli/internal/pkg/examples" + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" + "github.com/stackitcloud/stackit-cli/internal/pkg/services/logs/client" + logUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/logs/utils" + + "github.com/spf13/cobra" +) + +const ( + instanceIdFlag = "instance-id" +) + +type inputModel struct { + *globalflags.GlobalFlagModel + InstanceId string +} + +func NewCmd(params *types.CmdParams) *cobra.Command { + cmd := &cobra.Command{ + Use: "delete-all", + Short: "Deletes all Logs access token", + Long: "Deletes all Logs access token.", + Args: args.NoArgs, + Example: examples.Build( + examples.NewExample( + `Delete all access tokens in instance "xxx"`, + "$ stackit logs access-token delete-all --instance-id xxx", + ), + ), + RunE: func(cmd *cobra.Command, args []string) error { + ctx := context.Background() + model, err := parseInput(params.Printer, cmd, args) + if err != nil { + return err + } + + // Configure API client + apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion) + if err != nil { + return err + } + + instanceLabel, err := logUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.Region, model.InstanceId) + if err != nil { + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) + instanceLabel = model.InstanceId + } + + prompt := fmt.Sprintf("Are you sure you want to delete all access tokens for instance %q?", instanceLabel) + err = params.Printer.PromptForConfirmation(prompt) + if err != nil { + return err + } + + // Call API + req := buildRequest(ctx, model, apiClient) + items, err := req.Execute() + if err != nil { + return fmt.Errorf("delete all access token: %w", err) + } + if items == nil { + return fmt.Errorf("delete all access token: nil result") + } + + params.Printer.Outputf("Deleted %d access token(s)\n", len(utils.PtrValue(items.Tokens))) + return nil + }, + } + configureFlags(cmd) + return cmd +} + +func configureFlags(cmd *cobra.Command) { + cmd.Flags().Var(flags.UUIDFlag(), instanceIdFlag, "ID of the Logs instance") + + err := flags.MarkFlagsRequired(cmd, instanceIdFlag) + cobra.CheckErr(err) +} + +func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel, error) { + globalFlags := globalflags.Parse(p, cmd) + if globalFlags.ProjectId == "" { + return nil, &errors.ProjectIdError{} + } + + model := inputModel{ + GlobalFlagModel: globalFlags, + InstanceId: flags.FlagToStringValue(p, cmd, instanceIdFlag), + } + + p.DebugInputModel(model) + return &model, nil +} + +func buildRequest(ctx context.Context, model *inputModel, apiClient *logs.APIClient) logs.ApiDeleteAllAccessTokensRequest { + return apiClient.DeleteAllAccessTokens(ctx, model.ProjectId, model.Region, model.InstanceId) +} diff --git a/internal/cmd/logs/access_token/delete_all/delete_all_test.go b/internal/cmd/logs/access_token/delete_all/delete_all_test.go new file mode 100644 index 000000000..7cc8e8a81 --- /dev/null +++ b/internal/cmd/logs/access_token/delete_all/delete_all_test.go @@ -0,0 +1,163 @@ +package delete_all + +import ( + "context" + "testing" + + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/testutils" + + "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" + "github.com/google/uuid" + "github.com/stackitcloud/stackit-sdk-go/services/logs" +) + +const ( + testRegion = "eu01" +) + +type testCtxKey struct{} + +var ( + testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo") + testClient = &logs.APIClient{} + + testProjectId = uuid.NewString() + testInstanceId = uuid.NewString() +) + +func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string { + flagValues := map[string]string{ + globalflags.ProjectIdFlag: testProjectId, + globalflags.RegionFlag: testRegion, + + instanceIdFlag: testInstanceId, + } + for _, mod := range mods { + mod(flagValues) + } + return flagValues +} + +func fixtureInputModel(mods ...func(model *inputModel)) *inputModel { + model := &inputModel{ + GlobalFlagModel: &globalflags.GlobalFlagModel{ + Verbosity: globalflags.VerbosityDefault, + ProjectId: testProjectId, + Region: testRegion, + }, + + InstanceId: testInstanceId, + } + for _, mod := range mods { + mod(model) + } + return model +} + +func fixtureRequest(mods ...func(request *logs.ApiDeleteAllAccessTokensRequest)) logs.ApiDeleteAllAccessTokensRequest { + request := testClient.DeleteAllAccessTokens(testCtx, testProjectId, testRegion, testInstanceId) + for _, mod := range mods { + mod(&request) + } + return request +} + +func TestParseInput(t *testing.T) { + tests := []struct { + description string + argValues []string + flagValues map[string]string + isValid bool + expectedModel *inputModel + }{ + { + description: "base", + flagValues: fixtureFlagValues(), + isValid: true, + expectedModel: fixtureInputModel(), + }, + { + description: "no flag values", + flagValues: map[string]string{}, + isValid: false, + }, + { + description: "project id missing", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + delete(flagValues, globalflags.ProjectIdFlag) + }), + isValid: false, + }, + { + description: "project id invalid 1", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues[globalflags.ProjectIdFlag] = "" + }), + isValid: false, + }, + { + description: "project id invalid 2", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues[globalflags.ProjectIdFlag] = "invalid-uuid" + }), + isValid: false, + }, + { + description: "instance id missing", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + delete(flagValues, instanceIdFlag) + }), + isValid: false, + }, + { + description: "instance id invalid 1", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues[instanceIdFlag] = "" + }), + isValid: false, + }, + { + description: "instance id invalid 2", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues[instanceIdFlag] = "invalid-uuid" + }), + isValid: false, + }, + } + + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + testutils.TestParseInput(t, NewCmd, parseInput, tt.expectedModel, tt.argValues, tt.flagValues, tt.isValid) + }) + } +} + +func TestBuildRequest(t *testing.T) { + tests := []struct { + description string + model *inputModel + expectedRequest logs.ApiDeleteAllAccessTokensRequest + }{ + { + description: "base", + model: fixtureInputModel(), + expectedRequest: fixtureRequest(), + }, + } + + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + request := buildRequest(testCtx, tt.model, testClient) + + diff := cmp.Diff(request, tt.expectedRequest, + cmp.AllowUnexported(tt.expectedRequest), + cmpopts.EquateComparable(testCtx), + ) + if diff != "" { + t.Fatalf("Data does not match: %s", diff) + } + }) + } +} diff --git a/internal/cmd/logs/access_token/delete_all_expired/delete_all_expired.go b/internal/cmd/logs/access_token/delete_all_expired/delete_all_expired.go new file mode 100644 index 000000000..a4673a073 --- /dev/null +++ b/internal/cmd/logs/access_token/delete_all_expired/delete_all_expired.go @@ -0,0 +1,111 @@ +package delete_all_expired + +import ( + "context" + "fmt" + + "github.com/stackitcloud/stackit-cli/internal/pkg/args" + "github.com/stackitcloud/stackit-cli/internal/pkg/flags" + "github.com/stackitcloud/stackit-cli/internal/pkg/types" + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" + "github.com/stackitcloud/stackit-sdk-go/services/logs" + + "github.com/stackitcloud/stackit-cli/internal/pkg/errors" + "github.com/stackitcloud/stackit-cli/internal/pkg/examples" + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" + "github.com/stackitcloud/stackit-cli/internal/pkg/services/logs/client" + logUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/logs/utils" + + "github.com/spf13/cobra" +) + +const ( + instanceIdFlag = "instance-id" +) + +type inputModel struct { + *globalflags.GlobalFlagModel + InstanceId string +} + +func NewCmd(params *types.CmdParams) *cobra.Command { + cmd := &cobra.Command{ + Use: "delete-all-expired", + Short: "Deletes all expired Logs access token", + Long: "Deletes all expired Logs access token.", + Args: args.NoArgs, + Example: examples.Build( + examples.NewExample( + `Delete all expired access tokens in instance "xxx"`, + "$ stackit logs access-token delete-all-expired --instance-id xxx", + ), + ), + RunE: func(cmd *cobra.Command, args []string) error { + ctx := context.Background() + model, err := parseInput(params.Printer, cmd, args) + if err != nil { + return err + } + + // Configure API client + apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion) + if err != nil { + return err + } + + instanceLabel, err := logUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.Region, model.InstanceId) + if err != nil { + params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err) + instanceLabel = model.InstanceId + } + + prompt := fmt.Sprintf("Are you sure you want to delete all expired access tokens in instance %q?", instanceLabel) + err = params.Printer.PromptForConfirmation(prompt) + if err != nil { + return err + } + + // Call API + req := buildRequest(ctx, model, apiClient) + items, err := req.Execute() + if err != nil { + return fmt.Errorf("delete all expired access token: %w", err) + } + if items == nil { + return fmt.Errorf("delete all expired access token: nil result") + } + + params.Printer.Outputf("Deleted %d expired access token(s)\n", len(utils.PtrValue(items.Tokens))) + return nil + }, + } + configureFlags(cmd) + return cmd +} + +func configureFlags(cmd *cobra.Command) { + cmd.Flags().Var(flags.UUIDFlag(), instanceIdFlag, "ID of the Logs instance") + + err := flags.MarkFlagsRequired(cmd, instanceIdFlag) + cobra.CheckErr(err) +} + +func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel, error) { + globalFlags := globalflags.Parse(p, cmd) + if globalFlags.ProjectId == "" { + return nil, &errors.ProjectIdError{} + } + + model := inputModel{ + GlobalFlagModel: globalFlags, + InstanceId: flags.FlagToStringValue(p, cmd, instanceIdFlag), + } + + p.DebugInputModel(model) + return &model, nil +} + +func buildRequest(ctx context.Context, model *inputModel, apiClient *logs.APIClient) logs.ApiDeleteAllAccessTokensRequest { + return apiClient.DeleteAllExpiredAccessTokens(ctx, model.ProjectId, model.Region, model.InstanceId) +} diff --git a/internal/cmd/logs/access_token/delete_all_expired/delete_all_expired_test.go b/internal/cmd/logs/access_token/delete_all_expired/delete_all_expired_test.go new file mode 100644 index 000000000..f369afa91 --- /dev/null +++ b/internal/cmd/logs/access_token/delete_all_expired/delete_all_expired_test.go @@ -0,0 +1,163 @@ +package delete_all_expired + +import ( + "context" + "testing" + + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/testutils" + + "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" + "github.com/google/uuid" + "github.com/stackitcloud/stackit-sdk-go/services/logs" +) + +const ( + testRegion = "eu01" +) + +type testCtxKey struct{} + +var ( + testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo") + testClient = &logs.APIClient{} + + testProjectId = uuid.NewString() + testInstanceId = uuid.NewString() +) + +func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string { + flagValues := map[string]string{ + globalflags.ProjectIdFlag: testProjectId, + globalflags.RegionFlag: testRegion, + + instanceIdFlag: testInstanceId, + } + for _, mod := range mods { + mod(flagValues) + } + return flagValues +} + +func fixtureInputModel(mods ...func(model *inputModel)) *inputModel { + model := &inputModel{ + GlobalFlagModel: &globalflags.GlobalFlagModel{ + Verbosity: globalflags.VerbosityDefault, + ProjectId: testProjectId, + Region: testRegion, + }, + + InstanceId: testInstanceId, + } + for _, mod := range mods { + mod(model) + } + return model +} + +func fixtureRequest(mods ...func(request *logs.ApiDeleteAllExpiredAccessTokensRequest)) logs.ApiDeleteAllExpiredAccessTokensRequest { + request := testClient.DeleteAllExpiredAccessTokens(testCtx, testProjectId, testRegion, testInstanceId) + for _, mod := range mods { + mod(&request) + } + return request +} + +func TestParseInput(t *testing.T) { + tests := []struct { + description string + argValues []string + flagValues map[string]string + isValid bool + expectedModel *inputModel + }{ + { + description: "base", + flagValues: fixtureFlagValues(), + isValid: true, + expectedModel: fixtureInputModel(), + }, + { + description: "no flag values", + flagValues: map[string]string{}, + isValid: false, + }, + { + description: "project id missing", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + delete(flagValues, globalflags.ProjectIdFlag) + }), + isValid: false, + }, + { + description: "project id invalid 1", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues[globalflags.ProjectIdFlag] = "" + }), + isValid: false, + }, + { + description: "project id invalid 2", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues[globalflags.ProjectIdFlag] = "invalid-uuid" + }), + isValid: false, + }, + { + description: "instance id missing", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + delete(flagValues, instanceIdFlag) + }), + isValid: false, + }, + { + description: "instance id invalid 1", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues[instanceIdFlag] = "" + }), + isValid: false, + }, + { + description: "instance id invalid 2", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues[instanceIdFlag] = "invalid-uuid" + }), + isValid: false, + }, + } + + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + testutils.TestParseInput(t, NewCmd, parseInput, tt.expectedModel, tt.argValues, tt.flagValues, tt.isValid) + }) + } +} + +func TestBuildRequest(t *testing.T) { + tests := []struct { + description string + model *inputModel + expectedRequest logs.ApiDeleteAllExpiredAccessTokensRequest + }{ + { + description: "base", + model: fixtureInputModel(), + expectedRequest: fixtureRequest(), + }, + } + + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + request := buildRequest(testCtx, tt.model, testClient) + + diff := cmp.Diff(request, tt.expectedRequest, + cmp.AllowUnexported(tt.expectedRequest), + cmpopts.EquateComparable(testCtx), + ) + if diff != "" { + t.Fatalf("Data does not match: %s", diff) + } + }) + } +} diff --git a/internal/cmd/logs/access_token/describe/describe.go b/internal/cmd/logs/access_token/describe/describe.go new file mode 100644 index 000000000..21f734a51 --- /dev/null +++ b/internal/cmd/logs/access_token/describe/describe.go @@ -0,0 +1,135 @@ +package describe + +import ( + "context" + "fmt" + + "github.com/stackitcloud/stackit-cli/internal/pkg/flags" + "github.com/stackitcloud/stackit-cli/internal/pkg/types" + + "github.com/stackitcloud/stackit-cli/internal/pkg/args" + "github.com/stackitcloud/stackit-cli/internal/pkg/errors" + "github.com/stackitcloud/stackit-cli/internal/pkg/examples" + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" + "github.com/stackitcloud/stackit-cli/internal/pkg/services/logs/client" + "github.com/stackitcloud/stackit-cli/internal/pkg/tables" + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" + "github.com/stackitcloud/stackit-sdk-go/services/logs" + + "github.com/spf13/cobra" +) + +const ( + instanceIdFlag = "instance-id" + accessTokenIdArg = "ACCESS_TOKEN_ID" +) + +type inputModel struct { + *globalflags.GlobalFlagModel + InstanceId string + AccessTokenId string +} + +func NewCmd(params *types.CmdParams) *cobra.Command { + cmd := &cobra.Command{ + Use: fmt.Sprintf("describe %s", accessTokenIdArg), + Short: "Shows details of a Logs access token", + Long: "Shows details of a Logs access token.", + Args: args.SingleArg(accessTokenIdArg, utils.ValidateUUID), + Example: examples.Build( + examples.NewExample( + `Show details of a Logs access token with ID "xxx"`, + "$ stackit logs access-token describe xxx", + ), + examples.NewExample( + `Show details of a Logs access token with ID "xxx" in JSON format`, + "$ stackit logs access-token describe xxx --output-format json", + ), + ), + RunE: func(cmd *cobra.Command, args []string) error { + ctx := context.Background() + model, err := parseInput(params.Printer, cmd, args) + if err != nil { + return err + } + + // Configure API client + apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion) + if err != nil { + return err + } + + // Call API + req := buildRequest(ctx, model, apiClient) + resp, err := req.Execute() + if err != nil { + return fmt.Errorf("read access token: %w", err) + } + + return outputResult(params.Printer, model.OutputFormat, resp) + }, + } + configureFlags(cmd) + return cmd +} + +func configureFlags(cmd *cobra.Command) { + cmd.Flags().Var(flags.UUIDFlag(), instanceIdFlag, "ID of the Logs instance") + + err := flags.MarkFlagsRequired(cmd, instanceIdFlag) + cobra.CheckErr(err) +} + +func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) { + accessTokenId := inputArgs[0] + + globalFlags := globalflags.Parse(p, cmd) + if globalFlags.ProjectId == "" { + return nil, &errors.ProjectIdError{} + } + + model := inputModel{ + GlobalFlagModel: globalFlags, + InstanceId: flags.FlagToStringValue(p, cmd, instanceIdFlag), + AccessTokenId: accessTokenId, + } + + p.DebugInputModel(model) + return &model, nil +} + +func buildRequest(ctx context.Context, model *inputModel, apiClient *logs.APIClient) logs.ApiGetAccessTokenRequest { + return apiClient.GetAccessToken(ctx, model.ProjectId, model.Region, model.InstanceId, model.AccessTokenId) +} + +func outputResult(p *print.Printer, outputFormat string, token *logs.AccessToken) error { + if token == nil { + return fmt.Errorf("access token cannot be nil") + } + return p.OutputResult(outputFormat, token, func() error { + table := tables.NewTable() + table.AddRow("ID", utils.PtrString(token.Id)) + table.AddSeparator() + table.AddRow("DISPLAY NAME", utils.PtrString(token.DisplayName)) + table.AddSeparator() + table.AddRow("DESCRIPTION", utils.PtrString(token.Description)) + table.AddSeparator() + table.AddRow("PERMISSIONS", utils.PtrString(token.Permissions)) + table.AddSeparator() + table.AddRow("CREATOR", utils.PtrString(token.Creator)) + table.AddSeparator() + table.AddRow("STATE", utils.PtrString(token.Status)) + table.AddSeparator() + table.AddRow("EXPIRES", utils.PtrString(token.Expires)) + table.AddSeparator() + table.AddRow("VALID UNTIL", utils.PtrString(token.ValidUntil)) + table.AddSeparator() + + err := table.Display(p) + if err != nil { + return fmt.Errorf("render table: %w", err) + } + return nil + }) +} diff --git a/internal/cmd/logs/access_token/describe/describe_test.go b/internal/cmd/logs/access_token/describe/describe_test.go new file mode 100644 index 000000000..be083cd72 --- /dev/null +++ b/internal/cmd/logs/access_token/describe/describe_test.go @@ -0,0 +1,263 @@ +package describe + +import ( + "context" + "testing" + + "github.com/stackitcloud/stackit-cli/internal/pkg/types" + + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" + "github.com/stackitcloud/stackit-cli/internal/pkg/testutils" + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" + + "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" + "github.com/google/uuid" + "github.com/stackitcloud/stackit-sdk-go/services/logs" +) + +const ( + testRegion = "eu01" +) + +type testCtxKey struct{} + +var ( + testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo") + testClient = &logs.APIClient{} + + testProjectId = uuid.NewString() + testInstanceId = uuid.NewString() + testAccessTokenId = uuid.NewString() +) + +func fixtureArgValues(mods ...func(argValues []string)) []string { + argValues := []string{ + testAccessTokenId, + } + for _, mod := range mods { + mod(argValues) + } + return argValues +} + +func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string { + flagValues := map[string]string{ + globalflags.ProjectIdFlag: testProjectId, + globalflags.RegionFlag: testRegion, + + instanceIdFlag: testInstanceId, + } + for _, mod := range mods { + mod(flagValues) + } + return flagValues +} + +func fixtureInputModel(mods ...func(model *inputModel)) *inputModel { + model := &inputModel{ + GlobalFlagModel: &globalflags.GlobalFlagModel{ + Verbosity: globalflags.VerbosityDefault, + ProjectId: testProjectId, + Region: testRegion, + }, + + InstanceId: testInstanceId, + AccessTokenId: testAccessTokenId, + } + for _, mod := range mods { + mod(model) + } + return model +} + +func fixtureRequest(mods ...func(request *logs.ApiGetAccessTokenRequest)) logs.ApiGetAccessTokenRequest { + request := testClient.GetAccessToken(testCtx, testProjectId, testRegion, testInstanceId, testAccessTokenId) + for _, mod := range mods { + mod(&request) + } + return request +} + +func TestParseInput(t *testing.T) { + tests := []struct { + description string + argValues []string + flagValues map[string]string + isValid bool + expectedModel *inputModel + }{ + { + description: "base", + argValues: fixtureArgValues(), + flagValues: fixtureFlagValues(), + isValid: true, + expectedModel: fixtureInputModel(), + }, + { + description: "no values", + argValues: []string{}, + flagValues: map[string]string{}, + isValid: false, + }, + { + description: "no arg values", + argValues: []string{}, + flagValues: fixtureFlagValues(), + isValid: false, + }, + { + description: "no flag values", + argValues: fixtureArgValues(), + flagValues: map[string]string{}, + isValid: false, + }, + { + description: "project id missing", + argValues: fixtureArgValues(), + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + delete(flagValues, globalflags.ProjectIdFlag) + }), + isValid: false, + }, + { + description: "project id invalid 1", + argValues: fixtureArgValues(), + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues[globalflags.ProjectIdFlag] = "" + }), + isValid: false, + }, + { + description: "project id invalid 2", + argValues: fixtureArgValues(), + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues[globalflags.ProjectIdFlag] = "invalid-uuid" + }), + isValid: false, + }, + { + description: "instance id missing", + argValues: fixtureArgValues(), + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + delete(flagValues, instanceIdFlag) + }), + isValid: false, + }, + { + description: "instance id invalid 1", + argValues: fixtureArgValues(), + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues[instanceIdFlag] = "" + }), + isValid: false, + }, + { + description: "instance id invalid 2", + argValues: fixtureArgValues(), + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues[instanceIdFlag] = "invalid-uuid" + }), + isValid: false, + }, + { + description: "access token id invalid 1", + argValues: []string{""}, + flagValues: fixtureFlagValues(), + isValid: false, + }, + { + description: "access token id invalid 2", + argValues: []string{"invalid-uuid"}, + flagValues: fixtureFlagValues(), + isValid: false, + }, + } + + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + testutils.TestParseInput(t, NewCmd, parseInput, tt.expectedModel, tt.argValues, tt.flagValues, tt.isValid) + }) + } +} + +func TestBuildRequest(t *testing.T) { + tests := []struct { + description string + model *inputModel + expectedRequest logs.ApiGetAccessTokenRequest + }{ + { + description: "base", + model: fixtureInputModel(), + expectedRequest: fixtureRequest(), + }, + } + + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + request := buildRequest(testCtx, tt.model, testClient) + + diff := cmp.Diff(request, tt.expectedRequest, + cmp.AllowUnexported(tt.expectedRequest), + cmpopts.EquateComparable(testCtx), + ) + if diff != "" { + t.Fatalf("Data does not match: %s", diff) + } + }) + } +} + +func TestOutputResult(t *testing.T) { + type args struct { + outputFormat string + accessToken *logs.AccessToken + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "base", + args: args{ + accessToken: utils.Ptr(logs.AccessToken{ + Id: utils.Ptr(uuid.NewString()), + Permissions: utils.Ptr([]string{ + "read", + "write", + }), + DisplayName: utils.Ptr("Token"), + AccessToken: utils.Ptr("Secret access token"), + Creator: utils.Ptr(uuid.NewString()), + Expires: utils.Ptr(false), + Status: utils.Ptr(logs.ACCESSTOKENSTATUS_ACTIVE), + }), + }, + wantErr: false, + }, + { + name: "set empty access token", + args: args{ + accessToken: utils.Ptr(logs.AccessToken{}), + }, + wantErr: false, + }, + { + name: "empty", + args: args{}, + wantErr: true, + }, + } + p := print.NewPrinter() + p.Cmd = NewCmd(&types.CmdParams{Printer: p}) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := outputResult(p, tt.args.outputFormat, tt.args.accessToken); (err != nil) != tt.wantErr { + t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} diff --git a/internal/cmd/logs/access_token/list/list.go b/internal/cmd/logs/access_token/list/list.go new file mode 100644 index 000000000..309fd073f --- /dev/null +++ b/internal/cmd/logs/access_token/list/list.go @@ -0,0 +1,160 @@ +package list + +import ( + "context" + "fmt" + + "github.com/stackitcloud/stackit-cli/internal/pkg/types" + + "github.com/stackitcloud/stackit-cli/internal/pkg/args" + "github.com/stackitcloud/stackit-cli/internal/pkg/errors" + "github.com/stackitcloud/stackit-cli/internal/pkg/examples" + "github.com/stackitcloud/stackit-cli/internal/pkg/flags" + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" + "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" + "github.com/stackitcloud/stackit-cli/internal/pkg/services/logs/client" + "github.com/stackitcloud/stackit-cli/internal/pkg/tables" + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" + "github.com/stackitcloud/stackit-sdk-go/services/logs" + + "github.com/spf13/cobra" +) + +const ( + limitFlag = "limit" + instanceIdFlag = "instance-id" +) + +type inputModel struct { + *globalflags.GlobalFlagModel + InstanceId string + Limit *int64 +} + +func NewCmd(params *types.CmdParams) *cobra.Command { + cmd := &cobra.Command{ + Use: "list", + Short: "Lists all Logs access tokens of a project", + Long: "Lists all access tokens of a project.", + Args: args.NoArgs, + Example: examples.Build( + examples.NewExample( + `Lists all access tokens of the instance "xxx"`, + "$ stackit logs access-token list --instance-id xxx", + ), + examples.NewExample( + `Lists all access tokens in JSON format`, + "$ stackit logs access-token list --instance-id xxx --output-format json", + ), + examples.NewExample( + `Lists up to 10 access-token`, + "$ stackit logs access-token list --instance-id xxx --limit 10", + ), + ), + RunE: func(cmd *cobra.Command, args []string) error { + ctx := context.Background() + model, err := parseInput(params.Printer, cmd, args) + if err != nil { + return err + } + + // Configure API client + apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion) + if err != nil { + return err + } + + // Call API + req := buildRequest(ctx, model, apiClient) + resp, err := req.Execute() + if err != nil { + return fmt.Errorf("list access tokens: %w", err) + } + + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd) + if err != nil { + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) + projectLabel = model.ProjectId + } else if projectLabel == "" { + projectLabel = model.ProjectId + } + + // Truncate output + items := utils.PtrValue(resp.Tokens) + if model.Limit != nil && len(items) > int(*model.Limit) { + items = items[:*model.Limit] + } + + return outputResult(params.Printer, model.OutputFormat, items, projectLabel) + }, + } + configureFlags(cmd) + return cmd +} + +func configureFlags(cmd *cobra.Command) { + cmd.Flags().Int64(limitFlag, 0, "Maximum number of entries to list") + cmd.Flags().Var(flags.UUIDFlag(), instanceIdFlag, "ID of the Logs instance") + + err := flags.MarkFlagsRequired(cmd, instanceIdFlag) + cobra.CheckErr(err) +} + +func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel, error) { + globalFlags := globalflags.Parse(p, cmd) + if globalFlags.ProjectId == "" { + return nil, &errors.ProjectIdError{} + } + + limit := flags.FlagToInt64Pointer(p, cmd, limitFlag) + if limit != nil && *limit < 1 { + return nil, &errors.FlagValidationError{ + Flag: limitFlag, + Details: "must be greater than 0", + } + } + + model := inputModel{ + GlobalFlagModel: globalFlags, + Limit: limit, + InstanceId: flags.FlagToStringValue(p, cmd, instanceIdFlag), + } + + p.DebugInputModel(model) + return &model, nil +} + +func buildRequest(ctx context.Context, model *inputModel, apiClient *logs.APIClient) logs.ApiListAccessTokensRequest { + return apiClient.ListAccessTokens(ctx, model.ProjectId, model.Region, model.InstanceId) +} + +func outputResult(p *print.Printer, outputFormat string, tokens []logs.AccessToken, projectLabel string) error { + return p.OutputResult(outputFormat, tokens, func() error { + if len(tokens) == 0 { + p.Outputf("No access token found for project %q\n", projectLabel) + return nil + } + + table := tables.NewTable() + table.SetHeader("ID", "NAME", "DESCRIPTION", "PERMISSIONS", "VALID UNTIL", "STATUS") + + for _, token := range tokens { + table.AddRow( + utils.PtrString(token.Id), + utils.PtrString(token.DisplayName), + utils.PtrString(token.Description), + utils.PtrString(token.Permissions), + utils.PtrString(token.ValidUntil), + utils.PtrString(token.Status), + ) + table.AddSeparator() + } + + err := table.Display(p) + if err != nil { + return fmt.Errorf("render table: %w", err) + } + return nil + }) +} diff --git a/internal/cmd/logs/access_token/list/list_test.go b/internal/cmd/logs/access_token/list/list_test.go new file mode 100644 index 000000000..41c8c2b29 --- /dev/null +++ b/internal/cmd/logs/access_token/list/list_test.go @@ -0,0 +1,240 @@ +package list + +import ( + "context" + "testing" + + "github.com/stackitcloud/stackit-cli/internal/pkg/types" + + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" + "github.com/stackitcloud/stackit-cli/internal/pkg/testutils" + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" + + "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" + "github.com/google/uuid" + "github.com/stackitcloud/stackit-sdk-go/services/logs" +) + +const ( + testRegion = "eu01" +) + +type testCtxKey struct{} + +var ( + testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo") + testClient = &logs.APIClient{} + + testProjectId = uuid.NewString() + testInstanceId = uuid.NewString() +) + +func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string { + flagValues := map[string]string{ + globalflags.ProjectIdFlag: testProjectId, + globalflags.RegionFlag: testRegion, + + instanceIdFlag: testInstanceId, + limitFlag: "10", + } + for _, mod := range mods { + mod(flagValues) + } + return flagValues +} + +func fixtureInputModel(mods ...func(model *inputModel)) *inputModel { + model := &inputModel{ + GlobalFlagModel: &globalflags.GlobalFlagModel{ + Verbosity: globalflags.VerbosityDefault, + ProjectId: testProjectId, + Region: testRegion, + }, + + InstanceId: testInstanceId, + Limit: utils.Ptr(int64(10)), + } + for _, mod := range mods { + mod(model) + } + return model +} + +func fixtureRequest(mods ...func(request *logs.ApiListAccessTokensRequest)) logs.ApiListAccessTokensRequest { + request := testClient.ListAccessTokens(testCtx, testProjectId, testRegion, testInstanceId) + for _, mod := range mods { + mod(&request) + } + return request +} + +func TestParseInput(t *testing.T) { + tests := []struct { + description string + argValues []string + flagValues map[string]string + isValid bool + expectedModel *inputModel + }{ + { + description: "base", + flagValues: fixtureFlagValues(), + isValid: true, + expectedModel: fixtureInputModel(), + }, + { + description: "no flag values", + flagValues: map[string]string{}, + isValid: false, + }, + { + description: "project id missing", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + delete(flagValues, globalflags.ProjectIdFlag) + }), + isValid: false, + }, + { + description: "project id invalid 1", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues[globalflags.ProjectIdFlag] = "" + }), + isValid: false, + }, + { + description: "project id invalid 2", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues[globalflags.ProjectIdFlag] = "invalid-uuid" + }), + isValid: false, + }, + { + description: "instance id missing", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + delete(flagValues, instanceIdFlag) + }), + isValid: false, + }, + { + description: "instance id invalid 1", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues[instanceIdFlag] = "" + }), + isValid: false, + }, + { + description: "instance id invalid 2", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues[instanceIdFlag] = "invalid-uuid" + }), + isValid: false, + }, + { + description: "limit invalid", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues[limitFlag] = "invalid" + }), + isValid: false, + }, + { + description: "limit invalid 2", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues[limitFlag] = "0" + }), + isValid: false, + }, + } + + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + testutils.TestParseInput(t, NewCmd, parseInput, tt.expectedModel, tt.argValues, tt.flagValues, tt.isValid) + }) + } +} + +func TestBuildRequest(t *testing.T) { + tests := []struct { + description string + model *inputModel + expectedRequest logs.ApiListAccessTokensRequest + }{ + { + description: "base", + model: fixtureInputModel(), + expectedRequest: fixtureRequest(), + }, + } + + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + request := buildRequest(testCtx, tt.model, testClient) + + diff := cmp.Diff(request, tt.expectedRequest, + cmp.AllowUnexported(tt.expectedRequest), + cmpopts.EquateComparable(testCtx), + ) + if diff != "" { + t.Fatalf("Data does not match: %s", diff) + } + }) + } +} + +func TestOutputResult(t *testing.T) { + type args struct { + outputFormat string + accessTokens []logs.AccessToken + projectLabel string + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "base", + args: args{ + accessTokens: []logs.AccessToken{ + { + Id: utils.Ptr(uuid.NewString()), + Permissions: utils.Ptr([]string{ + "read", + "write", + }), + DisplayName: utils.Ptr("Token"), + AccessToken: utils.Ptr("Secret access token"), + Creator: utils.Ptr(uuid.NewString()), + Expires: utils.Ptr(false), + Status: utils.Ptr(logs.ACCESSTOKENSTATUS_ACTIVE), + }, + }, + }, + wantErr: false, + }, + { + name: "set empty access token", + args: args{ + accessTokens: []logs.AccessToken{ + {}, + }, + }, + wantErr: false, + }, + { + name: "empty", + args: args{}, + wantErr: false, + }, + } + p := print.NewPrinter() + p.Cmd = NewCmd(&types.CmdParams{Printer: p}) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := outputResult(p, tt.args.outputFormat, tt.args.accessTokens, tt.args.projectLabel); (err != nil) != tt.wantErr { + t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} diff --git a/internal/cmd/logs/access_token/update/update.go b/internal/cmd/logs/access_token/update/update.go new file mode 100644 index 000000000..85952136a --- /dev/null +++ b/internal/cmd/logs/access_token/update/update.go @@ -0,0 +1,144 @@ +package update + +import ( + "context" + "fmt" + + "github.com/stackitcloud/stackit-cli/internal/pkg/types" + "github.com/stackitcloud/stackit-sdk-go/services/logs" + + "github.com/stackitcloud/stackit-cli/internal/pkg/args" + cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors" + "github.com/stackitcloud/stackit-cli/internal/pkg/examples" + "github.com/stackitcloud/stackit-cli/internal/pkg/flags" + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" + "github.com/stackitcloud/stackit-cli/internal/pkg/services/logs/client" + logUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/logs/utils" + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" + + "github.com/spf13/cobra" +) + +const ( + instanceIdFlag = "instance-id" + displayNameFlag = "display-name" + descriptionFlag = "description" + accessTokenIdArg = "ACCESS_TOKEN_ID" +) + +type inputModel struct { + *globalflags.GlobalFlagModel + InstanceId string + AccessTokenId string + Description *string + DisplayName *string +} + +func NewCmd(params *types.CmdParams) *cobra.Command { + cmd := &cobra.Command{ + Use: fmt.Sprintf("update %s", accessTokenIdArg), + Short: "Updates a Logs access token", + Long: "Updates a access token.", + Args: args.SingleArg(accessTokenIdArg, utils.ValidateUUID), + Example: examples.Build( + examples.NewExample( + `Update access token with ID "xxx" with new name "access-token-1"`, + `$ stackit logs access-token update xxx --instance-id yyy --display-name access-token-1`, + ), + examples.NewExample( + `Update access token with ID "xxx" with new description "Access token for Service XY"`, + `$ stackit logs access-token update xxx --instance-id yyy --description "Access token for Service XY"`, + ), + ), + RunE: func(cmd *cobra.Command, args []string) error { + ctx := context.Background() + model, err := parseInput(params.Printer, cmd, args) + if err != nil { + return err + } + + // Configure API client + apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion) + if err != nil { + return err + } + + // Get the display name for confirmation + instanceLabel, err := logUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.Region, model.InstanceId) + if err != nil { + params.Printer.Debug(print.ErrorLevel, "get Logs instance: %v", err) + } + if instanceLabel == "" { + instanceLabel = model.InstanceId + } + + // Get the display name for confirmation + accessTokenLabel, err := logUtils.GetAccessTokenName(ctx, apiClient, model.ProjectId, model.Region, model.InstanceId, model.AccessTokenId) + if err != nil { + params.Printer.Debug(print.ErrorLevel, "get access token: %v", err) + } + if accessTokenLabel == "" { + accessTokenLabel = model.AccessTokenId + } + + prompt := fmt.Sprintf("Are you sure you want to update access token %q for instance %q?", accessTokenLabel, instanceLabel) + err = params.Printer.PromptForConfirmation(prompt) + if err != nil { + return err + } + + // Call API + req := buildRequest(ctx, model, apiClient) + err = req.Execute() + if err != nil { + return fmt.Errorf("update access token: %w", err) + } + + params.Printer.Outputf("Updated access token %q\n", accessTokenLabel) + return nil + }, + } + configureFlags(cmd) + return cmd +} + +func configureFlags(cmd *cobra.Command) { + cmd.Flags().Var(flags.UUIDFlag(), instanceIdFlag, "ID of the Logs instance") + cmd.Flags().String(displayNameFlag, "", "Display name for the access token") + cmd.Flags().String(descriptionFlag, "", "Description of the access token") + + err := flags.MarkFlagsRequired(cmd, instanceIdFlag) + cobra.CheckErr(err) +} + +func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) { + accessTokenId := inputArgs[0] + + globalFlags := globalflags.Parse(p, cmd) + if globalFlags.ProjectId == "" { + return nil, &cliErr.ProjectIdError{} + } + + model := inputModel{ + GlobalFlagModel: globalFlags, + AccessTokenId: accessTokenId, + DisplayName: flags.FlagToStringPointer(p, cmd, displayNameFlag), + InstanceId: flags.FlagToStringValue(p, cmd, instanceIdFlag), + Description: flags.FlagToStringPointer(p, cmd, descriptionFlag), + } + + p.DebugInputModel(model) + return &model, nil +} + +func buildRequest(ctx context.Context, model *inputModel, apiClient *logs.APIClient) logs.ApiUpdateAccessTokenRequest { + req := apiClient.UpdateAccessToken(ctx, model.ProjectId, model.Region, model.InstanceId, model.AccessTokenId) + + payload := logs.UpdateAccessTokenPayload{ + DisplayName: model.DisplayName, + Description: model.Description, + } + + return req.UpdateAccessTokenPayload(payload) +} diff --git a/internal/cmd/logs/access_token/update/update_test.go b/internal/cmd/logs/access_token/update/update_test.go new file mode 100644 index 000000000..fa458e5fc --- /dev/null +++ b/internal/cmd/logs/access_token/update/update_test.go @@ -0,0 +1,277 @@ +package update + +import ( + "context" + "testing" + + "github.com/stackitcloud/stackit-cli/internal/pkg/types" + + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" + + "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" + "github.com/google/uuid" + "github.com/stackitcloud/stackit-sdk-go/services/logs" +) + +const ( + testRegion = "eu01" + + testDisplayName = "display-name" + testDescription = "description" +) + +type testCtxKey struct{} + +var ( + testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo") + testClient = &logs.APIClient{} + + testProjectId = uuid.NewString() + testInstanceId = uuid.NewString() + testAccessTokenId = uuid.NewString() +) + +func fixtureArgValues(mods ...func(argValues []string)) []string { + argValues := []string{ + testAccessTokenId, + } + for _, mod := range mods { + mod(argValues) + } + return argValues +} + +func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string { + flagValues := map[string]string{ + globalflags.ProjectIdFlag: testProjectId, + globalflags.RegionFlag: testRegion, + + instanceIdFlag: testInstanceId, + displayNameFlag: testDisplayName, + descriptionFlag: testDescription, + } + for _, mod := range mods { + mod(flagValues) + } + return flagValues +} + +func fixtureInputModel(mods ...func(model *inputModel)) *inputModel { + model := &inputModel{ + GlobalFlagModel: &globalflags.GlobalFlagModel{ + Verbosity: globalflags.VerbosityDefault, + ProjectId: testProjectId, + Region: testRegion, + }, + + InstanceId: testInstanceId, + AccessTokenId: testAccessTokenId, + DisplayName: utils.Ptr(testDisplayName), + Description: utils.Ptr(testDescription), + } + for _, mod := range mods { + mod(model) + } + return model +} + +func fixtureRequest(mods ...func(request *logs.ApiUpdateAccessTokenRequest)) logs.ApiUpdateAccessTokenRequest { + request := testClient.UpdateAccessToken(testCtx, testProjectId, testRegion, testInstanceId, testAccessTokenId) + request = request.UpdateAccessTokenPayload(fixturePayload()) + for _, mod := range mods { + mod(&request) + } + return request +} + +func fixturePayload(mods ...func(payload *logs.UpdateAccessTokenPayload)) logs.UpdateAccessTokenPayload { + payload := logs.UpdateAccessTokenPayload{ + DisplayName: utils.Ptr(testDisplayName), + Description: utils.Ptr(testDescription), + } + for _, mod := range mods { + mod(&payload) + } + return payload +} + +func TestParseInput(t *testing.T) { + tests := []struct { + description string + argValues []string + flagValues map[string]string + isValid bool + expectedModel *inputModel + }{ + { + description: "base", + argValues: fixtureArgValues(), + flagValues: fixtureFlagValues(), + isValid: true, + expectedModel: fixtureInputModel(), + }, + { + description: "required only", + argValues: fixtureArgValues(), + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + delete(flagValues, displayNameFlag) + delete(flagValues, descriptionFlag) + }), + isValid: true, + expectedModel: fixtureInputModel(func(model *inputModel) { + model.DisplayName = nil + model.Description = nil + }), + }, + { + description: "no values", + argValues: []string{}, + flagValues: map[string]string{}, + isValid: false, + }, + { + description: "project id missing", + argValues: fixtureArgValues(), + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + delete(flagValues, globalflags.ProjectIdFlag) + }), + isValid: false, + }, + { + description: "project id invalid 1", + argValues: fixtureArgValues(), + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues[globalflags.ProjectIdFlag] = "" + }), + isValid: false, + }, + { + description: "project id invalid 2", + argValues: fixtureArgValues(), + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues[globalflags.ProjectIdFlag] = "invalid-uuid" + }), + isValid: false, + }, + { + description: "instance id missing", + argValues: fixtureArgValues(), + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + delete(flagValues, instanceIdFlag) + }), + isValid: false, + }, + { + description: "instance id invalid 1", + argValues: fixtureArgValues(), + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues[instanceIdFlag] = "" + }), + isValid: false, + }, + { + description: "instance id invalid 2", + argValues: fixtureArgValues(), + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues[instanceIdFlag] = "invalid-uuid" + }), + isValid: false, + }, + { + description: "access token id invalid 1", + argValues: []string{}, + flagValues: fixtureFlagValues(), + isValid: false, + }, + { + description: "access token id invalid 2", + argValues: []string{"invalid-uuid"}, + flagValues: fixtureFlagValues(), + isValid: false, + }, + } + + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + p := print.NewPrinter() + cmd := NewCmd(&types.CmdParams{Printer: p}) + err := globalflags.Configure(cmd.Flags()) + if err != nil { + t.Fatalf("configure global flags: %v", err) + } + + for flag, value := range tt.flagValues { + err := cmd.Flags().Set(flag, value) + if err != nil { + if !tt.isValid { + return + } + t.Fatalf("setting flag --%s=%s: %v", flag, value, err) + } + } + + err = cmd.ValidateRequiredFlags() + if err != nil { + if !tt.isValid { + return + } + t.Fatalf("error validating flags: %v", err) + } + + err = cmd.ValidateArgs(tt.argValues) + if err != nil { + if !tt.isValid { + return + } + t.Fatalf("error validating args: %v", err) + } + + model, err := parseInput(p, cmd, tt.argValues) + if err != nil { + if !tt.isValid { + return + } + t.Fatalf("error parsing flags: %v", err) + } + + if !tt.isValid { + t.Fatalf("did not fail on invalid input") + } + diff := cmp.Diff(model, tt.expectedModel) + if diff != "" { + t.Fatalf("Data does not match: %s", diff) + } + }) + } +} + +func TestBuildRequest(t *testing.T) { + tests := []struct { + description string + model *inputModel + expectedRequest logs.ApiUpdateAccessTokenRequest + }{ + { + description: "base", + model: fixtureInputModel(), + expectedRequest: fixtureRequest(), + }, + } + + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + request := buildRequest(testCtx, tt.model, testClient) + + diff := cmp.Diff(request, tt.expectedRequest, + cmp.AllowUnexported(tt.expectedRequest), + cmpopts.EquateComparable(testCtx), + ) + if diff != "" { + t.Fatalf("Data does not match: %s", diff) + } + }) + } +} diff --git a/internal/cmd/logs/logs.go b/internal/cmd/logs/logs.go index c6afc6a5f..e72cc5830 100644 --- a/internal/cmd/logs/logs.go +++ b/internal/cmd/logs/logs.go @@ -1,6 +1,7 @@ package logs import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/logs/access_token" "github.com/stackitcloud/stackit-cli/internal/cmd/logs/instance" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/types" @@ -23,4 +24,5 @@ func NewCmd(params *types.CmdParams) *cobra.Command { func addSubcommands(cmd *cobra.Command, params *types.CmdParams) { cmd.AddCommand(instance.NewCmd(params)) + cmd.AddCommand(access_token.NewCmd(params)) } diff --git a/internal/pkg/services/logs/utils/utils.go b/internal/pkg/services/logs/utils/utils.go index 4008db158..d67272254 100644 --- a/internal/pkg/services/logs/utils/utils.go +++ b/internal/pkg/services/logs/utils/utils.go @@ -15,6 +15,7 @@ var ( type LogsClient interface { GetLogsInstanceExecute(ctx context.Context, projectId, regionId, instanceId string) (*logs.LogsInstance, error) + GetAccessTokenExecute(ctx context.Context, projectId string, regionId string, instanceId string, tId string) (*logs.AccessToken, error) } func GetInstanceName(ctx context.Context, apiClient LogsClient, projectId, regionId, instanceId string) (string, error) { @@ -28,3 +29,15 @@ func GetInstanceName(ctx context.Context, apiClient LogsClient, projectId, regio } return *resp.DisplayName, nil } + +func GetAccessTokenName(ctx context.Context, apiClient LogsClient, projectId, regionId, instanceId, accessTokenId string) (string, error) { + resp, err := apiClient.GetAccessTokenExecute(ctx, projectId, regionId, instanceId, accessTokenId) + if err != nil { + return "", fmt.Errorf("get Logs access token: %w", err) + } else if resp == nil { + return "", ErrResponseNil + } else if resp.DisplayName == nil { + return "", ErrNameNil + } + return *resp.DisplayName, nil +} diff --git a/internal/pkg/services/logs/utils/utils_test.go b/internal/pkg/services/logs/utils/utils_test.go index 0c21b4d09..78839fb10 100644 --- a/internal/pkg/services/logs/utils/utils_test.go +++ b/internal/pkg/services/logs/utils/utils_test.go @@ -12,8 +12,9 @@ import ( ) var ( - testProjectId = uuid.NewString() - testInstanceId = uuid.NewString() + testProjectId = uuid.NewString() + testInstanceId = uuid.NewString() + testAccessTokenId = uuid.NewString() ) const ( @@ -22,8 +23,10 @@ const ( ) type logsClientMocked struct { - getInstanceFails bool - getInstanceResp *logs.LogsInstance + getInstanceFails bool + getInstanceResp *logs.LogsInstance + getAccessTokenFails bool + getAccessTokenResp *logs.AccessToken } func (m *logsClientMocked) GetLogsInstanceExecute(_ context.Context, _, _, _ string) (*logs.LogsInstance, error) { @@ -33,6 +36,13 @@ func (m *logsClientMocked) GetLogsInstanceExecute(_ context.Context, _, _, _ str return m.getInstanceResp, nil } +func (m *logsClientMocked) GetAccessTokenExecute(_ context.Context, _, _, _, _ string) (*logs.AccessToken, error) { + if m.getAccessTokenFails { + return nil, fmt.Errorf("could not get access token") + } + return m.getAccessTokenResp, nil +} + func TestGetInstanceName(t *testing.T) { tests := []struct { description string @@ -94,3 +104,65 @@ func TestGetInstanceName(t *testing.T) { }) } } + +func TestGetAccessTokenName(t *testing.T) { + tests := []struct { + description string + getAccessTokenFails bool + getAccessTokenResp *logs.AccessToken + isValid bool + expectedOutput string + }{ + { + description: "base", + getAccessTokenResp: &logs.AccessToken{ + DisplayName: utils.Ptr(testInstanceName), + }, + isValid: true, + expectedOutput: testInstanceName, + }, + { + description: "get instance fails", + getAccessTokenFails: true, + isValid: false, + }, + { + description: "response is nil", + getAccessTokenFails: false, + getAccessTokenResp: nil, + isValid: false, + }, + { + description: "name in response is nil", + getAccessTokenFails: false, + getAccessTokenResp: &logs.AccessToken{ + DisplayName: nil, + }, + isValid: false, + }, + } + + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + client := &logsClientMocked{ + getAccessTokenFails: tt.getAccessTokenFails, + getAccessTokenResp: tt.getAccessTokenResp, + } + + output, err := GetAccessTokenName(context.Background(), client, testProjectId, testRegion, testInstanceId, testAccessTokenId) + + if tt.isValid && err != nil { + t.Errorf("failed on valid input") + } + if !tt.isValid && err == nil { + t.Errorf("did not fail on invalid input") + } + if !tt.isValid { + return + } + if output != tt.expectedOutput { + t.Errorf("expected output to be %s, got %s", tt.expectedOutput, output) + } + }) + } +}