-
Notifications
You must be signed in to change notification settings - Fork 7
/
serverip.go
133 lines (116 loc) · 4.61 KB
/
serverip.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
package gsclient
import (
"context"
"errors"
"net/http"
"path"
)
// ServerIPRelationOperator provides an interface for operations on Server-IP relations.
type ServerIPRelationOperator interface {
GetServerIPList(ctx context.Context, id string) ([]ServerIPRelationProperties, error)
GetServerIP(ctx context.Context, serverID, ipID string) (ServerIPRelationProperties, error)
CreateServerIP(ctx context.Context, id string, body ServerIPRelationCreateRequest) error
DeleteServerIP(ctx context.Context, serverID, ipID string) error
LinkIP(ctx context.Context, serverID string, ipID string) error
UnlinkIP(ctx context.Context, serverID string, ipID string) error
}
// ServerIPRelationList holds a list of relations between a server and IP addresses.
type ServerIPRelationList struct {
// Array of relations between a server and IP addresses.
List []ServerIPRelationProperties `json:"ip_relations"`
}
// ServerIPRelation represents a single relation between a server and an IP address.
type ServerIPRelation struct {
// Properties of a relation between a server and IP addresses.
Properties ServerIPRelationProperties `json:"ip_relation"`
}
// ServerIPRelationProperties holds properties of a relation between a server and an IP address.
type ServerIPRelationProperties struct {
// The UUID of the server that this IP is attached to.
ServerUUID string `json:"server_uuid"`
// Defines the date and time the object was initially created.
CreateTime GSTime `json:"create_time"`
// The prefix of the IP Address.
Prefix string `json:"prefix"`
// Either 4 or 6.
Family int `json:"family"`
// The UUID of an object is always unique, and refers to a specific object.
ObjectUUID string `json:"object_uuid"`
// The IP Address (v4 or v6).
IP string `json:"ip"`
}
// ServerIPRelationCreateRequest represents a request for creating a relation between a server and an IP address.
type ServerIPRelationCreateRequest struct {
// You can only attach 1 IPv4 and/or IPv6 to a server based on the IP address's UUID.
ObjectUUID string `json:"object_uuid"`
}
// GetServerIPList gets a list of a specific server's IP addresses.
//
// See: https://gridscale.io/en//api-documentation/index.html#operation/getServerLinkedIps
func (c *Client) GetServerIPList(ctx context.Context, id string) ([]ServerIPRelationProperties, error) {
if id == "" {
return nil, errors.New("'id' is required")
}
r := gsRequest{
uri: path.Join(apiServerBase, id, "ips"),
method: http.MethodGet,
skipCheckingRequest: true,
}
var response ServerIPRelationList
err := r.execute(ctx, *c, &response)
return response.List, err
}
// GetServerIP gets an IP address of a specific server.
//
// See: https://gridscale.io/en//api-documentation/index.html#operation/getServerLinkedIp
func (c *Client) GetServerIP(ctx context.Context, serverID, ipID string) (ServerIPRelationProperties, error) {
if serverID == "" || ipID == "" {
return ServerIPRelationProperties{}, errors.New("'serverID' and 'ipID' are required")
}
r := gsRequest{
uri: path.Join(apiServerBase, serverID, "ips", ipID),
method: http.MethodGet,
skipCheckingRequest: true,
}
var response ServerIPRelation
err := r.execute(ctx, *c, &response)
return response.Properties, err
}
// CreateServerIP creates a link between a server and an IP address.
//
// See: https://gridscale.io/en//api-documentation/index.html#operation/linkIpToServer
func (c *Client) CreateServerIP(ctx context.Context, id string, body ServerIPRelationCreateRequest) error {
if id == "" || body.ObjectUUID == "" {
return errors.New("'server_id' and 'ip_id' are required")
}
r := gsRequest{
uri: path.Join(apiServerBase, id, "ips"),
method: http.MethodPost,
body: body,
}
return r.execute(ctx, *c, nil)
}
// DeleteServerIP removes a link between a server and an IP.
//
// See: https://gridscale.io/en//api-documentation/index.html#operation/unlinkIpFromServer
func (c *Client) DeleteServerIP(ctx context.Context, serverID, ipID string) error {
if serverID == "" || ipID == "" {
return errors.New("'serverID' and 'ipID' are required")
}
r := gsRequest{
uri: path.Join(apiServerBase, serverID, "ips", ipID),
method: http.MethodDelete,
}
return r.execute(ctx, *c, nil)
}
// LinkIP attaches an IP address to a server.
func (c *Client) LinkIP(ctx context.Context, serverID string, ipID string) error {
body := ServerIPRelationCreateRequest{
ObjectUUID: ipID,
}
return c.CreateServerIP(ctx, serverID, body)
}
// UnlinkIP detaches an IP address from a server.
func (c *Client) UnlinkIP(ctx context.Context, serverID string, ipID string) error {
return c.DeleteServerIP(ctx, serverID, ipID)
}