Skip to content

Commit 8be5211

Browse files
author
Iivari Leinonen
authored
Merge pull request #71 from UpCloudLtd/object-storage
Object storage
2 parents ebe8ed6 + 0f317b6 commit 8be5211

File tree

11 files changed

+571
-1
lines changed

11 files changed

+571
-1
lines changed

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
1717
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
1818
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1919
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
20+
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
2021
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
2122
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
2223
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=

upcloud/client/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

1515
// Constants
1616
const (
17-
DefaultAPIVersion = "1.3.4"
17+
DefaultAPIVersion = "1.3.6"
1818
DefaultAPIBaseURL = "https://api.upcloud.com"
1919

2020
// The default timeout (in seconds)

upcloud/object_storage.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package upcloud
2+
3+
import "encoding/json"
4+
5+
// ObjectStorage represents a Object Storage
6+
type ObjectStorage struct {
7+
Created string `json:"created"`
8+
Description string `json:"description"`
9+
Name string `json:"name"`
10+
Size int `json:"size"`
11+
State string `json:"state"`
12+
URL string `json:"url"`
13+
UUID string `json:"uuid"`
14+
Zone string `json:"zone"`
15+
}
16+
17+
// ObjectStorages represent a /object-storage response
18+
type ObjectStorages struct {
19+
ObjectStorages []ObjectStorage `json:"object_storages"`
20+
}
21+
22+
// UnmarshalJSON is a custom unmarshaller that deals with
23+
// deeply embedded values.
24+
func (o *ObjectStorages) UnmarshalJSON(b []byte) error {
25+
type objectStorageWrapper struct {
26+
ObjectStorages []ObjectStorage `json:"object_storage"`
27+
}
28+
29+
v := struct {
30+
ObjectStorages objectStorageWrapper `json:"object_storages"`
31+
}{}
32+
err := json.Unmarshal(b, &v)
33+
if err != nil {
34+
return err
35+
}
36+
37+
o.ObjectStorages = v.ObjectStorages.ObjectStorages
38+
39+
return nil
40+
}
41+
42+
// ObjectStorageDetails represents details about a Object Storage
43+
type ObjectStorageDetails struct {
44+
ObjectStorage
45+
UsedSpace int `json:"used_space"`
46+
}
47+
48+
// UnmarshalJSON is a custom unmarshaller that deals with
49+
// deeply embedded values.
50+
func (o *ObjectStorageDetails) UnmarshalJSON(b []byte) error {
51+
type localObjectStorageDetails ObjectStorageDetails
52+
53+
v := struct {
54+
ObjectStorageDetails localObjectStorageDetails `json:"object_storage"`
55+
}{}
56+
err := json.Unmarshal(b, &v)
57+
if err != nil {
58+
return err
59+
}
60+
61+
(*o) = ObjectStorageDetails(v.ObjectStorageDetails)
62+
63+
return nil
64+
}

upcloud/object_storage_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package upcloud
2+
3+
import (
4+
"encoding/json"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
// TestUnmarshalObjectStorages tests that Object Storages are unmarshaled correctly
11+
func TestUnmarshalObjectStorages(t *testing.T) {
12+
originalJSON := `
13+
{
14+
"object_storages": {
15+
"object_storage": [
16+
{
17+
"created": "2020-07-23T05:06:35Z",
18+
"description": "Example object storage",
19+
"name": "example-object-storage",
20+
"size": 250,
21+
"state": "started",
22+
"url": "https://example-object-storage.nl-ams1.upcloudobjects.com/",
23+
"uuid": "06832a75-be7b-4d23-be05-130dc3dfd9e7",
24+
"zone": "uk-lon1"
25+
}
26+
]
27+
}
28+
}
29+
`
30+
31+
objectStorages := ObjectStorages{}
32+
err := json.Unmarshal([]byte(originalJSON), &objectStorages)
33+
assert.Nil(t, err)
34+
assert.Len(t, objectStorages.ObjectStorages, 1)
35+
36+
objectStorage := objectStorages.ObjectStorages[0]
37+
assert.Equal(t, "2020-07-23T05:06:35Z", objectStorage.Created)
38+
assert.Equal(t, "Example object storage", objectStorage.Description)
39+
assert.Equal(t, "example-object-storage", objectStorage.Name)
40+
assert.Equal(t, 250, objectStorage.Size)
41+
assert.Equal(t, "started", objectStorage.State)
42+
assert.Equal(t, "https://example-object-storage.nl-ams1.upcloudobjects.com/", objectStorage.URL)
43+
assert.Equal(t, "06832a75-be7b-4d23-be05-130dc3dfd9e7", objectStorage.UUID)
44+
assert.Equal(t, "uk-lon1", objectStorage.Zone)
45+
}

upcloud/request/object_storage.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package request
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
)
7+
8+
// GetObjectStorageDetailsRequest represents a request for retrieving details about a Object Storage device
9+
type GetObjectStorageDetailsRequest struct {
10+
UUID string
11+
}
12+
13+
// RequestURL implements the Request interface
14+
func (r *GetObjectStorageDetailsRequest) RequestURL() string {
15+
return fmt.Sprintf("/object-storage/%s", r.UUID)
16+
}
17+
18+
// CreateObjectStorageRequest represents a request for creating a new Object Storage device
19+
type CreateObjectStorageRequest struct {
20+
Name string `json:"name,omitempty"`
21+
Description string `json:"description,omitempty"`
22+
Zone string `json:"zone"`
23+
AccessKey string `json:"access_key"`
24+
SecretKey string `json:"secret_key"`
25+
Size int `json:"size"`
26+
}
27+
28+
// MarshalJSON is a custom marshaller that deals with
29+
// deeply embedded values.
30+
func (r CreateObjectStorageRequest) MarshalJSON() ([]byte, error) {
31+
type localCreateObjectStorageRequest CreateObjectStorageRequest
32+
v := struct {
33+
ObjectStorage localCreateObjectStorageRequest `json:"object_storage"`
34+
}{}
35+
v.ObjectStorage = localCreateObjectStorageRequest(r)
36+
37+
return json.Marshal(&v)
38+
}
39+
40+
// RequestURL implements the Request interface
41+
func (r *CreateObjectStorageRequest) RequestURL() string {
42+
return "/object-storage"
43+
}
44+
45+
// ModifyObjectStorageRequest represents a request to modify a Object Storage
46+
type ModifyObjectStorageRequest struct {
47+
UUID string `json:"-"`
48+
Description string `json:"description,omitempty"`
49+
AccessKey string `json:"access_key,omitempty"`
50+
SecretKey string `json:"secret_key,omitempty"`
51+
Size int `json:"size,omitempty"`
52+
}
53+
54+
// MarshalJSON is a custom marshaller that deals with
55+
// deeply embedded values.
56+
func (r ModifyObjectStorageRequest) MarshalJSON() ([]byte, error) {
57+
type localModifyObjectStorageRequest ModifyObjectStorageRequest
58+
v := struct {
59+
ModifyObjectStorageRequest localModifyObjectStorageRequest `json:"object_storage"`
60+
}{}
61+
v.ModifyObjectStorageRequest = localModifyObjectStorageRequest(r)
62+
63+
return json.Marshal(&v)
64+
}
65+
66+
// RequestURL implements the Request interface
67+
func (r *ModifyObjectStorageRequest) RequestURL() string {
68+
return fmt.Sprintf("/object-storage/%s", r.UUID)
69+
}
70+
71+
// DeleteObjectStorageRequest represents a request to delete a Object Storage
72+
type DeleteObjectStorageRequest struct {
73+
UUID string
74+
}
75+
76+
// RequestURL implements the Request interface
77+
func (r *DeleteObjectStorageRequest) RequestURL() string {
78+
return fmt.Sprintf("/object-storage/%s", r.UUID)
79+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package request
2+
3+
import (
4+
"encoding/json"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
// TestGetObjectStorageDetailsRequest tests that GetObjectStorageDetailsRequest objects behave correctly
11+
func TestGetObjectStorageDetailsRequest(t *testing.T) {
12+
request := GetObjectStorageDetailsRequest{
13+
UUID: "foo",
14+
}
15+
16+
assert.Equal(t, "/object-storage/foo", request.RequestURL())
17+
}
18+
19+
// TestCreateObjectStorageRequest tests that CreateObjectStorageRequest objects behave correctly
20+
func TestCreateObjectStorageRequest(t *testing.T) {
21+
request := CreateObjectStorageRequest{
22+
Name: "app-object-storage",
23+
Description: "App object storage",
24+
Zone: "fi-hel2",
25+
Size: 500,
26+
AccessKey: "UCOB5HE4NVTVFMXXRBQ2",
27+
SecretKey: "ssLDVHvTRjHaEAPRcMiFep3HItcqdNUNtql3DcLx",
28+
}
29+
30+
expectedJSON := `
31+
{
32+
"object_storage": {
33+
"access_key": "UCOB5HE4NVTVFMXXRBQ2",
34+
"description": "App object storage",
35+
"name": "app-object-storage",
36+
"secret_key": "ssLDVHvTRjHaEAPRcMiFep3HItcqdNUNtql3DcLx",
37+
"zone": "fi-hel2",
38+
"size": 500
39+
}
40+
}
41+
`
42+
43+
actualJSON, err := json.Marshal(&request)
44+
assert.NoError(t, err)
45+
assert.JSONEq(t, expectedJSON, string(actualJSON))
46+
assert.Equal(t, "/object-storage", request.RequestURL())
47+
}
48+
49+
// TestModifyObjectStorageRequest tests that ModifyObjectStorageRequest objects behave correctly
50+
func TestModifyObjectStorageRequest(t *testing.T) {
51+
request := ModifyObjectStorageRequest{
52+
UUID: "foo",
53+
Description: "Modified object storage",
54+
AccessKey: "UCOB5HE4NVTVFMXXRBQ2",
55+
SecretKey: "ssLDVHvTRjHaEAPRcMiFep3HItcqdNUNtql3DcLx",
56+
}
57+
58+
expectedJSON := `
59+
{
60+
"object_storage": {
61+
"access_key": "UCOB5HE4NVTVFMXXRBQ2",
62+
"description": "Modified object storage",
63+
"secret_key": "ssLDVHvTRjHaEAPRcMiFep3HItcqdNUNtql3DcLx"
64+
}
65+
}
66+
`
67+
68+
actualJSON, err := json.Marshal(&request)
69+
assert.Nil(t, err)
70+
assert.JSONEq(t, expectedJSON, string(actualJSON))
71+
assert.Equal(t, "/object-storage/foo", request.RequestURL())
72+
}
73+
74+
// TestDeleteObjectStorageRequest tests that DeleteObjectStorageRequest objects behave correctly
75+
func TestDeleteObjectStorageRequest(t *testing.T) {
76+
request := DeleteObjectStorageRequest{
77+
UUID: "foo",
78+
}
79+
80+
assert.Equal(t, "/object-storage/foo", request.RequestURL())
81+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
version: 1
3+
interactions:
4+
- request:
5+
body: '{"object_storage":{"name":"go-test-getobjectstoragedetails","description":"App object storage","zone":"fi-hel2","access_key":"UCOB5HE4NVTVFMXXRBQ2","secret_key":"ssLDVHvTRjHaEAPRcMiFep3HItcqdNUNtql3DcLx","size":500}}'
6+
form: {}
7+
headers:
8+
Accept:
9+
- application/json
10+
Content-Type:
11+
- application/json
12+
url: https://api.upcloud.com/1.3/object-storage
13+
method: POST
14+
response:
15+
body: |
16+
{
17+
"object_storage" : {
18+
"created" : "2020-11-12T07:20:22Z",
19+
"description" : "App object storage",
20+
"name" : "go-test-getobjectstoragedetails",
21+
"size" : 500,
22+
"state" : "started",
23+
"url" : "https://go-test-getobjectstoragedetails.fi-hel2.upcloudobjects.com/",
24+
"used_space" : 0,
25+
"uuid" : "060f379b-6bab-440f-bcc8-6e49647dc3e2",
26+
"zone" : "fi-hel2"
27+
}
28+
}
29+
headers:
30+
Content-Length:
31+
- "405"
32+
Content-Type:
33+
- application/json; charset=UTF-8
34+
Date:
35+
- Thu, 12 Nov 2020 07:20:04 GMT
36+
Server:
37+
- Apache
38+
Strict-Transport-Security:
39+
- max-age=63072000
40+
status: 201 Created
41+
code: 201
42+
duration: ""
43+
- request:
44+
body: ""
45+
form: {}
46+
headers:
47+
Accept:
48+
- application/json
49+
Content-Type:
50+
- application/json
51+
url: https://api.upcloud.com/1.3/object-storage/060f379b-6bab-440f-bcc8-6e49647dc3e2
52+
method: GET
53+
response:
54+
body: |
55+
{
56+
"object_storage" : {
57+
"created" : "2020-11-12T07:20:22Z",
58+
"description" : "App object storage",
59+
"name" : "go-test-getobjectstoragedetails",
60+
"size" : 500,
61+
"state" : "started",
62+
"url" : "https://go-test-getobjectstoragedetails.fi-hel2.upcloudobjects.com/",
63+
"used_space" : 0,
64+
"uuid" : "060f379b-6bab-440f-bcc8-6e49647dc3e2",
65+
"zone" : "fi-hel2"
66+
}
67+
}
68+
headers:
69+
Content-Length:
70+
- "405"
71+
Content-Type:
72+
- application/json; charset=UTF-8
73+
Date:
74+
- Thu, 12 Nov 2020 07:20:23 GMT
75+
Server:
76+
- Apache
77+
Strict-Transport-Security:
78+
- max-age=63072000
79+
status: 200 OK
80+
code: 200
81+
duration: ""

0 commit comments

Comments
 (0)