Skip to content

Commit 278d4fc

Browse files
authored
Merge pull request #44 from thomasheller/delete-server-and-storages
Support deletion of servers including all attached storages, fixes #43
2 parents d393d55 + 281aa3e commit 278d4fc

File tree

4 files changed

+124
-3
lines changed

4 files changed

+124
-3
lines changed

upcloud/request/server.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package request
33
import (
44
"encoding/xml"
55
"fmt"
6-
"github.com/UpCloudLtd/upcloud-go-api/upcloud"
76
"strings"
87
"time"
8+
9+
"github.com/UpCloudLtd/upcloud-go-api/upcloud"
910
)
1011

1112
// Constants
@@ -192,6 +193,16 @@ func (r *DeleteServerRequest) RequestURL() string {
192193
return fmt.Sprintf("/server/%s", r.UUID)
193194
}
194195

196+
// DeleteServerAndStoragesRequest represents a request to delete a server and all attached storages
197+
type DeleteServerAndStoragesRequest struct {
198+
UUID string
199+
}
200+
201+
// RequestURL implements the Request interface
202+
func (r *DeleteServerAndStoragesRequest) RequestURL() string {
203+
return fmt.Sprintf("/server/%s/?storages=1", r.UUID)
204+
}
205+
195206
// TagServerRequest represents a request to tag a server with one or more tags
196207
type TagServerRequest struct {
197208
UUID string

upcloud/request/server_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package request
22

33
import (
44
"encoding/xml"
5-
"github.com/UpCloudLtd/upcloud-go-api/upcloud"
6-
"github.com/stretchr/testify/assert"
75
"testing"
86
"time"
7+
8+
"github.com/UpCloudLtd/upcloud-go-api/upcloud"
9+
"github.com/stretchr/testify/assert"
910
)
1011

1112
// TestGetServerDetailsRequest tests that GetServerDetailsRequest objects behave correctly
@@ -127,6 +128,15 @@ func TestDeleteServerRequest(t *testing.T) {
127128
assert.Equal(t, "/server/foo", request.RequestURL())
128129
}
129130

131+
// TestDeleteServerAndStoragesRequest tests that DeleteServerAndStoragesRequest objects behave correctly
132+
func TestDeleteServerAndStoragesRequest(t *testing.T) {
133+
request := DeleteServerAndStoragesRequest{
134+
UUID: "foo",
135+
}
136+
137+
assert.Equal(t, "/server/foo/?storages=1", request.RequestURL())
138+
}
139+
130140
// TestTagServerRequest tests that TestTagServer behaves correctly
131141
func TestTagServerRequest(t *testing.T) {
132142
// Test with multiple tags

upcloud/service/service.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,17 @@ func (s *Service) DeleteServer(r *request.DeleteServerRequest) error {
270270
return nil
271271
}
272272

273+
// DeleteServerAndStorages deletes the specified server and all attached storages
274+
func (s *Service) DeleteServerAndStorages(r *request.DeleteServerAndStoragesRequest) error {
275+
err := s.client.PerformDeleteRequest(s.client.CreateRequestUrl(r.RequestURL()))
276+
277+
if err != nil {
278+
return parseServiceError(err)
279+
}
280+
281+
return nil
282+
}
283+
273284
// TagServer tags a server with with one or more tags
274285
func (s *Service) TagServer(r *request.TagServerRequest) (*upcloud.ServerDetails, error) {
275286
serverDetails := upcloud.ServerDetails{}

upcloud/service/service_test.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,16 @@ func TestCreateModifyDeleteServer(t *testing.T) {
138138
serverDetails := createServer("TestCreateModifyDeleteServer")
139139
t.Logf("Server %s with UUID %s created", serverDetails.Title, serverDetails.UUID)
140140

141+
// Get details about the storage (UUID is required for testing)
142+
if len(serverDetails.StorageDevices) == 0 {
143+
t.Errorf("Server %s with UUID %s has no storages attached", serverDetails.Title, serverDetails.UUID)
144+
}
145+
146+
firstStorage := serverDetails.StorageDevices[0]
147+
storageUUID := firstStorage.UUID
148+
149+
t.Logf("First storage of server with UUID %s has UUID %s", serverDetails.UUID, storageUUID)
150+
141151
// Modify the server
142152
t.Log("Modifying the server ...")
143153

@@ -167,6 +177,76 @@ func TestCreateModifyDeleteServer(t *testing.T) {
167177
t.Logf("Deleting the server with UUID %s...", serverDetails.UUID)
168178
deleteServer(serverDetails.UUID)
169179
t.Log("Server is now deleted")
180+
181+
// Check if the storage still exists
182+
storages, err := svc.GetStorages(&request.GetStoragesRequest{
183+
Access: upcloud.StorageAccessPrivate,
184+
})
185+
handleError(err)
186+
187+
found := false
188+
189+
for _, storage := range storages.Storages {
190+
if storage.UUID == storageUUID {
191+
found = true
192+
break
193+
}
194+
}
195+
196+
if !found {
197+
t.Errorf("Storage with UUID %s not found. It should still exist after deleting server with UUID %s", storageUUID, serverDetails.UUID)
198+
}
199+
200+
t.Log("Storage still exists")
201+
}
202+
203+
// TestCreateDeleteServerAndStorage performs the following actions:
204+
//
205+
// - creates a server
206+
// - deletes the server including storage
207+
func TestCreateDeleteServerAndStorage(t *testing.T) {
208+
if testing.Short() {
209+
t.Skip("Skipping test in short mode")
210+
}
211+
t.Parallel()
212+
213+
// Create a server
214+
serverDetails := createServer("TestCreateDeleteServerAndStorage")
215+
t.Logf("Server %s with UUID %s created", serverDetails.Title, serverDetails.UUID)
216+
217+
// Get details about the storage (UUID is required for testing)
218+
if len(serverDetails.StorageDevices) == 0 {
219+
t.Errorf("Server %s with UUID %s has no storages attached", serverDetails.Title, serverDetails.UUID)
220+
}
221+
222+
firstStorage := serverDetails.StorageDevices[0]
223+
storageUUID := firstStorage.UUID
224+
225+
t.Logf("First storage of server with UUID %s has UUID %s", serverDetails.UUID, storageUUID)
226+
227+
// Stop the server
228+
t.Logf("Stopping server with UUID %s ...", serverDetails.UUID)
229+
stopServer(serverDetails.UUID)
230+
t.Log("Server is now stopped")
231+
232+
// Delete the server and storage
233+
t.Logf("Deleting the server with UUID %s, including storages...", serverDetails.UUID)
234+
deleteServerAndStorages(serverDetails.UUID)
235+
t.Log("Server is now deleted")
236+
237+
// Check if the storage was deleted
238+
storages, err := svc.GetStorages(&request.GetStoragesRequest{
239+
Access: upcloud.StorageAccessPrivate,
240+
})
241+
handleError(err)
242+
243+
for _, storage := range storages.Storages {
244+
if storage.UUID == storageUUID {
245+
t.Errorf("Storage with UUID %s still exists. It should have been deleted with server with UUID %s", storageUUID, serverDetails.UUID)
246+
}
247+
}
248+
249+
t.Log("Storage was deleted, too")
170250
}
171251

172252
// TestCreateModifyDeleteStorage performs the following actions:
@@ -713,6 +793,15 @@ func deleteServer(uuid string) {
713793
handleError(err)
714794
}
715795

796+
// Deletes the specified server and storages
797+
func deleteServerAndStorages(uuid string) {
798+
err := svc.DeleteServerAndStorages(&request.DeleteServerAndStoragesRequest{
799+
UUID: uuid,
800+
})
801+
802+
handleError(err)
803+
}
804+
716805
// Creates a piece of storage and returns the details about it, panic if creation fails
717806
func createStorage() *upcloud.StorageDetails {
718807
createStorageRequest := request.CreateStorageRequest{

0 commit comments

Comments
 (0)