File: vpc_subnet.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 (113 lines) | stat: -rw-r--r-- 2,693 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package linodego

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

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

// VPCSubnetLinodeInterface represents an interface on a Linode that is currently
// assigned to this VPC subnet.
type VPCSubnetLinodeInterface struct {
	ID     int  `json:"id"`
	Active bool `json:"active"`
}

// VPCSubnetLinode represents a Linode currently assigned to a VPC subnet.
type VPCSubnetLinode struct {
	ID         int                        `json:"id"`
	Interfaces []VPCSubnetLinodeInterface `json:"interfaces"`
}

type VPCSubnet struct {
	ID      int               `json:"id"`
	Label   string            `json:"label"`
	IPv4    string            `json:"ipv4"`
	Linodes []VPCSubnetLinode `json:"linodes"`
	Created *time.Time        `json:"-"`
	Updated *time.Time        `json:"-"`
}

type VPCSubnetCreateOptions struct {
	Label string `json:"label"`
	IPv4  string `json:"ipv4"`
}

type VPCSubnetUpdateOptions struct {
	Label string `json:"label"`
}

func (v *VPCSubnet) UnmarshalJSON(b []byte) error {
	type Mask VPCSubnet

	p := struct {
		*Mask

		Created *parseabletime.ParseableTime `json:"created"`
		Updated *parseabletime.ParseableTime `json:"updated"`
	}{
		Mask: (*Mask)(v),
	}
	if err := json.Unmarshal(b, &p); err != nil {
		return err
	}

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

	return nil
}

func (v VPCSubnet) GetCreateOptions() VPCSubnetCreateOptions {
	return VPCSubnetCreateOptions{
		Label: v.Label,
		IPv4:  v.IPv4,
	}
}

func (v VPCSubnet) GetUpdateOptions() VPCSubnetUpdateOptions {
	return VPCSubnetUpdateOptions{Label: v.Label}
}

func (c *Client) CreateVPCSubnet(
	ctx context.Context,
	opts VPCSubnetCreateOptions,
	vpcID int,
) (*VPCSubnet, error) {
	e := formatAPIPath("vpcs/%d/subnets", vpcID)
	return doPOSTRequest[VPCSubnet](ctx, c, e, opts)
}

func (c *Client) GetVPCSubnet(
	ctx context.Context,
	vpcID int,
	subnetID int,
) (*VPCSubnet, error) {
	e := formatAPIPath("vpcs/%d/subnets/%d", vpcID, subnetID)
	return doGETRequest[VPCSubnet](ctx, c, e)
}

func (c *Client) ListVPCSubnets(
	ctx context.Context,
	vpcID int,
	opts *ListOptions,
) ([]VPCSubnet, error) {
	return getPaginatedResults[VPCSubnet](ctx, c, formatAPIPath("vpcs/%d/subnets", vpcID), opts)
}

func (c *Client) UpdateVPCSubnet(
	ctx context.Context,
	vpcID int,
	subnetID int,
	opts VPCSubnetUpdateOptions,
) (*VPCSubnet, error) {
	e := formatAPIPath("vpcs/%d/subnets/%d", vpcID, subnetID)
	return doPUTRequest[VPCSubnet](ctx, c, e, opts)
}

func (c *Client) DeleteVPCSubnet(ctx context.Context, vpcID int, subnetID int) error {
	e := formatAPIPath("vpcs/%d/subnets/%d", vpcID, subnetID)
	return doDELETERequest(ctx, c, e)
}