-
Notifications
You must be signed in to change notification settings - Fork 3
/
server_groups.go
108 lines (87 loc) · 2.78 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
package cloudscale
import (
"context"
"fmt"
"net/http"
)
const serverGroupsBasePath = "v1/server-groups"
type ServerGroup struct {
ZonalResource
TaggedResource
HREF string `json:"href"`
UUID string `json:"uuid"`
Name string `json:"name"`
Type string `json:"type"`
Servers []ServerStub `json:"servers"`
}
type ServerGroupRequest struct {
ZonalResourceRequest
TaggedResourceRequest
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
}
type ServerGroupService interface {
Create(ctx context.Context, createRequest *ServerGroupRequest) (*ServerGroup, error)
Get(ctx context.Context, serverGroupID string) (*ServerGroup, error)
Update(ctx context.Context, networkID string, updateRequest *ServerGroupRequest) error
Delete(ctx context.Context, serverGroupID string) error
List(ctx context.Context, modifiers ...ListRequestModifier) ([]ServerGroup, error)
}
type ServerGroupServiceOperations struct {
client *Client
}
func (s ServerGroupServiceOperations) Create(ctx context.Context, createRequest *ServerGroupRequest) (*ServerGroup, error) {
req, err := s.client.NewRequest(ctx, http.MethodPost, serverGroupsBasePath, createRequest)
if err != nil {
return nil, err
}
serverGroup := new(ServerGroup)
err = s.client.Do(ctx, req, serverGroup)
if err != nil {
return nil, err
}
return serverGroup, nil
}
func (s ServerGroupServiceOperations) Get(ctx context.Context, serverGroupID string) (*ServerGroup, error) {
path := fmt.Sprintf("%s/%s", serverGroupsBasePath, serverGroupID)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, err
}
serverGroup := new(ServerGroup)
err = s.client.Do(ctx, req, serverGroup)
if err != nil {
return nil, err
}
return serverGroup, nil
}
func (f ServerGroupServiceOperations) Update(ctx context.Context, serverGroupID string, updateRequest *ServerGroupRequest) error {
path := fmt.Sprintf("%s/%s", serverGroupsBasePath, serverGroupID)
req, err := f.client.NewRequest(ctx, http.MethodPatch, path, updateRequest)
if err != nil {
return err
}
err = f.client.Do(ctx, req, nil)
if err != nil {
return err
}
return nil
}
func (s ServerGroupServiceOperations) Delete(ctx context.Context, serverGroupID string) error {
return genericDelete(s.client, ctx, serverGroupsBasePath, serverGroupID)
}
func (s ServerGroupServiceOperations) List(ctx context.Context, modifiers ...ListRequestModifier) ([]ServerGroup, error) {
req, err := s.client.NewRequest(ctx, http.MethodGet, serverGroupsBasePath, nil)
if err != nil {
return nil, err
}
for _, modifier := range modifiers {
modifier(req)
}
serverGroups := []ServerGroup{}
err = s.client.Do(ctx, req, &serverGroups)
if err != nil {
return nil, err
}
return serverGroups, nil
}