github.com/mwhudson/juju@v0.0.0-20160512215208-90ff01f3497f/provider/lxd/instance.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 // +build go1.3 5 6 package lxd 7 8 import ( 9 "github.com/juju/errors" 10 11 "github.com/juju/juju/instance" 12 "github.com/juju/juju/network" 13 "github.com/juju/juju/provider/common" 14 "github.com/juju/juju/status" 15 "github.com/juju/juju/tools/lxdclient" 16 ) 17 18 type environInstance struct { 19 raw *lxdclient.Instance 20 env *environ 21 } 22 23 var _ instance.Instance = (*environInstance)(nil) 24 25 func newInstance(raw *lxdclient.Instance, env *environ) *environInstance { 26 return &environInstance{ 27 raw: raw, 28 env: env, 29 } 30 } 31 32 // Id implements instance.Instance. 33 func (inst *environInstance) Id() instance.Id { 34 return instance.Id(inst.raw.Name) 35 } 36 37 // Status implements instance.Instance. 38 func (inst *environInstance) Status() instance.InstanceStatus { 39 jujuStatus := status.StatusPending 40 instStatus := inst.raw.Status() 41 switch instStatus { 42 case lxdclient.StatusStarting, lxdclient.StatusStarted: 43 jujuStatus = status.StatusAllocating 44 case lxdclient.StatusRunning: 45 jujuStatus = status.StatusRunning 46 case lxdclient.StatusFreezing, lxdclient.StatusFrozen, lxdclient.StatusThawed, lxdclient.StatusStopping, lxdclient.StatusStopped: 47 jujuStatus = status.StatusEmpty 48 default: 49 jujuStatus = status.StatusEmpty 50 } 51 return instance.InstanceStatus{ 52 Status: jujuStatus, 53 Message: instStatus, 54 } 55 56 } 57 58 // Addresses implements instance.Instance. 59 func (inst *environInstance) Addresses() ([]network.Address, error) { 60 return inst.env.raw.Addresses(inst.raw.Name) 61 } 62 63 // firewall stuff 64 65 // OpenPorts opens the given ports on the instance, which 66 // should have been started with the given machine id. 67 func (inst *environInstance) OpenPorts(machineID string, ports []network.PortRange) error { 68 // TODO(ericsnow) Make sure machineId matches inst.Id()? 69 name := common.MachineFullName(inst.env.Config().UUID(), machineID) 70 err := inst.env.raw.OpenPorts(name, ports...) 71 if errors.IsNotImplemented(err) { 72 // TODO(ericsnow) for now... 73 return nil 74 } 75 return errors.Trace(err) 76 } 77 78 // ClosePorts closes the given ports on the instance, which 79 // should have been started with the given machine id. 80 func (inst *environInstance) ClosePorts(machineID string, ports []network.PortRange) error { 81 name := common.MachineFullName(inst.env.Config().UUID(), machineID) 82 err := inst.env.raw.ClosePorts(name, ports...) 83 if errors.IsNotImplemented(err) { 84 // TODO(ericsnow) for now... 85 return nil 86 } 87 return errors.Trace(err) 88 } 89 90 // Ports returns the set of ports open on the instance, which 91 // should have been started with the given machine id. 92 // The ports are returned as sorted by SortPorts. 93 func (inst *environInstance) Ports(machineID string) ([]network.PortRange, error) { 94 name := common.MachineFullName(inst.env.Config().UUID(), machineID) 95 ports, err := inst.env.raw.Ports(name) 96 if errors.IsNotImplemented(err) { 97 // TODO(ericsnow) for now... 98 return nil, nil 99 } 100 return ports, errors.Trace(err) 101 }