Skip to content

Commit 0f5e641

Browse files
committed
feat(alb): template support for pool
1 parent 0fda41f commit 0f5e641

File tree

4 files changed

+115
-10
lines changed

4 files changed

+115
-10
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"activeHealthCheck": {
3+
"healthyThreshold": 1,
4+
"httpHealthChecks": {
5+
"okStatuses": [
6+
"string"
7+
],
8+
"path": "string"
9+
},
10+
"interval": "3s",
11+
"intervalJitter": "3s",
12+
"timeout": "3s",
13+
"unhealthyThreshold": 1
14+
},
15+
"name": "my-target-pool",
16+
"targetPort": 5732,
17+
"targets": [
18+
{
19+
"displayName": "my-target",
20+
"ip": "192.0.2.5"
21+
}
22+
],
23+
"tlsConfig": {
24+
"customCa": "string",
25+
"enabled": true,
26+
"skipCertificateValidation": true
27+
}
28+
}

internal/cmd/beta/alb/template/template.go

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,36 @@ import (
1919

2020
const (
2121
formatFlag = "format"
22+
typeFlag = "type"
2223
)
2324

2425
type inputModel struct {
2526
*globalflags.GlobalFlagModel
2627
Format *string
28+
Type *string
2729
}
2830

29-
//go:embed template.json
30-
var template string
31+
var (
32+
//go:embed template-alb.json
33+
templateAlb string
34+
//go:embed template-pool.json
35+
templatePool string
36+
)
3137

3238
func NewCmd(p *print.Printer) *cobra.Command {
3339
cmd := &cobra.Command{
3440
Use: "template",
35-
Short: "create an alb template",
36-
Long: "creates a json or yaml template file for creating/updating an application loadbalancer.",
41+
Short: "creates configuration templates to use for resource creation",
42+
Long: "creates a json or yaml template file for creating/updating an application loadbalancer or target pool.",
3743
Args: args.NoArgs,
3844
Example: examples.Build(
3945
examples.NewExample(
40-
`Creat a yaml template`,
41-
`$ stackit beta alb template --format=yaml`,
46+
`Create a yaml template`,
47+
`$ stackit beta alb template --format=yaml --type alb`,
4248
),
4349
examples.NewExample(
44-
`Creat a json template`,
45-
`$ stackit beta alb template --format=json`,
50+
`Create a json template`,
51+
`$ stackit beta alb template --format=json --type pool`,
4652
),
4753
),
4854
RunE: func(cmd *cobra.Command, _ []string) error {
@@ -51,10 +57,21 @@ func NewCmd(p *print.Printer) *cobra.Command {
5157
return err
5258
}
5359

60+
var (
61+
template string
62+
target any
63+
)
64+
if model.Type != nil && *model.Type == "pool" {
65+
template = templatePool
66+
target = alb.CreateLoadBalancerPayload{}
67+
} else {
68+
template = templateAlb
69+
target = alb.UpdateTargetPoolPayload{}
70+
}
71+
5472
if model.Format == nil || *model.Format == "json" {
5573
p.Outputln(template)
5674
} else if *model.Format == "yaml" {
57-
var target alb.CreateLoadBalancerPayload
5875
if err := json.Unmarshal([]byte(template), &target); err != nil {
5976
return fmt.Errorf("cannot unmarshal template: %w", err)
6077
}
@@ -75,7 +92,8 @@ func NewCmd(p *print.Printer) *cobra.Command {
7592
}
7693

7794
func configureFlags(cmd *cobra.Command) {
78-
cmd.Flags().VarP(flags.EnumFlag(true, "json", "json", "yaml"), formatFlag, "f", "Defines the output format (yaml or json), default is json")
95+
cmd.Flags().VarP(flags.EnumFlag(true, "json", "json", "yaml"), formatFlag, "f", "Defines the output format ('yaml' or 'json'), default is 'json'")
96+
cmd.Flags().VarP(flags.EnumFlag(true, "alb", "alb", "pool"), typeFlag, "t", "Defines the output type ('alb' or 'pool'), default is 'alb'")
7997
}
8098

8199
func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
@@ -87,6 +105,7 @@ func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
87105
model := inputModel{
88106
GlobalFlagModel: globalFlags,
89107
Format: flags.FlagToStringPointer(p, cmd, formatFlag),
108+
Type: flags.FlagToStringPointer(p, cmd, typeFlag),
90109
}
91110

92111
if p.IsVerbosityDebug() {

internal/cmd/beta/alb/template/template_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
77
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
8+
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
89

910
"github.com/google/go-cmp/cmp"
1011
"github.com/google/uuid"
@@ -75,6 +76,63 @@ func TestParseInput(t *testing.T) {
7576
}),
7677
isValid: false,
7778
},
79+
{
80+
description: "alb with yaml",
81+
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
82+
flagValues[formatFlag] = "yaml"
83+
flagValues[typeFlag] = "alb"
84+
}),
85+
isValid: true,
86+
expectedModel: fixtureInputModel(func(model *inputModel) {
87+
model.Format = utils.Ptr("yaml")
88+
model.Type = utils.Ptr("alb")
89+
}),
90+
}, {
91+
description: "alb with yaml",
92+
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
93+
flagValues[formatFlag] = "yaml"
94+
flagValues[typeFlag] = "alb"
95+
}),
96+
isValid: true,
97+
expectedModel: fixtureInputModel(func(model *inputModel) {
98+
model.Format = utils.Ptr("yaml")
99+
model.Type = utils.Ptr("alb")
100+
}),
101+
}, {
102+
description: "alb with json",
103+
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
104+
flagValues[formatFlag] = "json"
105+
flagValues[typeFlag] = "alb"
106+
}),
107+
isValid: true,
108+
expectedModel: fixtureInputModel(func(model *inputModel) {
109+
model.Format = utils.Ptr("json")
110+
model.Type = utils.Ptr("alb")
111+
}),
112+
}, {
113+
description: "pool with yaml",
114+
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
115+
flagValues[formatFlag] = "yaml"
116+
flagValues[typeFlag] = "pool"
117+
}),
118+
isValid: true,
119+
expectedModel: fixtureInputModel(func(model *inputModel) {
120+
model.Format = utils.Ptr("yaml")
121+
model.Type = utils.Ptr("pool")
122+
}),
123+
},
124+
{
125+
description: "pool with json",
126+
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
127+
flagValues[formatFlag] = "json"
128+
flagValues[typeFlag] = "pool"
129+
}),
130+
isValid: true,
131+
expectedModel: fixtureInputModel(func(model *inputModel) {
132+
model.Format = utils.Ptr("json")
133+
model.Type = utils.Ptr("pool")
134+
}),
135+
},
78136
}
79137

80138
for _, tt := range tests {

0 commit comments

Comments
 (0)