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