Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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-configs.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ stackit argus scrape-configs [flags]

* [stackit argus](./stackit_argus.md) - Provides functionality for Argus
* [stackit argus scrape-configs create](./stackit_argus_scrape-configs_create.md) - Creates a Scrape Config job for an Argus instance
* [stackit argus scrape-configs delete](./stackit_argus_scrape-configs_delete.md) - Deletes a Scrape Config job from an Argus instance
* [stackit argus scrape-configs generate-payload](./stackit_argus_scrape-configs_generate-payload.md) - Generates a payload to create/update Scrape Configurations for an Argus instance

40 changes: 40 additions & 0 deletions docs/stackit_argus_scrape-configs_delete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
## stackit argus scrape-configs delete
Comment thread
DiogoFerrao marked this conversation as resolved.
Outdated

Deletes a Scrape Config job from an Argus instance

### Synopsis

Deletes a Scrape Config job from an Argus instance.

```
stackit argus scrape-configs delete JOB_NAME [flags]
```

### Examples

```
Delete a Scrape Config job 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.

4 changes: 2 additions & 2 deletions internal/cmd/argus/argus.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package argus
import (
"github.com/stackitcloud/stackit-cli/internal/cmd/argus/instance"
"github.com/stackitcloud/stackit-cli/internal/cmd/argus/plans"
scrapeconfigs "github.com/stackitcloud/stackit-cli/internal/cmd/argus/scrape-configs"
scrapeconfig "github.com/stackitcloud/stackit-cli/internal/cmd/argus/scrape-config"
"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 @@ -26,5 +26,5 @@ func NewCmd(p *print.Printer) *cobra.Command {
func addSubcommands(cmd *cobra.Command, p *print.Printer) {
cmd.AddCommand(plans.NewCmd(p))
cmd.AddCommand(instance.NewCmd(p))
cmd.AddCommand(scrapeconfigs.NewCmd(p))
cmd.AddCommand(scrapeconfig.NewCmd(p))
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,29 @@ type inputModel struct {
func NewCmd(p *print.Printer) *cobra.Command {
cmd := &cobra.Command{
Use: "create",
Short: "Creates a Scrape Config job for an Argus instance",
Short: "Creates a scrape configuration for an Argus instance",
Long: fmt.Sprintf("%s\n%s\n%s\n%s",
"Creates a Scrape Config job for an Argus instance.",
"Creates a scrape configuration job for an Argus instance.",
"The payload can be provided as a JSON string or a file path prefixed with \"@\".",
"If no payload is provided, a default payload will be used.",
"See https://docs.api.stackit.cloud/documentation/argus/version/v1#tag/scrape-config/operation/v1_projects_instances_scrapeconfigs_create for information regarding the payload structure.",
),
Args: args.NoArgs,
Example: examples.Build(
examples.NewExample(
`Create a Scrape Config job on Argus instance "xxx" using default configuration`,
"$ stackit argus scrape-configs create"),
`Create a scrape configuration on Argus instance "xxx" using default configuration`,
"$ stackit argus scrape-config create"),
examples.NewExample(
`Create a Scrape Config job on Argus instance "xxx" using an API payload sourced from the file "./payload.json"`,
"$ stackit argus scrape-configs create --payload @./payload.json --instance-id xxx"),
`Create a scrape configuration on Argus instance "xxx" using an API payload sourced from the file "./payload.json"`,
"$ stackit argus scrape-config create --payload @./payload.json --instance-id xxx"),
examples.NewExample(
`Create a Scrape Config job on Argus instance "xxx" using an API payload provided as a JSON string`,
`$ stackit argus scrape-configs create --payload "{...}" --instance-id xxx`),
`Create a scrape configuration on Argus instance "xxx" using an API payload provided as a JSON string`,
`$ stackit argus scrape-config create --payload "{...}" --instance-id xxx`),
examples.NewExample(
`Generate a payload with default values, and adapt it with custom values for the different configuration options`,
`$ stackit argus scrape-configs generate-payload > ./payload.json`,
`$ stackit argus scrape-config generate-payload > ./payload.json`,
`<Modify payload in file, if needed>`,
`$ stackit argus scrape-configs create --payload @./payload.json --instance-id xxx`),
`$ stackit argus scrape-config create --payload @./payload.json --instance-id xxx`),
),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
Expand All @@ -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 Config job on Argus instance %q?", instanceLabel)
prompt := fmt.Sprintf("Are you sure you want to create a scrape configuration on Argus instance %q?", instanceLabel)
err = p.PromptForConfirmation(prompt)
if err != nil {
return err
Expand All @@ -97,7 +97,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
req := buildRequest(ctx, model, apiClient)
_, err = req.Execute()
if err != nil {
return fmt.Errorf("create Scrape Config job: %w", err)
return fmt.Errorf("create scrape configuration: %w", err)
}

jobName := model.Payload.JobName
Expand All @@ -108,7 +108,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
s.Start("Creating scrape config")
_, err = wait.CreateScrapeConfigWaitHandler(ctx, apiClient, model.InstanceId, *jobName, model.ProjectId).WaitWithContext(ctx)
if err != nil {
return fmt.Errorf("wait for Scrape Config job creation: %w", err)
return fmt.Errorf("wait for scrape configuration creation: %w", err)
}
s.Stop()
}
Expand All @@ -117,7 +117,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
if model.Async {
operationState = "Triggered creation of"
}
p.Outputf("%s Scrape Config job with name %q for Argus instance %q\n", operationState, *jobName, instanceLabel)
p.Outputf("%s scrape configuration with name %q for Argus instance %q\n", operationState, *jobName, instanceLabel)
return nil
},
}
Expand All @@ -126,7 +126,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
}

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). If unset, will use a default payload (you can check it by running "stackit argus scrape-configs generate-payload")`)
cmd.Flags().Var(flags.ReadFromFileFlag(), payloadFlag, `Request payload (JSON). Can be a string or a file path, if prefixed with "@" (example: @./payload.json). If unset, will use a default payload (you can check it by running "stackit argus scrape-config generate-payload")`)
cmd.Flags().Var(flags.UUIDFlag(), instanceIdFlag, "Instance ID")

err := flags.MarkFlagsRequired(cmd, instanceIdFlag)
Expand Down
126 changes: 126 additions & 0 deletions internal/cmd/argus/scrape-config/delete/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
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-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 a scrape configuration from an Argus instance",
Long: "Deletes a scrape configuration from an Argus instance.",
Args: args.SingleArg(jobNameArg, nil),
Example: examples.Build(
examples.NewExample(
`Delete a scrape configuration job with name "my-config" from Argus instance "xxx"`,
"$ stackit argus scrape-config 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
}

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 scrape configuration %q on Argus instance %q? (This cannot be undone)", model.JobName, 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 scrape configuration: %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 configuration with name %q for Argus instance %q\n", operationState, model.JobName, 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) {
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.ApiDeleteScrapeConfigRequest {
req := apiClient.DeleteScrapeConfig(ctx, model.InstanceId, model.JobName, model.ProjectId)
return req
}
Loading