Skip to content

Commit 661b76a

Browse files
committed
feat(instance): allow to create IPv6 only instances
With the routed IP, when creating a new instances with `ip=<IPv6_UUID>` you still end up with an IPv4 attached to your instance due to the `dynamic_ip_required` defaulting to `true`. This change allows one to create IPv6 only instances when passing the new option `ipv4=false` (which defaults to `true` to remain backward compatible).
1 parent 02c8e16 commit 661b76a

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

internal/namespaces/instance/v1/custom_server_create.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type instanceCreateServerRequest struct {
2828
IP string
2929
Tags []string
3030
IPv6 bool
31+
IPv4 bool
3132
Stopped bool
3233
SecurityGroupID string
3334
PlacementGroupID string
@@ -100,6 +101,11 @@ func serverCreateCommand() *core.Command {
100101
Name: "ipv6",
101102
Short: "Enable IPv6, to be used with routed-ip-enabled=false",
102103
},
104+
{
105+
Name: "ipv4",
106+
Short: "Enable/disable IPv4. Usefull for routed IPs",
107+
Default: core.DefaultValueSetter("true"),
108+
},
103109
{
104110
Name: "stopped",
105111
Short: "Do not start server after its creation",
@@ -209,6 +215,7 @@ func instanceServerCreateRun(ctx context.Context, argsI interface{}) (i interfac
209215
AddOrganizationID(args.OrganizationID).
210216
AddProjectID(args.ProjectID).
211217
AddEnableIPv6(scw.BoolPtr(args.IPv6)).
218+
AddEnableIPv4(scw.BoolPtr(args.IPv4)).
212219
AddTags(args.Tags).
213220
AddRoutedIPEnabled(args.RoutedIPEnabled).
214221
AddAdminPasswordEncryptionSSHKeyID(args.AdminPasswordEncryptionSSHKeyID).

internal/namespaces/instance/v1/custom_server_create_builder.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ type ServerBuilder struct {
3535
apiInstance *instance.API
3636
apiBlock *block.API
3737

38+
// IPv4 enabled
39+
enableIPv4 bool
40+
3841
// serverType is filled with the ServerType if CommercialType is found in the API.
3942
serverType *instance.ServerType
4043
// serverImage is filled with the Image if one is provided
@@ -82,6 +85,17 @@ func (sb *ServerBuilder) AddEnableIPv6(enableIPv6 *bool) *ServerBuilder {
8285
return sb
8386
}
8487

88+
func (sb *ServerBuilder) AddEnableIPv4(enableIPv4 *bool) *ServerBuilder {
89+
sb.enableIPv4 = *enableIPv4 //nolint: staticcheck
90+
91+
// disable dynamic IP in that case
92+
if !sb.enableIPv4 {
93+
sb.createReq.DynamicIPRequired = enableIPv4
94+
}
95+
96+
return sb
97+
}
98+
8599
func (sb *ServerBuilder) AddTags(tags []string) *ServerBuilder {
86100
if len(tags) > 0 {
87101
sb.createReq.Tags = tags
@@ -132,9 +146,16 @@ func (sb *ServerBuilder) rootVolumeIsSBS() bool {
132146
func (sb *ServerBuilder) defaultIPType() instance.IPType {
133147
if sb.createReq.RoutedIPEnabled != nil { //nolint: staticcheck // Field is deprecated but still supported
134148
if *sb.createReq.RoutedIPEnabled { //nolint: staticcheck // Field is deprecated but still supported
149+
if *sb.createReq.EnableIPv6 || !sb.enableIPv4 { //nolint: staticcheck // Field is deprecated but still supported
150+
return instance.IPTypeRoutedIPv6
151+
}
135152
return instance.IPTypeRoutedIPv4
136153
}
154+
// routed_ip_enabled defaults to true, you can only have NAT IPs when specifying it to false explicitly
137155
return instance.IPTypeNat
156+
} else if (sb.createReq.EnableIPv6 != nil && *sb.createReq.EnableIPv6) || !sb.enableIPv4 { //nolint: staticcheck // Field is deprecated but still supported
157+
// instances now have routed_ip_enabled by default
158+
return instance.IPTypeRoutedIPv6
138159
}
139160

140161
return ""

0 commit comments

Comments
 (0)