github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/provider/cloudsigma/instance.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package cloudsigma
     5  
     6  import (
     7  	"github.com/altoros/gosigma"
     8  	"github.com/juju/errors"
     9  
    10  	"github.com/juju/juju/instance"
    11  	"github.com/juju/juju/network"
    12  )
    13  
    14  var _ instance.Instance = (*sigmaInstance)(nil)
    15  
    16  type sigmaInstance struct {
    17  	server gosigma.Server
    18  }
    19  
    20  var ErrNoDNSName = errors.New("IPv4 address not found")
    21  
    22  // Id returns a provider-generated identifier for the Instance.
    23  func (i sigmaInstance) Id() instance.Id {
    24  	id := instance.Id(i.server.UUID())
    25  	logger.Tracef("sigmaInstance.Id: %s", id)
    26  	return id
    27  }
    28  
    29  // Status returns the provider-specific status for the instance.
    30  func (i sigmaInstance) Status() string {
    31  	status := i.server.Status()
    32  	logger.Tracef("sigmaInstance.Status: %s", status)
    33  	return status
    34  }
    35  
    36  // Refresh refreshes local knowledge of the instance from the provider.
    37  func (i sigmaInstance) Refresh() error {
    38  	if i.server == nil {
    39  		return errors.New("invalid instance")
    40  	}
    41  	err := i.server.Refresh()
    42  	logger.Tracef("sigmaInstance.Refresh: %s", err)
    43  	return err
    44  }
    45  
    46  // Addresses returns a list of hostnames or ip addresses
    47  // associated with the instance. This will supercede DNSName
    48  // which can be implemented by selecting a preferred address.
    49  func (i sigmaInstance) Addresses() ([]network.Address, error) {
    50  	ip := i.findIPv4()
    51  
    52  	if ip != "" {
    53  		addr := network.Address{
    54  			Value: ip,
    55  			Type:  network.IPv4Address,
    56  			Scope: network.ScopePublic,
    57  		}
    58  
    59  		logger.Tracef("sigmaInstance.Addresses: %v", addr)
    60  
    61  		return []network.Address{addr}, nil
    62  	}
    63  	return []network.Address{}, nil
    64  }
    65  
    66  // OpenPorts opens the given ports on the instance, which
    67  // should have been started with the given machine id.
    68  func (i sigmaInstance) OpenPorts(machineID string, ports []network.PortRange) error {
    69  	return errors.NotImplementedf("OpenPorts")
    70  }
    71  
    72  // ClosePorts closes the given ports on the instance, which
    73  // should have been started with the given machine id.
    74  func (i sigmaInstance) ClosePorts(machineID string, ports []network.PortRange) error {
    75  	return errors.NotImplementedf("ClosePorts")
    76  }
    77  
    78  // Ports returns the set of ports open on the instance, which
    79  // should have been started with the given machine id.
    80  // The ports are returned as sorted by SortPorts.
    81  func (i sigmaInstance) Ports(machineID string) ([]network.PortRange, error) {
    82  	return nil, errors.NotImplementedf("Ports")
    83  }
    84  
    85  func (i sigmaInstance) findIPv4() string {
    86  	addrs := i.server.IPv4()
    87  	if len(addrs) == 0 {
    88  		return ""
    89  	}
    90  	return addrs[0]
    91  }
    92  
    93  func (i *sigmaInstance) hardware(arch string, driveSize uint64) (*instance.HardwareCharacteristics, error) {
    94  	memory := i.server.Mem() / gosigma.Megabyte
    95  	cores := uint64(i.server.SMP())
    96  	cpu := i.server.CPU()
    97  	hw := instance.HardwareCharacteristics{
    98  		Mem:      &memory,
    99  		CpuCores: &cores,
   100  		CpuPower: &cpu,
   101  		Arch:     &arch,
   102  	}
   103  
   104  	diskSpace := driveSize / gosigma.Megabyte
   105  	if diskSpace > 0 {
   106  		hw.RootDisk = &diskSpace
   107  	}
   108  
   109  	return &hw, nil
   110  }