Skip to content

Commit b79eb23

Browse files
committed
Add nodebalancers field to VPC Subnet structs
1 parent b9aae43 commit b79eb23

7 files changed

Lines changed: 811 additions & 31 deletions

File tree

test/integration/fixtures/TestVPC_Subnet_WithNodeBalancer.yaml

Lines changed: 699 additions & 0 deletions
Large diffs are not rendered by default.

test/integration/instance_config_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,55 @@ func setupInstanceWith3Interfaces(t *testing.T, fixturesYaml string) (
249249
return client, vpc, vpcSubnet, instance, config, teardown
250250
}
251251

252+
func setupNodebalancer(t *testing.T, fixturesYaml string) (
253+
*Client,
254+
*VPC,
255+
*VPCSubnet,
256+
func(),
257+
) {
258+
t.Helper()
259+
client, fixtureTeardown := createTestClient(t, fixturesYaml)
260+
vpc, vpcSubnet, vpcWithSubnetTeardown, err := createVPCWithSubnet(
261+
t,
262+
client,
263+
func(client *Client, options *VPCCreateOptions) {
264+
options.Region = "nl-ams"
265+
},
266+
)
267+
if err != nil {
268+
t.Error(err)
269+
}
270+
271+
createOpts := NodeBalancerCreateOptions{
272+
Label: &label,
273+
Region: vpc.Region,
274+
ClientConnThrottle: &clientConnThrottle,
275+
FirewallID: GetFirewallID(),
276+
VPCs: []NodeBalancerVPCOptions{
277+
{
278+
IPv4Range: "192.168.0.64/30",
279+
IPv6Range: "",
280+
SubnetID: vpcSubnet.ID,
281+
},
282+
},
283+
}
284+
285+
nodebalancer, err := client.CreateNodeBalancer(context.Background(), createOpts)
286+
if err != nil {
287+
t.Fatalf("Error listing nodebalancers, expected struct, got error %v", err)
288+
}
289+
290+
teardown := func() {
291+
if err := client.DeleteNodeBalancer(context.Background(), nodebalancer.ID); err != nil {
292+
t.Errorf("Expected to delete a nodebalancer, but got %v", err)
293+
}
294+
vpcWithSubnetTeardown()
295+
fixtureTeardown()
296+
}
297+
298+
return client, vpc, vpcSubnet, teardown
299+
}
300+
252301
func TestInstance_ConfigInterfaces_AppendDelete(t *testing.T) {
253302
client, _, subnet, instance, config, teardown, err := setupVPCWithSubnetWithInstance(
254303
t,

test/integration/vpc_subnet_test.go

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/linode/linodego"
1010
. "github.com/linode/linodego"
11+
"github.com/stretchr/testify/assert"
1112
)
1213

1314
const (
@@ -153,35 +154,6 @@ func createVPCWithDualStackSubnet(t *testing.T, client *linodego.Client, vpcModi
153154
return vpc, &vpc.Subnets[0], teardown, err
154155
}
155156

156-
func createSubnetInVPC(
157-
t *testing.T,
158-
client *linodego.Client,
159-
vpc linodego.VPC,
160-
vpcModifier ...vpcModifier,
161-
) (
162-
*linodego.VPCSubnet,
163-
func(),
164-
error,
165-
) {
166-
t.Helper()
167-
createOpts := linodego.VPCSubnetCreateOptions{
168-
Label: "linodego-vpc-test-" + getUniqueText(),
169-
IPv4: TestSubnetIPv4,
170-
}
171-
vpcSubnet, err := client.CreateVPCSubnet(context.Background(), createOpts, vpc.ID)
172-
if err != nil {
173-
t.Fatal(formatVPCSubnetError(err, "creating", &vpc.ID, nil))
174-
}
175-
176-
teardown := func() {
177-
err = client.DeleteVPCSubnet(context.Background(), vpc.ID, vpcSubnet.ID)
178-
if err != nil {
179-
t.Error(formatVPCSubnetError(err, "deleting", &vpc.ID, &vpcSubnet.ID))
180-
}
181-
}
182-
return vpcSubnet, teardown, err
183-
}
184-
185157
func setupVPCWithSubnet(
186158
t *testing.T,
187159
fixturesYaml string,
@@ -376,3 +348,20 @@ func TestVPC_Subnet_WithInstance(t *testing.T) {
376348
t.Fatalf("nat_1_1 subnet IP mismatch")
377349
}
378350
}
351+
352+
func TestVPC_Subnet_WithNodeBalancer(t *testing.T) {
353+
client, vpc, vpcSubnet, teardown := setupNodebalancer(t, "fixtures/TestVPC_Subnet_WithNodeBalancer")
354+
355+
defer teardown()
356+
357+
refreshedSubnet, err := client.GetVPCSubnet(context.Background(), vpc.ID, vpcSubnet.ID)
358+
if err != nil {
359+
t.Fatal(err)
360+
}
361+
362+
assert.Equal(t, 0, len(refreshedSubnet.Linodes), "expected no linode")
363+
assert.Equal(t, 1, len(refreshedSubnet.Nodebalancers), "expected 1 assigned node balancer")
364+
assert.Equal(t, "192.168.0.64/30", refreshedSubnet.Nodebalancers[0].Ipv4Range, "expected matching ipv4 range")
365+
assert.Equal(t, 0, len(refreshedSubnet.Nodebalancers[0].Ipv6Ranges), "expected 0 ipv6 ranges")
366+
367+
}

test/unit/fixtures/vpc_subnet_get.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@
3333
]
3434
}
3535
],
36+
"nodebalancers": [
37+
{
38+
"id": 4444,
39+
"ipv4_range": "192.168.0.20/30",
40+
"ipv6_ranges": [
41+
{
42+
"range": "2830:0:0:4::/62"
43+
}
44+
]
45+
}
46+
],
3647
"created": "2025-01-01T10:00:00",
3748
"updated": "2025-01-02T10:00:00"
3849
}

test/unit/fixtures/vpc_subnets_list.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@
4444
]
4545
}
4646
],
47+
"nodebalancers": [
48+
{
49+
"id": 4444,
50+
"ipv4_range": "192.168.0.20/30",
51+
"ipv6_ranges": [
52+
{
53+
"range": "2830:0:0:4::/62"
54+
}
55+
]
56+
}
57+
],
4758
"updated": "2023-09-11T00:00:00"
4859
}
4960
],

test/unit/vpc_subnets_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ func TestVPCSubnet_Get(t *testing.T) {
7474
assert.Equal(t, 123, subnet.Databases[0].ID)
7575
assert.Equal(t, "10.0.0.4/32", *subnet.Databases[0].IPv4Range)
7676
assert.Equal(t, "fda3:9c1b:5e2a:1::/64", subnet.Databases[0].IPv6Ranges[0])
77+
78+
assert.Equal(t, 4444, subnet.Nodebalancers[0].ID)
79+
assert.Equal(t, "192.168.0.20/30", subnet.Nodebalancers[0].Ipv4Range)
80+
assert.Equal(t, "2830:0:0:4::/62", subnet.Nodebalancers[0].Ipv6Ranges[0].Range)
7781
}
7882

7983
func TestVPCSubnets_List(t *testing.T) {
@@ -120,6 +124,10 @@ func TestVPCSubnets_List(t *testing.T) {
120124
assert.Equal(t, 123, subnet.Databases[0].ID)
121125
assert.Equal(t, "10.0.0.4/32", *subnet.Databases[0].IPv4Range)
122126
assert.Equal(t, "fda3:9c1b:5e2a:1::/64", subnet.Databases[0].IPv6Ranges[0])
127+
128+
assert.Equal(t, 4444, subnet.Nodebalancers[0].ID)
129+
assert.Equal(t, "192.168.0.20/30", subnet.Nodebalancers[0].Ipv4Range)
130+
assert.Equal(t, "2830:0:0:4::/62", subnet.Nodebalancers[0].Ipv6Ranges[0].Range)
123131
}
124132

125133
func TestVPCSubnet_Update(t *testing.T) {

vpc_subnet.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ type VPCSubnetDatabase struct {
2929
IPv6Ranges []string `json:"ipv6_ranges"`
3030
}
3131

32+
// VPCSubnetNodebalancersRanges represents a single range assigned to a node balancer.
33+
type VPCSubnetNodebalancersRanges struct {
34+
Range string `json:"range"`
35+
}
36+
37+
// VPCSubnetNodebalancers represents a node balancer currently assigned to a VPC subnet.
38+
type VPCSubnetNodebalancers struct {
39+
ID int `json:"id"`
40+
Ipv4Range string `json:"ipv4_range"`
41+
Ipv6Ranges []VPCSubnetNodebalancersRanges `json:"ipv6_ranges"`
42+
}
43+
3244
type VPCSubnet struct {
3345
ID int `json:"id"`
3446
Label string `json:"label"`
@@ -37,8 +49,9 @@ type VPCSubnet struct {
3749
// NOTE: IPv6 VPCs may not currently be available to all users.
3850
IPv6 []VPCIPv6Range `json:"ipv6"`
3951

40-
Linodes []VPCSubnetLinode `json:"linodes"`
41-
Databases []VPCSubnetDatabase `json:"databases"`
52+
Linodes []VPCSubnetLinode `json:"linodes"`
53+
Databases []VPCSubnetDatabase `json:"databases"`
54+
Nodebalancers []VPCSubnetNodebalancers `json:"nodebalancers"`
4255

4356
Created *time.Time `json:"-"`
4457
Updated *time.Time `json:"-"`

0 commit comments

Comments
 (0)