def create_subnet(self, rollback_list): """Creates subnet equivalent NIOS objects. infoblox context contains subnet dictionary from ipam driver and network from db query because only subnet data is passed The following NIOS objects are created. - network view - network - ip range """ session = self.ib_cxt.context.session network = self.ib_cxt.network subnet = self.ib_cxt.subnet network_id = network.get('id') subnet_id = subnet.get('id') network_view_exists = (True if self.ib_cxt.mapping.network_view_id else False) if self.ib_cxt.mapping.authority_member is None: # after authority member reservation, ib_cxt.mapping is updated and # its connector and managers are loaded for the new member. self.ib_cxt.reserve_authority_member() # create a network view if it does not exist if not network_view_exists: ib_network_view, obj_created = self._create_ib_network_view() if ib_network_view and obj_created: rollback_list.append(ib_network_view) ib_network, created = self._create_ib_network() if ib_network: if created: rollback_list.append(ib_network) self._create_ib_ip_range(rollback_list) # tenats available only with wapi 2.0+ features = utils.get_features(self.grid_config.wapi_version) if features.tenants: ib_tenant = ib_objects.Tenant.search(self.ib_cxt.connector, id=self.ib_cxt.tenant_id) if (ib_tenant and self.ib_cxt.tenant_name and ib_tenant.name != self.ib_cxt.tenant_name): ib_tenant.name = self.ib_cxt.tenant_name ib_tenant.update() # associate the network view to neutron dbi.associate_network_view(session, self.ib_cxt.mapping.network_view_id, network_id, subnet_id) return ib_network
def create_subnet(self, rollback_list): """Creates subnet equivalent NIOS objects. infoblox context contains subnet dictionary from ipam driver and network from db query because only subnet data is passed The following NIOS objects are created. - network view - network - ip range """ session = self.ib_cxt.context.session network = self.ib_cxt.network subnet = self.ib_cxt.subnet network_id = network.get('id') subnet_id = subnet.get('id') network_view_exists = (True if self.ib_cxt.mapping.network_view_id else False) if self.ib_cxt.mapping.authority_member is None: # after authority member reservation, ib_cxt.mapping is updated and # its connector and managers are loaded for the new member. self.ib_cxt.reserve_authority_member() # create a network view if it does not exist if not network_view_exists: ib_network_view = self._create_ib_network_view() rollback_list.append(ib_network_view) ib_network, created = self._create_ib_network() if ib_network: if created: rollback_list.append(ib_network) self._create_ib_ip_range(rollback_list) # tenats available only with wapi 2.0+ features = utils.get_features(self.grid_config.wapi_version) if features.tenants: ib_tenant = ib_objects.Tenant.search(self.ib_cxt.connector, id=self.ib_cxt.tenant_id) if (ib_tenant and self.ib_cxt.tenant_name and ib_tenant.name != self.ib_cxt.tenant_name): ib_tenant.name = self.ib_cxt.tenant_name ib_tenant.update() # associate the network view to neutron dbi.associate_network_view(session, self.ib_cxt.mapping.network_view_id, network_id, subnet_id) return ib_network
def test_network_view_mapping_conditions_with_subnet_cidr_condition(self): user_id = 'test user' tenant_id = '90fbad5a098a4b7cb98826128d5b40b3' # prepare network network_name = 'Test Network' network = self.plugin_stub.create_network(tenant_id, network_name) # prepare subnet with cidr used in mapping conditions subnet_name = 'Test Subnet' subnet_cidr = '12.12.2.0/24' subnet = self.plugin_stub.create_subnet(tenant_id, subnet_name, network['id'], subnet_cidr) # make sure that mapping condition exists and prepare expectations db_conditions = dbi.get_mapping_conditions( self.ctx.session, grid_id=self.grid_id, neutron_object_value=subnet_cidr) self.assertEqual(1, len(db_conditions)) expected_network_view_id = db_conditions[0].network_view_id db_network_views = dbi.get_network_views(self.ctx.session, grid_id=self.grid_id) expected_netview_row = utils.find_one_in_list( 'id', expected_network_view_id, db_network_views) expected_authority_member_id = expected_netview_row.authority_member_id expected_network_view = expected_netview_row.network_view # prepare network view mapping to neutron network and subnet dbi.associate_network_view( self.ctx.session, expected_network_view_id, network['id'], subnet['id']) # test mapping where both tenant id and tenant cidr match ib_cxt = ib_context.InfobloxContext(self.ctx, user_id, network, subnet, self.grid_config, self.plugin) ib_cxt.connector = mock.Mock() ib_cxt.ibom = mock.Mock() ib_cxt.ip_allocator = mock.Mock() # validate mapping self.assertEqual(expected_network_view_id, ib_cxt.mapping.network_view_id) self.assertEqual(expected_network_view, ib_cxt.mapping.network_view) self.assertEqual(expected_authority_member_id, ib_cxt.mapping.authority_member.member_id)
def test_network_view_mapping_conditions_with_subnet_cidr_condition(self): user_id = 'test user' tenant_id = '90fbad5a098a4b7cb98826128d5b40b3' # prepare network network_name = 'Test Network' network = self.plugin_stub.create_network(tenant_id, network_name) # prepare subnet with cidr used in mapping conditions subnet_name = 'Test Subnet' subnet_cidr = '12.12.2.0/24' subnet = self.plugin_stub.create_subnet(tenant_id, subnet_name, network['id'], subnet_cidr) # make sure that mapping condition exists and prepare expectations db_conditions = dbi.get_mapping_conditions( self.ctx.session, grid_id=self.grid_id, neutron_object_value=subnet_cidr) self.assertEqual(1, len(db_conditions)) expected_network_view_id = db_conditions[0].network_view_id db_network_views = dbi.get_network_views(self.ctx.session, grid_id=self.grid_id) expected_netview_row = utils.find_one_in_list( 'id', expected_network_view_id, db_network_views) expected_authority_member_id = expected_netview_row.authority_member_id expected_network_view = expected_netview_row.network_view # prepare network view mapping to neutron network and subnet dbi.associate_network_view( self.ctx.session, expected_network_view_id, network['id'], subnet['id']) # test mapping where both tenant id and tenant cidr match ib_cxt = self._get_ib_context(user_id, network, subnet) ib_cxt.connector = mock.Mock() ib_cxt.ibom = mock.Mock() ib_cxt.ip_allocator = mock.Mock() # validate mapping self.assertEqual(expected_network_view_id, ib_cxt.mapping.network_view_id) self.assertEqual(expected_network_view, ib_cxt.mapping.network_view) self.assertEqual(expected_authority_member_id, ib_cxt.mapping.authority_member.member_id)
def test_network_view_mapping(self): # prepare grid self._create_default_grid() # prepare members self._create_simple_members() db_members = infoblox_db.get_members(self.ctx.session) gm_member = utils.find_one_in_list('member_type', 'GM', db_members) # prepare network network = models_v2.Network(name="Test Network", status="ON", admin_state_up=True) self.ctx.session.add(network) self.ctx.session.flush() # prepare network view netview_dict = {'hs-view-1': gm_member.member_id} self._create_network_views(netview_dict) db_network_views = infoblox_db.get_network_views(self.ctx.session) network_view_id = db_network_views[0].id # test associate network view network_id = network.id subnet_id = 'test-subnet-id' infoblox_db.associate_network_view(self.ctx.session, network_view_id, network_id, subnet_id) db_network_view_mappings = infoblox_db.get_network_view_mappings( self.ctx.session) self.assertEqual(network_id, db_network_view_mappings[0].network_id) self.assertEqual(subnet_id, db_network_view_mappings[0].subnet_id) db_network_views = infoblox_db.get_network_view_by_mapping( self.ctx.session, network_id=network_id, subnet_id=subnet_id) self.assertEqual(network_view_id, db_network_views[0].id) # test dissociate network view infoblox_db.dissociate_network_view(self.ctx.session, network_id, subnet_id) db_network_view_mappings = infoblox_db.get_network_view_mappings( self.ctx.session, network_id=network_id, subnet_id=subnet_id) self.assertEqual([], db_network_view_mappings)