Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/stackit_argus_scrape-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

51 changes: 51 additions & 0 deletions docs/stackit_argus_scrape-config_update.md
Original file line number Diff line number Diff line change
@@ -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_update for information regarding the payload structure.

```
stackit argus scrape-config update JOB_NAME [flags]
```

### Examples

```
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 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
$ stackit argus scrape-config generate-payload --job-name my-config > ./payload.json
<Modify payload in file>
$ 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

2 changes: 2 additions & 0 deletions internal/cmd/argus/scrape-config/scrape_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -27,4 +28,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))
}
130 changes: 130 additions & 0 deletions internal/cmd/argus/scrape-config/update/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
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 configuration of an Argus instance",
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_update for information regarding the payload structure.",
),
Args: args.SingleArg(jobNameArg, nil),
Example: examples.Build(
examples.NewExample(
`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 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`,
`$ stackit argus scrape-config generate-payload --job-name my-config > ./payload.json`,
`<Modify payload in file>`,
`$ 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 configuration %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
p.Info("Updated Argus scrape configuration with name %q\n", 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
}
Loading