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
8 changes: 6 additions & 2 deletions docs/stackit_argus_scrape-config_generate-payload.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,23 @@ stackit argus scrape-config generate-payload [flags]

```
Generate a Create payload with default values, and adapt it with custom values for the different configuration options
$ stackit argus scrape-config generate-payload > ./payload.json
$ stackit argus scrape-config generate-payload --file-path ./payload.json
<Modify payload in file, if needed>
$ stackit argus scrape-config 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-config generate-payload --job-name my-config --instance-id xxx > ./payload.json
$ stackit argus scrape-config generate-payload --job-name my-config --instance-id xxx --file-path ./payload.json
<Modify payload in file>
$ stackit argus scrape-config update my-config --payload @./payload.json

Generate an Update payload with the values of an existing configuration named "my-config" for Argus instance xxx, and preview it in the terminal
$ stackit argus scrape-config generate-payload --job-name my-config --instance-id xxx
```

### Options

```
-f, --file-path string If set, writes the payload to the given file. If unset, writes the payload to the standard output
-h, --help Help for "stackit argus scrape-config 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
Expand Down
12 changes: 8 additions & 4 deletions docs/stackit_load-balancer_generate-payload.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,25 @@ stackit load-balancer generate-payload [flags]

```
Generate a payload, and adapt it with custom values for the different configuration options
$ stackit load-balancer generate-payload > ./payload.json
$ stackit load-balancer generate-payload --file-path ./payload.json
<Modify payload in file, if needed>
$ stackit load-balancer create --payload @./payload.json

Generate a payload with values of an existing load balancer, and adapt it with custom values for the different configuration options
$ stackit load-balancer generate-payload --lb-name xxx > ./payload.json
$ stackit load-balancer generate-payload --lb-name xxx --file-path ./payload.json
<Modify payload in file>
$ stackit load-balancer update xxx --payload @./payload.json

Generate a payload with values of an existing load balancer, and preview it in the terminal
$ stackit load-balancer generate-payload --lb-name xxx
```

### Options

```
-h, --help Help for "stackit load-balancer generate-payload"
-n, --lb-name string If set, generates the payload with the current values of the given load balancer. If unset, generates the payload with empty values
-f, --file-path string If set, writes the payload to the given file. If unset, writes the payload to the standard output
-h, --help Help for "stackit load-balancer generate-payload"
-n, --lb-name string If set, generates the payload with the current values of the given load balancer. If unset, generates the payload with empty values
```

### Options inherited from parent commands
Expand Down
8 changes: 6 additions & 2 deletions docs/stackit_ske_cluster_generate-payload.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,24 @@ stackit ske cluster generate-payload [flags]

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

Generate a payload with values of a cluster, and adapt it with custom values for the different configuration options
$ stackit ske cluster generate-payload --cluster-name my-cluster > ./payload.json
$ stackit ske cluster generate-payload --cluster-name my-cluster --file-path ./payload.json
<Modify payload in file>
$ stackit ske cluster update my-cluster --payload @./payload.json

Generate a payload with values of a cluster, and preview it in the terminal
$ stackit ske cluster generate-payload --cluster-name my-cluster
```

### Options

```
-n, --cluster-name string If set, generates the payload with the current state of the given cluster. If unset, generates the payload with default values
-f, --file-path string If set, writes the payload to the given file. If unset, writes the payload to the standard output
-h, --help Help for "stackit ske cluster generate-payload"
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/stackitcloud/stackit-cli/internal/pkg/args"
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
"github.com/stackitcloud/stackit-cli/internal/pkg/fileutils"
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
Expand All @@ -20,12 +21,14 @@ import (
const (
jobNameFlag = "job-name"
instanceIdFlag = "instance-id"
filePathFlag = "file-path"
)

type inputModel struct {
*globalflags.GlobalFlagModel
JobName *string
InstanceId string
FilePath *string
}

func NewCmd(p *print.Printer) *cobra.Command {
Expand All @@ -44,14 +47,17 @@ func NewCmd(p *print.Printer) *cobra.Command {
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-config generate-payload > ./payload.json`,
`$ stackit argus scrape-config generate-payload --file-path ./payload.json`,
`<Modify payload in file, if needed>`,
`$ stackit argus scrape-config create my-config --payload @./payload.json`),
Comment thread
GokceGK marked this conversation as resolved.
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-config generate-payload --job-name my-config --instance-id xxx > ./payload.json`,
`$ stackit argus scrape-config generate-payload --job-name my-config --instance-id xxx --file-path ./payload.json`,
`<Modify payload in file>`,
`$ stackit argus scrape-config update 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 preview it in the terminal`,
`$ stackit argus scrape-config generate-payload --job-name my-config --instance-id xxx`),
),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
Expand All @@ -68,7 +74,7 @@ func NewCmd(p *print.Printer) *cobra.Command {

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

req := buildRequest(ctx, model, apiClient)
Expand All @@ -82,7 +88,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
return fmt.Errorf("map update scrape config payloads: %w", err)
}

return outputUpdateResult(p, payload)
return outputUpdateResult(p, model.FilePath, payload)
},
}
configureFlags(cmd)
Expand All @@ -92,6 +98,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
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")
cmd.Flags().StringP(filePathFlag, "f", "", "If set, writes the payload to the given file. If unset, writes the payload to the standard output")
}

func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
Expand All @@ -108,6 +115,7 @@ func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
GlobalFlagModel: globalFlags,
JobName: jobName,
InstanceId: flags.FlagToStringValue(p, cmd, instanceIdFlag),
FilePath: flags.FlagToStringPointer(p, cmd, filePathFlag),
}, nil
}

Expand All @@ -116,22 +124,38 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *argus.APICl
return req
}

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

if filePath != nil {
err = fileutils.WriteToFile(*filePath, string(payloadBytes))
if err != nil {
return fmt.Errorf("write payload to the file: %w", err)
}
} else {
p.Outputln(string(payloadBytes))
}

return nil
}

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

if filePath != nil {
err = fileutils.WriteToFile(*filePath, string(payloadBytes))
if err != nil {
return fmt.Errorf("write payload to the file: %w", err)
}
} else {
p.Outputln(string(payloadBytes))
}

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@ var testClient = &argus.APIClient{}
var testProjectId = uuid.NewString()
var testInstanceId = uuid.NewString()

const testJobName = "test-job-name"
const (
testJobName = "test-job-name"
testFilePath = "example-file"
)

func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string {
flagValues := map[string]string{
projectIdFlag: testProjectId,
instanceIdFlag: testInstanceId,
jobNameFlag: testJobName,
filePathFlag: testFilePath,
}
for _, mod := range mods {
mod(flagValues)
Expand All @@ -44,6 +48,7 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
},
InstanceId: testInstanceId,
JobName: utils.Ptr(testJobName),
FilePath: utils.Ptr(testFilePath),
}
for _, mod := range mods {
mod(model)
Expand Down Expand Up @@ -80,6 +85,16 @@ func TestParseInput(t *testing.T) {
GlobalFlagModel: &globalflags.GlobalFlagModel{Verbosity: globalflags.VerbosityDefault},
},
},
{
description: "file path missing",
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
delete(flagValues, filePathFlag)
}),
isValid: true,
expectedModel: fixtureInputModel(func(model *inputModel) {
model.FilePath = nil
}),
},
{
description: "job name missing",
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
Expand Down
40 changes: 32 additions & 8 deletions internal/cmd/load-balancer/generate-payload/generate_payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"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/fileutils"
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
Expand All @@ -20,11 +21,13 @@ import (

const (
loadBalancerNameFlag = "lb-name"
filePathFlag = "file-path"
)

type inputModel struct {
*globalflags.GlobalFlagModel
LoadBalancerName *string
FilePath *string
}

var (
Expand Down Expand Up @@ -118,14 +121,17 @@ func NewCmd(p *print.Printer) *cobra.Command {
Example: examples.Build(
examples.NewExample(
`Generate a payload, and adapt it with custom values for the different configuration options`,
`$ stackit load-balancer generate-payload > ./payload.json`,
`$ stackit load-balancer generate-payload --file-path ./payload.json`,
`<Modify payload in file, if needed>`,
`$ stackit load-balancer create --payload @./payload.json`),
examples.NewExample(
`Generate a payload with values of an existing load balancer, and adapt it with custom values for the different configuration options`,
`$ stackit load-balancer generate-payload --lb-name xxx > ./payload.json`,
`$ stackit load-balancer generate-payload --lb-name xxx --file-path ./payload.json`,
`<Modify payload in file>`,
`$ stackit load-balancer update xxx --payload @./payload.json`),
examples.NewExample(
`Generate a payload with values of an existing load balancer, and preview it in the terminal`,
`$ stackit load-balancer generate-payload --lb-name xxx`),
),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
Expand All @@ -142,7 +148,7 @@ func NewCmd(p *print.Printer) *cobra.Command {

if model.LoadBalancerName == nil {
createPayload := DefaultCreateLoadBalancerPayload
return outputCreateResult(p, &createPayload)
return outputCreateResult(p, model.FilePath, &createPayload)
}

req := buildRequest(ctx, model, apiClient)
Expand All @@ -162,7 +168,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
TargetPools: resp.TargetPools,
Version: resp.Version,
}
return outputUpdateResult(p, updatePayload)
return outputUpdateResult(p, model.FilePath, updatePayload)
},
}
configureFlags(cmd)
Expand All @@ -171,6 +177,7 @@ func NewCmd(p *print.Printer) *cobra.Command {

func configureFlags(cmd *cobra.Command) {
cmd.Flags().StringP(loadBalancerNameFlag, "n", "", "If set, generates the payload with the current values of the given load balancer. If unset, generates the payload with empty values")
cmd.Flags().StringP(filePathFlag, "f", "", "If set, writes the payload to the given file. If unset, writes the payload to the standard output")
}

func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
Expand All @@ -185,6 +192,7 @@ func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
model := inputModel{
GlobalFlagModel: globalFlags,
LoadBalancerName: loadBalancerName,
FilePath: flags.FlagToStringPointer(p, cmd, filePathFlag),
}

if p.IsVerbosityDebug() {
Expand All @@ -204,22 +212,38 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *loadbalance
return req
}

func outputCreateResult(p *print.Printer, payload *loadbalancer.CreateLoadBalancerPayload) error {
func outputCreateResult(p *print.Printer, filePath *string, payload *loadbalancer.CreateLoadBalancerPayload) error {
payloadBytes, err := json.MarshalIndent(*payload, "", " ")
if err != nil {
return fmt.Errorf("marshal create load balancer payload: %w", err)
}
p.Outputln(string(payloadBytes))

if filePath != nil {
err = fileutils.WriteToFile(*filePath, string(payloadBytes))
if err != nil {
return fmt.Errorf("write create load balancer payload to the file: %w", err)
}
} else {
p.Outputln(string(payloadBytes))
}

return nil
}

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

if filePath != nil {
err = fileutils.WriteToFile(*filePath, string(payloadBytes))
if err != nil {
return fmt.Errorf("write update load balancer payload to the file: %w", err)
}
} else {
p.Outputln(string(payloadBytes))
}

return nil
}
Expand Down
Loading