-
Notifications
You must be signed in to change notification settings - Fork 2
/
grant.go
107 lines (89 loc) · 2.49 KB
/
grant.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
package dbaas
import (
"context"
"encoding/json"
"fmt"
"net/http"
)
// GrantCreateOpts represents options for the grant Create request.
type GrantCreateOpts struct {
DatastoreID string `json:"datastore_id"`
DatabaseID string `json:"database_id"`
UserID string `json:"user_id"`
}
// Grant is the API response for the grants.
type Grant 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"`
DatabaseID string `json:"database_id"`
UserID string `json:"user_id"`
Status Status `json:"status"`
}
const GrantsURI = "/grants"
// Grant returns a grant based on the ID.
func (api *API) Grant(ctx context.Context, grantID string) (Grant, error) {
uri := fmt.Sprintf("%s/%s", GrantsURI, grantID)
resp, err := api.makeRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return Grant{}, err
}
var result struct {
Grant Grant `json:"grant"`
}
err = json.Unmarshal(resp, &result)
if err != nil {
return Grant{}, fmt.Errorf("Error during Unmarshal, %w", err)
}
return result.Grant, nil
}
// Grants returns all grants.
func (api *API) Grants(ctx context.Context) ([]Grant, error) {
resp, err := api.makeRequest(ctx, http.MethodGet, GrantsURI, nil)
if err != nil {
return []Grant{}, err
}
var result struct {
Grants []Grant `json:"grants"`
}
err = json.Unmarshal(resp, &result)
if err != nil {
return []Grant{}, fmt.Errorf("Error during Unmarshal, %w", err)
}
return result.Grants, nil
}
// CreateGrant creates a new grant.
func (api *API) CreateGrant(ctx context.Context, opts GrantCreateOpts) (Grant, error) {
createGrantOpts := struct {
Grant GrantCreateOpts `json:"grant"`
}{
Grant: opts,
}
requestBody, err := json.Marshal(createGrantOpts)
if err != nil {
return Grant{}, fmt.Errorf("Error marshalling params to JSON, %w", err)
}
resp, err := api.makeRequest(ctx, http.MethodPost, GrantsURI, requestBody)
if err != nil {
return Grant{}, err
}
var result struct {
Grant Grant `json:"grant"`
}
err = json.Unmarshal(resp, &result)
if err != nil {
return Grant{}, fmt.Errorf("Error during Unmarshal, %w", err)
}
return result.Grant, nil
}
// DeleteGrant deletes an existing grant.
func (api *API) DeleteGrant(ctx context.Context, grantID string) error {
uri := fmt.Sprintf("%s/%s", GrantsURI, grantID)
_, err := api.makeRequest(ctx, http.MethodDelete, uri, nil)
if err != nil {
return err
}
return nil
}