github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/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  	"github.com/juju/juju/state/api/base"
    10  	"github.com/juju/juju/state/api/params"
    11  	"github.com/juju/juju/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  func (st *State) call(method string, params, result interface{}) error {
    20  	return st.caller.Call("KeyUpdater", "", method, params, result)
    21  }
    22  
    23  // NewState returns a version of the state that provides functionality required by the worker.
    24  func NewState(caller base.Caller) *State {
    25  	return &State{caller}
    26  }
    27  
    28  // AuthorisedKeys returns the authorised ssh keys for the machine specified by machineTag.
    29  func (st *State) AuthorisedKeys(machineTag string) ([]string, error) {
    30  	var results params.StringsResults
    31  	args := params.Entities{
    32  		Entities: []params.Entity{{Tag: machineTag}},
    33  	}
    34  	err := st.call("AuthorisedKeys", args, &results)
    35  	if err != nil {
    36  		// TODO: Not directly tested
    37  		return nil, err
    38  	}
    39  	if len(results.Results) != 1 {
    40  		// TODO: Not directly tested
    41  		return nil, fmt.Errorf("expected 1 result, got %d", len(results.Results))
    42  	}
    43  	result := results.Results[0]
    44  	if err := result.Error; err != nil {
    45  		return nil, err
    46  	}
    47  	return result.Result, nil
    48  }
    49  
    50  // WatchAuthorisedKeys returns a notify watcher that looks for changes in the
    51  // authorised ssh keys for the machine specified by machineTag.
    52  func (st *State) WatchAuthorisedKeys(machineTag string) (watcher.NotifyWatcher, error) {
    53  	var results params.NotifyWatchResults
    54  	args := params.Entities{
    55  		Entities: []params.Entity{{Tag: machineTag}},
    56  	}
    57  	err := st.call("WatchAuthorisedKeys", args, &results)
    58  	if err != nil {
    59  		// TODO: Not directly tested
    60  		return nil, err
    61  	}
    62  	if len(results.Results) != 1 {
    63  		// TODO: Not directly tested
    64  		return nil, fmt.Errorf("expected 1 result, got %d", len(results.Results))
    65  	}
    66  	result := results.Results[0]
    67  	if result.Error != nil {
    68  		//  TODO: Not directly tested
    69  		return nil, result.Error
    70  	}
    71  	w := watcher.NewNotifyWatcher(st.caller, result)
    72  	return w, nil
    73  }