/
server_groups.go
150 lines (138 loc) · 4.85 KB
/
server_groups.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
149
150
package gobrightbox
import (
"time"
)
// ServerGroup represents a server group
// https://api.gb1.brightbox.com/1.0/#server_group
type ServerGroup struct {
ID string
Name string
CreatedAt *time.Time `json:"created_at"`
Description string
Default bool
Fqdn string
Account Account `json:"account"`
Servers []Server
FirewallPolicy *FirewallPolicy `json:"firewall_policy"`
}
// ServerGroupOptions is used in combination with CreateServerGroup and
// UpdateServerGroup to create and update server groups
type ServerGroupOptions struct {
ID string `json:"-"`
Name *string `json:"name,omitempty"`
Description *string `json:"description,omitempty"`
}
type serverGroupMemberOptions struct {
Servers []serverGroupMember `json:"servers"`
Destination string `json:"destination,omitempty"`
}
type serverGroupMember struct {
Server string `json:"server,omitempty"`
}
// ServerGroups retrieves a list of all server groups
func (c *Client) ServerGroups() ([]ServerGroup, error) {
var groups []ServerGroup
_, err := c.MakeAPIRequest("GET", "/1.0/server_groups", nil, &groups)
if err != nil {
return nil, err
}
return groups, err
}
// ServerGroup retrieves a detailed view on one server group
func (c *Client) ServerGroup(identifier string) (*ServerGroup, error) {
group := new(ServerGroup)
_, err := c.MakeAPIRequest("GET", "/1.0/server_groups/"+identifier, nil, group)
if err != nil {
return nil, err
}
return group, err
}
// CreateServerGroup creates a new server group
//
// It takes an instance of ServerGroupOptions. Not all attributes can be
// specified at create time (such as ID, which is allocated for you).
func (c *Client) CreateServerGroup(newServerGroup *ServerGroupOptions) (*ServerGroup, error) {
group := new(ServerGroup)
_, err := c.MakeAPIRequest("POST", "/1.0/server_groups", newServerGroup, &group)
if err != nil {
return nil, err
}
return group, nil
}
// UpdateServerGroup updates an existing server groups's attributes. Not all
// attributes can be changed (such as ID).
//
// Specify the server group you want to update using the ServerGroupOptions ID
// field.
//
// To change group memberships, use AddServersToServerGroup,
// RemoveServersFromServerGroup and MoveServersToServerGroup.
func (c *Client) UpdateServerGroup(updateServerGroup *ServerGroupOptions) (*ServerGroup, error) {
group := new(ServerGroup)
_, err := c.MakeAPIRequest("PUT", "/1.0/server_groups/"+updateServerGroup.ID, updateServerGroup, &group)
if err != nil {
return nil, err
}
return group, nil
}
// DestroyServerGroup destroys an existing server group
func (c *Client) DestroyServerGroup(identifier string) error {
_, err := c.MakeAPIRequest("DELETE", "/1.0/server_groups/"+identifier, nil, nil)
if err != nil {
return err
}
return nil
}
// AddServersToServerGroup adds servers to an existing server group.
//
// The identifier parameter specifies the destination group.
//
// The serverIDs paramater specifies the identifiers of the servers you want to add.
func (c *Client) AddServersToServerGroup(identifier string, serverIDs []string) (*ServerGroup, error) {
group := new(ServerGroup)
opts := new(serverGroupMemberOptions)
for _, id := range serverIDs {
opts.Servers = append(opts.Servers, serverGroupMember{Server: id})
}
_, err := c.MakeAPIRequest("POST", "/1.0/server_groups/"+identifier+"/add_servers", opts, &group)
if err != nil {
return nil, err
}
return group, nil
}
// RemoveServersFromServerGroup removes servers from an existing server group.
//
// The identifier parameter specifies the group.
//
// The serverIDs paramater specifies the identifiers of the servers you want to remove.
func (c *Client) RemoveServersFromServerGroup(identifier string, serverIDs []string) (*ServerGroup, error) {
group := new(ServerGroup)
opts := new(serverGroupMemberOptions)
for _, id := range serverIDs {
opts.Servers = append(opts.Servers, serverGroupMember{Server: id})
}
_, err := c.MakeAPIRequest("POST", "/1.0/server_groups/"+identifier+"/remove_servers", opts, &group)
if err != nil {
return nil, err
}
return group, nil
}
// MoveServersToServerGroup atomically moves servers from one group to another.
//
// The src parameter specifies the group to which the servers currently belong
//
// The dst parameter specifies the group to which you want to move the servers.
//
// The serverIDs parameter specifies the identifiers of the servers you want to move.
func (c *Client) MoveServersToServerGroup(src string, dst string, serverIDs []string) (*ServerGroup, error) {
group := new(ServerGroup)
opts := serverGroupMemberOptions{Destination: dst}
for _, id := range serverIDs {
opts.Servers = append(opts.Servers, serverGroupMember{Server: id})
}
_, err := c.MakeAPIRequest("POST", "/1.0/server_groups/"+src+"/move_servers", opts, &group)
if err != nil {
return nil, err
}
return group, nil
}