Skip to content

Commit 1b885dd

Browse files
committed
Add Storage Import resources
1 parent 582fe4f commit 1b885dd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+6457
-4390
lines changed

upcloud/client/client.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,19 @@ func (c *Client) PerformJSONDeleteRequest(url string) error {
137137
return err
138138
}
139139

140+
// PerformJSONPutUploadRequest performs a PUT request to the specified URL with an io.Reader
141+
// and returns the response body and eventual errors
142+
func (c *Client) PerformJSONPutUploadRequest(url string, requestBody io.Reader) ([]byte, error) {
143+
144+
request, err := http.NewRequest(http.MethodPut, url, requestBody)
145+
146+
if err != nil {
147+
return nil, err
148+
}
149+
150+
return c.performJSONRequest(request)
151+
}
152+
140153
// Adds common headers to the specified request
141154
func (c *Client) addJSONRequestHeaders(request *http.Request) *http.Request {
142155
request.SetBasicAuth(c.userName, c.password)

upcloud/request/storage.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ import (
88
"github.com/UpCloudLtd/upcloud-go-api/upcloud"
99
)
1010

11+
// Constants
12+
const (
13+
StorageImportSourceDirectUpload = "direct_upload"
14+
StorageImportSourceHTTPImport = "http_import"
15+
)
16+
1117
// GetStoragesRequest represents a request for retrieving all or some storages
1218
type GetStoragesRequest struct {
1319
// If specified, only storages with this access type will be retrieved
@@ -282,3 +288,46 @@ type RestoreBackupRequest struct {
282288
func (r *RestoreBackupRequest) RequestURL() string {
283289
return fmt.Sprintf("/storage/%s/restore", r.UUID)
284290
}
291+
292+
// CreateStorageImportRequest represent a request to import storage.
293+
type CreateStorageImportRequest struct {
294+
StorageUUID string `json:"-"`
295+
296+
Source string `json:"source"`
297+
SourceLocation string `json:"source_location,omitempty"`
298+
}
299+
300+
// MarshalJSON is a custom marshaller that deals with
301+
// deeply embedded values.
302+
func (r CreateStorageImportRequest) MarshalJSON() ([]byte, error) {
303+
type localStorageImportRequest CreateStorageImportRequest
304+
v := struct {
305+
StorageImportRequest localStorageImportRequest `json:"storage_import"`
306+
}{}
307+
v.StorageImportRequest = localStorageImportRequest(r)
308+
309+
return json.Marshal(&v)
310+
}
311+
312+
// RequestURL implements the Request interface
313+
func (r *CreateStorageImportRequest) RequestURL() string {
314+
return fmt.Sprintf("/storage/%s/import", r.StorageUUID)
315+
}
316+
317+
// GetStorageImportDetailsRequest represents a request to get details
318+
// about an import
319+
type GetStorageImportDetailsRequest struct {
320+
UUID string
321+
}
322+
323+
// RequestURL implements the Request interface
324+
func (r *GetStorageImportDetailsRequest) RequestURL() string {
325+
return fmt.Sprintf("/storage/%s/import", r.UUID)
326+
}
327+
328+
// WaitForStorageImportCompletionRequest represents a request to wait
329+
// for storage import to complete.
330+
type WaitForStorageImportCompletionRequest struct {
331+
StorageUUID string
332+
Timeout time.Duration
333+
}

upcloud/request/storage_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,37 @@ func TestRestoreBackupRequest(t *testing.T) {
246246

247247
assert.Equal(t, "/storage/foo/restore", request.RequestURL())
248248
}
249+
250+
// TestStorageImportRequest tests that StorageImportRequest marshals correctly
251+
func TestStorageImportRequest(t *testing.T) {
252+
request := CreateStorageImportRequest{
253+
StorageUUID: "foo",
254+
Source: StorageImportSourceHTTPImport,
255+
SourceLocation: "http://somewhere.com",
256+
}
257+
258+
expectedJSON := `
259+
{
260+
"storage_import": {
261+
"source": "http_import",
262+
"source_location": "http://somewhere.com"
263+
}
264+
}
265+
`
266+
267+
actualJSON, err := json.Marshal(&request)
268+
assert.NoError(t, err)
269+
270+
assert.JSONEq(t, expectedJSON, string(actualJSON))
271+
272+
assert.Equal(t, "/storage/foo/import", request.RequestURL())
273+
}
274+
275+
// TestGetStorageImportDetails tests that GetStorageImportDetails objects behave correctly
276+
func TestGetStorageImportDetails(t *testing.T) {
277+
request := GetStorageImportDetailsRequest{
278+
UUID: "foo",
279+
}
280+
281+
assert.Equal(t, "/storage/foo/import", request.RequestURL())
282+
}

0 commit comments

Comments
 (0)