Skip to content

Commit bacb5d2

Browse files
authored
feat(lb): custom command to support ipv6 update (#3759)
1 parent 0be6f78 commit bacb5d2

File tree

9 files changed

+1229
-3
lines changed

9 files changed

+1229
-3
lines changed

cmd/scw/testdata/test-all-usage-lblb-update-usage.golden

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ USAGE:
66
scw lb lb update <lb-id ...> [arg=value ...]
77

88
ARGS:
9-
lb-id Load Balancer ID
10-
name Load Balancer name
11-
description Load Balancer description
9+
lb-id Load Balancer ID
10+
name Load Balancer name
11+
description Load Balancer description
12+
ip (one of):
13+
[assign-flexible-ipv6] Automatically assign a flexible public IPv6 to the Load Balancer
14+
[ip-id] The IP ID to attach to the Load Balancer
1215
[tags.{index}] List of tags for the Load Balancer
1316
[ssl-compatibility-level] Determines the minimal SSL version which needs to be supported on the client side, in an SSL/TLS offloading context. Intermediate is suitable for general-purpose servers with a variety of clients, recommended for almost all systems. Modern is suitable for services with clients that support TLS 1.3 and don't need backward compatibility. Old is compatible with a small number of very old clients and should be used only as a last resort (ssl_compatibility_level_unknown | ssl_compatibility_level_intermediate | ssl_compatibility_level_modern | ssl_compatibility_level_old)
1417
[zone=fr-par-1] Zone to target. If none is passed will use default zone from the config (fr-par-1 | fr-par-2 | nl-ams-1 | nl-ams-2 | nl-ams-3 | pl-waw-1 | pl-waw-2 | pl-waw-3)

docs/commands/lb.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,8 @@ scw lb lb update <lb-id ...> [arg=value ...]
10421042
| lb-id | Required | Load Balancer ID |
10431043
| name | Required | Load Balancer name |
10441044
| description | Required | Load Balancer description |
1045+
| assign-flexible-ipv6 | | Automatically assign a flexible public IPv6 to the Load Balancer |
1046+
| ip-id | | The IP ID to attach to the Load Balancer |
10451047
| tags.{index} | | List of tags for the Load Balancer |
10461048
| ssl-compatibility-level | One of: `ssl_compatibility_level_unknown`, `ssl_compatibility_level_intermediate`, `ssl_compatibility_level_modern`, `ssl_compatibility_level_old` | Determines the minimal SSL version which needs to be supported on the client side, in an SSL/TLS offloading context. Intermediate is suitable for general-purpose servers with a variety of clients, recommended for almost all systems. Modern is suitable for services with clients that support TLS 1.3 and don't need backward compatibility. Old is compatible with a small number of very old clients and should be used only as a last resort |
10471049
| zone | Default: `fr-par-1`<br />One of: `fr-par-1`, `fr-par-2`, `nl-ams-1`, `nl-ams-2`, `nl-ams-3`, `pl-waw-1`, `pl-waw-2`, `pl-waw-3` | Zone to target. If none is passed will use default zone from the config |

internal/namespaces/lb/v1/custom_lb.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,85 @@ func lbGetBuilder(c *core.Command) *core.Command {
139139
}
140140

141141
func lbUpdateBuilder(c *core.Command) *core.Command {
142+
type lbUpdateRequestCustom struct {
143+
*lb.ZonedAPIUpdateLBRequest
144+
AssignFlexibleIPv6 bool `json:"assign_flexible_ipv6"`
145+
IPID string `json:"ip_id"`
146+
}
147+
148+
c.ArgsType = reflect.TypeOf(lbUpdateRequestCustom{})
149+
150+
c.ArgSpecs.AddBefore("tags.{index}", &core.ArgSpec{
151+
Name: "assign-flexible-ipv6",
152+
Short: "Automatically assign a flexible public IPv6 to the Load Balancer",
153+
OneOfGroup: "ip",
154+
})
155+
c.ArgSpecs.AddBefore("tags.{index}", &core.ArgSpec{
156+
Name: "ip-id",
157+
Short: "The IP ID to attach to the Load Balancer",
158+
OneOfGroup: "ip",
159+
})
160+
161+
c.Run = func(ctx context.Context, argsI interface{}) (interface{}, error) {
162+
request := argsI.(*lbUpdateRequestCustom)
163+
client := core.ExtractClient(ctx)
164+
lbAPI := lb.NewZonedAPI(client)
165+
166+
waitRequest := &lb.ZonedAPIWaitForLBRequest{
167+
LBID: request.LBID,
168+
Zone: request.Zone,
169+
Timeout: scw.TimeDurationPtr(defaultLBTimeout),
170+
RetryInterval: core.DefaultRetryInterval,
171+
}
172+
res, err := lbAPI.WaitForLb(waitRequest, scw.WithContext(ctx))
173+
if err != nil {
174+
return nil, err
175+
}
176+
177+
if request.IPID != "" {
178+
_, err = lbAPI.UpdateIP(&lb.ZonedAPIUpdateIPRequest{
179+
Zone: request.Zone,
180+
IPID: request.IPID,
181+
LBID: &request.LBID,
182+
}, scw.WithContext(ctx))
183+
if err != nil {
184+
return nil, err
185+
}
186+
}
187+
188+
if request.AssignFlexibleIPv6 {
189+
ip, err := lbAPI.CreateIP(&lb.ZonedAPICreateIPRequest{
190+
Zone: res.Zone,
191+
ProjectID: &res.ProjectID,
192+
IsIPv6: true,
193+
}, scw.WithContext(ctx))
194+
if err != nil {
195+
return nil, err
196+
}
197+
198+
_, err = lbAPI.UpdateIP(&lb.ZonedAPIUpdateIPRequest{
199+
Zone: ip.Zone,
200+
IPID: ip.ID,
201+
LBID: &res.ID,
202+
}, scw.WithContext(ctx))
203+
if err != nil {
204+
return nil, err
205+
}
206+
}
207+
208+
_, err = lbAPI.WaitForLb(waitRequest, scw.WithContext(ctx))
209+
if err != nil {
210+
return nil, err
211+
}
212+
213+
result, err := lbAPI.UpdateLB(request.ZonedAPIUpdateLBRequest)
214+
if err != nil {
215+
return nil, err
216+
}
217+
218+
return result, err
219+
}
220+
142221
c.Interceptor = interceptLB()
143222
return c
144223
}

internal/namespaces/lb/v1/custom_lb_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,33 @@ func Test_GetLB(t *testing.T) {
4040
}))
4141
}
4242

43+
func Test_UpdateLBIPv6(t *testing.T) {
44+
t.Run("Assigned", core.Test(&core.TestConfig{
45+
Commands: lb.GetCommands(),
46+
BeforeFunc: createLB(),
47+
Cmd: "scw lb lb update {{ .LB.ID }} name=cli-test-update assign-flexible-ipv6=true description=assigned",
48+
Check: core.TestCheckCombine(
49+
core.TestCheckExitCode(0),
50+
core.TestCheckGolden(),
51+
),
52+
AfterFunc: deleteLB(),
53+
}))
54+
55+
t.Run("IPID", core.Test(&core.TestConfig{
56+
Commands: lb.GetCommands(),
57+
BeforeFunc: core.BeforeFuncCombine(
58+
createIP(),
59+
createLB(),
60+
),
61+
Cmd: "scw lb lb update {{ .LB.ID }} name=cli-test-update ip-id={{ .IP.ID }} description=ip-id",
62+
Check: core.TestCheckCombine(
63+
core.TestCheckExitCode(0),
64+
core.TestCheckGolden(),
65+
),
66+
AfterFunc: deleteLB(),
67+
}))
68+
}
69+
4370
func Test_WaitLB(t *testing.T) {
4471
t.Run("Simple", core.Test(&core.TestConfig{
4572
Commands: lb.GetCommands(),

internal/namespaces/lb/v1/helper_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,10 @@ func detachPN() core.AfterFunc {
161161
"scw lb private-network detach {{ .LB.ID }} private-network-id={{ .PN.ID }}",
162162
)
163163
}
164+
165+
func createIP() core.BeforeFunc {
166+
return core.ExecStoreBeforeCmd(
167+
"IP",
168+
"scw lb ip create is-ipv6=true",
169+
)
170+
}

0 commit comments

Comments
 (0)