-
Notifications
You must be signed in to change notification settings - Fork 2
/
acl.go
164 lines (139 loc) · 4.01 KB
/
acl.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
package dbaas
import (
"context"
"encoding/json"
"fmt"
"net/http"
)
// ACL is the API response for the acls.
type ACL struct {
ID string `json:"id"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
ProjectID string `json:"project_id"`
DatastoreID string `json:"datastore_id"`
Pattern string `json:"pattern"`
PatternType string `json:"pattern_type"`
UserID string `json:"user_id"`
Status Status `json:"status"`
AllowRead bool `json:"allow_read"`
AllowWrite bool `json:"allow_write"`
}
// ACLCreateOpts represents options for the acl Create request.
type ACLCreateOpts struct {
DatastoreID string `json:"datastore_id"`
Pattern string `json:"pattern,omitempty"`
PatternType string `json:"pattern_type"`
UserID string `json:"user_id"`
AllowRead bool `json:"allow_read"`
AllowWrite bool `json:"allow_write"`
}
// ACLUpdateOpts represents options for the acl Update request.
type ACLUpdateOpts struct {
AllowRead bool `json:"allow_read"`
AllowWrite bool `json:"allow_write"`
}
// ACLQueryParams represents available query parameters for the acl.
type ACLQueryParams struct {
ID string `json:"id,omitempty"`
ProjectID string `json:"project_id,omitempty"`
DatastoreID string `json:"datastore_id,omitempty"`
Pattern string `json:"pattern,omitempty"`
PatternType string `json:"pattern_type,omitempty"`
UserID string `json:"user_id,omitempty"`
Status Status `json:"status,omitempty"`
}
const ACLsURI = "/acls"
// ACLs returns all ACLs.
func (api *API) ACLs(ctx context.Context, params *ACLQueryParams) ([]ACL, error) {
uri, err := setQueryParams(ACLsURI, params)
if err != nil {
return []ACL{}, err
}
resp, err := api.makeRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return []ACL{}, err
}
var result struct {
ACLs []ACL `json:"acls"`
}
err = json.Unmarshal(resp, &result)
if err != nil {
return []ACL{}, fmt.Errorf("Error during Unmarshal, %w", err)
}
return result.ACLs, nil
}
// ACL returns an ACL based on the ID.
func (api *API) ACL(ctx context.Context, aclID string) (ACL, error) {
uri := fmt.Sprintf("%s/%s", ACLsURI, aclID)
resp, err := api.makeRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return ACL{}, err
}
var result struct {
ACL ACL `json:"acl"`
}
err = json.Unmarshal(resp, &result)
if err != nil {
return ACL{}, fmt.Errorf("Error during Unmarshal, %w", err)
}
return result.ACL, nil
}
// CreateACL creates a new acl.
func (api *API) CreateACL(ctx context.Context, opts ACLCreateOpts) (ACL, error) {
createACLOpts := struct {
ACL ACLCreateOpts `json:"acl"`
}{
ACL: opts,
}
requestBody, err := json.Marshal(createACLOpts)
if err != nil {
return ACL{}, fmt.Errorf("Error marshalling params to JSON, %w", err)
}
resp, err := api.makeRequest(ctx, http.MethodPost, ACLsURI, requestBody)
if err != nil {
return ACL{}, err
}
var result struct {
ACL ACL `json:"acl"`
}
err = json.Unmarshal(resp, &result)
if err != nil {
return ACL{}, fmt.Errorf("Error during Unmarshal, %w", err)
}
return result.ACL, nil
}
// UpdateACL updates an existing acl.
func (api *API) UpdateACL(ctx context.Context, aclID string, opts ACLUpdateOpts) (ACL, error) {
uri := fmt.Sprintf("%s/%s", ACLsURI, aclID)
updateACLOpts := struct {
ACL ACLUpdateOpts `json:"acl"`
}{
ACL: opts,
}
requestBody, err := json.Marshal(updateACLOpts)
if err != nil {
return ACL{}, fmt.Errorf("Error marshalling params to JSON, %w", err)
}
resp, err := api.makeRequest(ctx, http.MethodPut, uri, requestBody)
if err != nil {
return ACL{}, err
}
var result struct {
ACL ACL `json:"acl"`
}
err = json.Unmarshal(resp, &result)
if err != nil {
return ACL{}, fmt.Errorf("Error during Unmarshal, %w", err)
}
return result.ACL, nil
}
// DeleteACL deletes an existing acl.
func (api *API) DeleteACL(ctx context.Context, aclID string) error {
uri := fmt.Sprintf("%s/%s", ACLsURI, aclID)
_, err := api.makeRequest(ctx, http.MethodDelete, uri, nil)
if err != nil {
return err
}
return nil
}