-
Notifications
You must be signed in to change notification settings - Fork 34
Onboard Argus scrape config: list and describe commands #240
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 12 commits into
feature/argus-scrape-config
from
df/argus-scrape-config-list-describe
Apr 23, 2024
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
906ec29
delete command implementation and testing
DiogoFerrao e4c6457
initial implementation
DiogoFerrao 6190512
finish implementation
DiogoFerrao 14c4631
remove json files
DiogoFerrao 5c34125
list command implementation and testing
DiogoFerrao 7700f74
describe command implementation and testing
DiogoFerrao e197945
verify pointer before dereference
DiogoFerrao 661fcc9
add config name to confirmation prompt in create cmd
DiogoFerrao ee523f0
generate docs
DiogoFerrao 34f67dd
improve describe output
DiogoFerrao f4ee437
improve describe output
DiogoFerrao 965dd61
address PR comments
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| ## stackit argus scrape-config describe | ||
|
|
||
| Shows details of a scrape configuration from an Argus instance | ||
|
|
||
| ### Synopsis | ||
|
|
||
| Shows details of a scrape configuration from an Argus instance. | ||
|
|
||
| ``` | ||
| stackit argus scrape-config describe JOB_NAME [flags] | ||
| ``` | ||
|
|
||
| ### Examples | ||
|
|
||
| ``` | ||
| Get details of a scrape configuration with name "my-config" from Argus instance "xxx" | ||
| $ stackit argus scrape-config describe my-config --instance-id xxx | ||
|
|
||
| Get details of a scrape configuration with name "my-config" from Argus instance "xxx" in a table format | ||
| $ stackit argus scrape-config describe my-config --output-format pretty | ||
| ``` | ||
|
|
||
| ### Options | ||
|
|
||
| ``` | ||
| -h, --help Help for "stackit argus scrape-config describe" | ||
| --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-config](./stackit_argus_scrape-config.md) - Provides functionality for scrape configurations 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| ## stackit argus scrape-config list | ||
|
|
||
| Lists all scrape configurations of an Argus instance | ||
|
|
||
| ### Synopsis | ||
|
|
||
| Lists all scrape configurations of an Argus instance. | ||
|
|
||
| ``` | ||
| stackit argus scrape-config list [flags] | ||
| ``` | ||
|
|
||
| ### Examples | ||
|
|
||
| ``` | ||
| List all scrape configurations of Argus instance "xxx" | ||
| $ stackit argus scrape-config list --instance-id xxx | ||
|
|
||
| List all scrape configurations of Argus instance "xxx" in JSON format | ||
| $ stackit argus scrape-config list --instance-id xxx --output-format json | ||
|
|
||
| List up to 10 scrape configurations of Argus instance "xxx" | ||
| $ stackit argus scrape-config list --instance-id xxx --limit 10 | ||
| ``` | ||
|
|
||
| ### Options | ||
|
|
||
| ``` | ||
| -h, --help Help for "stackit argus scrape-config list" | ||
| --instance-id string Instance ID | ||
| --limit int Maximum number of entries to list | ||
| ``` | ||
|
|
||
| ### 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 | ||
|
|
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,176 @@ | ||
| package describe | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "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/tables" | ||
| "github.com/stackitcloud/stackit-sdk-go/services/argus" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| 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("describe %s", jobNameArg), | ||
| Short: "Shows details of a scrape configuration from an Argus instance", | ||
| Long: "Shows details of a scrape configuration from an Argus instance.", | ||
| Args: args.SingleArg(jobNameArg, nil), | ||
| Example: examples.Build( | ||
| examples.NewExample( | ||
| `Get details of a scrape configuration with name "my-config" from Argus instance "xxx"`, | ||
| "$ stackit argus scrape-config describe my-config --instance-id xxx"), | ||
| examples.NewExample( | ||
| `Get details of a scrape configuration with name "my-config" from Argus instance "xxx" in a table format`, | ||
| "$ stackit argus scrape-config describe my-config --output-format pretty"), | ||
| ), | ||
| 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 | ||
| } | ||
|
|
||
| // Call API | ||
| req := buildRequest(ctx, model, apiClient) | ||
| resp, err := req.Execute() | ||
| if err != nil { | ||
| return fmt.Errorf("read scrape configuration: %w", err) | ||
| } | ||
|
|
||
| return outputResult(p, model.OutputFormat, resp.Data) | ||
| }, | ||
| } | ||
| 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.ApiGetScrapeConfigRequest { | ||
| req := apiClient.GetScrapeConfig(ctx, model.InstanceId, model.JobName, model.ProjectId) | ||
| return req | ||
| } | ||
|
|
||
| func outputResult(p *print.Printer, outputFormat string, config *argus.Job) error { | ||
| switch outputFormat { | ||
| case globalflags.PrettyOutputFormat: | ||
|
|
||
| saml2Enabled := "Enabled" | ||
| if config.Params != nil { | ||
| saml2 := (*config.Params)["saml2"] | ||
| if len(saml2) > 0 && saml2[0] == "disabled" { | ||
| saml2Enabled = "Disabled" | ||
| } | ||
| } | ||
|
|
||
| var targets []string | ||
| for _, target := range *config.StaticConfigs { | ||
| targetLabels := []string{} | ||
| targetLabelStr := "N/A" | ||
| if target.Labels != nil { | ||
| // make map prettier | ||
| for k, v := range *target.Labels { | ||
DiogoFerrao marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| targetLabels = append(targetLabels, fmt.Sprintf("%s:%s", k, v)) | ||
| } | ||
| if targetLabels != nil { | ||
| targetLabelStr = strings.Join(targetLabels, ",") | ||
| } | ||
| } | ||
| targetUrlsStr := "N/A" | ||
| if target.Targets != nil { | ||
| targetUrlsStr = strings.Join(*target.Targets, ",") | ||
| } | ||
| targets = append(targets, fmt.Sprintf("labels: %s\nurls: %s", targetLabelStr, targetUrlsStr)) | ||
| } | ||
|
|
||
| table := tables.NewTable() | ||
| table.AddRow("NAME", *config.JobName) | ||
| table.AddSeparator() | ||
| table.AddRow("METRICS PATH", *config.MetricsPath) | ||
| table.AddSeparator() | ||
| table.AddRow("SCHEME", *config.Scheme) | ||
| table.AddSeparator() | ||
| table.AddRow("SCRAPE INTERVAL", *config.ScrapeInterval) | ||
| table.AddSeparator() | ||
| table.AddRow("SCRAPE TIMEOUT", *config.ScrapeTimeout) | ||
| table.AddSeparator() | ||
| table.AddRow("SAML2", saml2Enabled) | ||
| table.AddSeparator() | ||
| if config.BasicAuth == nil { | ||
| table.AddRow("AUTHENTICATION", "None") | ||
| } else { | ||
| table.AddRow("AUTHENTICATION", "Basic Auth") | ||
| table.AddSeparator() | ||
| table.AddRow("USERNAME", *config.BasicAuth.Username) | ||
| table.AddSeparator() | ||
| table.AddRow("PASSWORD", *config.BasicAuth.Password) | ||
| } | ||
| table.AddSeparator() | ||
| for i, target := range targets { | ||
| table.AddRow(fmt.Sprintf("TARGET #%d", i+1), target) | ||
| table.AddSeparator() | ||
| } | ||
|
|
||
| err := table.Display(p) | ||
| if err != nil { | ||
| return fmt.Errorf("render table: %w", err) | ||
| } | ||
|
|
||
| return nil | ||
| default: | ||
| details, err := json.MarshalIndent(config, "", " ") | ||
| if err != nil { | ||
| return fmt.Errorf("marshal scrape configuration: %w", err) | ||
| } | ||
| p.Outputln(string(details)) | ||
|
|
||
| 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.