~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/apiserver/sshclient/shim.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
}
 
22
 
 
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)
 
29
}
 
30
 
 
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)
 
34
}
 
35
 
 
36
type backend struct {
 
37
        *state.State
 
38
}
 
39
 
 
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)
 
44
        if err != nil {
 
45
                return nil, errors.Trace(err)
 
46
        }
 
47
 
 
48
        switch tag := tag.(type) {
 
49
        case names.MachineTag:
 
50
                machine, err := b.State.Machine(tag.Id())
 
51
                if err != nil {
 
52
                        return nil, errors.Trace(err)
 
53
                }
 
54
                return machine, nil
 
55
        case names.UnitTag:
 
56
                unit, err := b.State.Unit(tag.Id())
 
57
                if err != nil {
 
58
                        return nil, errors.Trace(err)
 
59
                }
 
60
                machineId, err := unit.AssignedMachineId()
 
61
                if err != nil {
 
62
                        return nil, errors.Trace(err)
 
63
                }
 
64
                machine, err := b.State.Machine(machineId)
 
65
                if err != nil {
 
66
                        return nil, errors.Trace(err)
 
67
                }
 
68
                return machine, nil
 
69
        default:
 
70
                return nil, errors.Errorf("unsupported entity: %q", tagString)
 
71
        }
 
72
}