github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/apiserver/sshclient/shim.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the LGPLv3, see LICENCE file for details. 3 4 package sshclient 5 6 import ( 7 "github.com/juju/errors" 8 "gopkg.in/juju/names.v2" 9 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" 14 ) 15 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) 21 ModelTag() names.ModelTag 22 } 23 24 // SSHMachine specifies the methods on State.Machine of interest to 25 // the SSHClient facade. 26 type SSHMachine interface { 27 MachineTag() names.MachineTag 28 PublicAddress() (network.Address, error) 29 PrivateAddress() (network.Address, error) 30 } 31 32 // newFacade wraps New to express the supplied *state.State as a Backend. 33 func newFacade(st *state.State, res facade.Resources, auth facade.Authorizer) (*Facade, error) { 34 return New(&backend{st}, res, auth) 35 } 36 37 type backend struct { 38 *state.State 39 } 40 41 // GetMachineForEntity takes a machine or unit tag (as a string) and 42 // returns the associated SSHMachine. 43 func (b *backend) GetMachineForEntity(tagString string) (SSHMachine, error) { 44 tag, err := names.ParseTag(tagString) 45 if err != nil { 46 return nil, errors.Trace(err) 47 } 48 49 switch tag := tag.(type) { 50 case names.MachineTag: 51 machine, err := b.State.Machine(tag.Id()) 52 if err != nil { 53 return nil, errors.Trace(err) 54 } 55 return machine, nil 56 case names.UnitTag: 57 unit, err := b.State.Unit(tag.Id()) 58 if err != nil { 59 return nil, errors.Trace(err) 60 } 61 machineId, err := unit.AssignedMachineId() 62 if err != nil { 63 return nil, errors.Trace(err) 64 } 65 machine, err := b.State.Machine(machineId) 66 if err != nil { 67 return nil, errors.Trace(err) 68 } 69 return machine, nil 70 default: 71 return nil, errors.Errorf("unsupported entity: %q", tagString) 72 } 73 }