-
Notifications
You must be signed in to change notification settings - Fork 65
/
client.go
157 lines (140 loc) · 5.1 KB
/
client.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
package chef
import "fmt"
type ApiClientService struct {
client *Client
}
// ApiClient represents the native Go version of the deserialized Client type
type ApiClient struct {
Name string `json:"name"`
ClientName string `json:"clientname"`
OrgName string `json:"orgname"` // returned after get, not returned after update
Validator bool `json:"validator"`
JsonClass string `json:"json_class"`
ChefType string `json:"chef_type"`
}
// ApiNewClient structure to request a new client
type ApiNewClient struct {
Name string `json:"name,omitempty"` // name or clientname must be specified to create a client
ClientName string `json:"clientname,omitempty"`
Validator bool `json:"validator,omitempty"`
Admin bool `json:"admin,omitempty"` // not supported and ignored as of 12.1.0
CreateKey bool `json:"create_key,omitempty"` // not supported for update requests
}
// ApiNewClientResult
type ApiClientCreateResult struct {
Uri string `json:"uri,omitempty"`
ChefKey ChefKey `json:"chef_key,omitempty"`
}
// ApiClientListResult is map of the client names to client Uri
type ApiClientListResult map[string]string
// String makes ApiClientListResult implement the string result
func (c ApiClientListResult) String() (out string) {
for k, v := range c {
out += fmt.Sprintf("%s => %s\n", k, v)
}
return out
}
// List lists the clients in the Chef server.
//
// Chef API docs: https://docs.chef.io/api_chef_server/#get-11
func (e *ApiClientService) List() (data ApiClientListResult, err error) {
err = e.client.magicRequestDecoder("GET", "clients", nil, &data)
return
}
// Create makes a Client on the chef server
//
// Chef API docs: https://docs.chef.io/api_chef_server.html#clients
func (e *ApiClientService) Create(client ApiNewClient) (data *ApiClientCreateResult, err error) {
body, err := JSONReader(client)
if err != nil {
return
}
err = e.client.magicRequestDecoder("POST", "clients", body, &data)
return
}
// Delete removes a client on the Chef server
//
// Chef API docs: https://docs.chef.io/api_chef_server.html#clients-name
func (e *ApiClientService) Delete(name string) (err error) {
url := fmt.Sprintf("clients/%s", name)
err = e.client.magicRequestDecoder("DELETE", url, nil, nil)
return
}
// Get gets a client from the Chef server.
//
// Chef API docs: https://docs.chef.io/api_chef_server.html#clients-name
func (e *ApiClientService) Get(name string) (client ApiClient, err error) {
url := fmt.Sprintf("clients/%s", name)
err = e.client.magicRequestDecoder("GET", url, nil, &client)
return
}
// Put updates a client on the Chef server.
//
// Chef API docs: https://docs.chef.io/api_chef_server.html#clients-name
func (e *ApiClientService) Update(name string, client ApiNewClient) (data *ApiClient, err error) {
body, err := JSONReader(client)
url := fmt.Sprintf("clients/%s", name)
if err != nil {
return
}
err = e.client.magicRequestDecoder("PUT", url, body, &data)
return
}
// ListKeys lists the keys associated with a client on the Chef server.
//
// Chef API docs: https://docs.chef.io/api_chef_server.html#clients-client-keys
func (e *ApiClientService) ListKeys(name string) (data []KeyItem, err error) {
url := fmt.Sprintf("clients/%s/keys", name)
err = e.client.magicRequestDecoder("GET", url, nil, &data)
return
}
// AddKey add a key for a client on the Chef server.
// /clients/USERNAME/keys POST
// 201 - created
// 401 - not authenticated
// 403 - not authorizated
// 404 - client doesn't exist
// 409 - new name is already in use
//
// Chef API docs: https://docs.chef.io/api_chef_server.html#clients-name
func (e *ApiClientService) AddKey(name string, keyadd AccessKey) (key KeyItem, err error) {
url := fmt.Sprintf("clients/%s/keys", name)
body, err := JSONReader(keyadd)
err = e.client.magicRequestDecoder("POST", url, body, &key)
return
}
// DeleteKey delete a key for a client.
// /clients/USERNAME/keys/KEYNAME DELETE
// 200 - successful
// 401 - not authenticated
// 403 - not authorizated
// 404 - client doesn't exist
//
// Chef API docs: https://docs.chef.io/api_chef_server/#clientskeys
func (e *ApiClientService) DeleteKey(name string, keyname string) (key AccessKey, err error) {
url := fmt.Sprintf("clients/%s/keys/%s", name, keyname)
err = e.client.magicRequestDecoder("DELETE", url, nil, &key)
return
}
// GetKey gets a client key from the Chef server.
//
// Chef API docs: https://docs.chef.io/api_chef_server.html#clients-client-keys-key
func (e *ApiClientService) GetKey(name string, keyname string) (key AccessKey, err error) {
url := fmt.Sprintf("clients/%s/keys/%s", name, keyname)
err = e.client.magicRequestDecoder("GET", url, nil, &key)
return
}
// UpdateKey updates a key for a client.
// /clients/USERNAME/keys/KEYNAME PUT
// 200 - successful
// 401 - not authenticated
// 403 - not authorizated
// 404 - client doesn't exist
//
// Chef API docs: https://docs.chef.io/api_chef_server/#clientskeys
func (e *ApiClientService) UpdateKey(name string, keyname string, keyupd AccessKey) (key AccessKey, err error) {
url := fmt.Sprintf("clients/%s/keys/%s", name, keyname)
body, err := JSONReader(keyupd)
err = e.client.magicRequestDecoder("PUT", url, body, &key)
return
}