forked from linode/linodego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject_storage_buckets.go
198 lines (161 loc) · 5.46 KB
/
object_storage_buckets.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
package linodego
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/linode/linodego/internal/parseabletime"
)
// ObjectStorageBucket represents a ObjectStorage object
type ObjectStorageBucket struct {
Label string `json:"label"`
Cluster string `json:"cluster"`
Created *time.Time `json:"-"`
Hostname string `json:"hostname"`
}
// ObjectStorageBucketAccess holds Object Storage access info
type ObjectStorageBucketAccess struct {
ACL ObjectStorageACL `json:"acl"`
CorsEnabled bool `json:"cors_enabled"`
}
// UnmarshalJSON implements the json.Unmarshaler interface
func (i *ObjectStorageBucket) UnmarshalJSON(b []byte) error {
type Mask ObjectStorageBucket
p := struct {
*Mask
Created *parseabletime.ParseableTime `json:"created"`
}{
Mask: (*Mask)(i),
}
if err := json.Unmarshal(b, &p); err != nil {
return err
}
i.Created = (*time.Time)(p.Created)
return nil
}
// ObjectStorageBucketCreateOptions fields are those accepted by CreateObjectStorageBucket
type ObjectStorageBucketCreateOptions struct {
Cluster string `json:"cluster"`
Label string `json:"label"`
ACL ObjectStorageACL `json:"acl,omitempty"`
CorsEnabled *bool `json:"cors_enabled,omitempty"`
}
// ObjectStorageBucketUpdateAccessOptions fields are those accepted by UpdateObjectStorageBucketAccess
type ObjectStorageBucketUpdateAccessOptions struct {
ACL ObjectStorageACL `json:"acl,omitempty"`
CorsEnabled *bool `json:"cors_enabled,omitempty"`
}
// ObjectStorageACL options start with ACL and include all known ACL types
type ObjectStorageACL string
// ObjectStorageACL options represent the access control level of a bucket.
const (
ACLPrivate ObjectStorageACL = "private"
ACLPublicRead ObjectStorageACL = "public-read"
ACLAuthenticatedRead ObjectStorageACL = "authenticated-read"
ACLPublicReadWrite ObjectStorageACL = "public-read-write"
)
// ObjectStorageBucketsPagedResponse represents a paginated ObjectStorageBucket API response
type ObjectStorageBucketsPagedResponse struct {
*PageOptions
Data []ObjectStorageBucket `json:"data"`
}
// endpoint gets the endpoint URL for ObjectStorageBucket
func (ObjectStorageBucketsPagedResponse) endpoint(c *Client) string {
endpoint, err := c.ObjectStorageBuckets.Endpoint()
if err != nil {
panic(err)
}
return endpoint
}
// appendData appends ObjectStorageBuckets when processing paginated ObjectStorageBucket responses
func (resp *ObjectStorageBucketsPagedResponse) appendData(r *ObjectStorageBucketsPagedResponse) {
resp.Data = append(resp.Data, r.Data...)
}
// ListObjectStorageBuckets lists ObjectStorageBuckets
func (c *Client) ListObjectStorageBuckets(ctx context.Context, opts *ListOptions) ([]ObjectStorageBucket, error) {
response := ObjectStorageBucketsPagedResponse{}
err := c.listHelper(ctx, &response, opts)
if err != nil {
return nil, err
}
return response.Data, nil
}
// GetObjectStorageBucket gets the ObjectStorageBucket with the provided label
func (c *Client) GetObjectStorageBucket(ctx context.Context, clusterID, label string) (*ObjectStorageBucket, error) {
e, err := c.ObjectStorageBuckets.Endpoint()
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%s/%s", e, clusterID, label)
r, err := coupleAPIErrors(c.R(ctx).SetResult(&ObjectStorageBucket{}).Get(e))
if err != nil {
return nil, err
}
return r.Result().(*ObjectStorageBucket), nil
}
// CreateObjectStorageBucket creates an ObjectStorageBucket
func (c *Client) CreateObjectStorageBucket(ctx context.Context, createOpts ObjectStorageBucketCreateOptions) (*ObjectStorageBucket, error) {
var body string
e, err := c.ObjectStorageBuckets.Endpoint()
if err != nil {
return nil, err
}
req := c.R(ctx).SetResult(&ObjectStorageBucket{})
if bodyData, err := json.Marshal(createOpts); err == nil {
body = string(bodyData)
} else {
return nil, NewError(err)
}
r, err := coupleAPIErrors(req.
SetBody(body).
Post(e))
if err != nil {
return nil, err
}
return r.Result().(*ObjectStorageBucket), nil
}
// GetObjectStorageBucketAccess gets the current access config for a bucket
func (c *Client) GetObjectStorageBucketAccess(ctx context.Context, clusterID, label string) (*ObjectStorageBucketAccess, error) {
e, err := c.ObjectStorageBuckets.Endpoint()
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%s/%s/access", e, clusterID, label)
req := c.R(ctx).SetResult(&ObjectStorageBucketAccess{})
r, err := coupleAPIErrors(
req.Get(e))
if err != nil {
return nil, err
}
return r.Result().(*ObjectStorageBucketAccess), nil
}
// UpdateObjectStorageBucketAccess updates the access configuration for an ObjectStorageBucket
func (c *Client) UpdateObjectStorageBucketAccess(ctx context.Context, clusterID, label string, access ObjectStorageBucketUpdateAccessOptions) error {
e, err := c.ObjectStorageBuckets.Endpoint()
if err != nil {
return err
}
e = fmt.Sprintf("%s/%s/%s/access", e, clusterID, label)
bodyData, err := json.Marshal(access)
if err != nil {
return err
}
body := string(bodyData)
_, err = coupleAPIErrors(c.R(ctx).
SetBody(body).
Post(e))
if err != nil {
return err
}
return nil
}
// DeleteObjectStorageBucket deletes the ObjectStorageBucket with the specified label
func (c *Client) DeleteObjectStorageBucket(ctx context.Context, clusterID, label string) error {
e, err := c.ObjectStorageBuckets.Endpoint()
if err != nil {
return err
}
e = fmt.Sprintf("%s/%s/%s", e, clusterID, label)
_, err = coupleAPIErrors(c.R(ctx).Delete(e))
return err
}