/
api_clients.go
89 lines (80 loc) · 2.7 KB
/
api_clients.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
package brightbox
import (
"time"
)
// ApiClient represents an API client.
// https://api.gb1.brightbox.com/1.0/#api_client
type ApiClient struct {
Id string
Name string
Description string
Secret string
PermissionsGroup string `json:"permissions_group"`
RevokedAt *time.Time `json:"revoked_at"`
Account Account
}
// ApiClientOptions is used in conjunction with CreateApiClient and
// UpdateApiClient to create and update api clients
type ApiClientOptions struct {
Id string `json:"-"`
Name *string `json:"name,omitempty"`
Description *string `json:"description,omitempty"`
PermissionsGroup *string `json:"permissions_group,omitempty"`
}
// ApiClients retrieves a list of all API clients
func (c *Client) ApiClients() ([]ApiClient, error) {
var apiClients []ApiClient
_, err := c.MakeApiRequest("GET", "/1.0/api_clients", nil, &apiClients)
if err != nil {
return nil, err
}
return apiClients, err
}
// ApiClient retrieves a detailed view of one API client
func (c *Client) ApiClient(identifier string) (*ApiClient, error) {
apiClient := new(ApiClient)
_, err := c.MakeApiRequest("GET", "/1.0/api_clients/"+identifier, nil, apiClient)
if err != nil {
return nil, err
}
return apiClient, err
}
// CreateApiClient creates a new API client.
//
// It takes a ApiClientOptions struct for specifying name and other
// attributes. Not all attributes can be specified at create time
// (such as Id, which is allocated for you)
func (c *Client) CreateApiClient(options *ApiClientOptions) (*ApiClient, error) {
ac := new(ApiClient)
_, err := c.MakeApiRequest("POST", "/1.0/api_clients", options, &ac)
if err != nil {
return nil, err
}
return ac, nil
}
// UpdateApiClient updates an existing api client.
//
// It takes a ApiClientOptions struct for specifying Id, name and other
// attributes. Not all attributes can be specified at update time.
func (c *Client) UpdateApiClient(options *ApiClientOptions) (*ApiClient, error) {
ac := new(ApiClient)
_, err := c.MakeApiRequest("PUT", "/1.0/api_clients/"+options.Id, options, &ac)
if err != nil {
return nil, err
}
return ac, nil
}
// DestroyApiClient issues a request to deletes an existing api client
func (c *Client) DestroyApiClient(identifier string) error {
_, err := c.MakeApiRequest("DELETE", "/1.0/api_clients/"+identifier, nil, nil)
return err
}
// ResetSecretForApiClient requests a snapshot of an existing api client
func (c *Client) ResetSecretForApiClient(identifier string) (*ApiClient, error) {
ac := new(ApiClient)
_, err := c.MakeApiRequest("POST", "/1.0/api_clients/"+identifier+"/reset_secret", nil, &ac)
if err != nil {
return nil, err
}
return ac, nil
}