github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/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/common"
    12  	"github.com/juju/juju/provider/gce/google"
    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() string {
    36  	return inst.base.Status()
    37  }
    38  
    39  // Addresses implements instance.Instance.
    40  func (inst *environInstance) Addresses() ([]network.Address, error) {
    41  	return inst.base.Addresses(), nil
    42  }
    43  
    44  func findInst(id instance.Id, instances []instance.Instance) instance.Instance {
    45  	for _, inst := range instances {
    46  		if id == inst.Id() {
    47  			return inst
    48  		}
    49  	}
    50  	return nil
    51  }
    52  
    53  // firewall stuff
    54  
    55  // OpenPorts opens the given ports on the instance, which
    56  // should have been started with the given machine id.
    57  func (inst *environInstance) OpenPorts(machineID string, ports []network.PortRange) error {
    58  	// TODO(ericsnow) Make sure machineId matches inst.Id()?
    59  	name := common.MachineFullName(inst.env, machineID)
    60  	env := inst.env.getSnapshot()
    61  	err := env.gce.OpenPorts(name, ports...)
    62  	return errors.Trace(err)
    63  }
    64  
    65  // ClosePorts closes the given ports on the instance, which
    66  // should have been started with the given machine id.
    67  func (inst *environInstance) ClosePorts(machineID string, ports []network.PortRange) error {
    68  	name := common.MachineFullName(inst.env, machineID)
    69  	env := inst.env.getSnapshot()
    70  	err := env.gce.ClosePorts(name, ports...)
    71  	return errors.Trace(err)
    72  }
    73  
    74  // Ports returns the set of ports open on the instance, which
    75  // should have been started with the given machine id.
    76  // The ports are returned as sorted by SortPorts.
    77  func (inst *environInstance) Ports(machineID string) ([]network.PortRange, error) {
    78  	name := common.MachineFullName(inst.env, machineID)
    79  	env := inst.env.getSnapshot()
    80  	ports, err := env.gce.Ports(name)
    81  	return ports, errors.Trace(err)
    82  }