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