-
Notifications
You must be signed in to change notification settings - Fork 34
Onboard Argus Scrape Configs: generate-payload command #231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
DiogoFerrao
merged 10 commits into
feature/argus-scrape-config
from
df/argus-scrape-config-generate-payload
Apr 19, 2024
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a443ed0
Initial implementation
DiogoFerrao 63231b7
add utils, testing and make command work
DiogoFerrao 38aecfe
implement function to create default create config
DiogoFerrao 5a81e8e
update help, improve testing and remove verification when job name is…
DiogoFerrao 3604253
Improve testing
DiogoFerrao 918daf8
address PR comments
DiogoFerrao e0d7101
improve variable names
DiogoFerrao b1f35a7
update command help
DiogoFerrao 7613a43
address PR comments
DiogoFerrao 34efc7c
fix utils testing
DiogoFerrao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
internal/cmd/argus/scrape-configs/generate-payload/generate_payload.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.