diff --git a/docs/stackit_argus_credentials.md b/docs/stackit_argus_credentials.md index 41692ab73..17918cb5b 100644 --- a/docs/stackit_argus_credentials.md +++ b/docs/stackit_argus_credentials.md @@ -30,4 +30,6 @@ stackit argus credentials [flags] * [stackit argus](./stackit_argus.md) - Provides functionality for Argus * [stackit argus credentials create](./stackit_argus_credentials_create.md) - Creates credentials for an Argus instance. +* [stackit argus credentials delete](./stackit_argus_credentials_delete.md) - Deletes credentials of an Argus instance +* [stackit argus credentials list](./stackit_argus_credentials_list.md) - Lists the usernames of all credentials for an Argus instance diff --git a/docs/stackit_argus_credentials_create.md b/docs/stackit_argus_credentials_create.md index 74ea3449c..09d358f9d 100644 --- a/docs/stackit_argus_credentials_create.md +++ b/docs/stackit_argus_credentials_create.md @@ -4,7 +4,8 @@ Creates credentials for an Argus instance. ### Synopsis -Creates credentials for an Argus instance. +Creates credentials (username and password) for an Argus instance. +The credentials will be generated and included in the response. You won't be able to retrieve the password later. ``` stackit argus credentials create [flags] @@ -15,16 +16,12 @@ stackit argus credentials create [flags] ``` Create credentials for Argus instance with ID "xxx" $ stackit argus credentials create --instance-id xxx - - Create credentials for Argus instance with ID "xxx" and hide the password in the output - $ stackit argus credentials create --instance-id xxx --hide-password ``` ### Options ``` -h, --help Help for "stackit argus credentials create" - --hide-password Hide password in output --instance-id string Instance ID ``` diff --git a/docs/stackit_argus_credentials_delete.md b/docs/stackit_argus_credentials_delete.md new file mode 100644 index 000000000..6271d029b --- /dev/null +++ b/docs/stackit_argus_credentials_delete.md @@ -0,0 +1,40 @@ +## stackit argus credentials delete + +Deletes credentials of an Argus instance + +### Synopsis + +Deletes credentials of an Argus instance. + +``` +stackit argus credentials delete USERNAME [flags] +``` + +### Examples + +``` + Delete credentials of username "xxx" for Argus instance with ID "yyy" + $ stackit argus credentials delete xxx --instance-id yyy +``` + +### Options + +``` + -h, --help Help for "stackit argus credentials delete" + --instance-id string Instance ID +``` + +### 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"] + -p, --project-id string Project ID + --verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info") +``` + +### SEE ALSO + +* [stackit argus credentials](./stackit_argus_credentials.md) - Provides functionality for Argus credentials + diff --git a/docs/stackit_argus_credentials_list.md b/docs/stackit_argus_credentials_list.md new file mode 100644 index 000000000..cb1565ffb --- /dev/null +++ b/docs/stackit_argus_credentials_list.md @@ -0,0 +1,47 @@ +## stackit argus credentials list + +Lists the usernames of all credentials for an Argus instance + +### Synopsis + +Lists the usernames of all credentials for an Argus instance. + +``` +stackit argus credentials list [flags] +``` + +### Examples + +``` + List the usernames of all credentials for an Argus instance with ID "xxx" + $ stackit argus credentials list --instance-id xxx + + List the usernames of all credentials for an Argus instance in JSON format + $ stackit argus credentials list --instance-id xxx --output-format json + + List the usernames of up to 10 credentials for an Argus instance + $ stackit argus credentials list --instance-id xxx --limit 10 +``` + +### Options + +``` + -h, --help Help for "stackit argus credentials list" + --instance-id string Instance ID + --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"] + -p, --project-id string Project ID + --verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info") +``` + +### SEE ALSO + +* [stackit argus credentials](./stackit_argus_credentials.md) - Provides functionality for Argus credentials + diff --git a/internal/cmd/argus/credentials/create/create.go b/internal/cmd/argus/credentials/create/create.go index 7dd7c4527..141c3ec4d 100644 --- a/internal/cmd/argus/credentials/create/create.go +++ b/internal/cmd/argus/credentials/create/create.go @@ -18,30 +18,27 @@ import ( ) const ( - instanceIdFlag = "instance-id" - hidePasswordFlag = "hide-password" + instanceIdFlag = "instance-id" ) type inputModel struct { *globalflags.GlobalFlagModel - HidePassword bool - InstanceId string + InstanceId string } func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Creates credentials for an Argus instance.", - Long: "Creates credentials for an Argus instance.", - Args: args.NoArgs, + Long: fmt.Sprintf("%s\n%s", + "Creates credentials (username and password) for an Argus instance.", + "The credentials will be generated and included in the response. You won't be able to retrieve the password later."), + Args: args.NoArgs, Example: examples.Build( examples.NewExample( `Create credentials for Argus instance with ID "xxx"`, "$ stackit argus credentials create --instance-id xxx"), - examples.NewExample( - `Create credentials for Argus instance with ID "xxx" and hide the password in the output`, - "$ stackit argus credentials create --instance-id xxx --hide-password"), ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() @@ -85,11 +82,8 @@ func NewCmd(p *print.Printer) *cobra.Command { if username != "" { p.Outputf("Username: %s\n", username) } - if model.HidePassword { - p.Outputf("Password: \n") - } else { - p.Outputf("Password: %s\n", *resp.Credentials.Password) - } + + p.Outputf("Password: %s\n", *resp.Credentials.Password) return nil }, } @@ -99,7 +93,6 @@ func NewCmd(p *print.Printer) *cobra.Command { func configureFlags(cmd *cobra.Command) { cmd.Flags().Var(flags.UUIDFlag(), instanceIdFlag, "Instance ID") - cmd.Flags().Bool(hidePasswordFlag, false, "Hide password in output") err := flags.MarkFlagsRequired(cmd, instanceIdFlag) cobra.CheckErr(err) @@ -114,7 +107,6 @@ func parseInput(cmd *cobra.Command) (*inputModel, error) { return &inputModel{ GlobalFlagModel: globalFlags, InstanceId: flags.FlagToStringValue(cmd, instanceIdFlag), - HidePassword: flags.FlagToBoolValue(cmd, hidePasswordFlag), }, nil } diff --git a/internal/cmd/argus/credentials/create/create_test.go b/internal/cmd/argus/credentials/create/create_test.go index fdcdf220f..523f8ed9f 100644 --- a/internal/cmd/argus/credentials/create/create_test.go +++ b/internal/cmd/argus/credentials/create/create_test.go @@ -38,8 +38,7 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel { ProjectId: testProjectId, Verbosity: globalflags.VerbosityDefault, }, - InstanceId: testInstanceId, - HidePassword: false, + InstanceId: testInstanceId, } for _, mod := range mods { mod(model) @@ -115,38 +114,6 @@ func TestParseInput(t *testing.T) { }), isValid: false, }, - { - description: "hide password true", - flagValues: fixtureFlagValues(func(flagValues map[string]string) { - flagValues[hidePasswordFlag] = "true" - }), - isValid: true, - expectedModel: fixtureInputModel(func(model *inputModel) { - model.HidePassword = true - }), - }, - { - description: "hide password false", - flagValues: fixtureFlagValues(func(flagValues map[string]string) { - flagValues[hidePasswordFlag] = "false" - }), - expectedModel: fixtureInputModel(), - isValid: true, - }, - { - description: "hide password invalid 1", - flagValues: fixtureFlagValues(func(flagValues map[string]string) { - flagValues[hidePasswordFlag] = "invalid" - }), - isValid: false, - }, - { - description: "hide password invalid 2", - flagValues: fixtureFlagValues(func(flagValues map[string]string) { - flagValues[hidePasswordFlag] = "" - }), - isValid: false, - }, } for _, tt := range tests { diff --git a/internal/cmd/argus/credentials/credentials.go b/internal/cmd/argus/credentials/credentials.go index 2629f76d6..b5a64b94c 100644 --- a/internal/cmd/argus/credentials/credentials.go +++ b/internal/cmd/argus/credentials/credentials.go @@ -2,6 +2,8 @@ package credentials import ( "github.com/stackitcloud/stackit-cli/internal/cmd/argus/credentials/create" + "github.com/stackitcloud/stackit-cli/internal/cmd/argus/credentials/delete" + "github.com/stackitcloud/stackit-cli/internal/cmd/argus/credentials/list" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/print" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -23,4 +25,6 @@ func NewCmd(p *print.Printer) *cobra.Command { func addSubcommands(cmd *cobra.Command, p *print.Printer) { cmd.AddCommand(create.NewCmd(p)) + cmd.AddCommand(delete.NewCmd(p)) + cmd.AddCommand(list.NewCmd(p)) } diff --git a/internal/cmd/argus/credentials/delete/delete.go b/internal/cmd/argus/credentials/delete/delete.go new file mode 100644 index 000000000..6d76c2538 --- /dev/null +++ b/internal/cmd/argus/credentials/delete/delete.go @@ -0,0 +1,109 @@ +package delete + +import ( + "context" + "fmt" + + "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/services/argus/client" + argusUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/argus/utils" + "github.com/stackitcloud/stackit-sdk-go/services/argus" + + "github.com/spf13/cobra" +) + +const ( + usernameArg = "USERNAME" + + instanceIdFlag = "instance-id" +) + +type inputModel struct { + *globalflags.GlobalFlagModel + InstanceId string + Username string +} + +func NewCmd(p *print.Printer) *cobra.Command { + cmd := &cobra.Command{ + Use: fmt.Sprintf("delete %s", usernameArg), + Short: "Deletes credentials of an Argus instance", + Long: "Deletes credentials of an Argus instance.", + Args: args.SingleArg(usernameArg, nil), + Example: examples.Build( + examples.NewExample( + `Delete credentials of username "xxx" for Argus instance with ID "yyy"`, + "$ stackit argus credentials delete xxx --instance-id yyy"), + ), + RunE: func(cmd *cobra.Command, args []string) error { + ctx := context.Background() + model, err := parseInput(cmd, args) + if err != nil { + return err + } + + // Configure API client + apiClient, err := client.ConfigureClient(p) + if err != nil { + return err + } + + instanceLabel, err := argusUtils.GetInstanceName(ctx, apiClient, model.InstanceId, model.ProjectId) + if err != nil { + instanceLabel = model.InstanceId + } + + if !model.AssumeYes { + prompt := fmt.Sprintf("Are you sure you want to delete credentials for username %q of instance %q? (This cannot be undone)", model.Username, instanceLabel) + err = p.PromptForConfirmation(prompt) + if err != nil { + return err + } + } + + // Call API + req := buildRequest(ctx, model, apiClient) + _, err = req.Execute() + if err != nil { + return fmt.Errorf("delete Argus credentials: %w", err) + } + + p.Info("Deleted credentials for username %q of instance %q\n", model.Username, instanceLabel) + return nil + }, + } + configureFlags(cmd) + return cmd +} + +func configureFlags(cmd *cobra.Command) { + cmd.Flags().Var(flags.UUIDFlag(), instanceIdFlag, "Instance ID") + + err := flags.MarkFlagsRequired(cmd, instanceIdFlag) + cobra.CheckErr(err) +} + +func parseInput(cmd *cobra.Command, inputArgs []string) (*inputModel, error) { + username := inputArgs[0] + + globalFlags := globalflags.Parse(cmd) + if globalFlags.ProjectId == "" { + return nil, &errors.ProjectIdError{} + } + + return &inputModel{ + GlobalFlagModel: globalFlags, + InstanceId: flags.FlagToStringValue(cmd, instanceIdFlag), + Username: username, + }, nil +} + +func buildRequest(ctx context.Context, model *inputModel, apiClient *argus.APIClient) argus.ApiDeleteCredentialsRequest { + req := apiClient.DeleteCredentials(ctx, model.InstanceId, model.ProjectId, model.Username) + return req +} diff --git a/internal/cmd/argus/credentials/delete/delete_test.go b/internal/cmd/argus/credentials/delete/delete_test.go new file mode 100644 index 000000000..13b1316f9 --- /dev/null +++ b/internal/cmd/argus/credentials/delete/delete_test.go @@ -0,0 +1,238 @@ +package delete + +import ( + "context" + "testing" + + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + + "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" + "github.com/google/uuid" + "github.com/stackitcloud/stackit-sdk-go/services/argus" +) + +var projectIdFlag = globalflags.ProjectIdFlag + +type testCtxKey struct{} + +var testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo") +var testClient = &argus.APIClient{} +var testProjectId = uuid.NewString() +var testInstanceId = uuid.NewString() + +const testUsername = "test-username" + +func fixtureArgValues(mods ...func(argValues []string)) []string { + argValues := []string{ + testUsername, + } + for _, mod := range mods { + mod(argValues) + } + return argValues +} + +func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string { + flagValues := map[string]string{ + projectIdFlag: testProjectId, + instanceIdFlag: testInstanceId, + } + for _, mod := range mods { + mod(flagValues) + } + return flagValues +} + +func fixtureInputModel(mods ...func(model *inputModel)) *inputModel { + model := &inputModel{ + GlobalFlagModel: &globalflags.GlobalFlagModel{ + ProjectId: testProjectId, + Verbosity: globalflags.VerbosityDefault, + }, + InstanceId: testInstanceId, + Username: testUsername, + } + for _, mod := range mods { + mod(model) + } + return model +} + +func fixtureRequest(mods ...func(request *argus.ApiDeleteCredentialsRequest)) argus.ApiDeleteCredentialsRequest { + request := testClient.DeleteCredentials(testCtx, testInstanceId, testProjectId, testUsername) + 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, projectIdFlag) + }), + isValid: false, + }, + { + description: "project id invalid 1", + argValues: fixtureArgValues(), + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues[projectIdFlag] = "" + }), + isValid: false, + }, + { + description: "project id invalid 2", + argValues: fixtureArgValues(), + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues[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: "username invalid", + argValues: []string{""}, + flagValues: fixtureFlagValues(), + isValid: false, + }, + } + + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + cmd := NewCmd(nil) + 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.ValidateArgs(tt.argValues) + if err != nil { + if !tt.isValid { + return + } + t.Fatalf("error validating args: %v", err) + } + + err = cmd.ValidateRequiredFlags() + if err != nil { + if !tt.isValid { + return + } + t.Fatalf("error validating flags: %v", err) + } + + model, err := parseInput(cmd, tt.argValues) + if err != nil { + if !tt.isValid { + return + } + t.Fatalf("error parsing input: %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 argus.ApiDeleteCredentialsRequest + }{ + { + 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/argus/credentials/list/list.go b/internal/cmd/argus/credentials/list/list.go new file mode 100644 index 000000000..a93e521ba --- /dev/null +++ b/internal/cmd/argus/credentials/list/list.go @@ -0,0 +1,148 @@ +package list + +import ( + "context" + "encoding/json" + "fmt" + + "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/services/argus/client" + argusUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/argus/utils" + "github.com/stackitcloud/stackit-cli/internal/pkg/tables" + "github.com/stackitcloud/stackit-sdk-go/services/argus" + + "github.com/spf13/cobra" +) + +const ( + instanceIdFlag = "instance-id" + limitFlag = "limit" +) + +type inputModel struct { + *globalflags.GlobalFlagModel + InstanceId string + Limit *int64 +} + +func NewCmd(p *print.Printer) *cobra.Command { + cmd := &cobra.Command{ + Use: "list", + Short: "Lists the usernames of all credentials for an Argus instance", + Long: "Lists the usernames of all credentials for an Argus instance.", + Args: args.NoArgs, + Example: examples.Build( + examples.NewExample( + `List the usernames of all credentials for an Argus instance with ID "xxx"`, + "$ stackit argus credentials list --instance-id xxx"), + examples.NewExample( + `List the usernames of all credentials for an Argus instance in JSON format`, + "$ stackit argus credentials list --instance-id xxx --output-format json"), + examples.NewExample( + `List the usernames of up to 10 credentials for an Argus instance`, + "$ stackit argus credentials list --instance-id xxx --limit 10"), + ), + RunE: func(cmd *cobra.Command, args []string) error { + ctx := context.Background() + model, err := parseInput(cmd) + if err != nil { + return err + } + + // Configure API client + apiClient, err := client.ConfigureClient(p) + if err != nil { + return err + } + + // Call API + req := buildRequest(ctx, model, apiClient) + resp, err := req.Execute() + if err != nil { + return fmt.Errorf("list Argus credentials: %w", err) + } + credentials := *resp.Credentials + if len(credentials) == 0 { + instanceLabel, err := argusUtils.GetInstanceName(ctx, apiClient, model.InstanceId, model.ProjectId) + if err != nil { + instanceLabel = model.InstanceId + } + p.Info("No credentials found for instance %q\n", instanceLabel) + return nil + } + + // Truncate output + if model.Limit != nil && len(credentials) > int(*model.Limit) { + credentials = credentials[:*model.Limit] + } + return outputResult(p, model.OutputFormat, credentials) + }, + } + configureFlags(cmd) + return cmd +} + +func configureFlags(cmd *cobra.Command) { + cmd.Flags().Var(flags.UUIDFlag(), instanceIdFlag, "Instance ID") + cmd.Flags().Int64(limitFlag, 0, "Maximum number of entries to list") + + err := flags.MarkFlagsRequired(cmd, instanceIdFlag) + cobra.CheckErr(err) +} + +func parseInput(cmd *cobra.Command) (*inputModel, error) { + globalFlags := globalflags.Parse(cmd) + if globalFlags.ProjectId == "" { + return nil, &errors.ProjectIdError{} + } + + limit := flags.FlagToInt64Pointer(cmd, limitFlag) + if limit != nil && *limit < 1 { + return nil, &errors.FlagValidationError{ + Flag: limitFlag, + Details: "must be greater than 0", + } + } + + return &inputModel{ + GlobalFlagModel: globalFlags, + InstanceId: flags.FlagToStringValue(cmd, instanceIdFlag), + Limit: limit, + }, nil +} + +func buildRequest(ctx context.Context, model *inputModel, apiClient *argus.APIClient) argus.ApiListCredentialsRequest { + req := apiClient.ListCredentials(ctx, model.InstanceId, model.ProjectId) + return req +} + +func outputResult(p *print.Printer, outputFormat string, credentials []argus.ServiceKeysList) error { + switch outputFormat { + case globalflags.JSONOutputFormat: + details, err := json.MarshalIndent(credentials, "", " ") + if err != nil { + return fmt.Errorf("marshal Argus credentials list: %w", err) + } + p.Outputln(string(details)) + + return nil + default: + table := tables.NewTable() + table.SetHeader("USERNAME") + for i := range credentials { + c := credentials[i] + table.AddRow(*c.Name) + } + err := table.Display(p) + if err != nil { + return fmt.Errorf("render table: %w", err) + } + + return nil + } +} diff --git a/internal/cmd/argus/credentials/list/list_test.go b/internal/cmd/argus/credentials/list/list_test.go new file mode 100644 index 000000000..4c965a032 --- /dev/null +++ b/internal/cmd/argus/credentials/list/list_test.go @@ -0,0 +1,207 @@ +package list + +import ( + "context" + "testing" + + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "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/argus" +) + +var projectIdFlag = globalflags.ProjectIdFlag + +type testCtxKey struct{} + +var testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo") +var testClient = &argus.APIClient{} +var testProjectId = uuid.NewString() +var testInstanceId = uuid.NewString() + +func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string { + flagValues := map[string]string{ + projectIdFlag: testProjectId, + instanceIdFlag: testInstanceId, + limitFlag: "10", + } + for _, mod := range mods { + mod(flagValues) + } + return flagValues +} + +func fixtureInputModel(mods ...func(model *inputModel)) *inputModel { + model := &inputModel{ + GlobalFlagModel: &globalflags.GlobalFlagModel{ + ProjectId: testProjectId, + Verbosity: globalflags.VerbosityDefault, + }, + InstanceId: testInstanceId, + Limit: utils.Ptr(int64(10)), + } + for _, mod := range mods { + mod(model) + } + return model +} + +func fixtureRequest(mods ...func(request *argus.ApiListCredentialsRequest)) argus.ApiListCredentialsRequest { + request := testClient.ListCredentials(testCtx, testInstanceId, testProjectId) + for _, mod := range mods { + mod(&request) + } + return request +} + +func TestParseInput(t *testing.T) { + tests := []struct { + description string + flagValues map[string]string + isValid bool + expectedModel *inputModel + }{ + { + description: "base", + flagValues: fixtureFlagValues(), + isValid: true, + expectedModel: fixtureInputModel(), + }, + { + description: "no values", + flagValues: map[string]string{}, + isValid: false, + }, + { + description: "project id missing", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + delete(flagValues, projectIdFlag) + }), + isValid: false, + }, + { + description: "project id invalid 1", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues[projectIdFlag] = "" + }), + isValid: false, + }, + { + description: "project id invalid 2", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues[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) { + cmd := NewCmd(nil) + 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) + } + + model, err := parseInput(cmd) + 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 argus.ApiListCredentialsRequest + }{ + { + 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) + } + }) + } +}