forked from digitalocean/godo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reserved_ips.go
148 lines (121 loc) · 3.9 KB
/
reserved_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
147
148
package godo
import (
"context"
"fmt"
"net/http"
)
const resourceType = "ReservedIP"
const reservedIPsBasePath = "v2/reserved_ips"
// ReservedIPsService is an interface for interfacing with the reserved IPs
// endpoints of the Digital Ocean API.
// See: https://docs.digitalocean.com/reference/api/api-reference/#tag/Reserved-IPs
type ReservedIPsService interface {
List(context.Context, *ListOptions) ([]ReservedIP, *Response, error)
Get(context.Context, string) (*ReservedIP, *Response, error)
Create(context.Context, *ReservedIPCreateRequest) (*ReservedIP, *Response, error)
Delete(context.Context, string) (*Response, error)
}
// ReservedIPsServiceOp handles communication with the reserved IPs related methods of the
// DigitalOcean API.
type ReservedIPsServiceOp struct {
client *Client
}
var _ ReservedIPsService = &ReservedIPsServiceOp{}
// ReservedIP represents a Digital Ocean reserved IP.
type ReservedIP struct {
Region *Region `json:"region"`
Droplet *Droplet `json:"droplet"`
IP string `json:"ip"`
ProjectID string `json:"project_id"`
Locked bool `json:"locked"`
}
func (f ReservedIP) String() string {
return Stringify(f)
}
// URN returns the reserved IP in a valid DO API URN form.
func (f ReservedIP) URN() string {
return ToURN(resourceType, f.IP)
}
type reservedIPsRoot struct {
ReservedIPs []ReservedIP `json:"reserved_ips"`
Links *Links `json:"links"`
Meta *Meta `json:"meta"`
}
type reservedIPRoot struct {
ReservedIP *ReservedIP `json:"reserved_ip"`
Links *Links `json:"links,omitempty"`
}
// ReservedIPCreateRequest represents a request to create a reserved IP.
// Specify DropletID to assign the reserved IP to a Droplet or Region
// to reserve it to the region.
type ReservedIPCreateRequest struct {
Region string `json:"region,omitempty"`
DropletID int `json:"droplet_id,omitempty"`
ProjectID string `json:"project_id,omitempty"`
}
// List all reserved IPs.
func (r *ReservedIPsServiceOp) List(ctx context.Context, opt *ListOptions) ([]ReservedIP, *Response, error) {
path := reservedIPsBasePath
path, err := addOptions(path, opt)
if err != nil {
return nil, nil, err
}
req, err := r.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(reservedIPsRoot)
resp, err := r.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}
return root.ReservedIPs, resp, err
}
// Get an individual reserved IP.
func (r *ReservedIPsServiceOp) Get(ctx context.Context, ip string) (*ReservedIP, *Response, error) {
path := fmt.Sprintf("%s/%s", reservedIPsBasePath, ip)
req, err := r.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(reservedIPRoot)
resp, err := r.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.ReservedIP, resp, err
}
// Create a reserved IP. If the DropletID field of the request is not empty,
// the reserved IP will also be assigned to the droplet.
func (r *ReservedIPsServiceOp) Create(ctx context.Context, createRequest *ReservedIPCreateRequest) (*ReservedIP, *Response, error) {
path := reservedIPsBasePath
req, err := r.client.NewRequest(ctx, http.MethodPost, path, createRequest)
if err != nil {
return nil, nil, err
}
root := new(reservedIPRoot)
resp, err := r.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
return root.ReservedIP, resp, err
}
// Delete a reserved IP.
func (r *ReservedIPsServiceOp) Delete(ctx context.Context, ip string) (*Response, error) {
path := fmt.Sprintf("%s/%s", reservedIPsBasePath, ip)
req, err := r.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
resp, err := r.client.Do(ctx, req, nil)
return resp, err
}