Skip to content

Commit 3b92d78

Browse files
authored
feat(servergroup): add methods for adding/removing server to/from servergroup (#286)
1 parent 2de2f21 commit 3b92d78

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ See updating [Changelog example here](https://keepachangelog.com/en/1.0.0/)
55

66
## [Unreleased]
77

8+
### Added
9+
- Server groups: Add `AddServerToServerGroup` and `RemoveServerFromServerGroup` methods.
10+
811
## [6.11.0]
912

1013
### Added

upcloud/request/server_group.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,35 @@ type DeleteServerGroupRequest struct {
105105
func (s DeleteServerGroupRequest) RequestURL() string {
106106
return fmt.Sprintf("%s/%s", serverGroupBasePath, s.UUID)
107107
}
108+
109+
// AddServerToServerGroupRequest represents a request to add server to a server group
110+
type AddServerToServerGroupRequest struct {
111+
ServerUUID string `json:"uuid,omitempty"`
112+
UUID string `json:"-"`
113+
}
114+
115+
func (s AddServerToServerGroupRequest) RequestURL() string {
116+
return fmt.Sprintf("%s/%s/servers", serverGroupBasePath, s.UUID)
117+
}
118+
119+
// MarshalJSON is a custom marshaller that deals with deeply embedded values.
120+
func (r AddServerToServerGroupRequest) MarshalJSON() ([]byte, error) {
121+
type c AddServerToServerGroupRequest
122+
v := struct {
123+
Server c `json:"server"`
124+
}{}
125+
126+
v.Server = c(r)
127+
128+
return json.Marshal(&v)
129+
}
130+
131+
// RemoveServerFromServerGroupRequest represents a request to remove server from a server group
132+
type RemoveServerFromServerGroupRequest struct {
133+
ServerUUID string `json:"-"`
134+
UUID string `json:"-"`
135+
}
136+
137+
func (s RemoveServerFromServerGroupRequest) RequestURL() string {
138+
return fmt.Sprintf("%s/%s/servers/%s", serverGroupBasePath, s.UUID, s.ServerUUID)
139+
}

upcloud/request/server_group_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,22 @@ func TestModifyServerGroupRequest(t *testing.T) {
211211

212212
assert.Equal(t, "/server-group/id", r.RequestURL())
213213
}
214+
215+
func TestAddServerToServerGroupRequest(t *testing.T) {
216+
expected := `
217+
{
218+
"server": {
219+
"uuid": "test"
220+
}
221+
}
222+
`
223+
224+
r := AddServerToServerGroupRequest{
225+
UUID: "id",
226+
ServerUUID: "test",
227+
}
228+
229+
actual, err := json.Marshal(&r)
230+
assert.NoError(t, err)
231+
assert.JSONEq(t, expected, string(actual))
232+
}

upcloud/service/server_group.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ type ServerGroup interface {
1313
CreateServerGroup(ctx context.Context, r *request.CreateServerGroupRequest) (*upcloud.ServerGroup, error)
1414
ModifyServerGroup(ctx context.Context, r *request.ModifyServerGroupRequest) (*upcloud.ServerGroup, error)
1515
DeleteServerGroup(ctx context.Context, r *request.DeleteServerGroupRequest) error
16+
AddServerToServerGroup(ctx context.Context, r *request.AddServerToServerGroupRequest) error
17+
RemoveServerFromServerGroup(ctx context.Context, r *request.RemoveServerFromServerGroupRequest) error
1618
}
1719

1820
// GetServerGroups retrieves a list of server groups with context (EXPERIMENTAL).
@@ -49,3 +51,14 @@ func (s *Service) ModifyServerGroup(ctx context.Context, r *request.ModifyServer
4951
func (s *Service) DeleteServerGroup(ctx context.Context, r *request.DeleteServerGroupRequest) error {
5052
return s.delete(ctx, r)
5153
}
54+
55+
// AddServerToServerGroup adds a server to a server group with context (EXPERIMENTAL).
56+
func (s *Service) AddServerToServerGroup(ctx context.Context, r *request.AddServerToServerGroupRequest) error {
57+
var v interface{}
58+
return s.create(ctx, r, v)
59+
}
60+
61+
// RemoveServerFromServerGroup removes a server from a server group with context (EXPERIMENTAL).
62+
func (s *Service) RemoveServerFromServerGroup(ctx context.Context, r *request.RemoveServerFromServerGroupRequest) error {
63+
return s.delete(ctx, r)
64+
}

0 commit comments

Comments
 (0)