github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/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/status" 14 "github.com/juju/juju/tools/lxdclient" 15 ) 16 17 type environInstance struct { 18 raw *lxdclient.Instance 19 env *environ 20 } 21 22 var _ instance.Instance = (*environInstance)(nil) 23 24 func newInstance(raw *lxdclient.Instance, env *environ) *environInstance { 25 return &environInstance{ 26 raw: raw, 27 env: env, 28 } 29 } 30 31 // Id implements instance.Instance. 32 func (inst *environInstance) Id() instance.Id { 33 return instance.Id(inst.raw.Name) 34 } 35 36 // Status implements instance.Instance. 37 func (inst *environInstance) Status() instance.InstanceStatus { 38 jujuStatus := status.Pending 39 instStatus := inst.raw.Status() 40 switch instStatus { 41 case lxdclient.StatusStarting, lxdclient.StatusStarted: 42 jujuStatus = status.Allocating 43 case lxdclient.StatusRunning: 44 jujuStatus = status.Running 45 case lxdclient.StatusFreezing, lxdclient.StatusFrozen, lxdclient.StatusThawed, lxdclient.StatusStopping, lxdclient.StatusStopped: 46 jujuStatus = status.Empty 47 default: 48 jujuStatus = status.Empty 49 } 50 return instance.InstanceStatus{ 51 Status: jujuStatus, 52 Message: instStatus, 53 } 54 55 } 56 57 // Addresses implements instance.Instance. 58 func (inst *environInstance) Addresses() ([]network.Address, error) { 59 return inst.env.raw.Addresses(inst.raw.Name) 60 } 61 62 // firewall stuff 63 64 // OpenPorts opens the given ports on the instance, which 65 // should have been started with the given machine id. 66 func (inst *environInstance) OpenPorts(machineID string, ports []network.PortRange) error { 67 // TODO(ericsnow) Make sure machineId matches inst.Id()? 68 name, err := inst.env.namespace.Hostname(machineID) 69 if err != nil { 70 return errors.Trace(err) 71 } 72 err = inst.env.raw.OpenPorts(name, ports...) 73 if errors.IsNotImplemented(err) { 74 // TODO(ericsnow) for now... 75 return nil 76 } 77 return errors.Trace(err) 78 } 79 80 // ClosePorts closes the given ports on the instance, which 81 // should have been started with the given machine id. 82 func (inst *environInstance) ClosePorts(machineID string, ports []network.PortRange) error { 83 name, err := inst.env.namespace.Hostname(machineID) 84 if err != nil { 85 return errors.Trace(err) 86 } 87 err = inst.env.raw.ClosePorts(name, ports...) 88 if errors.IsNotImplemented(err) { 89 // TODO(ericsnow) for now... 90 return nil 91 } 92 return errors.Trace(err) 93 } 94 95 // Ports returns the set of ports open on the instance, which 96 // should have been started with the given machine id. 97 // The ports are returned as sorted by SortPorts. 98 func (inst *environInstance) Ports(machineID string) ([]network.PortRange, error) { 99 name, err := inst.env.namespace.Hostname(machineID) 100 if err != nil { 101 return nil, errors.Trace(err) 102 } 103 ports, err := inst.env.raw.Ports(name) 104 if errors.IsNotImplemented(err) { 105 // TODO(ericsnow) for now... 106 return nil, nil 107 } 108 return ports, errors.Trace(err) 109 }