github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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/core/instance"
    10  	"github.com/juju/juju/core/status"
    11  	"github.com/juju/juju/environs/context"
    12  	"github.com/juju/juju/environs/instances"
    13  	"github.com/juju/juju/network"
    14  	"github.com/juju/juju/provider/gce/google"
    15  )
    16  
    17  type environInstance struct {
    18  	base *google.Instance
    19  	env  *environ
    20  }
    21  
    22  var _ instances.Instance = (*environInstance)(nil)
    23  
    24  func newInstance(base *google.Instance, env *environ) *environInstance {
    25  	return &environInstance{
    26  		base: base,
    27  		env:  env,
    28  	}
    29  }
    30  
    31  // Id implements instances.Instance.
    32  func (inst *environInstance) Id() instance.Id {
    33  	return instance.Id(inst.base.ID)
    34  }
    35  
    36  // Status implements instances.Instance.
    37  func (inst *environInstance) Status(ctx context.ProviderCallContext) instance.Status {
    38  	instStatus := inst.base.Status()
    39  	jujuStatus := status.Provisioning
    40  	switch instStatus {
    41  	case "PROVISIONING", "STAGING":
    42  		jujuStatus = status.Provisioning
    43  	case "RUNNING":
    44  		jujuStatus = status.Running
    45  	case "STOPPING", "TERMINATED":
    46  		jujuStatus = status.Empty
    47  	default:
    48  		jujuStatus = status.Empty
    49  	}
    50  	return instance.Status{
    51  		Status:  jujuStatus,
    52  		Message: instStatus,
    53  	}
    54  }
    55  
    56  // Addresses implements instances.Instance.
    57  func (inst *environInstance) Addresses(ctx context.ProviderCallContext) ([]network.Address, error) {
    58  	return inst.base.Addresses(), nil
    59  }
    60  
    61  func findInst(id instance.Id, instances []instances.Instance) instances.Instance {
    62  	for _, inst := range instances {
    63  		if id == inst.Id() {
    64  			return inst
    65  		}
    66  	}
    67  	return nil
    68  }
    69  
    70  // firewall stuff
    71  
    72  // OpenPorts opens the given ports on the instance, which
    73  // should have been started with the given machine id.
    74  func (inst *environInstance) OpenPorts(ctx context.ProviderCallContext, machineID string, rules []network.IngressRule) error {
    75  	// TODO(ericsnow) Make sure machineId matches inst.Id()?
    76  	name, err := inst.env.namespace.Hostname(machineID)
    77  	if err != nil {
    78  		return errors.Trace(err)
    79  	}
    80  	err = inst.env.gce.OpenPorts(name, rules...)
    81  	return google.HandleCredentialError(errors.Trace(err), ctx)
    82  }
    83  
    84  // ClosePorts closes the given ports on the instance, which
    85  // should have been started with the given machine id.
    86  func (inst *environInstance) ClosePorts(ctx context.ProviderCallContext, machineID string, rules []network.IngressRule) error {
    87  	name, err := inst.env.namespace.Hostname(machineID)
    88  	if err != nil {
    89  		return errors.Trace(err)
    90  	}
    91  	err = inst.env.gce.ClosePorts(name, rules...)
    92  	return google.HandleCredentialError(errors.Trace(err), ctx)
    93  }
    94  
    95  // IngressRules returns the set of ingress rules applicable to the instance, which
    96  // should have been started with the given machine id.
    97  // The rules are returned as sorted by SortIngressRules.
    98  func (inst *environInstance) IngressRules(ctx context.ProviderCallContext, machineID string) ([]network.IngressRule, error) {
    99  	name, err := inst.env.namespace.Hostname(machineID)
   100  	if err != nil {
   101  		return nil, errors.Trace(err)
   102  	}
   103  	ports, err := inst.env.gce.IngressRules(name)
   104  	return ports, google.HandleCredentialError(errors.Trace(err), ctx)
   105  }