Skip to content

Commit

Permalink
feat(lb): custom command to support ipv6 update (#3759)
Browse files Browse the repository at this point in the history
  • Loading branch information
yfodil authored Apr 11, 2024
1 parent 0be6f78 commit bacb5d2
Show file tree
Hide file tree
Showing 9 changed files with 1,229 additions and 3 deletions.
9 changes: 6 additions & 3 deletions cmd/scw/testdata/test-all-usage-lblb-update-usage.golden
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ USAGE:
scw lb lb update <lb-id ...> [arg=value ...]

ARGS:
lb-id Load Balancer ID
name Load Balancer name
description Load Balancer description
lb-id Load Balancer ID
name Load Balancer name
description Load Balancer description
ip (one of):
[assign-flexible-ipv6] Automatically assign a flexible public IPv6 to the Load Balancer
[ip-id] The IP ID to attach to the Load Balancer
[tags.{index}] List of tags for the Load Balancer
[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)
[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)
Expand Down
2 changes: 2 additions & 0 deletions docs/commands/lb.md
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,8 @@ scw lb lb update <lb-id ...> [arg=value ...]
| lb-id | Required | Load Balancer ID |
| name | Required | Load Balancer name |
| description | Required | Load Balancer description |
| assign-flexible-ipv6 | | Automatically assign a flexible public IPv6 to the Load Balancer |
| ip-id | | The IP ID to attach to the Load Balancer |
| tags.{index} | | List of tags for the Load Balancer |
| 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 |
| 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 |
Expand Down
79 changes: 79 additions & 0 deletions internal/namespaces/lb/v1/custom_lb.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,85 @@ func lbGetBuilder(c *core.Command) *core.Command {
}

func lbUpdateBuilder(c *core.Command) *core.Command {
type lbUpdateRequestCustom struct {
*lb.ZonedAPIUpdateLBRequest
AssignFlexibleIPv6 bool `json:"assign_flexible_ipv6"`
IPID string `json:"ip_id"`
}

c.ArgsType = reflect.TypeOf(lbUpdateRequestCustom{})

c.ArgSpecs.AddBefore("tags.{index}", &core.ArgSpec{
Name: "assign-flexible-ipv6",
Short: "Automatically assign a flexible public IPv6 to the Load Balancer",
OneOfGroup: "ip",
})
c.ArgSpecs.AddBefore("tags.{index}", &core.ArgSpec{
Name: "ip-id",
Short: "The IP ID to attach to the Load Balancer",
OneOfGroup: "ip",
})

c.Run = func(ctx context.Context, argsI interface{}) (interface{}, error) {
request := argsI.(*lbUpdateRequestCustom)
client := core.ExtractClient(ctx)
lbAPI := lb.NewZonedAPI(client)

waitRequest := &lb.ZonedAPIWaitForLBRequest{
LBID: request.LBID,
Zone: request.Zone,
Timeout: scw.TimeDurationPtr(defaultLBTimeout),
RetryInterval: core.DefaultRetryInterval,
}
res, err := lbAPI.WaitForLb(waitRequest, scw.WithContext(ctx))
if err != nil {
return nil, err
}

if request.IPID != "" {
_, err = lbAPI.UpdateIP(&lb.ZonedAPIUpdateIPRequest{
Zone: request.Zone,
IPID: request.IPID,
LBID: &request.LBID,
}, scw.WithContext(ctx))
if err != nil {
return nil, err
}
}

if request.AssignFlexibleIPv6 {
ip, err := lbAPI.CreateIP(&lb.ZonedAPICreateIPRequest{
Zone: res.Zone,
ProjectID: &res.ProjectID,
IsIPv6: true,
}, scw.WithContext(ctx))
if err != nil {
return nil, err
}

_, err = lbAPI.UpdateIP(&lb.ZonedAPIUpdateIPRequest{
Zone: ip.Zone,
IPID: ip.ID,
LBID: &res.ID,
}, scw.WithContext(ctx))
if err != nil {
return nil, err
}
}

_, err = lbAPI.WaitForLb(waitRequest, scw.WithContext(ctx))
if err != nil {
return nil, err
}

result, err := lbAPI.UpdateLB(request.ZonedAPIUpdateLBRequest)
if err != nil {
return nil, err
}

return result, err
}

c.Interceptor = interceptLB()
return c
}
Expand Down
27 changes: 27 additions & 0 deletions internal/namespaces/lb/v1/custom_lb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,33 @@ func Test_GetLB(t *testing.T) {
}))
}

func Test_UpdateLBIPv6(t *testing.T) {
t.Run("Assigned", core.Test(&core.TestConfig{
Commands: lb.GetCommands(),
BeforeFunc: createLB(),
Cmd: "scw lb lb update {{ .LB.ID }} name=cli-test-update assign-flexible-ipv6=true description=assigned",
Check: core.TestCheckCombine(
core.TestCheckExitCode(0),
core.TestCheckGolden(),
),
AfterFunc: deleteLB(),
}))

t.Run("IPID", core.Test(&core.TestConfig{
Commands: lb.GetCommands(),
BeforeFunc: core.BeforeFuncCombine(
createIP(),
createLB(),
),
Cmd: "scw lb lb update {{ .LB.ID }} name=cli-test-update ip-id={{ .IP.ID }} description=ip-id",
Check: core.TestCheckCombine(
core.TestCheckExitCode(0),
core.TestCheckGolden(),
),
AfterFunc: deleteLB(),
}))
}

func Test_WaitLB(t *testing.T) {
t.Run("Simple", core.Test(&core.TestConfig{
Commands: lb.GetCommands(),
Expand Down
7 changes: 7 additions & 0 deletions internal/namespaces/lb/v1/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,10 @@ func detachPN() core.AfterFunc {
"scw lb private-network detach {{ .LB.ID }} private-network-id={{ .PN.ID }}",
)
}

func createIP() core.BeforeFunc {
return core.ExecStoreBeforeCmd(
"IP",
"scw lb ip create is-ipv6=true",
)
}
Loading

0 comments on commit bacb5d2

Please sign in to comment.