github.com/go-chef/chef@v0.30.1/client.go (about)

     1  package chef
     2  
     3  import "fmt"
     4  
     5  type ApiClientService struct {
     6  	client *Client
     7  }
     8  
     9  // ApiClient represents the native Go version of the deserialized Client type
    10  type ApiClient struct {
    11  	Name       string `json:"name"`
    12  	ClientName string `json:"clientname"`
    13  	OrgName    string `json:"orgname"` // returned after get, not returned after update
    14  	Validator  bool   `json:"validator"`
    15  	JsonClass  string `json:"json_class"`
    16  	ChefType   string `json:"chef_type"`
    17  }
    18  
    19  // ApiNewClient structure to request a new client
    20  type ApiNewClient struct {
    21  	Name       string `json:"name,omitempty"` // name or clientname must be specified to create a client
    22  	ClientName string `json:"clientname,omitempty"`
    23  	Validator  bool   `json:"validator,omitempty"`
    24  	Admin      bool   `json:"admin,omitempty"`      // not supported and ignored as of 12.1.0
    25  	CreateKey  bool   `json:"create_key,omitempty"` // not supported for update requests
    26  }
    27  
    28  // ApiNewClientResult
    29  type ApiClientCreateResult struct {
    30  	Uri     string  `json:"uri,omitempty"`
    31  	ChefKey ChefKey `json:"chef_key,omitempty"`
    32  }
    33  
    34  // ApiClientListResult is map of the client names to client Uri
    35  type ApiClientListResult map[string]string
    36  
    37  // String makes ApiClientListResult implement the string result
    38  func (c ApiClientListResult) String() (out string) {
    39  	for k, v := range c {
    40  		out += fmt.Sprintf("%s => %s\n", k, v)
    41  	}
    42  	return out
    43  }
    44  
    45  // List lists the clients in the Chef server.
    46  //
    47  // Chef API docs: https://docs.chef.io/api_chef_server/#get-11
    48  func (e *ApiClientService) List() (data ApiClientListResult, err error) {
    49  	err = e.client.magicRequestDecoder("GET", "clients", nil, &data)
    50  	return
    51  }
    52  
    53  // Create makes a Client on the chef server
    54  //
    55  // Chef API docs: https://docs.chef.io/api_chef_server.html#clients
    56  func (e *ApiClientService) Create(client ApiNewClient) (data *ApiClientCreateResult, err error) {
    57  	body, err := JSONReader(client)
    58  	if err != nil {
    59  		return
    60  	}
    61  	err = e.client.magicRequestDecoder("POST", "clients", body, &data)
    62  	return
    63  }
    64  
    65  // Delete removes a client on the Chef server
    66  //
    67  // Chef API docs: https://docs.chef.io/api_chef_server.html#clients-name
    68  func (e *ApiClientService) Delete(name string) (err error) {
    69  	url := fmt.Sprintf("clients/%s", name)
    70  	err = e.client.magicRequestDecoder("DELETE", url, nil, nil)
    71  	return
    72  }
    73  
    74  // Get gets a client from the Chef server.
    75  //
    76  // Chef API docs: https://docs.chef.io/api_chef_server.html#clients-name
    77  func (e *ApiClientService) Get(name string) (client ApiClient, err error) {
    78  	url := fmt.Sprintf("clients/%s", name)
    79  	err = e.client.magicRequestDecoder("GET", url, nil, &client)
    80  	return
    81  }
    82  
    83  // Put updates a client on the Chef server.
    84  //
    85  // Chef API docs: https://docs.chef.io/api_chef_server.html#clients-name
    86  func (e *ApiClientService) Update(name string, client ApiNewClient) (data *ApiClient, err error) {
    87  	body, err := JSONReader(client)
    88  	url := fmt.Sprintf("clients/%s", name)
    89  	if err != nil {
    90  		return
    91  	}
    92  	err = e.client.magicRequestDecoder("PUT", url, body, &data)
    93  	return
    94  }
    95  
    96  // ListKeys lists the keys associated with a client on the Chef server.
    97  //
    98  // Chef API docs: https://docs.chef.io/api_chef_server.html#clients-client-keys
    99  func (e *ApiClientService) ListKeys(name string) (data []KeyItem, err error) {
   100  	url := fmt.Sprintf("clients/%s/keys", name)
   101  	err = e.client.magicRequestDecoder("GET", url, nil, &data)
   102  	return
   103  }
   104  
   105  // AddKey add a key for a client on the Chef server.
   106  // /clients/USERNAME/keys POST
   107  // 201 - created
   108  // 401 - not authenticated
   109  // 403 - not authorizated
   110  // 404 - client doesn't exist
   111  // 409 - new name is already in use
   112  //
   113  // Chef API docs: https://docs.chef.io/api_chef_server.html#clients-name
   114  func (e *ApiClientService) AddKey(name string, keyadd AccessKey) (key KeyItem, err error) {
   115  	url := fmt.Sprintf("clients/%s/keys", name)
   116  	body, err := JSONReader(keyadd)
   117  	err = e.client.magicRequestDecoder("POST", url, body, &key)
   118  	return
   119  }
   120  
   121  // DeleteKey delete a key for a client.
   122  // /clients/USERNAME/keys/KEYNAME DELETE
   123  // 200 - successful
   124  // 401 - not authenticated
   125  // 403 - not authorizated
   126  // 404 - client doesn't exist
   127  //
   128  // Chef API docs: https://docs.chef.io/api_chef_server/#clientskeys
   129  func (e *ApiClientService) DeleteKey(name string, keyname string) (key AccessKey, err error) {
   130  	url := fmt.Sprintf("clients/%s/keys/%s", name, keyname)
   131  	err = e.client.magicRequestDecoder("DELETE", url, nil, &key)
   132  	return
   133  }
   134  
   135  // GetKey gets a client key from the Chef server.
   136  //
   137  // Chef API docs: https://docs.chef.io/api_chef_server.html#clients-client-keys-key
   138  func (e *ApiClientService) GetKey(name string, keyname string) (key AccessKey, err error) {
   139  	url := fmt.Sprintf("clients/%s/keys/%s", name, keyname)
   140  	err = e.client.magicRequestDecoder("GET", url, nil, &key)
   141  	return
   142  }
   143  
   144  // UpdateKey updates a key for a client.
   145  // /clients/USERNAME/keys/KEYNAME PUT
   146  // 200 - successful
   147  // 401 - not authenticated
   148  // 403 - not authorizated
   149  // 404 - client doesn't exist
   150  //
   151  // Chef API docs: https://docs.chef.io/api_chef_server/#clientskeys
   152  func (e *ApiClientService) UpdateKey(name string, keyname string, keyupd AccessKey) (key AccessKey, err error) {
   153  	url := fmt.Sprintf("clients/%s/keys/%s", name, keyname)
   154  	body, err := JSONReader(keyupd)
   155  	err = e.client.magicRequestDecoder("PUT", url, body, &key)
   156  	return
   157  }