-
Notifications
You must be signed in to change notification settings - Fork 82
/
instance_ips.go
146 lines (124 loc) · 4.69 KB
/
instance_ips.go
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package linodego
import (
"context"
"encoding/json"
"fmt"
"net/url"
)
// InstanceIPAddressResponse contains the IPv4 and IPv6 details for an Instance
type InstanceIPAddressResponse struct {
IPv4 *InstanceIPv4Response `json:"ipv4"`
IPv6 *InstanceIPv6Response `json:"ipv6"`
}
// InstanceIPv4Response contains the details of all IPv4 addresses associated with an Instance
type InstanceIPv4Response struct {
Public []*InstanceIP `json:"public"`
Private []*InstanceIP `json:"private"`
Shared []*InstanceIP `json:"shared"`
Reserved []*InstanceIP `json:"reserved"`
}
// InstanceIP represents an Instance IP with additional DNS and networking details
type InstanceIP struct {
Address string `json:"address"`
Gateway string `json:"gateway"`
SubnetMask string `json:"subnet_mask"`
Prefix int `json:"prefix"`
Type InstanceIPType `json:"type"`
Public bool `json:"public"`
RDNS string `json:"rdns"`
LinodeID int `json:"linode_id"`
Region string `json:"region"`
VPCNAT1To1 *InstanceIPNAT1To1 `json:"vpc_nat_1_1"`
}
// InstanceIPv6Response contains the IPv6 addresses and ranges for an Instance
type InstanceIPv6Response struct {
LinkLocal *InstanceIP `json:"link_local"`
SLAAC *InstanceIP `json:"slaac"`
Global []IPv6Range `json:"global"`
}
// InstanceIPNAT1To1 contains information about the NAT 1:1 mapping
// of a public IP address to a VPC subnet.
type InstanceIPNAT1To1 struct {
Address string `json:"address"`
SubnetID int `json:"subnet_id"`
VPCID int `json:"vpc_id"`
}
// IPv6Range represents a range of IPv6 addresses routed to a single Linode in a given Region
type IPv6Range struct {
Range string `json:"range"`
Region string `json:"region"`
Prefix int `json:"prefix"`
RouteTarget string `json:"route_target"`
// These fields are only returned by GetIPv6Range(...)
IsBGP bool `json:"is_bgp"`
Linodes []int `json:"linodes"`
}
// InstanceIPType constants start with IPType and include Linode Instance IP Types
type InstanceIPType string
// InstanceIPType constants represent the IP types an Instance IP may be
const (
IPTypeIPv4 InstanceIPType = "ipv4"
IPTypeIPv6 InstanceIPType = "ipv6"
IPTypeIPv6Pool InstanceIPType = "ipv6/pool"
IPTypeIPv6Range InstanceIPType = "ipv6/range"
)
// GetInstanceIPAddresses gets the IPAddresses for a Linode instance
func (c *Client) GetInstanceIPAddresses(ctx context.Context, linodeID int) (*InstanceIPAddressResponse, error) {
e := fmt.Sprintf("linode/instances/%d/ips", linodeID)
req := c.R(ctx).SetResult(&InstanceIPAddressResponse{})
r, err := coupleAPIErrors(req.Get(e))
if err != nil {
return nil, err
}
return r.Result().(*InstanceIPAddressResponse), nil
}
// GetInstanceIPAddress gets the IPAddress for a Linode instance matching a supplied IP address
func (c *Client) GetInstanceIPAddress(ctx context.Context, linodeID int, ipaddress string) (*InstanceIP, error) {
ipaddress = url.PathEscape(ipaddress)
e := fmt.Sprintf("linode/instances/%d/ips/%s", linodeID, ipaddress)
req := c.R(ctx).SetResult(&InstanceIP{})
r, err := coupleAPIErrors(req.Get(e))
if err != nil {
return nil, err
}
return r.Result().(*InstanceIP), nil
}
// AddInstanceIPAddress adds a public or private IP to a Linode instance
func (c *Client) AddInstanceIPAddress(ctx context.Context, linodeID int, public bool) (*InstanceIP, error) {
instanceipRequest := struct {
Type string `json:"type"`
Public bool `json:"public"`
}{"ipv4", public}
body, err := json.Marshal(instanceipRequest)
if err != nil {
return nil, err
}
e := fmt.Sprintf("linode/instances/%d/ips", linodeID)
req := c.R(ctx).SetResult(&InstanceIP{}).SetBody(string(body))
r, err := coupleAPIErrors(req.Post(e))
if err != nil {
return nil, err
}
return r.Result().(*InstanceIP), nil
}
// UpdateInstanceIPAddress updates the IPAddress with the specified instance id and IP address
func (c *Client) UpdateInstanceIPAddress(ctx context.Context, linodeID int, ipAddress string, opts IPAddressUpdateOptions) (*InstanceIP, error) {
body, err := json.Marshal(opts)
if err != nil {
return nil, err
}
ipAddress = url.PathEscape(ipAddress)
e := fmt.Sprintf("linode/instances/%d/ips/%s", linodeID, ipAddress)
req := c.R(ctx).SetResult(&InstanceIP{}).SetBody(string(body))
r, err := coupleAPIErrors(req.Put(e))
if err != nil {
return nil, err
}
return r.Result().(*InstanceIP), nil
}
func (c *Client) DeleteInstanceIPAddress(ctx context.Context, linodeID int, ipAddress string) error {
ipAddress = url.PathEscape(ipAddress)
e := fmt.Sprintf("linode/instances/%d/ips/%s", linodeID, ipAddress)
_, err := coupleAPIErrors(c.R(ctx).Delete(e))
return err
}