github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/state/api/keyupdater/authorisedkeys.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package keyupdater
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"launchpad.net/juju-core/state/api/base"
    10  	"launchpad.net/juju-core/state/api/params"
    11  	"launchpad.net/juju-core/state/api/watcher"
    12  )
    13  
    14  // State provides access to a worker's view of the state.
    15  type State struct {
    16  	caller base.Caller
    17  }
    18  
    19  // NewState returns a version of the state that provides functionality required by the worker.
    20  func NewState(caller base.Caller) *State {
    21  	return &State{caller}
    22  }
    23  
    24  // AuthorisedKeys returns the authorised ssh keys for the machine specified by machineTag.
    25  func (st *State) AuthorisedKeys(machineTag string) ([]string, error) {
    26  	var results params.StringsResults
    27  	args := params.Entities{
    28  		Entities: []params.Entity{{Tag: machineTag}},
    29  	}
    30  	err := st.caller.Call("KeyUpdater", "", "AuthorisedKeys", args, &results)
    31  	if err != nil {
    32  		// TODO: Not directly tested
    33  		return nil, err
    34  	}
    35  	if len(results.Results) != 1 {
    36  		// TODO: Not directly tested
    37  		return nil, fmt.Errorf("expected one result, got %d", len(results.Results))
    38  	}
    39  	result := results.Results[0]
    40  	if err := result.Error; err != nil {
    41  		return nil, err
    42  	}
    43  	return result.Result, nil
    44  }
    45  
    46  // WatchAuthorisedKeys returns a notify watcher that looks for changes in the
    47  // authorised ssh keys for the machine specified by machineTag.
    48  func (st *State) WatchAuthorisedKeys(machineTag string) (watcher.NotifyWatcher, error) {
    49  	var results params.NotifyWatchResults
    50  	args := params.Entities{
    51  		Entities: []params.Entity{{Tag: machineTag}},
    52  	}
    53  	err := st.caller.Call("KeyUpdater", "", "WatchAuthorisedKeys", args, &results)
    54  	if err != nil {
    55  		// TODO: Not directly tested
    56  		return nil, err
    57  	}
    58  	if len(results.Results) != 1 {
    59  		// TODO: Not directly tested
    60  		return nil, fmt.Errorf("expected one result, got %d", len(results.Results))
    61  	}
    62  	result := results.Results[0]
    63  	if result.Error != nil {
    64  		//  TODO: Not directly tested
    65  		return nil, result.Error
    66  	}
    67  	w := watcher.NewNotifyWatcher(st.caller, result)
    68  	return w, nil
    69  }