vpc_subnet.go raw
1 package linodego
2
3 import (
4 "context"
5 "encoding/json"
6 "time"
7
8 "github.com/linode/linodego/internal/parseabletime"
9 )
10
11 // VPCSubnetLinodeInterface represents an interface on a Linode that is currently
12 // assigned to this VPC subnet.
13 type VPCSubnetLinodeInterface struct {
14 ID int `json:"id"`
15 Active bool `json:"active"`
16 ConfigID *int `json:"config_id"`
17 }
18
19 // VPCSubnetLinode represents a Linode currently assigned to a VPC subnet.
20 type VPCSubnetLinode struct {
21 ID int `json:"id"`
22 Interfaces []VPCSubnetLinodeInterface `json:"interfaces"`
23 }
24
25 // VPCSubnetDatabase represents a Linode currently assigned to a VPC subnet.
26 type VPCSubnetDatabase struct {
27 ID int `json:"id"`
28 IPv4Range *string `json:"ipv4_range"`
29 IPv6Ranges []string `json:"ipv6_ranges"`
30 }
31
32 type VPCSubnet struct {
33 ID int `json:"id"`
34 Label string `json:"label"`
35 IPv4 string `json:"ipv4"`
36
37 // NOTE: IPv6 VPCs may not currently be available to all users.
38 IPv6 []VPCIPv6Range `json:"ipv6"`
39
40 Linodes []VPCSubnetLinode `json:"linodes"`
41 Databases []VPCSubnetDatabase `json:"databases"`
42
43 Created *time.Time `json:"-"`
44 Updated *time.Time `json:"-"`
45 }
46
47 type VPCSubnetCreateOptions struct {
48 Label string `json:"label"`
49 IPv4 string `json:"ipv4"`
50
51 // NOTE: IPv6 VPCs may not currently be available to all users.
52 IPv6 []VPCSubnetCreateOptionsIPv6 `json:"ipv6,omitempty"`
53 }
54
55 // VPCSubnetCreateOptionsIPv6 represents a single IPv6 range assigned to a VPC
56 // which is specified during a VPC subnet's creation.
57 // NOTE: IPv6 VPCs may not currently be available to all users.
58 type VPCSubnetCreateOptionsIPv6 struct {
59 Range *string `json:"range,omitempty"`
60 }
61
62 type VPCSubnetUpdateOptions struct {
63 Label string `json:"label"`
64 }
65
66 func (v *VPCSubnet) UnmarshalJSON(b []byte) error {
67 type Mask VPCSubnet
68
69 p := struct {
70 *Mask
71
72 Created *parseabletime.ParseableTime `json:"created"`
73 Updated *parseabletime.ParseableTime `json:"updated"`
74 }{
75 Mask: (*Mask)(v),
76 }
77 if err := json.Unmarshal(b, &p); err != nil {
78 return err
79 }
80
81 v.Created = (*time.Time)(p.Created)
82 v.Updated = (*time.Time)(p.Updated)
83
84 return nil
85 }
86
87 func (v VPCSubnet) GetCreateOptions() VPCSubnetCreateOptions {
88 return VPCSubnetCreateOptions{
89 Label: v.Label,
90 IPv4: v.IPv4,
91 IPv6: mapSlice(v.IPv6, func(i VPCIPv6Range) VPCSubnetCreateOptionsIPv6 {
92 return VPCSubnetCreateOptionsIPv6{
93 Range: copyValue(&i.Range),
94 }
95 }),
96 }
97 }
98
99 func (v VPCSubnet) GetUpdateOptions() VPCSubnetUpdateOptions {
100 return VPCSubnetUpdateOptions{Label: v.Label}
101 }
102
103 func (c *Client) CreateVPCSubnet(
104 ctx context.Context,
105 opts VPCSubnetCreateOptions,
106 vpcID int,
107 ) (*VPCSubnet, error) {
108 e := formatAPIPath("vpcs/%d/subnets", vpcID)
109 return doPOSTRequest[VPCSubnet](ctx, c, e, opts)
110 }
111
112 func (c *Client) GetVPCSubnet(
113 ctx context.Context,
114 vpcID int,
115 subnetID int,
116 ) (*VPCSubnet, error) {
117 e := formatAPIPath("vpcs/%d/subnets/%d", vpcID, subnetID)
118 return doGETRequest[VPCSubnet](ctx, c, e)
119 }
120
121 func (c *Client) ListVPCSubnets(
122 ctx context.Context,
123 vpcID int,
124 opts *ListOptions,
125 ) ([]VPCSubnet, error) {
126 return getPaginatedResults[VPCSubnet](ctx, c, formatAPIPath("vpcs/%d/subnets", vpcID), opts)
127 }
128
129 func (c *Client) UpdateVPCSubnet(
130 ctx context.Context,
131 vpcID int,
132 subnetID int,
133 opts VPCSubnetUpdateOptions,
134 ) (*VPCSubnet, error) {
135 e := formatAPIPath("vpcs/%d/subnets/%d", vpcID, subnetID)
136 return doPUTRequest[VPCSubnet](ctx, c, e, opts)
137 }
138
139 func (c *Client) DeleteVPCSubnet(ctx context.Context, vpcID int, subnetID int) error {
140 e := formatAPIPath("vpcs/%d/subnets/%d", vpcID, subnetID)
141 return doDELETERequest(ctx, c, e)
142 }
143