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