Class: BuildCloud::NetworkInterface
- Inherits:
-
Object
- Object
- BuildCloud::NetworkInterface
- Includes:
- Component
- Defined in:
- lib/build-cloud/networkinterface.rb
Constant Summary collapse
- @@objects =
[]
Class Method Summary collapse
Instance Method Summary collapse
- #create ⇒ Object
- #delete ⇒ Object
-
#initialize(fog_interfaces, log, options = {}) ⇒ NetworkInterface
constructor
A new instance of NetworkInterface.
- #read ⇒ Object (also: #fog_object)
Methods included from Component
Constructor Details
#initialize(fog_interfaces, log, options = {}) ⇒ NetworkInterface
Returns a new instance of NetworkInterface.
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/build-cloud/networkinterface.rb', line 25 def initialize ( fog_interfaces, log, = {} ) @compute = fog_interfaces[:compute] @log = log = @log.debug( .inspect ) (:name, :private_ip_address) require_one_of(:subnet_id, :subnet_name) require_one_of(:security_groups, :security_group_names) end |
Class Method Details
.get_id_by_name(name) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/build-cloud/networkinterface.rb', line 7 def self.get_id_by_name( name ) interface = self.search( :name => name ).first unless interface raise "Couldn't get an NetworkInterface object for #{name} - is it defined?" end interface_fog = interface.read unless interface_fog raise "Couldn't get a NetworkInterface fog object for #{name} - is it created?" end interface_fog.network_interface_id end |
Instance Method Details
#create ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/build-cloud/networkinterface.rb', line 39 def create return if exists? @log.info( "Creating network interface #{@options[:private_ip_address]}" ) = .dup unless [:subnet_id] [:subnet_id] = BuildCloud::Subnet.get_id_by_name( [:subnet_name] ) .delete(:subnet_name) end unless [:security_groups] [:group_set] = [] [:security_group_names].each do |sg| [:group_set] << BuildCloud::SecurityGroup.get_id_by_name( sg ) end .delete(:security_group_names) end [:description] = [:name] .delete(:name) interface = @compute.network_interfaces.new() interface.save wait_until_ready attributes = {} attributes[:resource_id] = interface.network_interface_id attributes[:key] = 'Name' attributes[:value] = [:description] interface_tag = @compute..new( attributes ) interface_tag.save if [:assign_new_public_ip] and ! [:existing_public_ip].nil? raise "Cannot specifiy both new and existing IP addresses" end if [:assign_new_public_ip] ### Need to use the request here, in order to support older accounts which still have ### 'EC2 Classic' regions/azs assigned ip = @compute.allocate_address(domain='vpc') public_ip = ip.body['publicIp'] allocation_id = ip.body['allocationId'] @compute.associate_address(nil, public_ip, interface.network_interface_id, allocation_id ) @log.info( "Assigned new public IP #{public_ip}" ) end unless [:existing_public_ip].nil? ip = @compute.addresses.get([:existing_public_ip]) public_ip = ip.public_ip allocation_id = ip.allocation_id @compute.associate_address(nil, ip.public_ip, interface.network_interface_id, allocation_id ) end @log.debug( interface.inspect ) @log.debug( interface_tag.inspect ) @log.debug( ip.inspect ) unless ! [:assign_new_public_ip] @log.debug( ip.inspect ) unless [:existing_public_ip].nil? end |
#delete ⇒ Object
115 116 117 118 119 120 121 122 123 |
# File 'lib/build-cloud/networkinterface.rb', line 115 def delete return unless exists? @log.info( "Deleting network interface with IP address #{@options[:private_ip_address]}" ) fog_object.destroy end |
#read ⇒ Object Also known as: fog_object
109 110 111 |
# File 'lib/build-cloud/networkinterface.rb', line 109 def read @compute.network_interfaces.select { |ni| ni.private_ip_address == [:private_ip_address]}.first end |