-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathobject_storage.go
260 lines (211 loc) · 8.34 KB
/
object_storage.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package govultr
import (
"context"
"fmt"
"net/http"
"github.com/google/go-querystring/query"
)
// ObjectStorageService is the interface to interact with the object storage endpoints on the Vultr API.
// Link : https://www.vultr.com/api/#tag/s3
type ObjectStorageService interface {
Create(ctx context.Context, objReq *ObjectStorageReq) (*ObjectStorage, *http.Response, error)
Get(ctx context.Context, id string) (*ObjectStorage, *http.Response, error)
Update(ctx context.Context, id string, objReq *ObjectStorageReq) error
Delete(ctx context.Context, id string) error
List(ctx context.Context, options *ListOptions) ([]ObjectStorage, *Meta, *http.Response, error)
ListCluster(ctx context.Context, options *ListOptions) ([]ObjectStorageCluster, *Meta, *http.Response, error)
RegenerateKeys(ctx context.Context, id string) (*S3Keys, *http.Response, error)
ListTiers(ctx context.Context) ([]ObjectStorageTier, *http.Response, error)
ListClusterTiers(ctx context.Context, clusterID int) ([]ObjectStorageTier, *http.Response, error)
}
// ObjectStorageServiceHandler handles interaction between the object storage service and the Vultr API.
type ObjectStorageServiceHandler struct {
client *Client
}
// ObjectStorage represents a Vultr Object Storage subscription.
type ObjectStorage struct {
ID string `json:"id"`
DateCreated string `json:"date_created"`
ObjectStoreClusterID int `json:"cluster_id"`
Region string `json:"region"`
Location string `json:"location"`
Label string `json:"label"`
Status string `json:"status"`
S3Keys
}
// ObjectStorageReq represents the parameters for creating and updating object
// storages
type ObjectStorageReq struct {
ClusterID int `json:"cluster_id,omitempty"`
TierID int `json:"tier_id,omitempty"`
Label string `json:"label"`
}
// S3Keys define your api access to your cluster
type S3Keys struct {
S3Hostname string `json:"s3_hostname"`
S3AccessKey string `json:"s3_access_key"`
S3SecretKey string `json:"s3_secret_key"`
}
// ObjectStorageCluster represents a Vultr Object Storage cluster.
type ObjectStorageCluster struct {
ID int `json:"id"`
Region string `json:"region"`
Name string `json:"name,omitempty"`
Hostname string `json:"hostname"`
Deploy string `json:"deploy"`
}
// ObjectStorageTier represents the object storage tier data
type ObjectStorageTier struct {
ID int `json:"id"`
Name string `json:"sales_name"`
Description string `json:"sales_desc"`
Price float32 `json:"price"`
PriceBandwidthGB float32 `json:"bw_gb_price"`
PriceDiskGB float32 `json:"disk_gb_price"`
RateLimitBytesSec int `json:"ratelimit_ops_bytes"`
RateLimitOpsSec int `json:"ratelimit_ops_secs"`
Default string `json:"is_default"`
Locations []ObjectStorageCluster `json:"locations,omitempty"`
Slug string `json:"slug"`
}
type objectStoragesBase struct {
ObjectStorages []ObjectStorage `json:"object_storages"`
Meta *Meta `json:"meta"`
}
type objectStorageBase struct {
ObjectStorage *ObjectStorage `json:"object_storage"`
}
type objectStorageClustersBase struct {
Clusters []ObjectStorageCluster `json:"clusters"`
Meta *Meta `json:"meta"`
}
type objectStorageTiersBase struct {
Tiers []ObjectStorageTier `json:"tiers"`
}
type s3KeysBase struct {
S3Credentials *S3Keys `json:"s3_credentials"`
}
// Create an object storage subscription
func (o *ObjectStorageServiceHandler) Create(ctx context.Context, objReq *ObjectStorageReq) (*ObjectStorage, *http.Response, error) {
uri := "/v2/object-storage"
req, err := o.client.NewRequest(ctx, http.MethodPost, uri, objReq)
if err != nil {
return nil, nil, err
}
objectStorage := new(objectStorageBase)
resp, err := o.client.DoWithContext(ctx, req, objectStorage)
if err != nil {
return nil, resp, err
}
return objectStorage.ObjectStorage, resp, nil
}
// Get returns a specified object storage by the provided ID
func (o *ObjectStorageServiceHandler) Get(ctx context.Context, id string) (*ObjectStorage, *http.Response, error) {
uri := fmt.Sprintf("/v2/object-storage/%s", id)
req, err := o.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, err
}
objectStorage := new(objectStorageBase)
resp, err := o.client.DoWithContext(ctx, req, objectStorage)
if err != nil {
return nil, resp, err
}
return objectStorage.ObjectStorage, resp, nil
}
// Update a Object Storage Subscription.
func (o *ObjectStorageServiceHandler) Update(ctx context.Context, id string, objReq *ObjectStorageReq) error {
uri := fmt.Sprintf("/v2/object-storage/%s", id)
req, err := o.client.NewRequest(ctx, http.MethodPut, uri, objReq)
if err != nil {
return err
}
_, err = o.client.DoWithContext(ctx, req, nil)
return err
}
// Delete a object storage subscription.
func (o *ObjectStorageServiceHandler) Delete(ctx context.Context, id string) error {
uri := fmt.Sprintf("/v2/object-storage/%s", id)
req, err := o.client.NewRequest(ctx, http.MethodDelete, uri, nil)
if err != nil {
return err
}
_, err = o.client.DoWithContext(ctx, req, nil)
return err
}
// List all object storage subscriptions on the current account. This includes both pending and active subscriptions.
func (o *ObjectStorageServiceHandler) List(ctx context.Context, options *ListOptions) ([]ObjectStorage, *Meta, *http.Response, error) { //nolint:dupl,lll
uri := "/v2/object-storage"
req, err := o.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, nil, err
}
newValues, err := query.Values(options)
if err != nil {
return nil, nil, nil, err
}
req.URL.RawQuery = newValues.Encode()
objectStorage := new(objectStoragesBase)
resp, err := o.client.DoWithContext(ctx, req, objectStorage)
if err != nil {
return nil, nil, resp, err
}
return objectStorage.ObjectStorages, objectStorage.Meta, resp, nil
}
// ListCluster returns back your object storage clusters.
// Clusters may be removed over time. The "deploy" field can be used to determine whether or not new deployments are allowed in the cluster.
func (o *ObjectStorageServiceHandler) ListCluster(ctx context.Context, options *ListOptions) ([]ObjectStorageCluster, *Meta, *http.Response, error) { //nolint:lll
uri := "/v2/object-storage/clusters"
req, err := o.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, nil, err
}
clusters := new(objectStorageClustersBase)
resp, err := o.client.DoWithContext(ctx, req, clusters)
if err != nil {
return nil, nil, resp, err
}
return clusters.Clusters, clusters.Meta, resp, nil
}
// RegenerateKeys of the S3 API Keys for an object storage subscription
func (o *ObjectStorageServiceHandler) RegenerateKeys(ctx context.Context, id string) (*S3Keys, *http.Response, error) {
uri := fmt.Sprintf("/v2/object-storage/%s/regenerate-keys", id)
req, err := o.client.NewRequest(ctx, http.MethodPost, uri, nil)
if err != nil {
return nil, nil, err
}
s3Keys := new(s3KeysBase)
resp, err := o.client.DoWithContext(ctx, req, s3Keys)
if err != nil {
return nil, resp, err
}
return s3Keys.S3Credentials, resp, nil
}
// ListTiers retrieves all tiers for object storage deployments
func (o *ObjectStorageServiceHandler) ListTiers(ctx context.Context) ([]ObjectStorageTier, *http.Response, error) {
uri := "/v2/object-storage/tiers"
req, err := o.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, err
}
tiers := new(objectStorageTiersBase)
resp, err := o.client.DoWithContext(ctx, req, tiers)
if err != nil {
return nil, resp, err
}
return tiers.Tiers, resp, nil
}
// ListClusterTiers retrieves all tiers for object storage deployments on a specific cluster
func (o *ObjectStorageServiceHandler) ListClusterTiers(ctx context.Context, clusterID int) ([]ObjectStorageTier, *http.Response, error) {
uri := fmt.Sprintf("/v2/object-storage/clusters/%d/tiers", clusterID)
req, err := o.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, err
}
tiers := new(objectStorageTiersBase)
resp, err := o.client.DoWithContext(ctx, req, tiers)
if err != nil {
return nil, resp, err
}
return tiers.Tiers, resp, nil
}