github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/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  // Addresses returns a list of hostnames or ip addresses
    37  // associated with the instance. This will supercede DNSName
    38  // which can be implemented by selecting a preferred address.
    39  func (i sigmaInstance) Addresses() ([]network.Address, error) {
    40  	ip := i.findIPv4()
    41  
    42  	if ip != "" {
    43  		addr := network.Address{
    44  			Value: ip,
    45  			Type:  network.IPv4Address,
    46  			Scope: network.ScopePublic,
    47  		}
    48  
    49  		logger.Tracef("sigmaInstance.Addresses: %v", addr)
    50  
    51  		return []network.Address{addr}, nil
    52  	}
    53  	return []network.Address{}, nil
    54  }
    55  
    56  // OpenPorts opens the given ports on the instance, which
    57  // should have been started with the given machine id.
    58  func (i sigmaInstance) OpenPorts(machineID string, ports []network.PortRange) error {
    59  	return errors.NotImplementedf("OpenPorts")
    60  }
    61  
    62  // ClosePorts closes the given ports on the instance, which
    63  // should have been started with the given machine id.
    64  func (i sigmaInstance) ClosePorts(machineID string, ports []network.PortRange) error {
    65  	return errors.NotImplementedf("ClosePorts")
    66  }
    67  
    68  // Ports returns the set of ports open on the instance, which
    69  // should have been started with the given machine id.
    70  // The ports are returned as sorted by SortPorts.
    71  func (i sigmaInstance) Ports(machineID string) ([]network.PortRange, error) {
    72  	return nil, errors.NotImplementedf("Ports")
    73  }
    74  
    75  func (i sigmaInstance) findIPv4() string {
    76  	addrs := i.server.IPv4()
    77  	if len(addrs) == 0 {
    78  		return ""
    79  	}
    80  	return addrs[0]
    81  }
    82  
    83  func (i *sigmaInstance) hardware(arch string, driveSize uint64) (*instance.HardwareCharacteristics, error) {
    84  	memory := i.server.Mem() / gosigma.Megabyte
    85  	cores := uint64(i.server.SMP())
    86  	cpu := i.server.CPU()
    87  	hw := instance.HardwareCharacteristics{
    88  		Mem:      &memory,
    89  		CpuCores: &cores,
    90  		CpuPower: &cpu,
    91  		Arch:     &arch,
    92  	}
    93  
    94  	diskSpace := driveSize / gosigma.Megabyte
    95  	if diskSpace > 0 {
    96  		hw.RootDisk = &diskSpace
    97  	}
    98  
    99  	return &hw, nil
   100  }