forked from gridscale/gsclient-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
objectstorage.go
218 lines (191 loc) · 7.85 KB
/
objectstorage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package gsclient
import (
"context"
"errors"
"net/http"
"path"
"strings"
)
// ObjectStorageOperator provides an interface for operations on object storages.
type ObjectStorageOperator interface {
GetObjectStorageAccessKeyList(ctx context.Context) ([]ObjectStorageAccessKey, error)
GetObjectStorageAccessKey(ctx context.Context, id string) (ObjectStorageAccessKey, error)
CreateObjectStorageAccessKey(ctx context.Context) (ObjectStorageAccessKeyCreateResponse, error)
AdvancedCreateObjectStorageAccessKey(ctx context.Context, body ObjectStorageAccessKeyCreateRequest) (ObjectStorageAccessKeyCreateResponse, error)
UpdateObjectStorageAccessKey(ctx context.Context, id string, body ObjectStorageAccessKeyUpdateRequest) error
DeleteObjectStorageAccessKey(ctx context.Context, id string) error
GetObjectStorageBucketList(ctx context.Context) ([]ObjectStorageBucket, error)
}
// ObjectStorageAccessKeyList holds a list of object storage access keys.
type ObjectStorageAccessKeyList struct {
// Array of Object Storages' access keys.
List []ObjectStorageAccessKeyProperties `json:"access_keys"`
}
// ObjectStorageAccessKey represents a single object storage access key.
type ObjectStorageAccessKey struct {
// Properties of an object storage access key.
Properties ObjectStorageAccessKeyProperties `json:"access_key"`
}
// ObjectStorageAccessKeyProperties holds properties of an object storage access key.
type ObjectStorageAccessKeyProperties struct {
// The object storage secret_key.
SecretKey string `json:"secret_key"`
// The object storage access_key.
AccessKey string `json:"access_key"`
// Account this credentials belong to.
User string `json:"user"`
// Comment for the access_key.
Comment string `json:"comment"`
// User UUID.
UserUUID string `json:"user_uuid"`
}
// ObjectStorageAccessKeyCreateRequest represents a request for creating an object storage access key.
type ObjectStorageAccessKeyCreateRequest struct {
// Comment for the access_key.
Comment string `json:"comment,omitempty"`
// If a user_uuid is sent along with the request, a user-specific key will get created.
// If no user_uuid is sent along a user with write-access to the contract will still
// only create a user-specific key for themselves while a user with admin-access to
// the contract will create a contract-level admin key.
UserUUID string `json:"user_uuid,omitempty"`
}
// ObjectStorageAccessKeyUpdateRequest represents a request for updating an object storage access key.
type ObjectStorageAccessKeyUpdateRequest struct {
// Comment for the access_key.
Comment *string `json:"comment,omitempty"`
}
// ObjectStorageAccessKeyCreateResponse represents a response for creating an object storage access key.
type ObjectStorageAccessKeyCreateResponse struct {
AccessKey struct {
////The object storage secret_key.
SecretKey string `json:"secret_key"`
// The object storage secret_key.
AccessKey string `json:"access_key"`
} `json:"access_key"`
// UUID of the request.
RequestUUID string `json:"request_uuid"`
}
// ObjectStorageBucketList holds a list of buckets.
type ObjectStorageBucketList struct {
// Array of Buckets
List []ObjectStorageBucketProperties `json:"buckets"`
}
// ObjectStorageBucket represents a single bucket.
type ObjectStorageBucket struct {
// Properties of a bucket.
Properties ObjectStorageBucketProperties `json:"bucket"`
}
// ObjectStorageBucketProperties holds properties of a bucket.
type ObjectStorageBucketProperties struct {
// The human-readable name of the object. It supports the full UTF-8 character set, with a maximum of 64 characters.
Name string `json:"name"`
// The current usage of the bucket.
Usage struct {
// The size of the the bucket (in kb).
SizeKb int `json:"size_kb"`
// The number of files in the bucket.
NumObjects int `json:"num_objects"`
} `json:"usage"`
}
// GetObjectStorageAccessKeyList gets a list of available object storage access keys.
//
// See: https://gridscale.io/en//api-documentation/index.html#operation/getAccessKeys
func (c *Client) GetObjectStorageAccessKeyList(ctx context.Context) ([]ObjectStorageAccessKey, error) {
r := gsRequest{
uri: path.Join(apiObjectStorageBase, "access_keys"),
method: http.MethodGet,
skipCheckingRequest: true,
}
var response ObjectStorageAccessKeyList
var accessKeys []ObjectStorageAccessKey
err := r.execute(ctx, *c, &response)
for _, properties := range response.List {
accessKeys = append(accessKeys, ObjectStorageAccessKey{Properties: properties})
}
return accessKeys, err
}
// GetObjectStorageAccessKey gets a specific object storage access key based on given id.
//
// See: https://gridscale.io/en//api-documentation/index.html#operation/getAccessKey
func (c *Client) GetObjectStorageAccessKey(ctx context.Context, id string) (ObjectStorageAccessKey, error) {
if strings.TrimSpace(id) == "" {
return ObjectStorageAccessKey{}, errors.New("'id' is required")
}
r := gsRequest{
uri: path.Join(apiObjectStorageBase, "access_keys", id),
method: http.MethodGet,
skipCheckingRequest: true,
}
var response ObjectStorageAccessKey
err := r.execute(ctx, *c, &response)
return response, err
}
// CreateObjectStorageAccessKey creates an object storage access key.
//
// See: https://gridscale.io/en//api-documentation/index.html#operation/createAccessKey
func (c *Client) CreateObjectStorageAccessKey(ctx context.Context) (ObjectStorageAccessKeyCreateResponse, error) {
r := gsRequest{
uri: path.Join(apiObjectStorageBase, "access_keys"),
method: http.MethodPost,
}
var response ObjectStorageAccessKeyCreateResponse
err := r.execute(ctx, *c, &response)
return response, err
}
// AdvancedCreateObjectStorageAccessKey creates an object storage access key with user_uuid and/or comment.
//
// See: https://gridscale.io/en//api-documentation/index.html#operation/createAccessKey
func (c *Client) AdvancedCreateObjectStorageAccessKey(ctx context.Context, body ObjectStorageAccessKeyCreateRequest) (ObjectStorageAccessKeyCreateResponse, error) {
r := gsRequest{
uri: path.Join(apiObjectStorageBase, "access_keys"),
method: http.MethodPost,
body: body,
}
var response ObjectStorageAccessKeyCreateResponse
err := r.execute(ctx, *c, &response)
return response, err
}
// UpdateObjectStorageAccessKey updates a specific object storage access key based on given id.
//
// See: https://my.gridscale.io/APIDoc#tag/object-storage/operation/updateAccessKey
func (c *Client) UpdateObjectStorageAccessKey(ctx context.Context, id string, body ObjectStorageAccessKeyUpdateRequest) error {
if strings.TrimSpace(id) == "" {
return errors.New("'id' is required")
}
r := gsRequest{
uri: path.Join(apiObjectStorageBase, "access_keys", id),
method: http.MethodPatch,
body: body,
}
return r.execute(ctx, *c, nil)
}
// DeleteObjectStorageAccessKey removed a specific object storage access key based on given id.
//
// See: https://gridscale.io/en//api-documentation/index.html#operation/deleteAccessKey
func (c *Client) DeleteObjectStorageAccessKey(ctx context.Context, id string) error {
if strings.TrimSpace(id) == "" {
return errors.New("'id' is required")
}
r := gsRequest{
uri: path.Join(apiObjectStorageBase, "access_keys", id),
method: http.MethodDelete,
}
return r.execute(ctx, *c, nil)
}
// GetObjectStorageBucketList gets a list of object storage buckets.
//
// See: https://gridscale.io/en//api-documentation/index.html#operation/getBuckets
func (c *Client) GetObjectStorageBucketList(ctx context.Context) ([]ObjectStorageBucket, error) {
r := gsRequest{
uri: path.Join(apiObjectStorageBase, "buckets"),
method: http.MethodGet,
skipCheckingRequest: true,
}
var response ObjectStorageBucketList
var buckets []ObjectStorageBucket
err := r.execute(ctx, *c, &response)
for _, properties := range response.List {
buckets = append(buckets, ObjectStorageBucket{Properties: properties})
}
return buckets, err
}