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

adding support for VLANs (#192) #198

Closed
wants to merge 6 commits into from
Closed
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
41 changes: 21 additions & 20 deletions provider/linode/linode_discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package linode

import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
Expand All @@ -14,11 +13,6 @@ import (
"golang.org/x/oauth2"
)

type Filter struct {
Region string `json:"region,omitempty"`
Tag string `json:"tags,omitempty"`
}

type Provider struct {
userAgent string
}
Expand All @@ -33,6 +27,7 @@ func (p *Provider) Help() string {
api_token: The Linode API token to use
region: The Linode region to filter on
tag_name: The tag name to filter on
vlan_label: The label of a attached VLAN
address_type: "private_v4", "public_v4", "private_v6" or "public_v6". (default: "private_v4")

Variables can also be provided by environment variables:
Expand All @@ -49,41 +44,47 @@ func (p *Provider) Addrs(args map[string]string, l *log.Logger) ([]string, error
l = log.New(ioutil.Discard, "", 0)
}

addressType := args["address_type"]
region := args["region"]
tagName := args["tag_name"]
vlanLabel := args["vlan_label"]
addressType := args["address_type"]
apiToken := argsOrEnv(args, "api_token", "LINODE_TOKEN")
l.Printf("[DEBUG] discover-linode: Using address_type=%s region=%s tag_name=%s", addressType, region, tagName)
l.Printf("[DEBUG] discover-linode: Using region=%s tag_name=%s vlan_label=%s address_type=%s", region, tagName, vlanLabel, addressType)

client := getLinodeClient(p.userAgent, apiToken)

filters := Filter{
Region: "",
Tag: "",
}

filters := linodego.Filter{}
if region != "" {
filters.Region = region
filters.AddField(linodego.Eq, "region", region)
}
if tagName != "" {
filters.Tag = tagName
filters.AddField(linodego.Eq, "tags", tagName)
}
jsonFilters, err := filters.MarshalJSON()
if err != nil {
return nil, fmt.Errorf("discover-linode: Cannont convert fields to a JSON Filter: %s", err)
}

jsonFilters, _ := json.Marshal(filters)
filterOpt := linodego.ListOptions{Filter: string(jsonFilters)}

linodes, err := client.ListInstances(context.Background(), &filterOpt)
ctx := context.Background()
linodes, err := client.ListInstances(ctx, &filterOpt)
if err != nil {
return nil, fmt.Errorf("discover-linode: Fetching Linode instances failed: %s", err)
}

var addrs []string
for _, linode := range linodes {
addr, err := client.GetInstanceIPAddresses(context.Background(), linode.ID)
addr, err := client.GetInstanceIPAddresses(ctx, linode.ID)
if err != nil {
return nil, fmt.Errorf("discover-linode: Fetching Linode IP address for instance %v failed: %s", linode.ID, err)
}

if vlanLabel != "" {
vlanIPAM, err := client.GetVLANIPAMAddress(ctx, linode.ID, vlanLabel)
if err != nil {
return nil, err
}
addrs = append(addrs, vlanIPAM)
}
switch addressType {
case "public_v4":
if len(addr.IPv4.Public) == 0 {
Expand Down
143 changes: 138 additions & 5 deletions provider/linode/linode_discover_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
package linode_test

import (
"context"
"log"
"net/http"
"os"
"testing"

discover "github.com/hashicorp/go-discover"
"github.com/hashicorp/go-discover/provider/linode"
"github.com/linode/linodego"
"golang.org/x/oauth2"
)

var _ discover.Provider = (*linode.Provider)(nil)
var _ discover.ProviderWithUserAgent = (*linode.Provider)(nil)

func TestAddrsTaggedDefault(t *testing.T) {
opts := linodego.InstanceCreateOptions{}
opts.Label = "go-discover-test-default"
opts.Tags = []string{"gd-tag1"}
opts.Region = "us-southeast"
opts.PrivateIP = true

_, destroy, err := buildInstance(t, opts)
if err != nil {
t.Fatal(err)
}
defer destroy()
args := discover.Config{
"provider": "linode",
"api_token": os.Getenv("LINODE_TOKEN"),
Expand All @@ -32,12 +47,23 @@ func TestAddrsTaggedDefault(t *testing.T) {
t.Fatal(err)
}

if len(addrs) != 2 {
if len(addrs) != 1 {
t.Fatalf("bad: %v", addrs)
}
}

func TestAddrsTaggedPublicV6(t *testing.T) {
opts := linodego.InstanceCreateOptions{}
opts.Label = "go-discover-test-public-v6"
opts.Tags = []string{"gd-tag1"}
opts.Region = "us-southeast"

_, destroy, err := buildInstance(t, opts)
if err != nil {
t.Fatal(err)
}
defer destroy()

args := discover.Config{
"provider": "linode",
"api_token": os.Getenv("LINODE_TOKEN"),
Expand All @@ -58,12 +84,23 @@ func TestAddrsTaggedPublicV6(t *testing.T) {
t.Fatal(err)
}

if len(addrs) != 2 {
if len(addrs) != 1 {
t.Fatalf("bad: %v", addrs)
}
}

func TestAddrsTaggedPublicV4(t *testing.T) {
opts := linodego.InstanceCreateOptions{}
opts.Label = "go-discover-test-public-v4"
opts.Tags = []string{"gd-tag1"}
opts.Region = "us-southeast"

_, destroy, err := buildInstance(t, opts)
if err != nil {
t.Fatal(err)
}
defer destroy()

args := discover.Config{
"provider": "linode",
"api_token": os.Getenv("LINODE_TOKEN"),
Expand All @@ -84,17 +121,29 @@ func TestAddrsTaggedPublicV4(t *testing.T) {
t.Fatal(err)
}

if len(addrs) != 2 {
if len(addrs) != 1 {
t.Fatalf("bad: %v", addrs)
}
}

func TestAddrsTaggedRegion(t *testing.T) {
opts := linodego.InstanceCreateOptions{}
opts.Label = "go-discover-test-tagged-region"
opts.Tags = []string{"gd-tag1"}
opts.Region = "us-southeast"
opts.PrivateIP = true

_, destroy, err := buildInstance(t, opts)
if err != nil {
t.Fatal(err)
}
defer destroy()

args := discover.Config{
"region": "us-southeast",
"provider": "linode",
"api_token": os.Getenv("LINODE_TOKEN"),
"tag_name": "gd-tag1",
"region": "us-east",
"api_token": os.Getenv("LINODE_TOKEN"),
}

if args["api_token"] == "" {
Expand All @@ -114,3 +163,87 @@ func TestAddrsTaggedRegion(t *testing.T) {
t.Fatalf("bad: %v", addrs)
}
}

func TestAddrsTaggedVLAN(t *testing.T) {
opts := linodego.InstanceCreateOptions{}
opts.Interfaces = []linodego.InstanceConfigInterface{
{
Label: "go-discover-test-vlan",
Purpose: linodego.InterfacePurposeVLAN,
IPAMAddress: "10.0.0.1/24",
},
}
opts.Label = "go-discover-test-vlan"
opts.Tags = []string{"gd-tag1"}
opts.Region = "us-southeast"
opts.Image = "linode/alpine3.16"
opts.RootPass = "supercoolpasspleasedontsteal"

_, destroy, err := buildInstance(t, opts)
if err != nil {
t.Fatal(err)
}
defer destroy()

args := discover.Config{
"region": "us-southeast",
"provider": "linode",
"tag_name": "gd-tag1",
"vlan_label": "go-discover-test-vlan",
"api_token": os.Getenv("LINODE_TOKEN"),
}

if args["api_token"] == "" {
t.Skip("Linode credentials missing")
}

p := &linode.Provider{}

l := log.New(os.Stderr, "", log.LstdFlags)
addrs, err := p.Addrs(args, l)

if err != nil {
t.Fatal(err)
}

if len(addrs) != 1 {
t.Fatalf("bad: %v", addrs)
}
}

func buildInstance(t *testing.T, opts linodego.InstanceCreateOptions) (*linodego.Instance, func(), error) {
t.Helper()
client := getLinodeClient(t)

falseBoot := true
opts.Type = "g6-nanode-1"
opts.Booted = &falseBoot
instance, err := client.CreateInstance(context.Background(), opts)
if err != nil {
return nil, nil, err
}

teardown := func() {
if terr := client.DeleteInstance(context.Background(), instance.ID); terr != nil {
t.Errorf("Error deleting test Instance: %s", terr)
}
}

return instance, teardown, nil
}

func getLinodeClient(t *testing.T) *linodego.Client {
t.Helper()
apiToken := os.Getenv("LINODE_TOKEN")
if apiToken == "" {
t.Fatal("failed to get $LINODE_TOKEN")
}
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: apiToken})
oauth2Client := &http.Client{
Transport: &oauth2.Transport{
Source: tokenSource,
},
}
client := linodego.NewClient(oauth2Client)
return &client
}