Skip to content

Commit 5341bfa

Browse files
committed
Feat/stackitcli 183 onboarding alb project api (#700)
* feat(credentials) * feat(credentials): code cleanup * feat(credentials): updated documentation * feat(credentials): bug fixes * feat(credentials): updated docs * feat(credentials): fixed testcases * feat(credentials): code cleanup * feat(alb project): support for quota and plans * feat(alb project): updated documentation * feat(alb project): review findings
1 parent 4d75403 commit 5341bfa

File tree

10 files changed

+825
-0
lines changed

10 files changed

+825
-0
lines changed

docs/stackit_beta_alb.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ stackit beta alb [flags]
3535
* [stackit beta alb delete](./stackit_beta_alb_delete.md) - Deletes an application loadbalancer
3636
* [stackit beta alb describe](./stackit_beta_alb_describe.md) - Describes an application loadbalancer
3737
* [stackit beta alb list](./stackit_beta_alb_list.md) - Lists albs
38+
* [stackit beta alb plans](./stackit_beta_alb_plans.md) - Lists the application load balancer plans
3839
* [stackit beta alb pool](./stackit_beta_alb_pool.md) - Manages target pools for application loadbalancers
40+
* [stackit beta alb quotas](./stackit_beta_alb_quotas.md) - Shows the application load balancer quotas
3941
* [stackit beta alb template](./stackit_beta_alb_template.md) - creates configuration templates to use for resource creation
4042
* [stackit beta alb update](./stackit_beta_alb_update.md) - Updates an application loadbalancer
4143

docs/stackit_beta_alb_plans.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
## stackit beta alb plans
2+
3+
Lists the application load balancer plans
4+
5+
### Synopsis
6+
7+
Lists the available application load balancer plans.
8+
9+
```
10+
stackit beta alb plans [flags]
11+
```
12+
13+
### Examples
14+
15+
```
16+
List all application load balancer plans
17+
$ stackit beta alb plans
18+
```
19+
20+
### Options
21+
22+
```
23+
-h, --help Help for "stackit beta alb plans"
24+
```
25+
26+
### Options inherited from parent commands
27+
28+
```
29+
-y, --assume-yes If set, skips all confirmation prompts
30+
--async If set, runs the command asynchronously
31+
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"]
32+
-p, --project-id string Project ID
33+
--region string Target region for region-specific requests
34+
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
35+
```
36+
37+
### SEE ALSO
38+
39+
* [stackit beta alb](./stackit_beta_alb.md) - Manages application loadbalancers
40+

docs/stackit_beta_alb_quotas.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
## stackit beta alb quotas
2+
3+
Shows the application load balancer quotas
4+
5+
### Synopsis
6+
7+
Shows the application load balancer quotas for the application load balancers.
8+
9+
```
10+
stackit beta alb quotas [flags]
11+
```
12+
13+
### Examples
14+
15+
```
16+
List all application load balancer quotas
17+
$ stackit beta alb quotas
18+
```
19+
20+
### Options
21+
22+
```
23+
-h, --help Help for "stackit beta alb quotas"
24+
```
25+
26+
### Options inherited from parent commands
27+
28+
```
29+
-y, --assume-yes If set, skips all confirmation prompts
30+
--async If set, runs the command asynchronously
31+
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"]
32+
-p, --project-id string Project ID
33+
--region string Target region for region-specific requests
34+
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
35+
```
36+
37+
### SEE ALSO
38+
39+
* [stackit beta alb](./stackit_beta_alb.md) - Manages application loadbalancers
40+

internal/cmd/beta/alb/alb.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import (
66
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/alb/delete"
77
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/alb/describe"
88
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/alb/list"
9+
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/alb/plans"
910
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/alb/pool"
11+
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/alb/quotas"
1012
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/alb/template"
1113
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/alb/update"
1214
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
@@ -39,5 +41,7 @@ func addSubcommands(cmd *cobra.Command, p *print.Printer) {
3941
describe.NewCmd(p),
4042
delete.NewCmd(p),
4143
pool.NewCmd(p),
44+
plans.NewCmd(p),
45+
quotas.NewCmd(p),
4246
)
4347
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package plans
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
8+
"github.com/goccy/go-yaml"
9+
"github.com/spf13/cobra"
10+
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
11+
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
12+
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
13+
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
14+
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
15+
"github.com/stackitcloud/stackit-cli/internal/pkg/projectname"
16+
"github.com/stackitcloud/stackit-cli/internal/pkg/services/alb/client"
17+
"github.com/stackitcloud/stackit-cli/internal/pkg/tables"
18+
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
19+
"github.com/stackitcloud/stackit-sdk-go/services/alb"
20+
)
21+
22+
type inputModel struct {
23+
*globalflags.GlobalFlagModel
24+
}
25+
26+
func NewCmd(p *print.Printer) *cobra.Command {
27+
cmd := &cobra.Command{
28+
Use: "plans",
29+
Short: "Lists the application load balancer plans",
30+
Long: "Lists the available application load balancer plans.",
31+
Args: args.NoArgs,
32+
Example: examples.Build(
33+
examples.NewExample(
34+
`List all application load balancer plans`,
35+
`$ stackit beta alb plans`,
36+
),
37+
),
38+
RunE: func(cmd *cobra.Command, _ []string) error {
39+
ctx := context.Background()
40+
model, err := parseInput(p, cmd)
41+
if err != nil {
42+
return err
43+
}
44+
45+
// Configure API client
46+
apiClient, err := client.ConfigureClient(p)
47+
if err != nil {
48+
return err
49+
}
50+
51+
projectLabel, err := projectname.GetProjectName(ctx, p, cmd)
52+
if err != nil {
53+
p.Debug(print.ErrorLevel, "get project name: %v", err)
54+
projectLabel = model.ProjectId
55+
} else if projectLabel == "" {
56+
projectLabel = model.ProjectId
57+
}
58+
59+
// Call API
60+
request := buildRequest(ctx, model, apiClient)
61+
62+
response, err := request.Execute()
63+
if err != nil {
64+
return fmt.Errorf("list plans: %w", err)
65+
}
66+
67+
if items := response.ValidPlans; items == nil || len(*items) == 0 {
68+
p.Info("No plans found for project %q", projectLabel)
69+
} else {
70+
if err := outputResult(p, model.OutputFormat, *items); err != nil {
71+
return fmt.Errorf("output plans: %w", err)
72+
}
73+
}
74+
75+
return nil
76+
},
77+
}
78+
79+
return cmd
80+
}
81+
82+
func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
83+
globalFlags := globalflags.Parse(p, cmd)
84+
if globalFlags.ProjectId == "" {
85+
return nil, &errors.ProjectIdError{}
86+
}
87+
88+
model := inputModel{
89+
GlobalFlagModel: globalFlags,
90+
}
91+
92+
if p.IsVerbosityDebug() {
93+
modelStr, err := print.BuildDebugStrFromInputModel(model)
94+
if err != nil {
95+
p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err)
96+
} else {
97+
p.Debug(print.DebugLevel, "parsed input values: %s", modelStr)
98+
}
99+
}
100+
101+
return &model, nil
102+
}
103+
104+
func buildRequest(ctx context.Context, model *inputModel, apiClient *alb.APIClient) alb.ApiListPlansRequest {
105+
request := apiClient.ListPlans(ctx, model.Region)
106+
107+
return request
108+
}
109+
110+
func outputResult(p *print.Printer, outputFormat string, items []alb.PlanDetails) error {
111+
switch outputFormat {
112+
case print.JSONOutputFormat:
113+
details, err := json.MarshalIndent(items, "", " ")
114+
if err != nil {
115+
return fmt.Errorf("marshal plans: %w", err)
116+
}
117+
p.Outputln(string(details))
118+
119+
return nil
120+
case print.YAMLOutputFormat:
121+
details, err := yaml.MarshalWithOptions(items, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
122+
if err != nil {
123+
return fmt.Errorf("marshal plans: %w", err)
124+
}
125+
p.Outputln(string(details))
126+
127+
return nil
128+
default:
129+
table := tables.NewTable()
130+
table.SetHeader("PLAN ID", "NAME", "FLAVOR", "MAX CONNS", "DESCRIPTION")
131+
for _, item := range items {
132+
table.AddRow(utils.PtrString(item.PlanId),
133+
utils.PtrString(item.Name),
134+
utils.PtrString(item.FlavorName),
135+
utils.PtrString(item.MaxConnections),
136+
utils.Truncate(item.Description, 70),
137+
)
138+
}
139+
err := table.Display(p)
140+
if err != nil {
141+
return fmt.Errorf("render table: %w", err)
142+
}
143+
144+
return nil
145+
}
146+
}

0 commit comments

Comments
 (0)