github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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/environs/config"
    12  	"github.com/juju/juju/instance"
    13  	"github.com/juju/juju/network"
    14  	"github.com/juju/juju/status"
    15  )
    16  
    17  type ec2Instance struct {
    18  	e *environ
    19  
    20  	*ec2.Instance
    21  }
    22  
    23  func (inst *ec2Instance) String() string {
    24  	return string(inst.Id())
    25  }
    26  
    27  var _ instance.Instance = (*ec2Instance)(nil)
    28  
    29  func (inst *ec2Instance) Id() instance.Id {
    30  	return instance.Id(inst.InstanceId)
    31  }
    32  
    33  func (inst *ec2Instance) Status() instance.InstanceStatus {
    34  	// pending | running | shutting-down | terminated | stopping | stopped
    35  	jujuStatus := status.StatusPending
    36  	switch inst.State.Name {
    37  	case "pending":
    38  		jujuStatus = status.StatusPending
    39  	case "running":
    40  		jujuStatus = status.StatusRunning
    41  	case "shutting-down", "terminated", "stopping", "stopped":
    42  		jujuStatus = status.StatusEmpty
    43  	default:
    44  		jujuStatus = status.StatusEmpty
    45  	}
    46  	return instance.InstanceStatus{
    47  		Status:  jujuStatus,
    48  		Message: inst.State.Name,
    49  	}
    50  
    51  }
    52  
    53  // Addresses implements network.Addresses() returning generic address
    54  // details for the instance, and requerying the ec2 api if required.
    55  func (inst *ec2Instance) Addresses() ([]network.Address, error) {
    56  	var addresses []network.Address
    57  	possibleAddresses := []network.Address{
    58  		{
    59  			Value: inst.IPAddress,
    60  			Type:  network.IPv4Address,
    61  			Scope: network.ScopePublic,
    62  		},
    63  		{
    64  			Value: inst.PrivateIPAddress,
    65  			Type:  network.IPv4Address,
    66  			Scope: network.ScopeCloudLocal,
    67  		},
    68  	}
    69  	for _, address := range possibleAddresses {
    70  		if address.Value != "" {
    71  			addresses = append(addresses, address)
    72  		}
    73  	}
    74  	return addresses, nil
    75  }
    76  
    77  func (inst *ec2Instance) OpenPorts(machineId string, ports []network.PortRange) error {
    78  	if inst.e.Config().FirewallMode() != config.FwInstance {
    79  		return fmt.Errorf("invalid firewall mode %q for opening ports on instance",
    80  			inst.e.Config().FirewallMode())
    81  	}
    82  	name := inst.e.machineGroupName(machineId)
    83  	legacyName := inst.e.legacyMachineGroupName(machineId)
    84  	if err := inst.e.openPortsInGroup(name, legacyName, ports); err != nil {
    85  		return err
    86  	}
    87  	logger.Infof("opened ports in security group %s: %v", name, ports)
    88  	return nil
    89  }
    90  
    91  func (inst *ec2Instance) ClosePorts(machineId string, ports []network.PortRange) error {
    92  	if inst.e.Config().FirewallMode() != config.FwInstance {
    93  		return fmt.Errorf("invalid firewall mode %q for closing ports on instance",
    94  			inst.e.Config().FirewallMode())
    95  	}
    96  	name := inst.e.machineGroupName(machineId)
    97  	legacyName := inst.e.legacyMachineGroupName(machineId)
    98  	if err := inst.e.closePortsInGroup(name, legacyName, 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) Ports(machineId string) ([]network.PortRange, error) {
   106  	if inst.e.Config().FirewallMode() != config.FwInstance {
   107  		return nil, fmt.Errorf("invalid firewall mode %q for retrieving ports from instance",
   108  			inst.e.Config().FirewallMode())
   109  	}
   110  	name := inst.e.machineGroupName(machineId)
   111  	ranges, err := inst.e.portsInGroup(name)
   112  	if err != nil {
   113  		return nil, err
   114  	}
   115  	return ranges, nil
   116  }