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