forked from go-chef/chef
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
88 lines (75 loc) · 2.4 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
package chef
import "fmt"
type ApiClientService struct {
client *Client
}
// Client 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"`
Admin bool `json:"admin"`
Validator bool `json:"validator"`
Certificate string `json:"certificate,omitempty"`
PublicKey string `json:"public_key,omitempty"`
PrivateKey string `json:"private_key,omitempty"`
Uri string `json:"uri,omitempty"`
JsonClass string `json:"json_class"`
ChefType string `json:"chef_type"`
}
type ApiNewClient struct {
Name string `json:"name"`
Admin bool `json:"admin"`
}
type ApiClientCreateResult struct {
Uri string `json:"uri,omitempty"`
PrivateKey string `json:"private_key,omitempty"`
}
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.html#id13
func (e *ApiClientService) List() (data ApiClientListResult, err error) {
err = e.client.magicRequestDecoder("GET", "clients", nil, &data)
return
}
// Get gets a client from the Chef server.
//
// Chef API docs: https://docs.chef.io/api_chef_server.html#id16
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
}
// Create makes a Client on the chef server
//
// Chef API docs: https://docs.chef.io/api_chef_server.html#id14
func (e *ApiClientService) Create(clientName string, admin bool) (data *ApiClientCreateResult, err error) {
post := ApiNewClient{
Name: clientName,
Admin: admin,
}
body, err := JSONReader(post)
if err != nil {
return
}
err = e.client.magicRequestDecoder("POST", "clients", body, &data)
return
}
// Put updates a client on the Chef server.
//
// Chef API docs: https://docs.chef.io/api_chef_server.html#id17
// Delete removes a client on the Chef server
//
// Chef API docs: https://docs.chef.io/api_chef_server.html#id15
func (e *ApiClientService) Delete(name string) (err error) {
err = e.client.magicRequestDecoder("DELETE", "clients/"+name, nil, nil)
return
}