github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/provider/ec2/instance.go (about)

     1  // Copyright 2011-2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package ec2
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"gopkg.in/amz.v3/ec2"
    10  
    11  	"github.com/juju/juju/core/instance"
    12  	"github.com/juju/juju/core/status"
    13  	"github.com/juju/juju/environs/config"
    14  	"github.com/juju/juju/environs/context"
    15  	"github.com/juju/juju/environs/instances"
    16  	"github.com/juju/juju/network"
    17  )
    18  
    19  type ec2Instance struct {
    20  	e *environ
    21  
    22  	*ec2.Instance
    23  }
    24  
    25  func (inst *ec2Instance) String() string {
    26  	return string(inst.Id())
    27  }
    28  
    29  var _ instances.Instance = (*ec2Instance)(nil)
    30  
    31  func (inst *ec2Instance) Id() instance.Id {
    32  	return instance.Id(inst.InstanceId)
    33  }
    34  
    35  func (inst *ec2Instance) Status(ctx context.ProviderCallContext) instance.Status {
    36  	// pending | running | shutting-down | terminated | stopping | stopped
    37  	jujuStatus := status.Pending
    38  	switch inst.State.Name {
    39  	case "pending":
    40  		jujuStatus = status.Pending
    41  	case "running":
    42  		jujuStatus = status.Running
    43  	case "shutting-down", "terminated", "stopping", "stopped":
    44  		jujuStatus = status.Empty
    45  	default:
    46  		jujuStatus = status.Empty
    47  	}
    48  	return instance.Status{
    49  		Status:  jujuStatus,
    50  		Message: inst.State.Name,
    51  	}
    52  
    53  }
    54  
    55  // Addresses implements network.Addresses() returning generic address
    56  // details for the instance, and requerying the ec2 api if required.
    57  func (inst *ec2Instance) Addresses(ctx context.ProviderCallContext) ([]network.Address, error) {
    58  	var addresses []network.Address
    59  	possibleAddresses := []network.Address{
    60  		{
    61  			Value: inst.IPAddress,
    62  			Type:  network.IPv4Address,
    63  			Scope: network.ScopePublic,
    64  		},
    65  		{
    66  			Value: inst.PrivateIPAddress,
    67  			Type:  network.IPv4Address,
    68  			Scope: network.ScopeCloudLocal,
    69  		},
    70  	}
    71  	for _, address := range possibleAddresses {
    72  		if address.Value != "" {
    73  			addresses = append(addresses, address)
    74  		}
    75  	}
    76  	return addresses, nil
    77  }
    78  
    79  func (inst *ec2Instance) OpenPorts(ctx context.ProviderCallContext, machineId string, rules []network.IngressRule) error {
    80  	if inst.e.Config().FirewallMode() != config.FwInstance {
    81  		return fmt.Errorf("invalid firewall mode %q for opening ports on instance",
    82  			inst.e.Config().FirewallMode())
    83  	}
    84  	name := inst.e.machineGroupName(machineId)
    85  	if err := inst.e.openPortsInGroup(ctx, name, rules); err != nil {
    86  		return err
    87  	}
    88  	logger.Infof("opened ports in security group %s: %v", name, rules)
    89  	return nil
    90  }
    91  
    92  func (inst *ec2Instance) ClosePorts(ctx context.ProviderCallContext, machineId string, ports []network.IngressRule) error {
    93  	if inst.e.Config().FirewallMode() != config.FwInstance {
    94  		return fmt.Errorf("invalid firewall mode %q for closing ports on instance",
    95  			inst.e.Config().FirewallMode())
    96  	}
    97  	name := inst.e.machineGroupName(machineId)
    98  	if err := inst.e.closePortsInGroup(ctx, name, ports); err != nil {
    99  		return err
   100  	}
   101  	logger.Infof("closed ports in security group %s: %v", name, ports)
   102  	return nil
   103  }
   104  
   105  func (inst *ec2Instance) IngressRules(ctx context.ProviderCallContext, machineId string) ([]network.IngressRule, error) {
   106  	if inst.e.Config().FirewallMode() != config.FwInstance {
   107  		return nil, fmt.Errorf("invalid firewall mode %q for retrieving ingress rules from instance",
   108  			inst.e.Config().FirewallMode())
   109  	}
   110  	name := inst.e.machineGroupName(machineId)
   111  	ranges, err := inst.e.ingressRulesInGroup(ctx, name)
   112  	if err != nil {
   113  		return nil, err
   114  	}
   115  	return ranges, nil
   116  }