Skip to content

Commit 69bdf03

Browse files
authored
feat(storage): delete request parameter backups (#198)
Possibility to control what to do with backups related to the storage that is about to be deleted using `Backups` field in `DeleteStorageRequest`.
1 parent f54c3ae commit 69bdf03

5 files changed

Lines changed: 132 additions & 36 deletions

File tree

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+
- storage: possibility to control what to do with backups related to the storage that is about to be deleted
10+
811
## [5.2.1]
912

1013
### Added

upcloud/request/storage.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@ import (
88
"github.com/UpCloudLtd/upcloud-go-api/v5/upcloud"
99
)
1010

11+
type DeleteStorageBackupsMode string
12+
1113
// Constants
1214
const (
1315
StorageImportSourceDirectUpload = "direct_upload"
1416
StorageImportSourceHTTPImport = "http_import"
17+
18+
DeleteStorageBackupsModeKeep DeleteStorageBackupsMode = "keep"
19+
DeleteStorageBackupsModeKeepLatest DeleteStorageBackupsMode = "keep_latest"
20+
DeleteStorageBackupsModeDelete DeleteStorageBackupsMode = "delete"
1521
)
1622

1723
// GetStoragesRequest represents a request for retrieving all or some storages
@@ -156,11 +162,15 @@ func (r DetachStorageRequest) MarshalJSON() ([]byte, error) {
156162

157163
// DeleteStorageRequest represents a request to delete a storage device
158164
type DeleteStorageRequest struct {
159-
UUID string
165+
UUID string
166+
Backups DeleteStorageBackupsMode
160167
}
161168

162169
// RequestURL implements the Request interface
163170
func (r *DeleteStorageRequest) RequestURL() string {
171+
if r.Backups != "" {
172+
return fmt.Sprintf("/storage/%s?backups=%s", r.UUID, r.Backups)
173+
}
164174
return fmt.Sprintf("/storage/%s", r.UUID)
165175
}
166176

upcloud/request/storage_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,19 @@ func TestDeleteStorageRequest(t *testing.T) {
140140
request := DeleteStorageRequest{
141141
UUID: "foo",
142142
}
143+
assert.Equal(t, "/storage/foo", request.RequestURL())
143144

145+
request = DeleteStorageRequest{
146+
UUID: "foo",
147+
Backups: "",
148+
}
144149
assert.Equal(t, "/storage/foo", request.RequestURL())
150+
151+
request = DeleteStorageRequest{
152+
UUID: "foo",
153+
Backups: DeleteStorageBackupsModeDelete,
154+
}
155+
assert.Equal(t, "/storage/foo?backups=delete", request.RequestURL())
145156
}
146157

147158
// TestCloneStorageRequest testa that CloneStorageRequest objects behave correctly

upcloud/service/fixtures/createrestorebackup.yaml

Lines changed: 93 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ interactions:
1010
Content-Type:
1111
- application/json
1212
User-Agent:
13-
- upcloud-go-api-ctx/4.9.0
13+
- upcloud-go-api/5.2.1
1414
url: https://api.upcloud.com/1.3/storage
1515
method: POST
1616
response:
@@ -26,7 +26,7 @@ interactions:
2626
"backups" : {
2727
"backup" : []
2828
},
29-
"created" : "2022-10-27T18:22:25Z",
29+
"created" : "2023-01-12T07:59:21Z",
3030
"license" : 0,
3131
"servers" : {
3232
"server" : []
@@ -36,7 +36,7 @@ interactions:
3636
"tier" : "maxiops",
3737
"title" : "Test storage",
3838
"type" : "normal",
39-
"uuid" : "01e9531b-ab82-444f-a4d2-d4cd5518d2b4",
39+
"uuid" : "01a9b582-2254-4683-b408-1d41d6910b75",
4040
"zone" : "fi-hel2"
4141
}
4242
}
@@ -46,7 +46,7 @@ interactions:
4646
Content-Type:
4747
- application/json; charset=UTF-8
4848
Date:
49-
- Thu, 27 Oct 2022 18:22:25 GMT
49+
- Thu, 12 Jan 2023 07:59:21 GMT
5050
Server:
5151
- Apache
5252
Strict-Transport-Security:
@@ -55,34 +55,34 @@ interactions:
5555
code: 201
5656
duration: ""
5757
- request:
58-
body: '{"storage":{"title":"backup-1666894954000000000"}}'
58+
body: '{"storage":{"title":"backup-1673510370000000000"}}'
5959
form: {}
6060
headers:
6161
Accept:
6262
- application/json
6363
Content-Type:
6464
- application/json
6565
User-Agent:
66-
- upcloud-go-api-ctx/4.9.0
67-
url: https://api.upcloud.com/1.3/storage/01e9531b-ab82-444f-a4d2-d4cd5518d2b4/backup
66+
- upcloud-go-api/5.2.1
67+
url: https://api.upcloud.com/1.3/storage/01a9b582-2254-4683-b408-1d41d6910b75/backup
6868
method: POST
6969
response:
7070
body: |
7171
{
7272
"storage" : {
7373
"access" : "private",
74-
"created" : "2022-10-27T18:22:34Z",
74+
"created" : "2023-01-12T07:59:30Z",
7575
"license" : 0,
76-
"origin" : "01e9531b-ab82-444f-a4d2-d4cd5518d2b4",
76+
"origin" : "01a9b582-2254-4683-b408-1d41d6910b75",
7777
"progress" : "0",
7878
"servers" : {
7979
"server" : []
8080
},
8181
"size" : 10,
8282
"state" : "maintenance",
83-
"title" : "backup-1666894954000000000",
83+
"title" : "backup-1673510370000000000",
8484
"type" : "backup",
85-
"uuid" : "01e2b24b-b76f-4568-b5ca-40b5066eafd7",
85+
"uuid" : "0160e290-c379-4183-9f50-586ac8b41500",
8686
"zone" : "fi-hel2"
8787
}
8888
}
@@ -92,7 +92,7 @@ interactions:
9292
Content-Type:
9393
- application/json; charset=UTF-8
9494
Date:
95-
- Thu, 27 Oct 2022 18:22:34 GMT
95+
- Thu, 12 Jan 2023 07:59:30 GMT
9696
Server:
9797
- Apache
9898
Strict-Transport-Security:
@@ -109,8 +109,8 @@ interactions:
109109
Content-Type:
110110
- application/json
111111
User-Agent:
112-
- upcloud-go-api-ctx/4.9.0
113-
url: https://api.upcloud.com/1.3/storage/01e9531b-ab82-444f-a4d2-d4cd5518d2b4
112+
- upcloud-go-api/5.2.1
113+
url: https://api.upcloud.com/1.3/storage/01a9b582-2254-4683-b408-1d41d6910b75
114114
method: GET
115115
response:
116116
body: |
@@ -124,10 +124,10 @@ interactions:
124124
},
125125
"backups" : {
126126
"backup" : [
127-
"01e2b24b-b76f-4568-b5ca-40b5066eafd7"
127+
"0160e290-c379-4183-9f50-586ac8b41500"
128128
]
129129
},
130-
"created" : "2022-10-27T18:22:25Z",
130+
"created" : "2023-01-12T07:59:21Z",
131131
"license" : 0,
132132
"servers" : {
133133
"server" : []
@@ -137,7 +137,7 @@ interactions:
137137
"tier" : "maxiops",
138138
"title" : "Test storage",
139139
"type" : "normal",
140-
"uuid" : "01e9531b-ab82-444f-a4d2-d4cd5518d2b4",
140+
"uuid" : "01a9b582-2254-4683-b408-1d41d6910b75",
141141
"zone" : "fi-hel2"
142142
}
143143
}
@@ -147,7 +147,7 @@ interactions:
147147
Content-Type:
148148
- application/json; charset=UTF-8
149149
Date:
150-
- Thu, 27 Oct 2022 18:22:40 GMT
150+
- Thu, 12 Jan 2023 07:59:36 GMT
151151
Server:
152152
- Apache
153153
Strict-Transport-Security:
@@ -164,25 +164,25 @@ interactions:
164164
Content-Type:
165165
- application/json
166166
User-Agent:
167-
- upcloud-go-api-ctx/4.9.0
168-
url: https://api.upcloud.com/1.3/storage/01e2b24b-b76f-4568-b5ca-40b5066eafd7
167+
- upcloud-go-api/5.2.1
168+
url: https://api.upcloud.com/1.3/storage/0160e290-c379-4183-9f50-586ac8b41500
169169
method: GET
170170
response:
171171
body: |
172172
{
173173
"storage" : {
174174
"access" : "private",
175-
"created" : "2022-10-27T18:22:34Z",
175+
"created" : "2023-01-12T07:59:30Z",
176176
"license" : 0,
177-
"origin" : "01e9531b-ab82-444f-a4d2-d4cd5518d2b4",
177+
"origin" : "01a9b582-2254-4683-b408-1d41d6910b75",
178178
"servers" : {
179179
"server" : []
180180
},
181181
"size" : 10,
182182
"state" : "online",
183-
"title" : "backup-1666894954000000000",
183+
"title" : "backup-1673510370000000000",
184184
"type" : "backup",
185-
"uuid" : "01e2b24b-b76f-4568-b5ca-40b5066eafd7",
185+
"uuid" : "0160e290-c379-4183-9f50-586ac8b41500",
186186
"zone" : "fi-hel2"
187187
}
188188
}
@@ -192,7 +192,7 @@ interactions:
192192
Content-Type:
193193
- application/json; charset=UTF-8
194194
Date:
195-
- Thu, 27 Oct 2022 18:22:40 GMT
195+
- Thu, 12 Jan 2023 07:59:36 GMT
196196
Server:
197197
- Apache
198198
Strict-Transport-Security:
@@ -201,22 +201,22 @@ interactions:
201201
code: 200
202202
duration: ""
203203
- request:
204-
body: '{"UUID":"01e2b24b-b76f-4568-b5ca-40b5066eafd7"}'
204+
body: '{"UUID":"0160e290-c379-4183-9f50-586ac8b41500"}'
205205
form: {}
206206
headers:
207207
Accept:
208208
- application/json
209209
Content-Type:
210210
- application/json
211211
User-Agent:
212-
- upcloud-go-api-ctx/4.9.0
213-
url: https://api.upcloud.com/1.3/storage/01e2b24b-b76f-4568-b5ca-40b5066eafd7/restore
212+
- upcloud-go-api/5.2.1
213+
url: https://api.upcloud.com/1.3/storage/0160e290-c379-4183-9f50-586ac8b41500/restore
214214
method: POST
215215
response:
216216
body: ""
217217
headers:
218218
Date:
219-
- Thu, 27 Oct 2022 18:22:40 GMT
219+
- Thu, 12 Jan 2023 07:59:36 GMT
220220
Server:
221221
- Apache
222222
Strict-Transport-Security:
@@ -233,8 +233,8 @@ interactions:
233233
Content-Type:
234234
- application/json
235235
User-Agent:
236-
- upcloud-go-api-ctx/4.9.0
237-
url: https://api.upcloud.com/1.3/storage/01e9531b-ab82-444f-a4d2-d4cd5518d2b4
236+
- upcloud-go-api/5.2.1
237+
url: https://api.upcloud.com/1.3/storage/01a9b582-2254-4683-b408-1d41d6910b75
238238
method: GET
239239
response:
240240
body: |
@@ -248,10 +248,10 @@ interactions:
248248
},
249249
"backups" : {
250250
"backup" : [
251-
"01e2b24b-b76f-4568-b5ca-40b5066eafd7"
251+
"0160e290-c379-4183-9f50-586ac8b41500"
252252
]
253253
},
254-
"created" : "2022-10-27T18:22:25Z",
254+
"created" : "2023-01-12T07:59:21Z",
255255
"license" : 0,
256256
"servers" : {
257257
"server" : []
@@ -261,7 +261,7 @@ interactions:
261261
"tier" : "maxiops",
262262
"title" : "Test storage",
263263
"type" : "normal",
264-
"uuid" : "01e9531b-ab82-444f-a4d2-d4cd5518d2b4",
264+
"uuid" : "01a9b582-2254-4683-b408-1d41d6910b75",
265265
"zone" : "fi-hel2"
266266
}
267267
}
@@ -271,11 +271,69 @@ interactions:
271271
Content-Type:
272272
- application/json; charset=UTF-8
273273
Date:
274-
- Thu, 27 Oct 2022 18:25:52 GMT
274+
- Thu, 12 Jan 2023 08:01:29 GMT
275275
Server:
276276
- Apache
277277
Strict-Transport-Security:
278278
- max-age=63072000
279279
status: 200 OK
280280
code: 200
281281
duration: ""
282+
- request:
283+
body: ""
284+
form: {}
285+
headers:
286+
Accept:
287+
- application/json
288+
Content-Type:
289+
- application/json
290+
User-Agent:
291+
- upcloud-go-api/5.2.1
292+
url: https://api.upcloud.com/1.3/storage/01a9b582-2254-4683-b408-1d41d6910b75?backups=delete
293+
method: DELETE
294+
response:
295+
body: ""
296+
headers:
297+
Date:
298+
- Thu, 12 Jan 2023 08:01:29 GMT
299+
Server:
300+
- Apache
301+
Strict-Transport-Security:
302+
- max-age=63072000
303+
status: 204 No Content
304+
code: 204
305+
duration: ""
306+
- request:
307+
body: ""
308+
form: {}
309+
headers:
310+
Accept:
311+
- application/json
312+
Content-Type:
313+
- application/json
314+
User-Agent:
315+
- upcloud-go-api/5.2.1
316+
url: https://api.upcloud.com/1.3/storage/0160e290-c379-4183-9f50-586ac8b41500
317+
method: GET
318+
response:
319+
body: |
320+
{
321+
"error" : {
322+
"error_code" : "STORAGE_NOT_FOUND",
323+
"error_message" : "The storage 0160e290-c379-4183-9f50-586ac8b41500 does not exist."
324+
}
325+
}
326+
headers:
327+
Content-Length:
328+
- "157"
329+
Content-Type:
330+
- application/json; charset=UTF-8
331+
Date:
332+
- Thu, 12 Jan 2023 08:01:30 GMT
333+
Server:
334+
- Apache
335+
Strict-Transport-Security:
336+
- max-age=63072000
337+
status: 404 Not Found
338+
code: 404
339+
duration: ""

upcloud/service/storage_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"crypto/sha256"
77
"encoding/hex"
8+
"errors"
89
"fmt"
910
"net/http"
1011
"os"
@@ -301,6 +302,19 @@ func TestCreateRestoreBackup(t *testing.T) {
301302
backupDetails, err = svc.GetStorageDetails(ctx, &request.GetStorageDetailsRequest{UUID: backupDetails.Origin})
302303
require.NoError(t, err)
303304
assert.Equal(t, upcloud.StorageStateOnline, backupDetails.State)
305+
306+
require.NoError(t, svc.DeleteStorage(ctx, &request.DeleteStorageRequest{
307+
UUID: storageDetails.UUID,
308+
Backups: request.DeleteStorageBackupsModeDelete,
309+
}))
310+
311+
var ucErr *upcloud.Error
312+
for _, b := range storageDetails.BackupUUIDs {
313+
_, err = svc.GetStorageDetails(ctx, &request.GetStorageDetailsRequest{UUID: b})
314+
require.Error(t, err)
315+
require.True(t, errors.As(err, &ucErr))
316+
assert.Equal(t, http.StatusNotFound, ucErr.Status)
317+
}
304318
})
305319
}
306320

0 commit comments

Comments
 (0)