github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/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  	"launchpad.net/juju-core/state/api"
     8  	"launchpad.net/juju-core/state/api/params"
     9  	"launchpad.net/juju-core/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  // NewClient returns a new keymanager client.
    18  func NewClient(st *api.State) *Client {
    19  	return &Client{st}
    20  }
    21  
    22  // Close closes the underlying State connection.
    23  func (c *Client) Close() error {
    24  	return c.st.Close()
    25  }
    26  
    27  // ListKeys returns the authorised ssh keys for the specified users.
    28  func (c *Client) ListKeys(mode ssh.ListMode, users ...string) ([]params.StringsResult, error) {
    29  	p := params.ListSSHKeys{Mode: mode}
    30  	p.Entities.Entities = make([]params.Entity, len(users))
    31  	for i, userName := range users {
    32  		p.Entities.Entities[i] = params.Entity{Tag: userName}
    33  	}
    34  	results := new(params.StringsResults)
    35  	err := c.st.Call("KeyManager", "", "ListKeys", p, results)
    36  	return results.Results, err
    37  }
    38  
    39  // AddKeys adds the authorised ssh keys for the specified user.
    40  func (c *Client) AddKeys(user string, keys ...string) ([]params.ErrorResult, error) {
    41  	p := params.ModifyUserSSHKeys{User: user, Keys: keys}
    42  	results := new(params.ErrorResults)
    43  	err := c.st.Call("KeyManager", "", "AddKeys", p, results)
    44  	return results.Results, err
    45  }
    46  
    47  // DeleteKeys deletes the authorised ssh keys for the specified user.
    48  func (c *Client) DeleteKeys(user string, keys ...string) ([]params.ErrorResult, error) {
    49  	p := params.ModifyUserSSHKeys{User: user, Keys: keys}
    50  	results := new(params.ErrorResults)
    51  	err := c.st.Call("KeyManager", "", "DeleteKeys", p, results)
    52  	return results.Results, err
    53  }
    54  
    55  // ImportKeys imports the authorised ssh keys with the specified key ids for the specified user.
    56  func (c *Client) ImportKeys(user string, keyIds ...string) ([]params.ErrorResult, error) {
    57  	p := params.ModifyUserSSHKeys{User: user, Keys: keyIds}
    58  	results := new(params.ErrorResults)
    59  	err := c.st.Call("KeyManager", "", "ImportKeys", p, results)
    60  	return results.Results, err
    61  }