github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/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  	"github.com/juju/errors"
     8  	"github.com/juju/names"
     9  
    10  	"github.com/juju/juju/api/base"
    11  	"github.com/juju/juju/api/watcher"
    12  	"github.com/juju/juju/apiserver/params"
    13  )
    14  
    15  // State provides access to a worker's view of the state.
    16  type State struct {
    17  	facade base.FacadeCaller
    18  }
    19  
    20  // NewState returns a version of the state that provides functionality required by the worker.
    21  func NewState(caller base.APICaller) *State {
    22  	return &State{base.NewFacadeCaller(caller, "KeyUpdater")}
    23  }
    24  
    25  // AuthorisedKeys returns the authorised ssh keys for the machine specified by machineTag.
    26  func (st *State) AuthorisedKeys(tag names.MachineTag) ([]string, error) {
    27  	var results params.StringsResults
    28  	args := params.Entities{
    29  		Entities: []params.Entity{{Tag: tag.String()}},
    30  	}
    31  	err := st.facade.FacadeCall("AuthorisedKeys", args, &results)
    32  	if err != nil {
    33  		// TODO: Not directly tested
    34  		return nil, err
    35  	}
    36  	if len(results.Results) != 1 {
    37  		// TODO: Not directly tested
    38  		return nil, errors.Errorf("expected 1 result, got %d", len(results.Results))
    39  	}
    40  	result := results.Results[0]
    41  	if err := result.Error; err != nil {
    42  		return nil, err
    43  	}
    44  	return result.Result, nil
    45  }
    46  
    47  // WatchAuthorisedKeys returns a notify watcher that looks for changes in the
    48  // authorised ssh keys for the machine specified by machineTag.
    49  func (st *State) WatchAuthorisedKeys(tag names.MachineTag) (watcher.NotifyWatcher, error) {
    50  	var results params.NotifyWatchResults
    51  	args := params.Entities{
    52  		Entities: []params.Entity{{Tag: tag.String()}},
    53  	}
    54  	err := st.facade.FacadeCall("WatchAuthorisedKeys", args, &results)
    55  	if err != nil {
    56  		// TODO: Not directly tested
    57  		return nil, err
    58  	}
    59  	if len(results.Results) != 1 {
    60  		// TODO: Not directly tested
    61  		return nil, errors.Errorf("expected 1 result, got %d", len(results.Results))
    62  	}
    63  	result := results.Results[0]
    64  	if result.Error != nil {
    65  		//  TODO: Not directly tested
    66  		return nil, result.Error
    67  	}
    68  	w := watcher.NewNotifyWatcher(st.facade.RawAPICaller(), result)
    69  	return w, nil
    70  }