github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/state/api/keymanager/client.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package keymanager
     5  
     6  import (
     7  	"github.com/juju/juju/state/api"
     8  	"github.com/juju/juju/state/api/params"
     9  	"github.com/juju/juju/utils/ssh"
    10  )
    11  
    12  // Client provides access to the keymanager, used to add/delete/list authorised ssh keys.
    13  type Client struct {
    14  	st *api.State
    15  }
    16  
    17  func (c *Client) call(method string, params, result interface{}) error {
    18  	return c.st.Call("KeyManager", "", method, params, result)
    19  }
    20  
    21  // NewClient returns a new keymanager client.
    22  func NewClient(st *api.State) *Client {
    23  	return &Client{st}
    24  }
    25  
    26  // Close closes the underlying State connection.
    27  func (c *Client) Close() error {
    28  	return c.st.Close()
    29  }
    30  
    31  // ListKeys returns the authorised ssh keys for the specified users.
    32  func (c *Client) ListKeys(mode ssh.ListMode, users ...string) ([]params.StringsResult, error) {
    33  	p := params.ListSSHKeys{Mode: mode}
    34  	p.Entities.Entities = make([]params.Entity, len(users))
    35  	for i, userName := range users {
    36  		p.Entities.Entities[i] = params.Entity{Tag: userName}
    37  	}
    38  	results := new(params.StringsResults)
    39  	err := c.call("ListKeys", p, results)
    40  	return results.Results, err
    41  }
    42  
    43  // AddKeys adds the authorised ssh keys for the specified user.
    44  func (c *Client) AddKeys(user string, keys ...string) ([]params.ErrorResult, error) {
    45  	p := params.ModifyUserSSHKeys{User: user, Keys: keys}
    46  	results := new(params.ErrorResults)
    47  	err := c.call("AddKeys", p, results)
    48  	return results.Results, err
    49  }
    50  
    51  // DeleteKeys deletes the authorised ssh keys for the specified user.
    52  func (c *Client) DeleteKeys(user string, keys ...string) ([]params.ErrorResult, error) {
    53  	p := params.ModifyUserSSHKeys{User: user, Keys: keys}
    54  	results := new(params.ErrorResults)
    55  	err := c.call("DeleteKeys", p, results)
    56  	return results.Results, err
    57  }
    58  
    59  // ImportKeys imports the authorised ssh keys with the specified key ids for the specified user.
    60  func (c *Client) ImportKeys(user string, keyIds ...string) ([]params.ErrorResult, error) {
    61  	p := params.ModifyUserSSHKeys{User: user, Keys: keyIds}
    62  	results := new(params.ErrorResults)
    63  	err := c.call("ImportKeys", p, results)
    64  	return results.Results, err
    65  }