From 3f7eda28af73d630c2ba47759bc8ad4feac6872d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diogo=20Ferr=C3=A3o?= Date: Mon, 22 Apr 2024 13:29:25 +0200 Subject: [PATCH 1/9] delete command implementation and testing --- docs/stackit_argus_scrape-configs_delete.md | 40 +++ .../cmd/argus/scrape-configs/delete/delete.go | 120 +++++++++ .../scrape-configs/delete/delete_test.go | 233 ++++++++++++++++++ 3 files changed, 393 insertions(+) create mode 100644 docs/stackit_argus_scrape-configs_delete.md create mode 100644 internal/cmd/argus/scrape-configs/delete/delete.go create mode 100644 internal/cmd/argus/scrape-configs/delete/delete_test.go diff --git a/docs/stackit_argus_scrape-configs_delete.md b/docs/stackit_argus_scrape-configs_delete.md new file mode 100644 index 000000000..5886abbde --- /dev/null +++ b/docs/stackit_argus_scrape-configs_delete.md @@ -0,0 +1,40 @@ +## stackit argus scrape-configs delete + +Deletes an Argus Scrape Config + +### Synopsis + +Deletes an Argus Scrape Config. + +``` +stackit argus scrape-configs delete JOB_NAME [flags] +``` + +### Examples + +``` + Delete an Argus Scrape config with name "my-config" from Argus instance "xxx" + $ stackit argus scrape-configs delete my-config --instance-id xxx +``` + +### Options + +``` + -h, --help Help for "stackit argus scrape-configs 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 scrape-configs](./stackit_argus_scrape-configs.md) - Provides functionality for scrape configs in Argus. + diff --git a/internal/cmd/argus/scrape-configs/delete/delete.go b/internal/cmd/argus/scrape-configs/delete/delete.go new file mode 100644 index 000000000..605790280 --- /dev/null +++ b/internal/cmd/argus/scrape-configs/delete/delete.go @@ -0,0 +1,120 @@ +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" + "github.com/stackitcloud/stackit-cli/internal/pkg/spinner" + + "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-sdk-go/services/argus" + "github.com/stackitcloud/stackit-sdk-go/services/argus/wait" +) + +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("delete %s", jobNameArg), + Short: "Deletes an Argus Scrape Config", + Long: "Deletes an Argus Scrape Config.", + Args: args.SingleArg(jobNameArg, nil), + Example: examples.Build( + examples.NewExample( + `Delete an Argus Scrape config with name "my-config" from Argus instance "xxx"`, + "$ stackit argus scrape-configs delete my-config --instance-id xxx"), + ), + 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 + } + + if !model.AssumeYes { + prompt := fmt.Sprintf("Are you sure you want to delete Scrape Config %q? (This cannot be undone)", model.JobName) + 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 Scrape Config: %w", err) + } + + // Wait for async operation, if async mode not enabled + if !model.Async { + s := spinner.New(p) + s.Start("Deleting scrape config") + _, err = wait.DeleteScrapeConfigWaitHandler(ctx, apiClient, model.InstanceId, model.JobName, model.ProjectId).WaitWithContext(ctx) + if err != nil { + return fmt.Errorf("wait for Scrape Config deletion: %w", err) + } + s.Stop() + } + + operationState := "Deleted" + if model.Async { + operationState = "Triggered deletion of" + } + p.Info("%s Scrape Config %q\n", operationState, model.JobName) + 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) { + clusterName := inputArgs[0] + + globalFlags := globalflags.Parse(cmd) + if globalFlags.ProjectId == "" { + return nil, &errors.ProjectIdError{} + } + + return &inputModel{ + GlobalFlagModel: globalFlags, + JobName: clusterName, + InstanceId: flags.FlagToStringValue(cmd, instanceIdFlag), + }, nil +} + +func buildRequest(ctx context.Context, model *inputModel, apiClient *argus.APIClient) argus.ApiDeleteScrapeConfigRequest { + req := apiClient.DeleteScrapeConfig(ctx, model.InstanceId, model.JobName, model.ProjectId) + return req +} diff --git a/internal/cmd/argus/scrape-configs/delete/delete_test.go b/internal/cmd/argus/scrape-configs/delete/delete_test.go new file mode 100644 index 000000000..f6f7ae5b6 --- /dev/null +++ b/internal/cmd/argus/scrape-configs/delete/delete_test.go @@ -0,0 +1,233 @@ +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() +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.ApiDeleteScrapeConfigRequest)) argus.ApiDeleteScrapeConfigRequest { + request := testClient.DeleteScrapeConfig(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.ApiDeleteScrapeConfigRequest + }{ + { + 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) + } + }) + } +} From 561fae7267647aedea45a413608e379a3ca23b48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diogo=20Ferr=C3=A3o?= Date: Mon, 22 Apr 2024 16:16:26 +0200 Subject: [PATCH 2/9] initial implementation --- .../cmd/argus/scrape-config/scrape_config.go | 1 + .../cmd/argus/scrape-configs/update/update.go | 131 ++++++++++ .../scrape-configs/update/update_test.go | 247 ++++++++++++++++++ update.json | 20 ++ 4 files changed, 399 insertions(+) create mode 100644 internal/cmd/argus/scrape-configs/update/update.go create mode 100644 internal/cmd/argus/scrape-configs/update/update_test.go create mode 100644 update.json diff --git a/internal/cmd/argus/scrape-config/scrape_config.go b/internal/cmd/argus/scrape-config/scrape_config.go index 41517c774..135aac749 100644 --- a/internal/cmd/argus/scrape-config/scrape_config.go +++ b/internal/cmd/argus/scrape-config/scrape_config.go @@ -27,4 +27,5 @@ func addSubcommands(cmd *cobra.Command, p *print.Printer) { cmd.AddCommand(generatepayload.NewCmd(p)) cmd.AddCommand(create.NewCmd(p)) cmd.AddCommand(delete.NewCmd(p)) + cmd.AddCommand(update.NewCmd(p)) } diff --git a/internal/cmd/argus/scrape-configs/update/update.go b/internal/cmd/argus/scrape-configs/update/update.go new file mode 100644 index 000000000..00a128671 --- /dev/null +++ b/internal/cmd/argus/scrape-configs/update/update.go @@ -0,0 +1,131 @@ +package update + +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/spf13/cobra" + "github.com/stackitcloud/stackit-sdk-go/services/argus" +) + +const ( + jobNameArg = "JOB_NAME" + + instanceIdFlag = "instance-id" + payloadFlag = "payload" +) + +type inputModel struct { + *globalflags.GlobalFlagModel + JobName string + InstanceId string + Payload argus.UpdateScrapeConfigPayload +} + +func NewCmd(p *print.Printer) *cobra.Command { + cmd := &cobra.Command{ + Use: fmt.Sprintf("update %s", jobNameArg), + Short: "Updates a Scrape Config of an Argus instance", + Long: fmt.Sprintf("%s\n%s\n%s", + "Updates a Scrape Config of an Argus instance.", + "The payload can be provided as a JSON string or a file path prefixed with \"@\".", + "See https://docs.api.stackit.cloud/documentation/argus/version/v1#tag/scrape-config/operation/v1_projects_instances_scrapeconfigs_partial_update for information regarding the payload structure.", + ), + Args: args.SingleArg(jobNameArg, nil), + Example: examples.Build( + examples.NewExample( + `Update a Scrape Config from Argus instance "xxx", using an API payload sourced from the file "./payload.json"`, + "$ stackit argus scrape-configs update my-config --payload @./payload.json --instance-id xxx"), + examples.NewExample( + `Update an Scrape Config from Argus instance "xxx", using an API payload provided as a JSON string`, + `$ stackit argus scrape-configs update my-config --payload "{...}" --instance-id xxx`), + examples.NewExample( + `Generate a payload with the current values of a Scrape Config, and adapt it with custom values for the different configuration options`, + `$ stackit argus scrape-config generate-payload --job-name my-config > ./payload.json`, + ``, + `$ stackit argus scrape-configs update my-config --payload @./payload.json`), + ), + 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 + } + + if !model.AssumeYes { + prompt := fmt.Sprintf("Are you sure you want to update scrape config %q?", model.JobName) + 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("update scrape config: %w", err) + } + + // The API has no status to wait on, so async mode is default + operationState := "Triggered update of" + p.Info("%s Argus Scrape Config %q\n", operationState, model.JobName) + return nil + }, + } + configureFlags(cmd) + return cmd +} + +func configureFlags(cmd *cobra.Command) { + cmd.Flags().Var(flags.ReadFromFileFlag(), payloadFlag, `Request payload (JSON). Can be a string or a file path, if prefixed with "@". Example: @./payload.json`) + cmd.Flags().Var(flags.UUIDFlag(), instanceIdFlag, "Instance ID") + + err := flags.MarkFlagsRequired(cmd, instanceIdFlag, payloadFlag) + cobra.CheckErr(err) +} + +func parseInput(cmd *cobra.Command, inputArgs []string) (*inputModel, error) { + clusterName := inputArgs[0] + + globalFlags := globalflags.Parse(cmd) + if globalFlags.ProjectId == "" { + return nil, &errors.ProjectIdError{} + } + + payloadString := flags.FlagToStringValue(cmd, payloadFlag) + var payload argus.UpdateScrapeConfigPayload + err := json.Unmarshal([]byte(payloadString), &payload) + if err != nil { + return nil, fmt.Errorf("encode payload: %w", err) + } + + return &inputModel{ + GlobalFlagModel: globalFlags, + JobName: clusterName, + Payload: payload, + InstanceId: flags.FlagToStringValue(cmd, instanceIdFlag), + }, nil +} + +func buildRequest(ctx context.Context, model *inputModel, apiClient *argus.APIClient) argus.ApiUpdateScrapeConfigRequest { + req := apiClient.UpdateScrapeConfig(ctx, model.InstanceId, model.JobName, model.ProjectId) + + req = req.UpdateScrapeConfigPayload(model.Payload) + return req +} diff --git a/internal/cmd/argus/scrape-configs/update/update_test.go b/internal/cmd/argus/scrape-configs/update/update_test.go new file mode 100644 index 000000000..5402e5659 --- /dev/null +++ b/internal/cmd/argus/scrape-configs/update/update_test.go @@ -0,0 +1,247 @@ +package update + +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" + "github.com/stackitcloud/stackit-sdk-go/services/ske" +) + +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" + +var testPayload = argus.UpdateScrapeConfigPayload{ + BasicAuth: &argus.CreateScrapeConfigPayloadBasicAuth{ + Username: utils.Ptr("username"), + Password: utils.Ptr("password"), + }, + BearerToken: utils.Ptr("bearerToken"), + HonorLabels: utils.Ptr(true), + HonorTimeStamps: utils.Ptr(true), + MetricsPath: utils.Ptr("/metrics"), + MetricsRelabelConfigs: &[]argus.CreateScrapeConfigPayloadMetricsRelabelConfigsInner{ + { + Action: utils.Ptr("replace"), + Modulus: utils.Ptr(1.0), + Regex: utils.Ptr("regex"), + Replacement: utils.Ptr("replacement"), + Separator: utils.Ptr("separator"), + SourceLabels: &[]string{"sourceLabel"}, + TargetLabel: utils.Ptr("targetLabel"), + }, + }, + Params: &map[string]interface{}{ + "key": []interface{}{string("value1"), string("value2")}, + "key2": []interface{}{}, + }, +} + +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, + payloadFlag: `{ + + }`, + } + 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, + }, + ClusterName: testJobName, + Payload: testPayload, + } + for _, mod := range mods { + mod(model) + } + return model +} + +func fixtureRequest(mods ...func(request *argus.ApiUpdateScrapeConfigRequest)) argus.ApiUpdateScrapeConfigRequest { + request := testClient.ScrapeConfig(testCtx, testProjectId, fixtureInputModel().ClusterName) + request = request.CreateOrUpdateClusterPayload(testPayload) + 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: "invalid json", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues[payloadFlag] = "not json" + }), + isValid: false, + expectedModel: fixtureInputModel(), + }, + } + + 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 + expectedRequest ske.ApiCreateOrUpdateClusterRequest + isValid bool + }{ + { + 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/update.json b/update.json new file mode 100644 index 000000000..dd3f17b4c --- /dev/null +++ b/update.json @@ -0,0 +1,20 @@ +{ + "honorLabels": false, + "honorTimeStamps": false, + "metricsPath": "/metrics123", + "params": {}, + "sampleLimit": 5000, + "scheme": "https", + "scrapeInterval": "5m", + "scrapeTimeout": "2m", + "staticConfigs": [ + { + "labels": { + "job": "prometheus" + }, + "targets": [ + "localhost:9090" + ] + } + ] +} From 481cf4fe66d9cd1f06679ad015565aabc6365e01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diogo=20Ferr=C3=A3o?= Date: Mon, 22 Apr 2024 17:42:15 +0200 Subject: [PATCH 3/9] finish implementation --- .../cmd/argus/scrape-config/scrape_config.go | 1 + .../update/update.go | 21 +- .../update/update_test.go | 63 ++++- .../cmd/argus/scrape-configs/delete/delete.go | 120 --------- .../scrape-configs/delete/delete_test.go | 233 ------------------ 5 files changed, 67 insertions(+), 371 deletions(-) rename internal/cmd/argus/{scrape-configs => scrape-config}/update/update.go (80%) rename internal/cmd/argus/{scrape-configs => scrape-config}/update/update_test.go (78%) delete mode 100644 internal/cmd/argus/scrape-configs/delete/delete.go delete mode 100644 internal/cmd/argus/scrape-configs/delete/delete_test.go diff --git a/internal/cmd/argus/scrape-config/scrape_config.go b/internal/cmd/argus/scrape-config/scrape_config.go index 135aac749..aab92fa9e 100644 --- a/internal/cmd/argus/scrape-config/scrape_config.go +++ b/internal/cmd/argus/scrape-config/scrape_config.go @@ -4,6 +4,7 @@ import ( "github.com/stackitcloud/stackit-cli/internal/cmd/argus/scrape-config/create" "github.com/stackitcloud/stackit-cli/internal/cmd/argus/scrape-config/delete" generatepayload "github.com/stackitcloud/stackit-cli/internal/cmd/argus/scrape-config/generate-payload" + "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" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" diff --git a/internal/cmd/argus/scrape-configs/update/update.go b/internal/cmd/argus/scrape-config/update/update.go similarity index 80% rename from internal/cmd/argus/scrape-configs/update/update.go rename to internal/cmd/argus/scrape-config/update/update.go index 00a128671..095914689 100644 --- a/internal/cmd/argus/scrape-configs/update/update.go +++ b/internal/cmd/argus/scrape-config/update/update.go @@ -34,22 +34,22 @@ type inputModel struct { func NewCmd(p *print.Printer) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update %s", jobNameArg), - Short: "Updates a Scrape Config of an Argus instance", + Short: "Updates a scrape configuration of an Argus instance", Long: fmt.Sprintf("%s\n%s\n%s", - "Updates a Scrape Config of an Argus instance.", + "Updates a scrape configuration of an Argus instance.", "The payload can be provided as a JSON string or a file path prefixed with \"@\".", "See https://docs.api.stackit.cloud/documentation/argus/version/v1#tag/scrape-config/operation/v1_projects_instances_scrapeconfigs_partial_update for information regarding the payload structure.", ), Args: args.SingleArg(jobNameArg, nil), Example: examples.Build( examples.NewExample( - `Update a Scrape Config from Argus instance "xxx", using an API payload sourced from the file "./payload.json"`, - "$ stackit argus scrape-configs update my-config --payload @./payload.json --instance-id xxx"), + `Update a scrape configuration from Argus instance "xxx", using an API payload sourced from the file "./payload.json"`, + "$ stackit argus scrape-config update my-config --payload @./payload.json --instance-id xxx"), examples.NewExample( - `Update an Scrape Config from Argus instance "xxx", using an API payload provided as a JSON string`, - `$ stackit argus scrape-configs update my-config --payload "{...}" --instance-id xxx`), + `Update an scrape configuration from Argus instance "xxx", using an API payload provided as a JSON string`, + `$ stackit argus scrape-config update my-config --payload "{...}" --instance-id xxx`), examples.NewExample( - `Generate a payload with the current values of a Scrape Config, and adapt it with custom values for the different configuration options`, + `Generate a payload with the current values of a scrape configuration, and adapt it with custom values for the different configuration options`, `$ stackit argus scrape-config generate-payload --job-name my-config > ./payload.json`, ``, `$ stackit argus scrape-configs update my-config --payload @./payload.json`), @@ -68,7 +68,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } if !model.AssumeYes { - prompt := fmt.Sprintf("Are you sure you want to update scrape config %q?", model.JobName) + prompt := fmt.Sprintf("Are you sure you want to update scrape configuration %q?", model.JobName) err = p.PromptForConfirmation(prompt) if err != nil { return err @@ -83,8 +83,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } // The API has no status to wait on, so async mode is default - operationState := "Triggered update of" - p.Info("%s Argus Scrape Config %q\n", operationState, model.JobName) + p.Info("Triggered update of Argus scrape configuration with name %q\n", model.JobName) return nil }, } @@ -115,6 +114,8 @@ func parseInput(cmd *cobra.Command, inputArgs []string) (*inputModel, error) { return nil, fmt.Errorf("encode payload: %w", err) } + fmt.Println("Payload: ", payload) + return &inputModel{ GlobalFlagModel: globalFlags, JobName: clusterName, diff --git a/internal/cmd/argus/scrape-configs/update/update_test.go b/internal/cmd/argus/scrape-config/update/update_test.go similarity index 78% rename from internal/cmd/argus/scrape-configs/update/update_test.go rename to internal/cmd/argus/scrape-config/update/update_test.go index 5402e5659..87a8b7fa1 100644 --- a/internal/cmd/argus/scrape-configs/update/update_test.go +++ b/internal/cmd/argus/scrape-config/update/update_test.go @@ -11,7 +11,6 @@ import ( "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/uuid" "github.com/stackitcloud/stackit-sdk-go/services/argus" - "github.com/stackitcloud/stackit-sdk-go/services/ske" ) var projectIdFlag = globalflags.ProjectIdFlag @@ -62,9 +61,32 @@ func fixtureArgValues(mods ...func(argValues []string)) []string { func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string { flagValues := map[string]string{ - projectIdFlag: testProjectId, + projectIdFlag: testProjectId, + instanceIdFlag: testInstanceId, payloadFlag: `{ - + "basicAuth": { + "username": "username", + "password": "password" + }, + "bearerToken": "bearerToken", + "honorLabels": true, + "honorTimestamps": true, + "metricsPath": "/metrics", + "metricsRelabelConfigs": [ + { + "action": "replace", + "modulus": 1.0, + "regex": "regex", + "replacement": "replacement", + "separator": "separator", + "sourceLabels": ["sourceLabel"], + "targetLabel": "targetLabel" + } + ], + "params": { + "key": ["value1", "value2"], + "key2": [] + } }`, } for _, mod := range mods { @@ -79,8 +101,9 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel { ProjectId: testProjectId, Verbosity: globalflags.VerbosityDefault, }, - ClusterName: testJobName, - Payload: testPayload, + JobName: testJobName, + InstanceId: testInstanceId, + Payload: testPayload, } for _, mod := range mods { mod(model) @@ -89,8 +112,8 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel { } func fixtureRequest(mods ...func(request *argus.ApiUpdateScrapeConfigRequest)) argus.ApiUpdateScrapeConfigRequest { - request := testClient.ScrapeConfig(testCtx, testProjectId, fixtureInputModel().ClusterName) - request = request.CreateOrUpdateClusterPayload(testPayload) + request := testClient.UpdateScrapeConfig(testCtx, testInstanceId, testJobName, testProjectId) + request = request.UpdateScrapeConfigPayload(testPayload) for _, mod := range mods { mod(&request) } @@ -154,6 +177,30 @@ func TestParseInput(t *testing.T) { }), 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: "invalid json", flagValues: fixtureFlagValues(func(flagValues map[string]string) { @@ -221,7 +268,7 @@ func TestBuildRequest(t *testing.T) { tests := []struct { description string model *inputModel - expectedRequest ske.ApiCreateOrUpdateClusterRequest + expectedRequest argus.ApiUpdateScrapeConfigRequest isValid bool }{ { diff --git a/internal/cmd/argus/scrape-configs/delete/delete.go b/internal/cmd/argus/scrape-configs/delete/delete.go deleted file mode 100644 index 605790280..000000000 --- a/internal/cmd/argus/scrape-configs/delete/delete.go +++ /dev/null @@ -1,120 +0,0 @@ -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" - "github.com/stackitcloud/stackit-cli/internal/pkg/spinner" - - "github.com/spf13/cobra" - "github.com/stackitcloud/stackit-sdk-go/services/argus" - "github.com/stackitcloud/stackit-sdk-go/services/argus/wait" -) - -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("delete %s", jobNameArg), - Short: "Deletes an Argus Scrape Config", - Long: "Deletes an Argus Scrape Config.", - Args: args.SingleArg(jobNameArg, nil), - Example: examples.Build( - examples.NewExample( - `Delete an Argus Scrape config with name "my-config" from Argus instance "xxx"`, - "$ stackit argus scrape-configs delete my-config --instance-id xxx"), - ), - 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 - } - - if !model.AssumeYes { - prompt := fmt.Sprintf("Are you sure you want to delete Scrape Config %q? (This cannot be undone)", model.JobName) - 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 Scrape Config: %w", err) - } - - // Wait for async operation, if async mode not enabled - if !model.Async { - s := spinner.New(p) - s.Start("Deleting scrape config") - _, err = wait.DeleteScrapeConfigWaitHandler(ctx, apiClient, model.InstanceId, model.JobName, model.ProjectId).WaitWithContext(ctx) - if err != nil { - return fmt.Errorf("wait for Scrape Config deletion: %w", err) - } - s.Stop() - } - - operationState := "Deleted" - if model.Async { - operationState = "Triggered deletion of" - } - p.Info("%s Scrape Config %q\n", operationState, model.JobName) - 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) { - clusterName := inputArgs[0] - - globalFlags := globalflags.Parse(cmd) - if globalFlags.ProjectId == "" { - return nil, &errors.ProjectIdError{} - } - - return &inputModel{ - GlobalFlagModel: globalFlags, - JobName: clusterName, - InstanceId: flags.FlagToStringValue(cmd, instanceIdFlag), - }, nil -} - -func buildRequest(ctx context.Context, model *inputModel, apiClient *argus.APIClient) argus.ApiDeleteScrapeConfigRequest { - req := apiClient.DeleteScrapeConfig(ctx, model.InstanceId, model.JobName, model.ProjectId) - return req -} diff --git a/internal/cmd/argus/scrape-configs/delete/delete_test.go b/internal/cmd/argus/scrape-configs/delete/delete_test.go deleted file mode 100644 index f6f7ae5b6..000000000 --- a/internal/cmd/argus/scrape-configs/delete/delete_test.go +++ /dev/null @@ -1,233 +0,0 @@ -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() -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.ApiDeleteScrapeConfigRequest)) argus.ApiDeleteScrapeConfigRequest { - request := testClient.DeleteScrapeConfig(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.ApiDeleteScrapeConfigRequest - }{ - { - 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) - } - }) - } -} From 84eef117af8c33c7a595f567e3eee9c50d5032da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diogo=20Ferr=C3=A3o?= Date: Mon, 22 Apr 2024 17:43:42 +0200 Subject: [PATCH 4/9] generate docs --- docs/stackit_argus_scrape-config.md | 1 + docs/stackit_argus_scrape-config_update.md | 51 +++++++++++++++++++++ docs/stackit_argus_scrape-configs_delete.md | 40 ---------------- 3 files changed, 52 insertions(+), 40 deletions(-) create mode 100644 docs/stackit_argus_scrape-config_update.md delete mode 100644 docs/stackit_argus_scrape-configs_delete.md diff --git a/docs/stackit_argus_scrape-config.md b/docs/stackit_argus_scrape-config.md index 42927dd0d..d888ab89d 100644 --- a/docs/stackit_argus_scrape-config.md +++ b/docs/stackit_argus_scrape-config.md @@ -32,4 +32,5 @@ stackit argus scrape-config [flags] * [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 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 update](./stackit_argus_scrape-config_update.md) - Updates a scrape configuration of an Argus instance diff --git a/docs/stackit_argus_scrape-config_update.md b/docs/stackit_argus_scrape-config_update.md new file mode 100644 index 000000000..6b8722fe1 --- /dev/null +++ b/docs/stackit_argus_scrape-config_update.md @@ -0,0 +1,51 @@ +## stackit argus scrape-config update + +Updates a scrape configuration of an Argus instance + +### Synopsis + +Updates a scrape configuration of an Argus instance. +The payload can be provided as a JSON string or a file path prefixed with "@". +See https://docs.api.stackit.cloud/documentation/argus/version/v1#tag/scrape-config/operation/v1_projects_instances_scrapeconfigs_partial_update for information regarding the payload structure. + +``` +stackit argus scrape-config update JOB_NAME [flags] +``` + +### Examples + +``` + Update a scrape configuration from Argus instance "xxx", using an API payload sourced from the file "./payload.json" + $ stackit argus scrape-config update my-config --payload @./payload.json --instance-id xxx + + Update an scrape configuration from Argus instance "xxx", using an API payload provided as a JSON string + $ stackit argus scrape-config update my-config --payload "{...}" --instance-id xxx + + Generate a payload with the current values of a scrape configuration, and adapt it with custom values for the different configuration options + $ stackit argus scrape-config generate-payload --job-name my-config > ./payload.json + + $ stackit argus scrape-configs update my-config --payload @./payload.json +``` + +### Options + +``` + -h, --help Help for "stackit argus scrape-config update" + --instance-id string Instance ID + --payload string Request payload (JSON). Can be a string or a file path, if prefixed with "@". Example: @./payload.json +``` + +### 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-configs_delete.md b/docs/stackit_argus_scrape-configs_delete.md deleted file mode 100644 index 5886abbde..000000000 --- a/docs/stackit_argus_scrape-configs_delete.md +++ /dev/null @@ -1,40 +0,0 @@ -## stackit argus scrape-configs delete - -Deletes an Argus Scrape Config - -### Synopsis - -Deletes an Argus Scrape Config. - -``` -stackit argus scrape-configs delete JOB_NAME [flags] -``` - -### Examples - -``` - Delete an Argus Scrape config with name "my-config" from Argus instance "xxx" - $ stackit argus scrape-configs delete my-config --instance-id xxx -``` - -### Options - -``` - -h, --help Help for "stackit argus scrape-configs 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 scrape-configs](./stackit_argus_scrape-configs.md) - Provides functionality for scrape configs in Argus. - From d6672b1b1c149f9513bcede97119be94e5daefbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diogo=20Ferr=C3=A3o?= Date: Mon, 22 Apr 2024 17:45:04 +0200 Subject: [PATCH 5/9] cleanup --- internal/cmd/argus/scrape-config/update/update.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/internal/cmd/argus/scrape-config/update/update.go b/internal/cmd/argus/scrape-config/update/update.go index 095914689..2d1c70365 100644 --- a/internal/cmd/argus/scrape-config/update/update.go +++ b/internal/cmd/argus/scrape-config/update/update.go @@ -83,7 +83,7 @@ func NewCmd(p *print.Printer) *cobra.Command { } // The API has no status to wait on, so async mode is default - p.Info("Triggered update of Argus scrape configuration with name %q\n", model.JobName) + p.Info("Updated Argus scrape configuration with name %q\n", model.JobName) return nil }, } @@ -114,8 +114,6 @@ func parseInput(cmd *cobra.Command, inputArgs []string) (*inputModel, error) { return nil, fmt.Errorf("encode payload: %w", err) } - fmt.Println("Payload: ", payload) - return &inputModel{ GlobalFlagModel: globalFlags, JobName: clusterName, From 30e8c9c2a240669f80fb477c5a34bd74d35ffbe7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diogo=20Ferr=C3=A3o?= Date: Mon, 22 Apr 2024 17:54:33 +0200 Subject: [PATCH 6/9] remove json files --- update.json | 20 -------------------- 1 file changed, 20 deletions(-) delete mode 100644 update.json diff --git a/update.json b/update.json deleted file mode 100644 index dd3f17b4c..000000000 --- a/update.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "honorLabels": false, - "honorTimeStamps": false, - "metricsPath": "/metrics123", - "params": {}, - "sampleLimit": 5000, - "scheme": "https", - "scrapeInterval": "5m", - "scrapeTimeout": "2m", - "staticConfigs": [ - { - "labels": { - "job": "prometheus" - }, - "targets": [ - "localhost:9090" - ] - } - ] -} From 103a8bc09eb5d6ac7e68f5ed25daf0f6f7e06662 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diogo=20Ferr=C3=A3o?= Date: Tue, 23 Apr 2024 10:30:20 +0200 Subject: [PATCH 7/9] Address PR comments --- internal/cmd/argus/scrape-config/update/update.go | 7 +++---- internal/cmd/argus/scrape-config/update/update_test.go | 10 ++++++++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/internal/cmd/argus/scrape-config/update/update.go b/internal/cmd/argus/scrape-config/update/update.go index 2d1c70365..8d03ce6ca 100644 --- a/internal/cmd/argus/scrape-config/update/update.go +++ b/internal/cmd/argus/scrape-config/update/update.go @@ -38,15 +38,15 @@ func NewCmd(p *print.Printer) *cobra.Command { Long: fmt.Sprintf("%s\n%s\n%s", "Updates a scrape configuration of an Argus instance.", "The payload can be provided as a JSON string or a file path prefixed with \"@\".", - "See https://docs.api.stackit.cloud/documentation/argus/version/v1#tag/scrape-config/operation/v1_projects_instances_scrapeconfigs_partial_update for information regarding the payload structure.", + "See https://docs.api.stackit.cloud/documentation/argus/version/v1#tag/scrape-config/operation/v1_projects_instances_scrapeconfigs_update for information regarding the payload structure.", ), Args: args.SingleArg(jobNameArg, nil), Example: examples.Build( examples.NewExample( - `Update a scrape configuration from Argus instance "xxx", using an API payload sourced from the file "./payload.json"`, + `Update a scrape configuration with name "my-config" from Argus instance "xxx", using an API payload sourced from the file "./payload.json"`, "$ stackit argus scrape-config update my-config --payload @./payload.json --instance-id xxx"), examples.NewExample( - `Update an scrape configuration from Argus instance "xxx", using an API payload provided as a JSON string`, + `Update an scrape configuration with name "my-config" from Argus instance "xxx", using an API payload provided as a JSON string`, `$ stackit argus scrape-config update my-config --payload "{...}" --instance-id xxx`), examples.NewExample( `Generate a payload with the current values of a scrape configuration, and adapt it with custom values for the different configuration options`, @@ -82,7 +82,6 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("update scrape config: %w", err) } - // The API has no status to wait on, so async mode is default p.Info("Updated Argus scrape configuration with name %q\n", model.JobName) return nil }, diff --git a/internal/cmd/argus/scrape-config/update/update_test.go b/internal/cmd/argus/scrape-config/update/update_test.go index 87a8b7fa1..c89bc2db1 100644 --- a/internal/cmd/argus/scrape-config/update/update_test.go +++ b/internal/cmd/argus/scrape-config/update/update_test.go @@ -206,8 +206,14 @@ func TestParseInput(t *testing.T) { flagValues: fixtureFlagValues(func(flagValues map[string]string) { flagValues[payloadFlag] = "not json" }), - isValid: false, - expectedModel: fixtureInputModel(), + isValid: false, + }, + { + description: "payload missing", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + delete(flagValues, payloadFlag) + }), + isValid: false, }, } From fff2ae75a13113d1ed2bdcfe0d91a6db2f1dbb78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diogo=20Ferr=C3=A3o?= Date: Tue, 23 Apr 2024 10:31:48 +0200 Subject: [PATCH 8/9] generate docs --- docs/stackit_argus_scrape-config_update.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/stackit_argus_scrape-config_update.md b/docs/stackit_argus_scrape-config_update.md index 6b8722fe1..253e1732e 100644 --- a/docs/stackit_argus_scrape-config_update.md +++ b/docs/stackit_argus_scrape-config_update.md @@ -6,7 +6,7 @@ Updates a scrape configuration of an Argus instance Updates a scrape configuration of an Argus instance. The payload can be provided as a JSON string or a file path prefixed with "@". -See https://docs.api.stackit.cloud/documentation/argus/version/v1#tag/scrape-config/operation/v1_projects_instances_scrapeconfigs_partial_update for information regarding the payload structure. +See https://docs.api.stackit.cloud/documentation/argus/version/v1#tag/scrape-config/operation/v1_projects_instances_scrapeconfigs_update for information regarding the payload structure. ``` stackit argus scrape-config update JOB_NAME [flags] @@ -15,10 +15,10 @@ stackit argus scrape-config update JOB_NAME [flags] ### Examples ``` - Update a scrape configuration from Argus instance "xxx", using an API payload sourced from the file "./payload.json" + Update a scrape configuration with name "my-config" from Argus instance "xxx", using an API payload sourced from the file "./payload.json" $ stackit argus scrape-config update my-config --payload @./payload.json --instance-id xxx - Update an scrape configuration from Argus instance "xxx", using an API payload provided as a JSON string + Update an scrape configuration with name "my-config" from Argus instance "xxx", using an API payload provided as a JSON string $ stackit argus scrape-config update my-config --payload "{...}" --instance-id xxx Generate a payload with the current values of a scrape configuration, and adapt it with custom values for the different configuration options From f42ccea35d14cfaa138bd649638c2eb144b23456 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diogo=20Ferr=C3=A3o?= Date: Tue, 23 Apr 2024 10:49:46 +0200 Subject: [PATCH 9/9] add comment about async method --- internal/cmd/argus/scrape-config/update/update.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/cmd/argus/scrape-config/update/update.go b/internal/cmd/argus/scrape-config/update/update.go index 8d03ce6ca..25bdeb908 100644 --- a/internal/cmd/argus/scrape-config/update/update.go +++ b/internal/cmd/argus/scrape-config/update/update.go @@ -82,6 +82,7 @@ func NewCmd(p *print.Printer) *cobra.Command { return fmt.Errorf("update scrape config: %w", err) } + // The API has no status to wait on, so async mode is default p.Info("Updated Argus scrape configuration with name %q\n", model.JobName) return nil },