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
13 changes: 0 additions & 13 deletions docs/stackit.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,22 @@ stackit [flags]

### SEE ALSO

* [stackit argus](./stackit_argus.md) - Provides functionality for Argus
* [stackit argus](./stackit_argus.md) - Provides functionality for Argus
* [stackit auth](./stackit_auth.md) - Provides authentication functionality
* [stackit config](./stackit_config.md) - Provides functionality for CLI configuration options
* [stackit curl](./stackit_curl.md) - Executes an authenticated HTTP request to an endpoint
* [stackit dns](./stackit_dns.md) - Provides functionality for DNS
* [stackit logme](./stackit_logme.md) - Provides functionality for LogMe
* [stackit logme](./stackit_logme.md) - Provides functionality for LogMe
* [stackit mariadb](./stackit_mariadb.md) - Provides functionality for MariaDB
* [stackit mariadb](./stackit_mariadb.md) - Provides functionality for MariaDB
* [stackit mongodbflex](./stackit_mongodbflex.md) - Provides functionality for MongoDB Flex
* [stackit mongodbflex](./stackit_mongodbflex.md) - Provides functionality for MongoDB Flex
* [stackit object-storage](./stackit_object-storage.md) - Provides functionality regarding Object Storage
* [stackit object-storage](./stackit_object-storage.md) - Provides functionality regarding Object Storage
* [stackit opensearch](./stackit_opensearch.md) - Provides functionality for OpenSearch
* [stackit opensearch](./stackit_opensearch.md) - Provides functionality for OpenSearch
* [stackit organization](./stackit_organization.md) - Provides functionality regarding organizations
* [stackit organization](./stackit_organization.md) - Provides functionality regarding organizations
* [stackit postgresflex](./stackit_postgresflex.md) - Provides functionality for PostgreSQL Flex
* [stackit postgresflex](./stackit_postgresflex.md) - Provides functionality for PostgreSQL Flex
* [stackit project](./stackit_project.md) - Provides functionality regarding projects
* [stackit rabbitmq](./stackit_rabbitmq.md) - Provides functionality for RabbitMQ
* [stackit rabbitmq](./stackit_rabbitmq.md) - Provides functionality for RabbitMQ
* [stackit redis](./stackit_redis.md) - Provides functionality for Redis
* [stackit redis](./stackit_redis.md) - Provides functionality for Redis
* [stackit secrets-manager](./stackit_secrets-manager.md) - Provides functionality for Secrets Manager
* [stackit secrets-manager](./stackit_secrets-manager.md) - Provides functionality for Secrets Manager
* [stackit service-account](./stackit_service-account.md) - Provides functionality for service accounts
* [stackit service-account](./stackit_service-account.md) - Provides functionality for service accounts
* [stackit ske](./stackit_ske.md) - Provides functionality for SKE
* [stackit ske](./stackit_ske.md) - Provides functionality for SKE

1 change: 1 addition & 0 deletions docs/stackit_argus.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ stackit argus [flags]
* [stackit](./stackit.md) - Manage STACKIT resources using the command line
* [stackit argus instance](./stackit_argus_instance.md) - Provides functionality for Argus instances
* [stackit argus plans](./stackit_argus_plans.md) - Lists all Argus service plans
* [stackit argus scrape-configs](./stackit_argus_scrape-configs.md) - Provides functionality for scrape configs in Argus.

33 changes: 33 additions & 0 deletions docs/stackit_argus_scrape-configs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
## stackit argus scrape-configs

Provides functionality for scrape configs in Argus.

### Synopsis

Provides functionality for scrape configurations in Argus.

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

### Options

```
-h, --help Help for "stackit argus scrape-configs"
```

### 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](./stackit_argus.md) - Provides functionality for Argus
* [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

54 changes: 54 additions & 0 deletions docs/stackit_argus_scrape-configs_generate-payload.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
## stackit argus scrape-configs generate-payload

Generates a payload to create/update Scrape Configurations for an Argus instance

### Synopsis

Generates a JSON payload with values to be used as --payload input for Scrape Configurations creation or update.
This command can be used to generate a payload to update an existing Scrape Config job or to create a new Scrape Config job.
To update an existing Scrape Config job, provide the job name and the instance ID of the Argus instance.
To obtain a default payload to create a new Scrape Config job, run the command with no flags.
Note that the default values provided, particularly for the job name, the metrics path and URL of the targets, should be changed to your use case.
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.


```
stackit argus scrape-configs generate-payload [flags]
```

### Examples

```
Generate a Create payload with default values, and adapt it with custom values for the different configuration options
$ stackit argus scrape-configs generate-payload > ./payload.json
<Modify payload in file, if needed>
$ stackit argus scrape-configs create my-config --payload @./payload.json

Generate an Update payload with the values of an existing configuration named "my-config" for Argus instance xxx, and adapt it with custom values for the different configuration options
$ stackit argus scrape-configs generate-payload --job-name my-config --instance-id xxx > ./payload.json
<Modify payload in file>
$ stackit argus scrape-configs update my-config --payload @./payload.json
```

### Options

```
-h, --help Help for "stackit argus scrape-configs generate-payload"
--instance-id string Instance ID
-n, --job-name string If set, generates an update payload with the current state of the given scrape config. If unset, generates a create payload with default values
```

### 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.

2 changes: 2 additions & 0 deletions internal/cmd/argus/argus.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +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"
"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 @@ -25,4 +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))
}
137 changes: 137 additions & 0 deletions internal/cmd/argus/scrape-configs/generate-payload/generate_payload.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package generatepayload

import (
"context"
"encoding/json"
"fmt"

"github.com/stackitcloud/stackit-cli/internal/pkg/args"
"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/spf13/cobra"
"github.com/stackitcloud/stackit-sdk-go/services/argus"
)

const (
jobNameFlag = "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: "generate-payload",
Short: "Generates a payload to create/update Scrape Configurations for an Argus instance ",
Long: fmt.Sprintf("%s\n%s\n%s\n%s\n%s\n%s\n",
"Generates a JSON payload with values to be used as --payload input for Scrape Configurations creation or update.",
"This command can be used to generate a payload to update an existing Scrape Config job or to create a new Scrape Config job.",
"To update an existing Scrape Config job, provide the job name and the instance ID of the Argus instance.",
"To obtain a default payload to create a new Scrape Config job, run the command with no flags.",
"Note that some of the default values provided, such as the job name, the metrics path and URL of the targets, should be adapted to your use case.",
"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(
`Generate a Create payload with default values, and adapt it with custom values for the different configuration options`,
`$ stackit argus scrape-configs generate-payload > ./payload.json`,
`<Modify payload in file, if needed>`,
`$ stackit argus scrape-configs create my-config --payload @./payload.json`),
examples.NewExample(
`Generate an Update payload with the values of an existing configuration named "my-config" for Argus instance xxx, and adapt it with custom values for the different configuration options`,
`$ stackit argus scrape-configs generate-payload --job-name my-config --instance-id xxx > ./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)
if err != nil {
return err
}

// Configure API client
apiClient, err := client.ConfigureClient(p)
if err != nil {
return err
}

if model.JobName == nil {
createPayload := argusUtils.DefaultCreateScrapeConfigPayload
return outputCreateResult(p, &createPayload)
}

req := buildRequest(ctx, model, apiClient)
resp, err := req.Execute()
if err != nil {
return fmt.Errorf("read Argus Scrape Config: %w", err)
}

payload, err := argusUtils.MapToUpdateScrapeConfigPayload(resp)
if err != nil {
return fmt.Errorf("map update scrape config payloads: %w", err)
}

return outputUpdateResult(p, payload)
},
}
configureFlags(cmd)
return cmd
}

func configureFlags(cmd *cobra.Command) {
cmd.Flags().Var(flags.UUIDFlag(), instanceIdFlag, "Instance ID")
cmd.Flags().StringP(jobNameFlag, "n", "", "If set, generates an update payload with the current state of the given scrape config. If unset, generates a create payload with default values")
}

func parseInput(cmd *cobra.Command) (*inputModel, error) {
globalFlags := globalflags.Parse(cmd)

jobName := flags.FlagToStringPointer(cmd, jobNameFlag)
instanceId := flags.FlagToStringValue(cmd, instanceIdFlag)

if jobName != nil && (globalFlags.ProjectId == "" || instanceId == "") {
return nil, fmt.Errorf("if a job-name is provided then instance-id and project-id must to be provided")
}

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 outputCreateResult(p *print.Printer, payload *argus.CreateScrapeConfigPayload) error {
payloadBytes, err := json.MarshalIndent(*payload, "", " ")
if err != nil {
return fmt.Errorf("marshal payload: %w", err)
}
p.Outputln(string(payloadBytes))

return nil
}

func outputUpdateResult(p *print.Printer, payload *argus.UpdateScrapeConfigPayload) error {
payloadBytes, err := json.MarshalIndent(*payload, "", " ")
if err != nil {
return fmt.Errorf("marshal payload: %w", err)
}
p.Outputln(string(payloadBytes))

return nil
}
Loading