github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/provider/gce/instance.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package gce
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  
     9  	"github.com/juju/juju/instance"
    10  	"github.com/juju/juju/network"
    11  	"github.com/juju/juju/provider/gce/google"
    12  	"github.com/juju/juju/status"
    13  )
    14  
    15  type environInstance struct {
    16  	base *google.Instance
    17  	env  *environ
    18  }
    19  
    20  var _ instance.Instance = (*environInstance)(nil)
    21  
    22  func newInstance(base *google.Instance, env *environ) *environInstance {
    23  	return &environInstance{
    24  		base: base,
    25  		env:  env,
    26  	}
    27  }
    28  
    29  // Id implements instance.Instance.
    30  func (inst *environInstance) Id() instance.Id {
    31  	return instance.Id(inst.base.ID)
    32  }
    33  
    34  // Status implements instance.Instance.
    35  func (inst *environInstance) Status() instance.InstanceStatus {
    36  	instStatus := inst.base.Status()
    37  	jujuStatus := status.Provisioning
    38  	switch instStatus {
    39  	case "PROVISIONING", "STAGING":
    40  		jujuStatus = status.Provisioning
    41  	case "RUNNING":
    42  		jujuStatus = status.Running
    43  	case "STOPPING", "TERMINATED":
    44  		jujuStatus = status.Empty
    45  	default:
    46  		jujuStatus = status.Empty
    47  	}
    48  	return instance.InstanceStatus{
    49  		Status:  jujuStatus,
    50  		Message: instStatus,
    51  	}
    52  }
    53  
    54  // Addresses implements instance.Instance.
    55  func (inst *environInstance) Addresses() ([]network.Address, error) {
    56  	return inst.base.Addresses(), nil
    57  }
    58  
    59  func findInst(id instance.Id, instances []instance.Instance) instance.Instance {
    60  	for _, inst := range instances {
    61  		if id == inst.Id() {
    62  			return inst
    63  		}
    64  	}
    65  	return nil
    66  }
    67  
    68  // firewall stuff
    69  
    70  // OpenPorts opens the given ports on the instance, which
    71  // should have been started with the given machine id.
    72  func (inst *environInstance) OpenPorts(machineID string, ports []network.PortRange) error {
    73  	// TODO(ericsnow) Make sure machineId matches inst.Id()?
    74  	name, err := inst.env.namespace.Hostname(machineID)
    75  	if err != nil {
    76  		return errors.Trace(err)
    77  	}
    78  	err = inst.env.gce.OpenPorts(name, ports...)
    79  	return errors.Trace(err)
    80  }
    81  
    82  // ClosePorts closes the given ports on the instance, which
    83  // should have been started with the given machine id.
    84  func (inst *environInstance) ClosePorts(machineID string, ports []network.PortRange) error {
    85  	name, err := inst.env.namespace.Hostname(machineID)
    86  	if err != nil {
    87  		return errors.Trace(err)
    88  	}
    89  	err = inst.env.gce.ClosePorts(name, ports...)
    90  	return errors.Trace(err)
    91  }
    92  
    93  // Ports returns the set of ports open on the instance, which
    94  // should have been started with the given machine id.
    95  // The ports are returned as sorted by SortPorts.
    96  func (inst *environInstance) Ports(machineID string) ([]network.PortRange, error) {
    97  	name, err := inst.env.namespace.Hostname(machineID)
    98  	if err != nil {
    99  		return nil, errors.Trace(err)
   100  	}
   101  	ports, err := inst.env.gce.Ports(name)
   102  	return ports, errors.Trace(err)
   103  }