Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nodebalancer VPC support #678

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions nodebalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ type NodeBalancerTransfer struct {
In *float64 `json:"in"`
}

type NodeBalancerVPCOptions struct {
IPv4Range string `json:"ipv4_range"`
IPv6Range string `json:"ipv6_range,omitempty"`
SubnetID int `json:"subnet_id"`
}

// NodeBalancerCreateOptions are the options permitted for CreateNodeBalancer
type NodeBalancerCreateOptions struct {
Label *string `json:"label,omitempty"`
Expand All @@ -52,6 +58,7 @@ type NodeBalancerCreateOptions struct {
Configs []*NodeBalancerConfigCreateOptions `json:"configs,omitempty"`
Tags []string `json:"tags"`
FirewallID int `json:"firewall_id,omitempty"`
VPCs []*NodeBalancerVPCOptions `json:"vpcs,omitempty"`
}

// NodeBalancerUpdateOptions are the options permitted for UpdateNodeBalancer
Expand Down
9 changes: 5 additions & 4 deletions nodebalancer_config_nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ var (

// NodeBalancerNodeCreateOptions fields are those accepted by CreateNodeBalancerNode
type NodeBalancerNodeCreateOptions struct {
Address string `json:"address"`
Label string `json:"label"`
Weight int `json:"weight,omitempty"`
Mode NodeMode `json:"mode,omitempty"`
Address string `json:"address"`
Label string `json:"label"`
Weight int `json:"weight,omitempty"`
Mode NodeMode `json:"mode,omitempty"`
SubnetID int `json:"subnet_id,omitempty"`
}

// NodeBalancerNodeUpdateOptions fields are those accepted by UpdateNodeBalancerNode
Expand Down
28 changes: 28 additions & 0 deletions nodebalancer_config_vpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package linodego

import (
"context"
)

// NodeBalancerVPCConfig objects represent a VPC config for a NodeBalancer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add LA disclaimer to the NB VPC-related fields, structs, and methods? Something along the lines of:

NOTE: NodeBalancer VPC support may not currently be available to all users.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ofc! Let me add this

// s
// NOTE: NodeBalancer VPC support may not currently be available to all users.
type NodeBalancerVPCConfig struct {
ID int `json:"id"`
IPv4Range string `json:"ipv4_range"`
IPv6Range string `json:"ipv6_range,omitempty"`
NodeBalancerID int `json:"nodebalancer_id"`
SubnetID int `json:"subnet_id"`
VPCID int `json:"vpc_id"`
}

// ListNodeBalancerVPCConfigs lists NodeBalancer VPC configs
func (c *Client) ListNodeBalancerVPCConfigs(ctx context.Context, nodebalancerID int, opts *ListOptions) ([]NodeBalancerVPCConfig, error) {
return getPaginatedResults[NodeBalancerVPCConfig](ctx, c, formatAPIPath("nodebalancers/%d/vpcs", nodebalancerID), opts)
}

// GetNodeBalancerVPCConfig gets the NodeBalancer VPC config with the specified id
func (c *Client) GetNodeBalancerVPCConfig(ctx context.Context, nodebalancerID int, vpcID int) (*NodeBalancerVPCConfig, error) {
e := formatAPIPath("nodebalancers/%d/vpcs/%d", nodebalancerID, vpcID)
return doGETRequest[NodeBalancerVPCConfig](ctx, c, e)
}
682 changes: 682 additions & 0 deletions test/integration/fixtures/TestNodeBalancerVpcConfig_Get.yaml

Large diffs are not rendered by default.

619 changes: 619 additions & 0 deletions test/integration/fixtures/TestNodeBalancerVpcConfig_List.yaml

Large diffs are not rendered by default.

546 changes: 546 additions & 0 deletions test/integration/fixtures/TestNodeBalancer_With_VPC_Create.yaml

Large diffs are not rendered by default.

49 changes: 49 additions & 0 deletions test/integration/nodebalancer_config_vpc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package integration

import (
"context"
"testing"

"github.com/stretchr/testify/require"
)

func TestNodeBalancerVPCConfig_List(t *testing.T) {
client, nodebalancer, teardown, err := setupNodeBalancerWithVPC(t, "fixtures/TestNodeBalancerVpcConfig_List")
if err != nil {
t.Errorf("Error setting up nodebalancer: %s", err)
}
defer teardown()

configs, err := client.ListNodeBalancerVPCConfigs(context.Background(), nodebalancer.ID, nil)
if err != nil {
t.Errorf("Error listing nodebalancer VPC configs: %s", err)
}

// We expect the list to be not empty and have at least one VPC config
require.NotEmpty(t, configs)
require.Len(t, configs, 1)
}

func TestNodeBalancerVPCConfig_Get(t *testing.T) {
client, nodebalancer, teardown, err := setupNodeBalancerWithVPC(t, "fixtures/TestNodeBalancerVpcConfig_Get")
if err != nil {
t.Errorf("Error setting up nodebalancer: %s", err)
}
defer teardown()

// Get the VPC config list for the nodebalancer (should only have one)
configs, err := client.ListNodeBalancerVPCConfigs(context.Background(), nodebalancer.ID, nil)
if err != nil {
t.Errorf("Error listing nodebalancer VPC configs: %s", err)
}
require.NotEmpty(t, configs)
require.Len(t, configs, 1)

// Get the VPC config by ID
config, err := client.GetNodeBalancerVPCConfig(context.Background(), nodebalancer.ID, configs[0].ID)
if err != nil {
t.Errorf("Error getting nodebalancer VPC config: %s", err)
}
require.NotNil(t, config)
require.Equal(t, configs[0].ID, config.ID)
}
54 changes: 54 additions & 0 deletions test/integration/nodebalancers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ func TestNodeBalancer_Create_create_smoke(t *testing.T) {
assertDateSet(t, nodebalancer.Updated)
}

func TestNodeBalancer_Create_with_vpc(t *testing.T) {
_, nodebalancer, teardown, err := setupNodeBalancerWithVPC(t, "fixtures/TestNodeBalancer_With_VPC_Create")
defer teardown()

if err != nil {
t.Errorf("Error creating nodebalancer: %v", err)
}

// when comparing fixtures to random value Label will differ, compare the known suffix
if !strings.Contains(*nodebalancer.Label, label) {
t.Errorf("nodebalancer returned does not match nodebalancer create request")
}

assertDateSet(t, nodebalancer.Created)
assertDateSet(t, nodebalancer.Updated)
}

func TestNodeBalancer_Update(t *testing.T) {
client, nodebalancer, teardown, err := setupNodeBalancer(t, "fixtures/TestNodeBalancer_Update")
defer teardown()
Expand Down Expand Up @@ -104,3 +121,40 @@ func setupNodeBalancer(t *testing.T, fixturesYaml string) (*linodego.Client, *li
}
return client, nodebalancer, teardown, err
}

func setupNodeBalancerWithVPC(t *testing.T, fixturesYaml string) (*linodego.Client, *linodego.NodeBalancer, func(), error) {
t.Helper()
var fixtureTeardown func()
client, fixtureTeardown := createTestClient(t, fixturesYaml)
vpc, subnet, vpcTeardown, err := createVPCWithSubnet(t, client)
if err != nil {
t.Errorf("Error creating vpc, got error %v", err)
}
createOpts := linodego.NodeBalancerCreateOptions{
Label: &label,
Region: vpc.Region,
ClientConnThrottle: &clientConnThrottle,
FirewallID: GetFirewallID(),
VPCs: []*linodego.NodeBalancerVPCOptions{
{
IPv4Range: "192.168.0.64/30",
IPv6Range: "",
SubnetID: subnet.ID,
},
},
}

nodebalancer, err := client.CreateNodeBalancer(context.Background(), createOpts)
if err != nil {
t.Fatalf("Error listing nodebalancers, expected struct, got error %v", err)
}

teardown := func() {
if err := client.DeleteNodeBalancer(context.Background(), nodebalancer.ID); err != nil {
t.Errorf("Expected to delete a nodebalancer, but got %v", err)
}
vpcTeardown()
fixtureTeardown()
}
return client, nodebalancer, teardown, err
}
Loading