github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/provider/maas/maas2instance.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package maas 5 6 import ( 7 "fmt" 8 "strings" 9 10 "github.com/juju/gomaasapi" 11 12 "github.com/juju/juju/instance" 13 "github.com/juju/juju/network" 14 ) 15 16 type maas2Instance struct { 17 machine gomaasapi.Machine 18 constraintMatches gomaasapi.ConstraintMatches 19 } 20 21 var _ maasInstance = (*maas2Instance)(nil) 22 23 func (mi *maas2Instance) zone() (string, error) { 24 return mi.machine.Zone().Name(), nil 25 } 26 27 func (mi *maas2Instance) hostname() (string, error) { 28 return mi.machine.Hostname(), nil 29 } 30 31 func (mi *maas2Instance) hardwareCharacteristics() (*instance.HardwareCharacteristics, error) { 32 nodeArch := strings.Split(mi.machine.Architecture(), "/")[0] 33 nodeCpuCount := uint64(mi.machine.CPUCount()) 34 nodeMemoryMB := uint64(mi.machine.Memory()) 35 // zone can't error on the maas2Instance implementaation. 36 zone, _ := mi.zone() 37 tags := mi.machine.Tags() 38 hc := &instance.HardwareCharacteristics{ 39 Arch: &nodeArch, 40 CpuCores: &nodeCpuCount, 41 Mem: &nodeMemoryMB, 42 AvailabilityZone: &zone, 43 Tags: &tags, 44 } 45 return hc, nil 46 } 47 48 func (mi *maas2Instance) String() string { 49 return fmt.Sprintf("%s:%s", mi.machine.Hostname(), mi.machine.SystemID()) 50 } 51 52 func (mi *maas2Instance) Id() instance.Id { 53 return instance.Id(mi.machine.SystemID()) 54 } 55 56 func (mi *maas2Instance) Addresses() ([]network.Address, error) { 57 machineAddresses := mi.machine.IPAddresses() 58 addresses := make([]network.Address, len(machineAddresses)) 59 for i, address := range machineAddresses { 60 addresses[i] = network.NewAddress(address) 61 } 62 return addresses, nil 63 } 64 65 // Status returns a juju status based on the maas instance returned 66 // status message. 67 func (mi *maas2Instance) Status() instance.InstanceStatus { 68 // TODO (babbageclunk): this should rerequest to get live status. 69 statusName := mi.machine.StatusName() 70 statusMsg := mi.machine.StatusMessage() 71 return convertInstanceStatus(statusName, statusMsg, mi.Id()) 72 } 73 74 // MAAS does not do firewalling so these port methods do nothing. 75 func (mi *maas2Instance) OpenPorts(machineId string, ports []network.PortRange) error { 76 logger.Debugf("unimplemented OpenPorts() called") 77 return nil 78 } 79 80 func (mi *maas2Instance) ClosePorts(machineId string, ports []network.PortRange) error { 81 logger.Debugf("unimplemented ClosePorts() called") 82 return nil 83 } 84 85 func (mi *maas2Instance) Ports(machineId string) ([]network.PortRange, error) { 86 logger.Debugf("unimplemented Ports() called") 87 return nil, nil 88 }