interfaces.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 type LinodeInterface struct {
12 ID int `json:"id"`
13 Version int `json:"version"`
14 MACAddress string `json:"mac_address"`
15 Created *time.Time `json:"-"`
16 Updated *time.Time `json:"-"`
17 DefaultRoute *InterfaceDefaultRoute `json:"default_route"`
18 Public *PublicInterface `json:"public"`
19 VPC *VPCInterface `json:"vpc"`
20 VLAN *VLANInterface `json:"vlan"`
21 }
22
23 type InterfaceDefaultRoute struct {
24 IPv4 *bool `json:"ipv4,omitempty"`
25 IPv6 *bool `json:"ipv6,omitempty"`
26 }
27
28 type PublicInterface struct {
29 IPv4 *PublicInterfaceIPv4 `json:"ipv4"`
30 IPv6 *PublicInterfaceIPv6 `json:"ipv6"`
31 }
32
33 type PublicInterfaceIPv4 struct {
34 Addresses []PublicInterfaceIPv4Address `json:"addresses"`
35 Shared []PublicInterfaceIPv4Shared `json:"shared"`
36 }
37
38 type PublicInterfaceIPv6 struct {
39 Ranges []PublicInterfaceIPv6Range `json:"ranges"`
40 Shared []PublicInterfaceIPv6Range `json:"shared"`
41 SLAAC []PublicInterfaceIPv6SLAAC `json:"slaac"`
42 }
43
44 type PublicInterfaceIPv4Address struct {
45 Address string `json:"address"`
46 Primary bool `json:"primary"`
47 }
48
49 type PublicInterfaceIPv4Shared struct {
50 Address string `json:"address"`
51 LinodeID int `json:"linode_id"`
52 }
53
54 type PublicInterfaceIPv6Range struct {
55 Range string `json:"range"`
56 RouteTarget *string `json:"route_target"`
57 }
58
59 type PublicInterfaceIPv6SLAAC struct {
60 Prefix int `json:"prefix"`
61 Address string `json:"address"`
62 }
63
64 type VPCInterface struct {
65 VPCID int `json:"vpc_id"`
66 SubnetID int `json:"subnet_id"`
67 IPv4 VPCInterfaceIPv4 `json:"ipv4"`
68
69 // NOTE: IPv6 VPC interfaces may not currently be available to all users.
70 IPv6 VPCInterfaceIPv6 `json:"ipv6"`
71 }
72
73 type VPCInterfaceIPv4 struct {
74 Addresses []VPCInterfaceIPv4Address `json:"addresses"`
75 Ranges []VPCInterfaceIPv4Range `json:"ranges"`
76 }
77
78 type VPCInterfaceIPv4Address struct {
79 Address string `json:"address"`
80 Primary bool `json:"primary"`
81 NAT1To1Address *string `json:"nat_1_1_address"`
82 }
83
84 type VPCInterfaceIPv4Range struct {
85 Range string `json:"range"`
86 }
87
88 // VPCInterfaceIPv6 contains the IPv6 configuration for a VPC.
89 // NOTE: IPv6 VPC interfaces may not currently be available to all users.
90 type VPCInterfaceIPv6 struct {
91 SLAAC []VPCInterfaceIPv6SLAAC `json:"slaac"`
92 Ranges []VPCInterfaceIPv6Range `json:"ranges"`
93 IsPublic *bool `json:"is_public"`
94 }
95
96 // VPCInterfaceIPv6SLAAC contains the information for a single IPv6 SLAAC under a VPC.
97 // NOTE: IPv6 VPC interfaces may not currently be available to all users.
98 type VPCInterfaceIPv6SLAAC struct {
99 Range string `json:"range"`
100 Address string `json:"address"`
101 }
102
103 // VPCInterfaceIPv6Range contains the information for a single IPv6 range under a VPC.
104 // NOTE: IPv6 VPC interfaces may not currently be available to all users.
105 type VPCInterfaceIPv6Range struct {
106 Range string `json:"range"`
107 }
108
109 type VLANInterface struct {
110 VLANLabel string `json:"vlan_label"`
111 IPAMAddress *string `json:"ipam_address,omitempty"`
112 }
113
114 type LinodeInterfaceCreateOptions struct {
115 FirewallID **int `json:"firewall_id,omitempty"`
116 DefaultRoute *InterfaceDefaultRoute `json:"default_route,omitempty"`
117 Public *PublicInterfaceCreateOptions `json:"public,omitempty"`
118 VPC *VPCInterfaceCreateOptions `json:"vpc,omitempty"`
119 VLAN *VLANInterface `json:"vlan,omitempty"`
120 }
121
122 type LinodeInterfaceUpdateOptions struct {
123 DefaultRoute *InterfaceDefaultRoute `json:"default_route,omitempty"`
124 Public *PublicInterfaceCreateOptions `json:"public,omitempty"`
125 VPC *VPCInterfaceUpdateOptions `json:"vpc,omitempty"`
126 }
127
128 type PublicInterfaceCreateOptions struct {
129 IPv4 *PublicInterfaceIPv4CreateOptions `json:"ipv4,omitempty"`
130 IPv6 *PublicInterfaceIPv6CreateOptions `json:"ipv6,omitempty"`
131 }
132
133 type PublicInterfaceIPv4CreateOptions struct {
134 Addresses *[]PublicInterfaceIPv4AddressCreateOptions `json:"addresses,omitempty"`
135 }
136
137 type PublicInterfaceIPv4AddressCreateOptions struct {
138 Address *string `json:"address,omitempty"`
139 Primary *bool `json:"primary,omitempty"`
140 }
141
142 type PublicInterfaceIPv6CreateOptions struct {
143 Ranges *[]PublicInterfaceIPv6RangeCreateOptions `json:"ranges,omitempty"`
144 }
145
146 type PublicInterfaceIPv6RangeCreateOptions struct {
147 Range string `json:"range"`
148 }
149
150 type VPCInterfaceCreateOptions struct {
151 SubnetID int `json:"subnet_id"`
152 IPv4 *VPCInterfaceIPv4CreateOptions `json:"ipv4,omitempty"`
153 IPv6 *VPCInterfaceIPv6CreateOptions `json:"ipv6,omitempty"`
154 }
155
156 type VPCInterfaceIPv4CreateOptions struct {
157 Addresses *[]VPCInterfaceIPv4AddressCreateOptions `json:"addresses,omitempty"`
158 Ranges *[]VPCInterfaceIPv4RangeCreateOptions `json:"ranges,omitempty"`
159 }
160
161 type VPCInterfaceIPv4AddressCreateOptions struct {
162 Address *string `json:"address,omitempty"`
163 Primary *bool `json:"primary,omitempty"`
164 NAT1To1Address *string `json:"nat_1_1_address,omitempty"`
165 }
166
167 type VPCInterfaceIPv4RangeCreateOptions struct {
168 Range string `json:"range"`
169 }
170
171 // VPCInterfaceIPv6CreateOptions specifies IPv6 configuration parameters for VPC creation.
172 // NOTE: IPv6 interfaces may not currently be available to all users.
173 type VPCInterfaceIPv6CreateOptions struct {
174 SLAAC *[]VPCInterfaceIPv6SLAACCreateOptions `json:"slaac,omitempty"`
175 Ranges *[]VPCInterfaceIPv6RangeCreateOptions `json:"ranges,omitempty"`
176 IsPublic *bool `json:"is_public"`
177 }
178
179 // VPCInterfaceIPv6SLAACCreateOptions defines the IPv6 SLAAC configuration parameters for VPC creation.
180 // NOTE: IPv6 interfaces may not currently be available to all users.
181 type VPCInterfaceIPv6SLAACCreateOptions struct {
182 Range string `json:"range"`
183 }
184
185 // VPCInterfaceIPv6RangeCreateOptions defines the IPv6 range configuration parameters for VPC creation.
186 // NOTE: IPv6 interfaces may not currently be available to all users.
187 type VPCInterfaceIPv6RangeCreateOptions struct {
188 Range string `json:"range"`
189 }
190
191 type VPCInterfaceUpdateOptions struct {
192 IPv4 *VPCInterfaceIPv4CreateOptions `json:"ipv4,omitempty"`
193 IPv6 *VPCInterfaceIPv6CreateOptions `json:"ipv6,omitempty"`
194 }
195
196 type LinodeInterfacesUpgrade struct {
197 ConfigID int `json:"config_id"`
198 DryRun bool `json:"dry_run"`
199 Interfaces []LinodeInterface `json:"interfaces"`
200 }
201
202 type LinodeInterfacesUpgradeOptions struct {
203 ConfigID *int `json:"config_id,omitempty"`
204 DryRun *bool `json:"dry_run,omitempty"`
205 }
206
207 type InterfaceSettings struct {
208 NetworkHelper bool `json:"network_helper"`
209 DefaultRoute InterfaceDefaultRouteSetting `json:"default_route"`
210 }
211
212 type InterfaceSettingsUpdateOptions struct {
213 NetworkHelper *bool `json:"network_helper,omitempty"`
214 DefaultRoute *InterfaceDefaultRouteSettingUpdateOptions `json:"default_route,omitempty"`
215 }
216
217 type InterfaceDefaultRouteSettingUpdateOptions struct {
218 IPv4InterfaceID *int `json:"ipv4_interface_id,omitempty"`
219 IPv6InterfaceID *int `json:"ipv6_interface_id,omitempty"`
220 }
221
222 type InterfaceDefaultRouteSetting struct {
223 IPv4InterfaceID *int `json:"ipv4_interface_id"`
224 IPv4EligibleInterfaceIDs []int `json:"ipv4_eligible_interface_ids"`
225 IPv6InterfaceID *int `json:"ipv6_interface_id"`
226 IPv6EligibleInterfaceIDs []int `json:"ipv6_eligible_interface_ids"`
227 }
228
229 func (i *LinodeInterface) UnmarshalJSON(b []byte) error {
230 type Mask LinodeInterface
231
232 p := struct {
233 *Mask
234
235 Created *parseabletime.ParseableTime `json:"created"`
236 Updated *parseabletime.ParseableTime `json:"updated"`
237 }{
238 Mask: (*Mask)(i),
239 }
240
241 if err := json.Unmarshal(b, &p); err != nil {
242 return err
243 }
244
245 i.Created = (*time.Time)(p.Created)
246 i.Updated = (*time.Time)(p.Updated)
247
248 return nil
249 }
250
251 func (c *Client) ListInterfaces(ctx context.Context, linodeID int, opts *ListOptions) ([]LinodeInterface, error) {
252 e := formatAPIPath("linode/instances/%d/interfaces", linodeID)
253 return getPaginatedResults[LinodeInterface](ctx, c, e, opts)
254 }
255
256 func (c *Client) GetInterface(ctx context.Context, linodeID int, interfaceID int) (*LinodeInterface, error) {
257 e := formatAPIPath("linode/instances/%d/interfaces/%d", linodeID, interfaceID)
258 return doGETRequest[LinodeInterface](ctx, c, e)
259 }
260
261 func (c *Client) CreateInterface(ctx context.Context, linodeID int, opts LinodeInterfaceCreateOptions) (*LinodeInterface, error) {
262 e := formatAPIPath("linode/instances/%d/interfaces", linodeID)
263 return doPOSTRequest[LinodeInterface](ctx, c, e, opts)
264 }
265
266 func (c *Client) UpdateInterface(ctx context.Context, linodeID int, interfaceID int, opts LinodeInterfaceUpdateOptions) (*LinodeInterface, error) {
267 e := formatAPIPath("linode/instances/%d/interfaces/%d", linodeID, interfaceID)
268 return doPUTRequest[LinodeInterface](ctx, c, e, opts)
269 }
270
271 func (c *Client) DeleteInterface(ctx context.Context, linodeID int, interfaceID int) error {
272 e := formatAPIPath("linode/instances/%d/interfaces/%d", linodeID, interfaceID)
273 return doDELETERequest(ctx, c, e)
274 }
275
276 func (c *Client) UpgradeInterfaces(ctx context.Context, linodeID int, opts LinodeInterfacesUpgradeOptions) (*LinodeInterfacesUpgrade, error) {
277 e := formatAPIPath("linode/instances/%d/upgrade-interfaces", linodeID)
278 return doPOSTRequest[LinodeInterfacesUpgrade](ctx, c, e, opts)
279 }
280
281 func (c *Client) ListInterfaceFirewalls(ctx context.Context, linodeID int, interfaceID int, opts *ListOptions) ([]Firewall, error) {
282 e := formatAPIPath("linode/instances/%d/interfaces/%d/firewalls", linodeID, interfaceID)
283 return getPaginatedResults[Firewall](ctx, c, e, opts)
284 }
285
286 func (c *Client) GetInterfaceSettings(ctx context.Context, linodeID int) (*InterfaceSettings, error) {
287 e := formatAPIPath("linode/instances/%d/interfaces/settings", linodeID)
288 return doGETRequest[InterfaceSettings](ctx, c, e)
289 }
290
291 func (c *Client) UpdateInterfaceSettings(ctx context.Context, linodeID int, opts InterfaceSettingsUpdateOptions) (*InterfaceSettings, error) {
292 e := formatAPIPath("linode/instances/%d/interfaces/settings", linodeID)
293 return doPUTRequest[InterfaceSettings](ctx, c, e, opts)
294 }
295