#!/usr/bin/env ruby # (c) Copyright 2014 Hewlett-Packard Development Company, L.P. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. require_relative 'connection.rb' include Connection module Network def create_network(name) network = Connection.network.networks.create(:name => name) end def delete_network(network_name) network = Connection.network.networks.all(:name => network_name)[0] Connection.network.networks.get(network.id).destroy end def create_subnet(network_id, name) subnet = Connection.network.subnets.create( :network_id => network_id, :name => name, :cidr => get_next_subnet, :ip_version => '4' ) end def delete_subnet(subnet_id) Connection.network.subnets.get(subnet_id).destroy end def get_subnet(name) subnet = Connection.network.subnets.all(:name => name)[0] end def get_router(name) routers = Connection.network.routers.all({:name => name}) router = nil for r in routers do router = r end router end def create_router_interface(subnet_id, router) interface = router.add_interface(subnet_id, nil) end def delete_router_interface(subnet_id, router) router.remove_interface(subnet_id) end def create_router(name) router = Connection.network.routers.create( :name => name, :admin_state_up => true ) end def delete_router(router_id) Connection.network.routers.get(router_id).destroy end end def get_next_subnet subnet_values = Array.new subnets = Connection.network.subnets.all for s in subnets do subnet_values.push(s.cidr) end gap = false count = 0 range_used = Array.new new_subnet = 0 new_cidr = '' subnet_values = subnet_values.sort! for value in subnet_values range_used.push(value[5]) end for n in range_used 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 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