File: vlans.go

package info (click to toggle)
golang-github-linode-linodego 1.55.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,112 kB
  • sloc: makefile: 96; sh: 52; python: 24
file content (68 lines) | stat: -rw-r--r-- 1,655 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package linodego

import (
	"context"
	"encoding/json"
	"fmt"
	"time"

	"github.com/linode/linodego/internal/parseabletime"
)

type VLAN struct {
	Label   string     `json:"label"`
	Linodes []int      `json:"linodes"`
	Region  string     `json:"region"`
	Created *time.Time `json:"-"`
}

// UnmarshalJSON for VLAN responses
func (v *VLAN) UnmarshalJSON(b []byte) error {
	type Mask VLAN

	p := struct {
		*Mask

		Created *parseabletime.ParseableTime `json:"created"`
	}{
		Mask: (*Mask)(v),
	}

	if err := json.Unmarshal(b, &p); err != nil {
		return err
	}

	v.Created = (*time.Time)(p.Created)

	return nil
}

// ListVLANs returns a paginated list of VLANs
func (c *Client) ListVLANs(ctx context.Context, opts *ListOptions) ([]VLAN, error) {
	return getPaginatedResults[VLAN](ctx, c, "networking/vlans", opts)
}

// GetVLANIPAMAddress returns the IPAM Address for a given VLAN Label as a string (10.0.0.1/24)
func (c *Client) GetVLANIPAMAddress(ctx context.Context, linodeID int, vlanLabel string) (string, error) {
	f := Filter{}
	f.AddField(Eq, "interfaces", vlanLabel)

	vlanFilter, err := f.MarshalJSON()
	if err != nil {
		return "", fmt.Errorf("Unable to convert VLAN label: %s to a filterable object: %w", vlanLabel, err)
	}

	cfgs, err := c.ListInstanceConfigs(ctx, linodeID, &ListOptions{Filter: string(vlanFilter)})
	if err != nil {
		return "", fmt.Errorf("Fetching configs for instance %v failed: %w", linodeID, err)
	}

	interfaces := cfgs[0].Interfaces
	for _, face := range interfaces {
		if face.Label == vlanLabel {
			return face.IPAMAddress, nil
		}
	}

	return "", fmt.Errorf("Failed to find IPAMAddress for VLAN: %s", vlanLabel)
}