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