github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/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.Pending
    36  	switch inst.State.Name {
    37  	case "pending":
    38  		jujuStatus = status.Pending
    39  	case "running":
    40  		jujuStatus = status.Running
    41  	case "shutting-down", "terminated", "stopping", "stopped":
    42  		jujuStatus = status.Empty
    43  	default:
    44  		jujuStatus = status.Empty
    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  	if err := inst.e.openPortsInGroup(name, ports); err != nil {
    84  		return err
    85  	}
    86  	logger.Infof("opened ports in security group %s: %v", name, ports)
    87  	return nil
    88  }
    89  
    90  func (inst *ec2Instance) ClosePorts(machineId string, ports []network.PortRange) error {
    91  	if inst.e.Config().FirewallMode() != config.FwInstance {
    92  		return fmt.Errorf("invalid firewall mode %q for closing ports on instance",
    93  			inst.e.Config().FirewallMode())
    94  	}
    95  	name := inst.e.machineGroupName(machineId)
    96  	if err := inst.e.closePortsInGroup(name, ports); err != nil {
    97  		return err
    98  	}
    99  	logger.Infof("closed ports in security group %s: %v", name, ports)
   100  	return nil
   101  }
   102  
   103  func (inst *ec2Instance) Ports(machineId string) ([]network.PortRange, error) {
   104  	if inst.e.Config().FirewallMode() != config.FwInstance {
   105  		return nil, fmt.Errorf("invalid firewall mode %q for retrieving ports from instance",
   106  			inst.e.Config().FirewallMode())
   107  	}
   108  	name := inst.e.machineGroupName(machineId)
   109  	ranges, err := inst.e.portsInGroup(name)
   110  	if err != nil {
   111  		return nil, err
   112  	}
   113  	return ranges, nil
   114  }