-
Notifications
You must be signed in to change notification settings - Fork 17
Onboard Load Balancer generate-payload command #250
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
GokceGK
merged 13 commits into
feature/onboard-load-balancer
from
gg/onboard-loadbalancer-generate-payload
Apr 29, 2024
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2a15a4a
onboard load-balancer generate-payload command
GokceGK 51e9128
add go mod
GokceGK 4e4d4a0
generate docs
GokceGK db675c4
remove version from the default payload which will be used for create…
GokceGK 6514e09
Merge branch 'feature/onboard-load-balancer' into gg/onboard-loadbala…
GokceGK 9379065
Update description
GokceGK 1847fe2
Update description
GokceGK 819f94b
split create and update payloads
GokceGK 5a72dd0
move default create payload to a const
GokceGK 64747ae
delete unused utils files
GokceGK d08e584
update docs
GokceGK b2a779c
remove redundant var declaration
GokceGK 1c1ea37
Merge branch 'feature/onboard-load-balancer' into gg/onboard-loadbala…
vicentepinto98 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,48 @@ | ||
## stackit load-balancer generate-payload | ||
|
||
Generates a payload to create/update a Load Balancer | ||
|
||
### Synopsis | ||
|
||
Generates a JSON payload with values to be used as --payload input for load balancer creation or update. | ||
See https://docs.api.stackit.cloud/documentation/load-balancer/version/v1#tag/Load-Balancer/operation/APIService_CreateLoadBalancer for information regarding the payload structure. | ||
|
||
``` | ||
stackit load-balancer generate-payload [flags] | ||
``` | ||
|
||
### Examples | ||
|
||
``` | ||
Generate a payload, and adapt it with custom values for the different configuration options | ||
$ stackit load-balancer generate-payload > ./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 --instance-name my-lb > ./payload.json | ||
<Modify payload in file> | ||
$ stackit load-balancer update my-lb --payload @./payload.json | ||
``` | ||
|
||
### Options | ||
|
||
``` | ||
-h, --help Help for "stackit load-balancer generate-payload" | ||
-n, --instance-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 | ||
|
||
``` | ||
-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" "none"] | ||
-p, --project-id string Project ID | ||
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info") | ||
``` | ||
|
||
### SEE ALSO | ||
|
||
* [stackit load-balancer](./stackit_load-balancer.md) - Provides functionality for Load Balancer | ||
|
214 changes: 214 additions & 0 deletions
214
internal/cmd/load-balancer/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,214 @@ | ||
package generatepayload | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"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/load-balancer/client" | ||
"github.com/stackitcloud/stackit-cli/internal/pkg/utils" | ||
"github.com/stackitcloud/stackit-sdk-go/services/loadbalancer" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
const ( | ||
instanceNameFlag = "instance-name" | ||
) | ||
|
||
type inputModel struct { | ||
*globalflags.GlobalFlagModel | ||
InstanceName *string | ||
} | ||
|
||
var ( | ||
defaultPayloadListener = &loadbalancer.Listener{ | ||
DisplayName: utils.Ptr(""), | ||
Name: utils.Ptr(""), | ||
Port: utils.Ptr(int64(0)), | ||
Protocol: utils.Ptr(""), | ||
ServerNameIndicators: &[]loadbalancer.ServerNameIndicator{ | ||
{ | ||
Name: utils.Ptr(""), | ||
}, | ||
}, | ||
TargetPool: utils.Ptr(""), | ||
Tcp: &loadbalancer.OptionsTCP{ | ||
IdleTimeout: utils.Ptr(""), | ||
}, | ||
Udp: &loadbalancer.OptionsUDP{ | ||
IdleTimeout: utils.Ptr(""), | ||
}, | ||
} | ||
|
||
defaultPayloadNetwork = &loadbalancer.Network{ | ||
NetworkId: utils.Ptr(""), | ||
Role: utils.Ptr(""), | ||
} | ||
|
||
defaultPayloadTargetPool = &loadbalancer.TargetPool{ | ||
ActiveHealthCheck: &loadbalancer.ActiveHealthCheck{ | ||
HealthyThreshold: utils.Ptr(int64(0)), | ||
Interval: utils.Ptr(""), | ||
IntervalJitter: utils.Ptr(""), | ||
Timeout: utils.Ptr(""), | ||
UnhealthyThreshold: utils.Ptr(int64(0)), | ||
}, | ||
Name: utils.Ptr(""), | ||
SessionPersistence: &loadbalancer.SessionPersistence{ | ||
UseSourceIpAddress: utils.Ptr(false), | ||
}, | ||
TargetPort: utils.Ptr(int64(0)), | ||
Targets: &[]loadbalancer.Target{ | ||
{ | ||
DisplayName: utils.Ptr(""), | ||
Ip: utils.Ptr(""), | ||
}, | ||
}, | ||
} | ||
|
||
DefaultCreateLoadBalancerPayload = loadbalancer.CreateLoadBalancerPayload{ | ||
ExternalAddress: utils.Ptr(""), | ||
Listeners: &[]loadbalancer.Listener{ | ||
*defaultPayloadListener, | ||
}, | ||
Name: utils.Ptr(""), | ||
Networks: &[]loadbalancer.Network{ | ||
*defaultPayloadNetwork, | ||
}, | ||
Options: &loadbalancer.LoadBalancerOptions{ | ||
AccessControl: &loadbalancer.LoadbalancerOptionAccessControl{ | ||
AllowedSourceRanges: &[]string{ | ||
"", | ||
}, | ||
}, | ||
EphemeralAddress: utils.Ptr(false), | ||
Observability: &loadbalancer.LoadbalancerOptionObservability{ | ||
Logs: &loadbalancer.LoadbalancerOptionLogs{ | ||
CredentialsRef: utils.Ptr(""), | ||
PushUrl: utils.Ptr(""), | ||
}, | ||
Metrics: &loadbalancer.LoadbalancerOptionMetrics{ | ||
CredentialsRef: utils.Ptr(""), | ||
PushUrl: utils.Ptr(""), | ||
}, | ||
}, | ||
PrivateNetworkOnly: utils.Ptr(false), | ||
}, | ||
PrivateAddress: utils.Ptr(""), | ||
TargetPools: &[]loadbalancer.TargetPool{ | ||
*defaultPayloadTargetPool, | ||
}, | ||
} | ||
) | ||
|
||
func NewCmd(p *print.Printer) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "generate-payload", | ||
Short: "Generates a payload to create/update a Load Balancer", | ||
Long: fmt.Sprintf("%s\n%s", | ||
"Generates a JSON payload with values to be used as --payload input for load balancer creation or update.", | ||
"See https://docs.api.stackit.cloud/documentation/load-balancer/version/v1#tag/Load-Balancer/operation/APIService_CreateLoadBalancer for information regarding the payload structure.", | ||
), | ||
Args: args.NoArgs, | ||
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`, | ||
`<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 --instance-name my-lb > ./payload.json`, | ||
`<Modify payload in file>`, | ||
`$ stackit load-balancer update my-lb --payload @./payload.json`), | ||
), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
ctx := context.Background() | ||
model, err := parseInput(p, cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Configure API client | ||
apiClient, err := client.ConfigureClient(p) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if model.InstanceName == nil { | ||
createPayload := DefaultCreateLoadBalancerPayload | ||
return outputCreateResult(p, &createPayload) | ||
} | ||
|
||
req := buildRequest(ctx, model, apiClient) | ||
resp, err := req.Execute() | ||
if err != nil { | ||
return fmt.Errorf("read load balancer: %w", err) | ||
} | ||
updatePayload := &loadbalancer.UpdateLoadBalancerPayload{ | ||
ExternalAddress: resp.ExternalAddress, | ||
Listeners: resp.Listeners, | ||
Name: resp.Name, | ||
Networks: resp.Networks, | ||
Options: resp.Options, | ||
PrivateAddress: resp.PrivateAddress, | ||
TargetPools: resp.TargetPools, | ||
Version: resp.Version, | ||
} | ||
return outputUpdateResult(p, updatePayload) | ||
}, | ||
} | ||
configureFlags(cmd) | ||
return cmd | ||
} | ||
|
||
func configureFlags(cmd *cobra.Command) { | ||
cmd.Flags().StringP(instanceNameFlag, "n", "", "If set, generates the payload with the current values of the given load balancer. If unset, generates the payload with empty values") | ||
} | ||
|
||
func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) { | ||
globalFlags := globalflags.Parse(p, cmd) | ||
|
||
instanceName := flags.FlagToStringPointer(p, cmd, instanceNameFlag) | ||
// If instanceName is provided, projectId is needed as well | ||
if instanceName != nil && globalFlags.ProjectId == "" { | ||
return nil, &errors.ProjectIdError{} | ||
} | ||
|
||
return &inputModel{ | ||
GlobalFlagModel: globalFlags, | ||
InstanceName: instanceName, | ||
}, nil | ||
} | ||
|
||
func buildRequest(ctx context.Context, model *inputModel, apiClient *loadbalancer.APIClient) loadbalancer.ApiGetLoadBalancerRequest { | ||
req := apiClient.GetLoadBalancer(ctx, model.ProjectId, *model.InstanceName) | ||
return req | ||
} | ||
|
||
func outputCreateResult(p *print.Printer, 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)) | ||
|
||
return nil | ||
} | ||
|
||
func outputUpdateResult(p *print.Printer, 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)) | ||
|
||
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.