github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/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  	// A fresh status is not obtained here because the interface it is intended
    69  	// to satisfy gets a new maas2Instance before each call, using a fresh status
    70  	// would cause us to mask errors since this interface does not contemplate
    71  	// returing them.
    72  	statusName := mi.machine.StatusName()
    73  	statusMsg := mi.machine.StatusMessage()
    74  	return convertInstanceStatus(statusName, statusMsg, mi.Id())
    75  }
    76  
    77  // MAAS does not do firewalling so these port methods do nothing.
    78  func (mi *maas2Instance) OpenPorts(machineId string, ports []network.PortRange) error {
    79  	logger.Debugf("unimplemented OpenPorts() called")
    80  	return nil
    81  }
    82  
    83  func (mi *maas2Instance) ClosePorts(machineId string, ports []network.PortRange) error {
    84  	logger.Debugf("unimplemented ClosePorts() called")
    85  	return nil
    86  }
    87  
    88  func (mi *maas2Instance) Ports(machineId string) ([]network.PortRange, error) {
    89  	logger.Debugf("unimplemented Ports() called")
    90  	return nil, nil
    91  }