def create_router(self, tenant_id): is_router = True # checking whether the created router exist routers = self.driver.shell.network_manager.list_routers() routers = routers['routers'] for router in routers: if router.get('tenant_id') == tenant_id: router_id = router.get('id') router = self.driver.shell.network_manager.show_router(router_id) is_router = False if is_router: config = OSConfig() # Information of public network(external network) from configuration file extnet_name = config.get('network', 'external_network_name') # find the network information related with a new interface networks = self.driver.shell.network_manager.list_networks() networks = networks['networks'] for network in networks: if (network.get('name') == extnet_name) or \ (network.get('name') == 'public') or (network.get('name') == 'ext-net'): pub_net_id = network.get('id') # Information of subnet network name from configuration file subnet_name = config.get('subnet', 'name') subnets = self.driver.shell.network_manager.list_subnets() subnets = subnets['subnets'] for subnet in subnets: if ((subnet.get('name') == subnet_name) or (subnet.get('name') == 'private-subnet')) and \ (subnet.get('tenant_id') == tenant_id): pri_sbnet_id = subnet.get('id') # create a router and connect external gateway related with public network r_body = {'router': {'name': 'router', 'admin_state_up': True, 'external_gateway_info':{'network_id': pub_net_id}}} router = self.driver.shell.network_manager.create_router(body=r_body) # create a internal port of the router router_id = router['router']['id'] int_pt_body = {'subnet_id': pri_sbnet_id} int_port = self.driver.shell.network_manager.add_interface_router( router=router_id, body=int_pt_body) logger.info("Created a router with interfaces") return router
def create_floatingip(self, tenant_name, instances): config = OSConfig() # Information of public network(external network) from configuration file extnet_name = config.get('network', 'external_network_name') tenant = self.driver.shell.auth_manager.tenants.find(name=tenant_name) networks = self.driver.shell.network_manager.list_networks().get('networks') for network in networks: if (network.get('name') == extnet_name) or \ (network.get('name') == 'public') or (network.get('name') == 'ext-net'): pub_net_id = network.get('id') break else: logger.warning("We shoud need the public network ID for floating IPs!") raise ValueError("The public network ID was not found!") ports = self.driver.shell.network_manager.list_ports().get('ports') for port in ports: device_id = port.get('device_id') for instance in instances: if device_id == instance.id: body = { "floatingip": { "floating_network_id": pub_net_id, "tenant_id": tenant.id, "port_id": port.get('id') } } self.driver.shell.network_manager.create_floatingip(body=body)
def create_network(self, tenant_id): is_network = True networks = self.driver.shell.network_manager.list_networks() networks = networks['networks'] for network in networks: if network.get('tenant_id') == tenant_id: network_id = network.get('id') net_info = self.driver.shell.network_manager.show_network(network_id) net_dict = net_info['network'] is_network = False time.sleep(5) # This reason for waiting is that OS can't quickly handle "create API". if is_network: import pdb pdb.set_trace() config = OSConfig() # TODO : Change part # Check type of tenant network in Openstack type = config.get('network', 'type').lower() if type == 'vlan': phy_int = config.get('network:vlan', 'physical_network') seg_id = int(config.get('network:vlan', 'segmentation_id')) n_body = {'network': {'name': 'private', 'tenant_id': tenant_id, 'provider:network_type': 'vlan', 'provider:physical_network': phy_int, 'provider:segmentation_id': seg_id} } elif type == 'flat': n_body = { 'network': {'name': 'private', 'tenant_id': tenant_id} } elif type == 'local': n_body = { 'network': {'name': 'private', 'tenant_id': tenant_id} } elif type == 'vxlan_gre': n_body = { 'network': {'name': 'private', 'tenant_id': tenant_id} } else: logger.error('You need to write the information in /etc/sfa/network.ini') # create a new network new_net = self.driver.shell.network_manager.create_network(body=n_body) net_dict = new_net['network'] logger.info("Created a network [%s] as below" % net_dict['name']) logger.info(net_dict) # Information of subnet from configuration file sub_name = config.get('subnet', 'name') version = int(config.get('subnet', 'version')) cidr = config.get('subnet', 'cidr') gw_ip = config.get('subnet', 'gateway_ip') dns_servers = config.get('subnet', 'dns_nameservers').split() is_dhcp = bool(config.get('subnet', 'enable_dhcp')) if is_dhcp is True: alloc_start = config.get('subnet', 'allocation_start') alloc_end = config.get('subnet', 'allocation_end') else: alloc_start = None alloc_end = None network_id = net_dict['id'] # create a new subnet for network sn_body = {'subnets': [{ 'name': sub_name, 'cidr': cidr, 'tenant_id': tenant_id, 'network_id': network_id, 'ip_version': version, 'enable_dhcp': is_dhcp, 'gateway_ip': gw_ip, 'dns_nameservers': dns_servers, 'allocation_pools': [{'start': alloc_start, 'end': alloc_end}] }]} new_subnet = self.driver.shell.network_manager.create_subnet(body=sn_body) logger.info("Created a subnet of network [%s] as below" % net_dict['name']) logger.info(new_subnet) return net_dict