/
instance.go
97 lines (82 loc) · 2.58 KB
/
instance.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
// +build go1.3
package lxd
import (
"github.com/juju/errors"
"github.com/juju/juju/instance"
"github.com/juju/juju/network"
"github.com/juju/juju/provider/common"
"github.com/juju/juju/provider/lxd/lxdclient"
)
type environInstance struct {
raw *lxdclient.Instance
env *environ
}
var _ instance.Instance = (*environInstance)(nil)
func newInstance(raw *lxdclient.Instance, env *environ) *environInstance {
return &environInstance{
raw: raw,
env: env,
}
}
// Id implements instance.Instance.
func (inst *environInstance) Id() instance.Id {
return instance.Id(inst.raw.Name)
}
// Status implements instance.Instance.
func (inst *environInstance) Status() string {
return inst.raw.Status()
}
// Addresses implements instance.Instance.
func (inst *environInstance) Addresses() ([]network.Address, error) {
// TODO(ericsnow) This may need to be more dynamic.
return inst.raw.Addresses, nil
}
func findInst(id instance.Id, instances []instance.Instance) instance.Instance {
for _, inst := range instances {
if id == inst.Id() {
return inst
}
}
return nil
}
// firewall stuff
// OpenPorts opens the given ports on the instance, which
// should have been started with the given machine id.
func (inst *environInstance) OpenPorts(machineID string, ports []network.PortRange) error {
// TODO(ericsnow) Make sure machineId matches inst.Id()?
name := common.MachineFullName(inst.env, machineID)
env := inst.env.getSnapshot()
err := env.raw.OpenPorts(name, ports...)
if errors.IsNotImplemented(err) {
// TODO(ericsnow) for now...
return nil
}
return errors.Trace(err)
}
// ClosePorts closes the given ports on the instance, which
// should have been started with the given machine id.
func (inst *environInstance) ClosePorts(machineID string, ports []network.PortRange) error {
name := common.MachineFullName(inst.env, machineID)
env := inst.env.getSnapshot()
err := env.raw.ClosePorts(name, ports...)
if errors.IsNotImplemented(err) {
// TODO(ericsnow) for now...
return nil
}
return errors.Trace(err)
}
// Ports returns the set of ports open on the instance, which
// should have been started with the given machine id.
// The ports are returned as sorted by SortPorts.
func (inst *environInstance) Ports(machineID string) ([]network.PortRange, error) {
name := common.MachineFullName(inst.env, machineID)
env := inst.env.getSnapshot()
ports, err := env.raw.Ports(name)
if errors.IsNotImplemented(err) {
// TODO(ericsnow) for now...
return nil, nil
}
return ports, errors.Trace(err)
}