1
// Copyright 2016 Canonical Ltd.
2
// Licensed under the LGPLv3, see LICENCE file for details.
7
"github.com/juju/errors"
8
"gopkg.in/juju/names.v2"
10
"github.com/juju/juju/apiserver/facade"
11
"github.com/juju/juju/environs/config"
12
"github.com/juju/juju/network"
13
"github.com/juju/juju/state"
16
// Backend defines the State API used by the sshclient facade.
17
type Backend interface {
18
ModelConfig() (*config.Config, error)
19
GetMachineForEntity(tag string) (SSHMachine, error)
20
GetSSHHostKeys(names.MachineTag) (state.SSHHostKeys, error)
23
// SSHMachine specifies the methods on State.Machine of interest to
24
// the SSHClient facade.
25
type SSHMachine interface {
26
MachineTag() names.MachineTag
27
PublicAddress() (network.Address, error)
28
PrivateAddress() (network.Address, error)
31
// newFacade wraps New to express the supplied *state.State as a Backend.
32
func newFacade(st *state.State, res facade.Resources, auth facade.Authorizer) (*Facade, error) {
33
return New(&backend{st}, res, auth)
40
// GetMachineForEntity takes a machine or unit tag (as a string) and
41
// returns the associated SSHMachine.
42
func (b *backend) GetMachineForEntity(tagString string) (SSHMachine, error) {
43
tag, err := names.ParseTag(tagString)
45
return nil, errors.Trace(err)
48
switch tag := tag.(type) {
49
case names.MachineTag:
50
machine, err := b.State.Machine(tag.Id())
52
return nil, errors.Trace(err)
56
unit, err := b.State.Unit(tag.Id())
58
return nil, errors.Trace(err)
60
machineId, err := unit.AssignedMachineId()
62
return nil, errors.Trace(err)
64
machine, err := b.State.Machine(machineId)
66
return nil, errors.Trace(err)
70
return nil, errors.Errorf("unsupported entity: %q", tagString)