Class: Rudy::AWS::EC2::Addresses
- Inherits:
-
Object
- Object
- Rudy::AWS::EC2::Addresses
- Includes:
- Base, ObjectBase
- Defined in:
- lib/rudy/aws/ec2/address.rb
Instance Attribute Summary
Attributes included from Base
Class Method Summary collapse
Instance Method Summary collapse
- #any? ⇒ Boolean
-
#associate(address, instance) ⇒ Object
Associate an elastic IP to an instance.
-
#associated?(address) ⇒ Boolean
address
is an IP address or Rudy::AWS::EC2::Address object Returns true if the given address is associated to an instance. - #create ⇒ Object
- #destroy(address) ⇒ Object
-
#disassociate(address) ⇒ Object
Disssociate an elastic IP from an instance.
-
#exists?(address) ⇒ Boolean
address
is an IP address or Rudy::AWS::EC2::Address object Returns true if the given address is assigned to the current account. - #get(address) ⇒ Object
-
#list(addresses = []) ⇒ Object
Returns a Array of Rudy::AWS::EC2::Address objects.
-
#list_as_hash(addresses = []) ⇒ Object
Returns a Hash of Rudy::AWS::EC2::Address objects.
Methods included from Base
Methods included from Huxtable
change_environment, change_position, change_region, change_role, change_zone, #check_keys, #config_dirname, create_domain, #current_group_name, #current_machine_address, #current_machine_count, #current_machine_group, #current_machine_hostname, #current_machine_image, #current_machine_name, #current_machine_size, #current_user, #current_user_keypairpath, debug?, #debug?, domain, domain_exists?, #group_metadata, #has_keypair?, #has_keys?, #has_pem_keys?, #has_root_keypair?, keypair_path_to_name, #known_machine_group?, #root_keypairname, #root_keypairpath, #switch_user, update_config, update_global, update_logger, #user_keypairname, #user_keypairpath
Class Method Details
.from_hash(h) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/rudy/aws/ec2/address.rb', line 120 def self.from_hash(h) # requestId: 5ebcad80-eed9-4221-86f6-8d19d7acffe4 # addressesSet: # item: # - publicIp: 75.101.137.7 # instanceId: address = Rudy::AWS::EC2::Address.new address.ipaddress = h['publicIp'] address.instid = h['instanceId'] if h['instanceId'] && !h['instanceId'].empty? address end |
Instance Method Details
#any? ⇒ Boolean
110 111 112 |
# File 'lib/rudy/aws/ec2/address.rb', line 110 def any? !list_as_hash.nil? end |
#associate(address, instance) ⇒ Object
Associate an elastic IP to an instance
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/rudy/aws/ec2/address.rb', line 51 def associate(address, instance) raise NoInstanceID unless instance raise NoAddress unless address address = address.ipaddress if address.is_a?(Rudy::AWS::EC2::Address) instance = instance.awsid if instance.is_a?(Rudy::AWS::EC2::Instance) raise UnknownAddress unless exists?(address) raise AddressAssociated if associated?(address) opts ={ :instance_id => instance, :public_ip => address } ret = @ec2.associate_address(opts) (ret && ret['return'] == 'true') end |
#associated?(address) ⇒ Boolean
address
is an IP address or Rudy::AWS::EC2::Address object Returns true if the given address is associated to an instance
145 146 147 148 149 150 151 |
# File 'lib/rudy/aws/ec2/address.rb', line 145 def associated?(address) address = address.ipaddress if address.is_a?(Rudy::AWS::EC2::Address) list.each do |a| return true if a.ipaddress == address && a.instid end false end |
#create ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/rudy/aws/ec2/address.rb', line 30 def create ret = @ec2.allocate_address return false unless ret && ret['publicIp'] address = Rudy::AWS::EC2::Address.new address.ipaddress = ret['publicIp'] address end |
#destroy(address) ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/rudy/aws/ec2/address.rb', line 38 def destroy(address) address = address.ipaddress if address.is_a?(Rudy::AWS::EC2::Address) raise UnknownAddress unless exists?(address) opts ={ :public_ip => address || raise("No public IP address supplied") } ret = @ec2.release_address(opts) (ret && ret['return'] == 'true') end |
#disassociate(address) ⇒ Object
Disssociate an elastic IP from an instance
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/rudy/aws/ec2/address.rb', line 69 def disassociate(address) raise NoAddress unless address address = address.ipaddress if address.is_a?(Rudy::AWS::EC2::Address) instance = instance.awsid if instance.is_a?(Rudy::AWS::EC2::Instance) raise UnknownAddress unless exists?(address) raise AddressNotAssociated unless associated?(address) opts ={ :public_ip => address } ret = @ec2.disassociate_address(opts) (ret && ret['return'] == 'true') end |
#exists?(address) ⇒ Boolean
address
is an IP address or Rudy::AWS::EC2::Address object Returns true if the given address is assigned to the current account
135 136 137 138 139 140 141 |
# File 'lib/rudy/aws/ec2/address.rb', line 135 def exists?(address) address = address.ipaddress if address.is_a?(Rudy::AWS::EC2::Address) list.each do |a| return true if a.ipaddress == address end false end |
#get(address) ⇒ Object
114 115 116 117 118 |
# File 'lib/rudy/aws/ec2/address.rb', line 114 def get(address) raise "Address cannot be nil" if address.nil? address = address.ipaddress if address.is_a?(Rudy::AWS::EC2::Address) (list(address) || []).first end |
#list(addresses = []) ⇒ Object
Returns a Array of Rudy::AWS::EC2::Address objects.
86 87 88 89 90 |
# File 'lib/rudy/aws/ec2/address.rb', line 86 def list(addresses=[]) addresses = list_as_hash(addresses) addresses &&= addresses.values addresses end |
#list_as_hash(addresses = []) ⇒ Object
Returns a Hash of Rudy::AWS::EC2::Address objects. The key of the IP address.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/rudy/aws/ec2/address.rb', line 93 def list_as_hash(addresses=[]) addresses ||= [] addresses = [addresses].flatten.compact alist = @ec2.describe_addresses(:addresses=> addresses) return nil unless alist['addressesSet'].is_a?(Hash) addresses = {} alist['addressesSet']['item'].each do |address| address = Addresses.from_hash(address) addresses[address.ipaddress] = address end addresses end |