github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/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  	"github.com/juju/juju/status"
    13  )
    14  
    15  var _ instance.Instance = (*sigmaInstance)(nil)
    16  
    17  type sigmaInstance struct {
    18  	server gosigma.Server
    19  }
    20  
    21  var ErrNoDNSName = errors.New("IPv4 address not found")
    22  
    23  // Id returns a provider-generated identifier for the Instance.
    24  func (i sigmaInstance) Id() instance.Id {
    25  	id := instance.Id(i.server.UUID())
    26  	logger.Tracef("sigmaInstance.Id: %s", id)
    27  	return id
    28  }
    29  
    30  // Status returns the provider-specific status for the instance.
    31  func (i sigmaInstance) Status() instance.InstanceStatus {
    32  	entityStatus := i.server.Status()
    33  	logger.Tracef("sigmaInstance.Status: %s", entityStatus)
    34  	jujuStatus := status.Pending
    35  	switch entityStatus {
    36  	case gosigma.ServerStarting:
    37  		jujuStatus = status.Allocating
    38  	case gosigma.ServerRunning:
    39  		jujuStatus = status.Running
    40  	case gosigma.ServerStopping, gosigma.ServerStopped:
    41  		jujuStatus = status.Empty
    42  	case gosigma.ServerUnavailable:
    43  		// I am not sure about this one.
    44  		jujuStatus = status.Pending
    45  	default:
    46  		jujuStatus = status.Pending
    47  	}
    48  
    49  	return instance.InstanceStatus{
    50  		Status:  jujuStatus,
    51  		Message: entityStatus,
    52  	}
    53  
    54  }
    55  
    56  // Addresses returns a list of hostnames or ip addresses
    57  // associated with the instance. This will supercede DNSName
    58  // which can be implemented by selecting a preferred address.
    59  func (i sigmaInstance) Addresses() ([]network.Address, error) {
    60  	ip := i.findIPv4()
    61  
    62  	if ip != "" {
    63  		addr := network.Address{
    64  			Value: ip,
    65  			Type:  network.IPv4Address,
    66  			Scope: network.ScopePublic,
    67  		}
    68  
    69  		logger.Tracef("sigmaInstance.Addresses: %v", addr)
    70  
    71  		return []network.Address{addr}, nil
    72  	}
    73  	return []network.Address{}, nil
    74  }
    75  
    76  // OpenPorts opens the given ports on the instance, which
    77  // should have been started with the given machine id.
    78  func (i sigmaInstance) OpenPorts(machineID string, ports []network.PortRange) error {
    79  	return errors.NotImplementedf("OpenPorts")
    80  }
    81  
    82  // ClosePorts closes the given ports on the instance, which
    83  // should have been started with the given machine id.
    84  func (i sigmaInstance) ClosePorts(machineID string, ports []network.PortRange) error {
    85  	return errors.NotImplementedf("ClosePorts")
    86  }
    87  
    88  // Ports returns the set of ports open on the instance, which
    89  // should have been started with the given machine id.
    90  // The ports are returned as sorted by SortPorts.
    91  func (i sigmaInstance) Ports(machineID string) ([]network.PortRange, error) {
    92  	return nil, errors.NotImplementedf("Ports")
    93  }
    94  
    95  func (i sigmaInstance) findIPv4() string {
    96  	addrs := i.server.IPv4()
    97  	if len(addrs) == 0 {
    98  		return ""
    99  	}
   100  	return addrs[0]
   101  }
   102  
   103  func (i *sigmaInstance) hardware(arch string, driveSize uint64) (*instance.HardwareCharacteristics, error) {
   104  	memory := i.server.Mem() / gosigma.Megabyte
   105  	cores := uint64(i.server.SMP())
   106  	cpu := i.server.CPU()
   107  	hw := instance.HardwareCharacteristics{
   108  		Mem:      &memory,
   109  		CpuCores: &cores,
   110  		CpuPower: &cpu,
   111  		Arch:     &arch,
   112  	}
   113  
   114  	diskSpace := driveSize / gosigma.Megabyte
   115  	if diskSpace > 0 {
   116  		hw.RootDisk = &diskSpace
   117  	}
   118  
   119  	return &hw, nil
   120  }