object_storage.go raw
1 package govultr
2
3 import (
4 "context"
5 "fmt"
6 "net/http"
7
8 "github.com/google/go-querystring/query"
9 )
10
11 // ObjectStorageService is the interface to interact with the object storage endpoints on the Vultr API.
12 // Link : https://www.vultr.com/api/#tag/s3
13 type ObjectStorageService interface {
14 Create(ctx context.Context, objReq *ObjectStorageReq) (*ObjectStorage, *http.Response, error)
15 Get(ctx context.Context, id string) (*ObjectStorage, *http.Response, error)
16 Update(ctx context.Context, id string, objReq *ObjectStorageReq) error
17 Delete(ctx context.Context, id string) error
18 List(ctx context.Context, options *ListOptions) ([]ObjectStorage, *Meta, *http.Response, error)
19
20 ListCluster(ctx context.Context, options *ListOptions) ([]ObjectStorageCluster, *Meta, *http.Response, error)
21 RegenerateKeys(ctx context.Context, id string) (*S3Keys, *http.Response, error)
22
23 ListTiers(ctx context.Context) ([]ObjectStorageTier, *http.Response, error)
24 ListClusterTiers(ctx context.Context, clusterID int) ([]ObjectStorageTier, *http.Response, error)
25 }
26
27 // ObjectStorageServiceHandler handles interaction between the object storage service and the Vultr API.
28 type ObjectStorageServiceHandler struct {
29 client *Client
30 }
31
32 // ObjectStorage represents a Vultr Object Storage subscription.
33 type ObjectStorage struct {
34 ID string `json:"id"`
35 DateCreated string `json:"date_created"`
36 ObjectStoreClusterID int `json:"cluster_id"`
37 Region string `json:"region"`
38 Location string `json:"location"`
39 Label string `json:"label"`
40 Status string `json:"status"`
41 S3Keys
42 }
43
44 // ObjectStorageReq represents the parameters for creating and updating object
45 // storages
46 type ObjectStorageReq struct {
47 ClusterID int `json:"cluster_id,omitempty"`
48 TierID int `json:"tier_id,omitempty"`
49 Label string `json:"label"`
50 }
51
52 // S3Keys define your api access to your cluster
53 type S3Keys struct {
54 S3Hostname string `json:"s3_hostname"`
55 S3AccessKey string `json:"s3_access_key"`
56 S3SecretKey string `json:"s3_secret_key"`
57 }
58
59 // ObjectStorageCluster represents a Vultr Object Storage cluster.
60 type ObjectStorageCluster struct {
61 ID int `json:"id"`
62 Region string `json:"region"`
63 Name string `json:"name,omitempty"`
64 Hostname string `json:"hostname"`
65 Deploy string `json:"deploy"`
66 }
67
68 // ObjectStorageTier represents the object storage tier data
69 type ObjectStorageTier struct {
70 ID int `json:"id"`
71 Name string `json:"sales_name"`
72 Description string `json:"sales_desc"`
73 Price float32 `json:"price"`
74 PriceBandwidthGB float32 `json:"bw_gb_price"`
75 PriceDiskGB float32 `json:"disk_gb_price"`
76 RateLimitBytesSec int `json:"ratelimit_ops_bytes"`
77 RateLimitOpsSec int `json:"ratelimit_ops_secs"`
78 Default string `json:"is_default"`
79 Locations []ObjectStorageCluster `json:"locations,omitempty"`
80 Slug string `json:"slug"`
81 }
82
83 type objectStoragesBase struct {
84 ObjectStorages []ObjectStorage `json:"object_storages"`
85 Meta *Meta `json:"meta"`
86 }
87
88 type objectStorageBase struct {
89 ObjectStorage *ObjectStorage `json:"object_storage"`
90 }
91
92 type objectStorageClustersBase struct {
93 Clusters []ObjectStorageCluster `json:"clusters"`
94 Meta *Meta `json:"meta"`
95 }
96
97 type objectStorageTiersBase struct {
98 Tiers []ObjectStorageTier `json:"tiers"`
99 }
100
101 type s3KeysBase struct {
102 S3Credentials *S3Keys `json:"s3_credentials"`
103 }
104
105 // Create an object storage subscription
106 func (o *ObjectStorageServiceHandler) Create(ctx context.Context, objReq *ObjectStorageReq) (*ObjectStorage, *http.Response, error) {
107 uri := "/v2/object-storage"
108
109 req, err := o.client.NewRequest(ctx, http.MethodPost, uri, objReq)
110 if err != nil {
111 return nil, nil, err
112 }
113
114 objectStorage := new(objectStorageBase)
115 resp, err := o.client.DoWithContext(ctx, req, objectStorage)
116 if err != nil {
117 return nil, resp, err
118 }
119
120 return objectStorage.ObjectStorage, resp, nil
121 }
122
123 // Get returns a specified object storage by the provided ID
124 func (o *ObjectStorageServiceHandler) Get(ctx context.Context, id string) (*ObjectStorage, *http.Response, error) {
125 uri := fmt.Sprintf("/v2/object-storage/%s", id)
126
127 req, err := o.client.NewRequest(ctx, http.MethodGet, uri, nil)
128 if err != nil {
129 return nil, nil, err
130 }
131
132 objectStorage := new(objectStorageBase)
133 resp, err := o.client.DoWithContext(ctx, req, objectStorage)
134 if err != nil {
135 return nil, resp, err
136 }
137
138 return objectStorage.ObjectStorage, resp, nil
139 }
140
141 // Update a Object Storage Subscription.
142 func (o *ObjectStorageServiceHandler) Update(ctx context.Context, id string, objReq *ObjectStorageReq) error {
143 uri := fmt.Sprintf("/v2/object-storage/%s", id)
144
145 req, err := o.client.NewRequest(ctx, http.MethodPut, uri, objReq)
146 if err != nil {
147 return err
148 }
149
150 _, err = o.client.DoWithContext(ctx, req, nil)
151 return err
152 }
153
154 // Delete a object storage subscription.
155 func (o *ObjectStorageServiceHandler) Delete(ctx context.Context, id string) error {
156 uri := fmt.Sprintf("/v2/object-storage/%s", id)
157
158 req, err := o.client.NewRequest(ctx, http.MethodDelete, uri, nil)
159 if err != nil {
160 return err
161 }
162
163 _, err = o.client.DoWithContext(ctx, req, nil)
164 return err
165 }
166
167 // List all object storage subscriptions on the current account. This includes both pending and active subscriptions.
168 func (o *ObjectStorageServiceHandler) List(ctx context.Context, options *ListOptions) ([]ObjectStorage, *Meta, *http.Response, error) { //nolint:dupl,lll
169 uri := "/v2/object-storage"
170
171 req, err := o.client.NewRequest(ctx, http.MethodGet, uri, nil)
172 if err != nil {
173 return nil, nil, nil, err
174 }
175
176 newValues, err := query.Values(options)
177 if err != nil {
178 return nil, nil, nil, err
179 }
180
181 req.URL.RawQuery = newValues.Encode()
182
183 objectStorage := new(objectStoragesBase)
184 resp, err := o.client.DoWithContext(ctx, req, objectStorage)
185 if err != nil {
186 return nil, nil, resp, err
187 }
188
189 return objectStorage.ObjectStorages, objectStorage.Meta, resp, nil
190 }
191
192 // ListCluster returns back your object storage clusters.
193 // Clusters may be removed over time. The "deploy" field can be used to determine whether or not new deployments are allowed in the cluster.
194 func (o *ObjectStorageServiceHandler) ListCluster(ctx context.Context, options *ListOptions) ([]ObjectStorageCluster, *Meta, *http.Response, error) { //nolint:lll
195 uri := "/v2/object-storage/clusters"
196 req, err := o.client.NewRequest(ctx, http.MethodGet, uri, nil)
197 if err != nil {
198 return nil, nil, nil, err
199 }
200
201 clusters := new(objectStorageClustersBase)
202 resp, err := o.client.DoWithContext(ctx, req, clusters)
203 if err != nil {
204 return nil, nil, resp, err
205 }
206
207 return clusters.Clusters, clusters.Meta, resp, nil
208 }
209
210 // RegenerateKeys of the S3 API Keys for an object storage subscription
211 func (o *ObjectStorageServiceHandler) RegenerateKeys(ctx context.Context, id string) (*S3Keys, *http.Response, error) {
212 uri := fmt.Sprintf("/v2/object-storage/%s/regenerate-keys", id)
213
214 req, err := o.client.NewRequest(ctx, http.MethodPost, uri, nil)
215 if err != nil {
216 return nil, nil, err
217 }
218
219 s3Keys := new(s3KeysBase)
220 resp, err := o.client.DoWithContext(ctx, req, s3Keys)
221 if err != nil {
222 return nil, resp, err
223 }
224
225 return s3Keys.S3Credentials, resp, nil
226 }
227
228 // ListTiers retrieves all tiers for object storage deployments
229 func (o *ObjectStorageServiceHandler) ListTiers(ctx context.Context) ([]ObjectStorageTier, *http.Response, error) {
230 uri := "/v2/object-storage/tiers"
231 req, err := o.client.NewRequest(ctx, http.MethodGet, uri, nil)
232 if err != nil {
233 return nil, nil, err
234 }
235
236 tiers := new(objectStorageTiersBase)
237 resp, err := o.client.DoWithContext(ctx, req, tiers)
238 if err != nil {
239 return nil, resp, err
240 }
241
242 return tiers.Tiers, resp, nil
243 }
244
245 // ListClusterTiers retrieves all tiers for object storage deployments on a specific cluster
246 func (o *ObjectStorageServiceHandler) ListClusterTiers(ctx context.Context, clusterID int) ([]ObjectStorageTier, *http.Response, error) {
247 uri := fmt.Sprintf("/v2/object-storage/clusters/%d/tiers", clusterID)
248 req, err := o.client.NewRequest(ctx, http.MethodGet, uri, nil)
249 if err != nil {
250 return nil, nil, err
251 }
252
253 tiers := new(objectStorageTiersBase)
254 resp, err := o.client.DoWithContext(ctx, req, tiers)
255 if err != nil {
256 return nil, resp, err
257 }
258
259 return tiers.Tiers, resp, nil
260 }
261