lib/network.rb in forj-0.0.21 vs lib/network.rb in forj-0.0.22

- old
+ new

@@ -17,109 +17,154 @@ require 'require_relative' require_relative 'connection.rb' include Connection +require_relative 'log.rb' +include Logging # # Network module # module Network - def create_network(name) - Connection.network.networks.create(:name => name) + begin + Logging.info(format('creating network %{name}', name: name)) + Connection.network.networks.create(:name => name) + rescue => e + Logging.error(e.message) + end end def delete_network(network_name) - network = Connection.network.networks.all(:name => network_name)[0] - Connection.network.networks.get(network.id).destroy + begin + network = Connection.network.networks.all(:name => network_name)[0] + Connection.network.networks.get(network.id).destroy + rescue => e + Logging.error(e.message) + end end def create_subnet(network_id, name) - Connection.network.subnets.create( - :network_id => network_id, - :name => name, - :cidr => get_next_subnet, - :ip_version => '4' - ) + begin + Connection.network.subnets.create( + :network_id => network_id, + :name => name, + :cidr => get_next_subnet, + :ip_version => '4' + ) + rescue => e + Logging.error(e.message) + end end def delete_subnet(subnet_id) - Connection.network.subnets.get(subnet_id).destroy + begin + Connection.network.subnets.get(subnet_id).destroy + rescue => e + Logging.error(e.message) + end end def get_subnet(name) - Connection.network.subnets.all(:name => name)[0] + begin + Connection.network.subnets.all(:name => name)[0] + rescue => e + Logging.error(e.message) + end end def get_router(name) - routers = Connection.network.routers.all({:name => name}) - router = nil + begin + routers = Connection.network.routers.all({:name => name}) + router = nil - routers.each do|r| - router = r - end + routers.each do|r| + router = r + end - router + router + rescue => e + Logging.error(e.message) + end end def create_router_interface(subnet_id, router) - router.add_interface(subnet_id, nil) + begin + router.add_interface(subnet_id, nil) + rescue => e + Logging.error(e.message) + end end def delete_router_interface(subnet_id, router) - router.remove_interface(subnet_id) + begin + router.remove_interface(subnet_id) + rescue => e + Logging.error(e.message) + end end def create_router(name) - Connection.network.routers.create( - :name => name, - :admin_state_up => true - ) + begin + Connection.network.routers.create( + :name => name, + :admin_state_up => true + ) + rescue => e + Logging.error(e.message) + end end def delete_router(router_id) - Connection.network.routers.get(router_id).destroy + begin + Connection.network.routers.get(router_id).destroy + rescue => e + Logging.error(e.message) + end end - end def get_next_subnet - subnet_values = Array.new - subnets = Connection.network.subnets.all + begin + subnet_values = Array.new + subnets = Connection.network.subnets.all - subnets.each do|s| - subnet_values.push(s.cidr) - end + subnets.each do|s| + subnet_values.push(s.cidr) + end - gap = false - count = 0 - range_used = Array.new - new_subnet = 0 - new_cidr = '' + gap = false + count = 0 + range_used = Array.new + new_subnet = 0 + new_cidr = '' - subnet_values = subnet_values.sort! + subnet_values = subnet_values.sort! - subnet_values.each do|value| - range_used.push(value[5]) - end + subnet_values.each do|value| + range_used.push(value[5]) + end - range_used.each do |n| - if count.to_i == n.to_i + range_used.each do |n| + if count.to_i == n.to_i + else + new_subnet = count + gap = true + break + end + count += 1 + end + + if gap + new_cidr = '10.0.%s.0/24' % [count] else - new_subnet = count - gap = true - break + max_value = range_used.max + new_subnet = max_value.to_i + 1 + new_cidr = '10.0.%s.0/24' % [new_subnet] end - count += 1 + new_cidr + rescue => e + Logging.error(e.message) end - - if gap - new_cidr = '10.0.%s.0/24' % [count] - else - max_value = range_used.max - new_subnet = max_value.to_i + 1 - new_cidr = '10.0.%s.0/24' % [new_subnet] - end - new_cidr end