diff --git a/docs/stackit_argus_scrape-config.md b/docs/stackit_argus_scrape-config.md index d888ab89d..a04d07494 100644 --- a/docs/stackit_argus_scrape-config.md +++ b/docs/stackit_argus_scrape-config.md @@ -31,6 +31,8 @@ stackit argus scrape-config [flags] * [stackit argus](./stackit_argus.md) - Provides functionality for Argus * [stackit argus scrape-config create](./stackit_argus_scrape-config_create.md) - Creates a scrape configuration for an Argus instance * [stackit argus scrape-config delete](./stackit_argus_scrape-config_delete.md) - Deletes a scrape configuration from an Argus instance +* [stackit argus scrape-config describe](./stackit_argus_scrape-config_describe.md) - Shows details of a scrape configuration from an Argus instance * [stackit argus scrape-config generate-payload](./stackit_argus_scrape-config_generate-payload.md) - Generates a payload to create/update scrape configurations for an Argus instance +* [stackit argus scrape-config list](./stackit_argus_scrape-config_list.md) - Lists all scrape configurations of an Argus instance * [stackit argus scrape-config update](./stackit_argus_scrape-config_update.md) - Updates a scrape configuration of an Argus instance diff --git a/docs/stackit_argus_scrape-config_describe.md b/docs/stackit_argus_scrape-config_describe.md new file mode 100644 index 000000000..567b14e85 --- /dev/null +++ b/docs/stackit_argus_scrape-config_describe.md @@ -0,0 +1,43 @@ +## stackit argus scrape-config describe + +Shows details of a scrape configuration from an Argus instance + +### Synopsis + +Shows details of a scrape configuration from an Argus instance. + +``` +stackit argus scrape-config describe JOB_NAME [flags] +``` + +### Examples + +``` + Get details of a scrape configuration with name "my-config" from Argus instance "xxx" + $ stackit argus scrape-config describe my-config --instance-id xxx + + Get details of a scrape configuration with name "my-config" from Argus instance "xxx" in a table format + $ stackit argus scrape-config describe my-config --output-format pretty +``` + +### Options + +``` + -h, --help Help for "stackit argus scrape-config describe" + --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 scrape-config](./stackit_argus_scrape-config.md) - Provides functionality for scrape configurations in Argus + diff --git a/docs/stackit_argus_scrape-config_list.md b/docs/stackit_argus_scrape-config_list.md new file mode 100644 index 000000000..10a7fd368 --- /dev/null +++ b/docs/stackit_argus_scrape-config_list.md @@ -0,0 +1,47 @@ +## stackit argus scrape-config list + +Lists all scrape configurations of an Argus instance + +### Synopsis + +Lists all scrape configurations of an Argus instance. + +``` +stackit argus scrape-config list [flags] +``` + +### Examples + +``` + List all scrape configurations of Argus instance "xxx" + $ stackit argus scrape-config list --instance-id xxx + + List all scrape configurations of Argus instance "xxx" in JSON format + $ stackit argus scrape-config list --instance-id xxx --output-format json + + List up to 10 scrape configurations of Argus instance "xxx" + $ stackit argus scrape-config list --instance-id xxx --limit 10 +``` + +### Options + +``` + -h, --help Help for "stackit argus scrape-config 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 scrape-config](./stackit_argus_scrape-config.md) - Provides functionality for scrape configurations in Argus + diff --git a/internal/cmd/argus/scrape-config/create/create.go b/internal/cmd/argus/scrape-config/create/create.go index 4eeda25d9..329d59e1a 100644 --- a/internal/cmd/argus/scrape-config/create/create.go +++ b/internal/cmd/argus/scrape-config/create/create.go @@ -77,7 +77,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } if !model.AssumeYes { - prompt := fmt.Sprintf("Are you sure you want to create a scrape configuration on Argus instance %q?", instanceLabel) + prompt := fmt.Sprintf("Are you sure you want to create scrape configuration %q on Argus instance %q?", *model.Payload.JobName, instanceLabel) err = p.PromptForConfirmation(prompt) if err != nil { return err diff --git a/internal/cmd/argus/scrape-config/describe/describe.go b/internal/cmd/argus/scrape-config/describe/describe.go new file mode 100644 index 000000000..9d9af74cf --- /dev/null +++ b/internal/cmd/argus/scrape-config/describe/describe.go @@ -0,0 +1,176 @@ +package describe + +import ( + "context" + "encoding/json" + "fmt" + "strings" + + "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" + "github.com/stackitcloud/stackit-cli/internal/pkg/tables" + "github.com/stackitcloud/stackit-sdk-go/services/argus" + + "github.com/spf13/cobra" +) + +const ( + jobNameArg = "JOB_NAME" + + instanceIdFlag = "instance-id" +) + +type inputModel struct { + *globalflags.GlobalFlagModel + JobName string + InstanceId string +} + +func NewCmd(p *print.Printer) *cobra.Command { + cmd := &cobra.Command{ + Use: fmt.Sprintf("describe %s", jobNameArg), + Short: "Shows details of a scrape configuration from an Argus instance", + Long: "Shows details of a scrape configuration from an Argus instance.", + Args: args.SingleArg(jobNameArg, nil), + Example: examples.Build( + examples.NewExample( + `Get details of a scrape configuration with name "my-config" from Argus instance "xxx"`, + "$ stackit argus scrape-config describe my-config --instance-id xxx"), + examples.NewExample( + `Get details of a scrape configuration with name "my-config" from Argus instance "xxx" in a table format`, + "$ stackit argus scrape-config describe my-config --output-format pretty"), + ), + 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 + } + + // Call API + req := buildRequest(ctx, model, apiClient) + resp, err := req.Execute() + if err != nil { + return fmt.Errorf("read scrape configuration: %w", err) + } + + return outputResult(p, model.OutputFormat, resp.Data) + }, + } + 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) { + jobName := inputArgs[0] + + globalFlags := globalflags.Parse(cmd) + if globalFlags.ProjectId == "" { + return nil, &errors.ProjectIdError{} + } + + return &inputModel{ + GlobalFlagModel: globalFlags, + JobName: jobName, + InstanceId: flags.FlagToStringValue(cmd, instanceIdFlag), + }, nil +} + +func buildRequest(ctx context.Context, model *inputModel, apiClient *argus.APIClient) argus.ApiGetScrapeConfigRequest { + req := apiClient.GetScrapeConfig(ctx, model.InstanceId, model.JobName, model.ProjectId) + return req +} + +func outputResult(p *print.Printer, outputFormat string, config *argus.Job) error { + switch outputFormat { + case globalflags.PrettyOutputFormat: + + saml2Enabled := "Enabled" + if config.Params != nil { + saml2 := (*config.Params)["saml2"] + if len(saml2) > 0 && saml2[0] == "disabled" { + saml2Enabled = "Disabled" + } + } + + var targets []string + for _, target := range *config.StaticConfigs { + targetLabels := []string{} + targetLabelStr := "N/A" + if target.Labels != nil { + // make map prettier + for k, v := range *target.Labels { + targetLabels = append(targetLabels, fmt.Sprintf("%s:%s", k, v)) + } + if targetLabels != nil { + targetLabelStr = strings.Join(targetLabels, ",") + } + } + targetUrlsStr := "N/A" + if target.Targets != nil { + targetUrlsStr = strings.Join(*target.Targets, ",") + } + targets = append(targets, fmt.Sprintf("labels: %s\nurls: %s", targetLabelStr, targetUrlsStr)) + } + + table := tables.NewTable() + table.AddRow("NAME", *config.JobName) + table.AddSeparator() + table.AddRow("METRICS PATH", *config.MetricsPath) + table.AddSeparator() + table.AddRow("SCHEME", *config.Scheme) + table.AddSeparator() + table.AddRow("SCRAPE INTERVAL", *config.ScrapeInterval) + table.AddSeparator() + table.AddRow("SCRAPE TIMEOUT", *config.ScrapeTimeout) + table.AddSeparator() + table.AddRow("SAML2", saml2Enabled) + table.AddSeparator() + if config.BasicAuth == nil { + table.AddRow("AUTHENTICATION", "None") + } else { + table.AddRow("AUTHENTICATION", "Basic Auth") + table.AddSeparator() + table.AddRow("USERNAME", *config.BasicAuth.Username) + table.AddSeparator() + table.AddRow("PASSWORD", *config.BasicAuth.Password) + } + table.AddSeparator() + for i, target := range targets { + table.AddRow(fmt.Sprintf("TARGET #%d", i+1), target) + table.AddSeparator() + } + + err := table.Display(p) + if err != nil { + return fmt.Errorf("render table: %w", err) + } + + return nil + default: + details, err := json.MarshalIndent(config, "", " ") + if err != nil { + return fmt.Errorf("marshal scrape configuration: %w", err) + } + p.Outputln(string(details)) + + return nil + } +} diff --git a/internal/cmd/argus/scrape-config/describe/describe_test.go b/internal/cmd/argus/scrape-config/describe/describe_test.go new file mode 100644 index 000000000..ab2217f3e --- /dev/null +++ b/internal/cmd/argus/scrape-config/describe/describe_test.go @@ -0,0 +1,233 @@ +package describe + +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() +var testJobName = "my-config" + +func fixtureArgValues(mods ...func(argValues []string)) []string { + argValues := []string{ + testJobName, + } + 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, + }, + JobName: testJobName, + InstanceId: testInstanceId, + } + for _, mod := range mods { + mod(model) + } + return model +} + +func fixtureRequest(mods ...func(request *argus.ApiGetScrapeConfigRequest)) argus.ApiGetScrapeConfigRequest { + request := testClient.GetScrapeConfig(testCtx, testInstanceId, testJobName, testProjectId) + 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, + }, + } + + 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 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 + isValid bool + expectedRequest argus.ApiGetScrapeConfigRequest + }{ + { + description: "base", + model: fixtureInputModel(), + isValid: true, + 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/scrape-config/list/list.go b/internal/cmd/argus/scrape-config/list/list.go new file mode 100644 index 000000000..385a20597 --- /dev/null +++ b/internal/cmd/argus/scrape-config/list/list.go @@ -0,0 +1,162 @@ +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" + "github.com/stackitcloud/stackit-cli/internal/pkg/tables" + + argusUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/argus/utils" + + "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-sdk-go/services/argus" +) + +const ( + limitFlag = "limit" + instanceIdFlag = "instance-id" +) + +type inputModel struct { + *globalflags.GlobalFlagModel + Limit *int64 + InstanceId string +} + +func NewCmd(p *print.Printer) *cobra.Command { + cmd := &cobra.Command{ + Use: "list", + Short: "Lists all scrape configurations of an Argus instance", + Long: "Lists all scrape configurations of an Argus instance.", + Args: args.NoArgs, + Example: examples.Build( + examples.NewExample( + `List all scrape configurations of Argus instance "xxx"`, + "$ stackit argus scrape-config list --instance-id xxx"), + examples.NewExample( + `List all scrape configurations of Argus instance "xxx" in JSON format`, + "$ stackit argus scrape-config list --instance-id xxx --output-format json"), + examples.NewExample( + `List up to 10 scrape configurations of Argus instance "xxx"`, + "$ stackit argus scrape-config 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("get scrape configurations: %w", err) + } + configs := *resp.Data + if len(configs) == 0 { + instanceLabel, err := argusUtils.GetInstanceName(ctx, apiClient, model.InstanceId, model.ProjectId) + if err != nil { + instanceLabel = model.InstanceId + } + p.Info("No scrape configurations found for instance %q\n", instanceLabel) + return nil + } + + // Truncate output + if model.Limit != nil && len(configs) > int(*model.Limit) { + configs = configs[:*model.Limit] + } + + return outputResult(p, model.OutputFormat, configs) + }, + } + + 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, "Instance ID") + + 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, + Limit: flags.FlagToInt64Pointer(cmd, limitFlag), + InstanceId: flags.FlagToStringValue(cmd, instanceIdFlag), + }, nil +} + +func buildRequest(ctx context.Context, model *inputModel, apiClient *argus.APIClient) argus.ApiListScrapeConfigsRequest { + req := apiClient.ListScrapeConfigs(ctx, model.InstanceId, model.ProjectId) + return req +} + +func outputResult(p *print.Printer, outputFormat string, configs []argus.Job) error { + switch outputFormat { + case globalflags.JSONOutputFormat: + details, err := json.MarshalIndent(configs, "", " ") + if err != nil { + return fmt.Errorf("marshal scrape configurations list: %w", err) + } + p.Outputln(string(details)) + + return nil + default: + table := tables.NewTable() + table.SetHeader("NAME", "TARGETS", "SCRAPE INTERVAL") + for i := range configs { + c := configs[i] + + targets := 0 + if c.StaticConfigs != nil { + for _, sc := range *c.StaticConfigs { + if sc.Targets == nil { + continue + } + targets += len(*sc.Targets) + } + } + + table.AddRow(*c.JobName, targets, *c.ScrapeInterval) + } + err := table.Display(p) + if err != nil { + return fmt.Errorf("render table: %w", err) + } + + return nil + } +} diff --git a/internal/cmd/argus/scrape-config/list/list_test.go b/internal/cmd/argus/scrape-config/list/list_test.go new file mode 100644 index 000000000..9a5952a32 --- /dev/null +++ b/internal/cmd/argus/scrape-config/list/list_test.go @@ -0,0 +1,210 @@ +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/spf13/cobra" + "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, + limitFlag: "10", + 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, + }, + Limit: utils.Ptr(int64(10)), + InstanceId: testInstanceId, + } + for _, mod := range mods { + mod(model) + } + return model +} + +func fixtureRequest(mods ...func(request *argus.ApiListScrapeConfigsRequest)) argus.ApiListScrapeConfigsRequest { + request := testClient.ListScrapeConfigs(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 := &cobra.Command{} + err := globalflags.Configure(cmd.Flags()) + if err != nil { + t.Fatalf("configure global flags: %v", err) + } + + configureFlags(cmd) + + 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.ApiListScrapeConfigsRequest + }{ + { + 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/scrape-config/scrape_config.go b/internal/cmd/argus/scrape-config/scrape_config.go index aab92fa9e..781569aa9 100644 --- a/internal/cmd/argus/scrape-config/scrape_config.go +++ b/internal/cmd/argus/scrape-config/scrape_config.go @@ -3,7 +3,9 @@ package scrapeconfig import ( "github.com/stackitcloud/stackit-cli/internal/cmd/argus/scrape-config/create" "github.com/stackitcloud/stackit-cli/internal/cmd/argus/scrape-config/delete" + "github.com/stackitcloud/stackit-cli/internal/cmd/argus/scrape-config/describe" generatepayload "github.com/stackitcloud/stackit-cli/internal/cmd/argus/scrape-config/generate-payload" + "github.com/stackitcloud/stackit-cli/internal/cmd/argus/scrape-config/list" "github.com/stackitcloud/stackit-cli/internal/cmd/argus/scrape-config/update" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/print" @@ -29,4 +31,6 @@ func addSubcommands(cmd *cobra.Command, p *print.Printer) { cmd.AddCommand(create.NewCmd(p)) cmd.AddCommand(delete.NewCmd(p)) cmd.AddCommand(update.NewCmd(p)) + cmd.AddCommand(list.NewCmd(p)) + cmd.AddCommand(describe.NewCmd(p)) }