def _register_services(self): session = self.context.session service = const.SERVICE_TYPE_DHCP for member in self.mapping.dhcp_members: service_members = dbi.get_service_members( session, network_view_id=self.mapping.network_view_id, member_id=member.member_id, grid_id=self.grid_id, service=service) if not service_members: dbi.add_service_member(session, self.mapping.network_view_id, member.member_id, service) service = const.SERVICE_TYPE_DNS for member in self.mapping.dns_members: service_members = dbi.get_service_members( session, network_view_id=self.mapping.network_view_id, member_id=member.member_id, grid_id=self.grid_id, service=service) if not service_members: dbi.add_service_member(session, self.mapping.network_view_id, member.member_id, service)
def _sync_network_mapping(self, discovered_networks, discovered_delegations): """Discover networks and sync with db. The discovered network json contains the following data: - network view - network - cloud_info for delegated member if cloud platform is supported - dhcp members :param discovered_networks: discovered network json :param discovered_delegations: discovered delegation members :return: None """ session = self._context.session self._load_persisted_mappings() discovered_mapping = self._get_member_mapping(discovered_networks, discovered_delegations) # add or remove authority mapping members persisted_authority_members = utils.get_composite_values_from_records( ['network_view_id', 'member_id', 'mapping_relation'], self.db_authority_members, DELIMITER) persisted_set = set(persisted_authority_members) discovered_set = set(discovered_mapping['authority_members']) addable_set = discovered_set.difference(persisted_set) removable_set = persisted_set.difference(discovered_set) for authority_member_info in addable_set: authority_member = authority_member_info.split(DELIMITER) network_view_id = authority_member[0] member_id = authority_member[1] mapping_relation = authority_member[2] dbi.add_mapping_member(session, network_view_id, member_id, mapping_relation) for authority_member_info in removable_set: authority_member = authority_member_info.split(DELIMITER) network_view_id = authority_member[0] member_id = authority_member[1] dbi.remove_mapping_member(session, network_view_id, member_id) # add or remove service members persisted_service_members = utils.get_composite_values_from_records( ['network_view_id', 'member_id', 'service'], self.db_service_members, DELIMITER) persisted_set = set(persisted_service_members) discovered_set = set(discovered_mapping['service_members']) addable_set = discovered_set.difference(persisted_set) removable_set = persisted_set.difference(discovered_set) for service_member_info in addable_set: service_member = service_member_info.split(DELIMITER) network_view_id = service_member[0] member_id = service_member[1] service = service_member[2] dbi.add_service_member(session, network_view_id, member_id, service) for service_member_info in removable_set: service_member = service_member_info.split(DELIMITER) network_view_id = service_member[0] member_id = service_member[1] service = service_member[2] dbi.remove_service_member(session, network_view_id, member_id=member_id, service=service)
def test_service_member(self): # prepare grid self._create_default_grid() # prepare grid members member_list = [{'member_id': 'm1', 'member_name': 'm1.com', 'member_ip': '10.10.1.1', 'member_ipv6': None, 'member_type': const.MEMBER_TYPE_GRID_MASTER, 'member_status': 'ON'}, {'member_id': 'm2', 'member_name': 'm2.com', 'member_ip': '10.10.1.2', 'member_ipv6': 'fd44:acb:5df6:1083::22', 'member_type': const.MEMBER_TYPE_CP_MEMBER, 'member_status': 'ON'}] self._create_members(member_list, self.grid_id) db_members = infoblox_db.get_members(self.ctx.session, grid_id=self.grid_id) gm_member = utils.find_one_in_list('member_type', 'GM', db_members) m2_member = utils.find_one_in_list('member_id', 'm2', db_members) # create network views netview_dict = {'default': gm_member.member_id} self._create_network_views(netview_dict) db_network_views = infoblox_db.get_network_views(self.ctx.session) netview_default = utils.find_one_in_list('network_view', 'default', db_network_views) # test addition infoblox_db.add_service_member(self.ctx.session, netview_default.id, gm_member.member_id, const.SERVICE_TYPE_DHCP) infoblox_db.add_service_member(self.ctx.session, netview_default.id, m2_member.member_id, const.SERVICE_TYPE_DHCP) db_service_members = infoblox_db.get_service_members( self.ctx.session, netview_default.id) gm_dhcp_member = utils.find_one_in_list('member_id', gm_member.member_id, db_service_members) self.assertIsNotNone(gm_dhcp_member) m2_dhcp_member = utils.find_one_in_list('member_id', m2_member.member_id, db_service_members) self.assertIsNotNone(m2_dhcp_member) # test removal infoblox_db.remove_service_member(self.ctx.session, netview_default.id) db_service_members = infoblox_db.get_service_members( self.ctx.session, netview_default.id) gm_dhcp_member = utils.find_one_in_list('member_id', gm_member.member_id, db_service_members) self.assertIsNone(gm_dhcp_member) m2m_dhcp_member = utils.find_one_in_list('member_id', m2_member.member_id, db_service_members) self.assertIsNone(m2m_dhcp_member)
def test_get_next_dhcp_member(self): # prepare grid self._create_default_grid() # prepare grid members member_list = [{'member_id': 'm1', 'member_name': 'm1.com', 'member_ip': '10.10.1.1', 'member_ipv6': None, 'member_type': const.MEMBER_TYPE_GRID_MASTER, 'member_status': 'ON'}, {'member_id': 'm2', 'member_name': 'm2.com', 'member_ip': '10.10.1.2', 'member_ipv6': 'fd44:acb:5df6:1083::22', 'member_type': const.MEMBER_TYPE_CP_MEMBER, 'member_status': 'ON'}, {'member_id': 'm3', 'member_name': 'm3.com', 'member_ip': '10.10.1.3', 'member_ipv6': None, 'member_type': const.MEMBER_TYPE_CP_MEMBER, 'member_status': 'ON'}, {'member_id': 'm4', 'member_name': 'm4.com', 'member_ip': '10.10.1.4', 'member_ipv6': None, 'member_type': const.MEMBER_TYPE_REGULAR_MEMBER, 'member_status': 'ON'}] self._create_members(member_list, self.grid_id) db_members = infoblox_db.get_members(self.ctx.session, grid_id=self.grid_id) gm_member = utils.find_one_in_list('member_type', 'GM', db_members) m2_member = utils.find_one_in_list('member_id', 'm2', db_members) m3_member = utils.find_one_in_list('member_id', 'm3', db_members) m4_member = utils.find_one_in_list('member_id', 'm4', db_members) # create network views netview_dict = {'default': gm_member.member_id, 'testview': m2_member.member_id} self._create_network_views(netview_dict) db_network_views = infoblox_db.get_network_views(self.ctx.session) netview_default = utils.find_one_in_list('network_view', 'default', db_network_views) netview_testview = utils.find_one_in_list('network_view', 'testview', db_network_views) # add mapping members # - m1 (gm) is the authority member for 'default' view # - m2 is the authority member for 'testview' view as CPM infoblox_db.add_mapping_member(self.ctx.session, netview_default.id, gm_member.member_id, const.MAPPING_RELATION_GM_OWNED) infoblox_db.add_mapping_member(self.ctx.session, netview_testview.id, m2_member.member_id, const.MAPPING_RELATION_DELEGATED) # add service members # - m4 is a member that serves DHCP/DNS for a network under # the non-delegated view, 'default' # - m2 is the delegated member to the network view, 'testview' infoblox_db.add_service_member(self.ctx.session, netview_default.id, m4_member.member_id, const.SERVICE_TYPE_DHCP) infoblox_db.add_service_member(self.ctx.session, netview_default.id, m4_member.member_id, const.SERVICE_TYPE_DNS) infoblox_db.add_service_member(self.ctx.session, netview_default.id, m2_member.member_id, const.SERVICE_TYPE_DHCP) infoblox_db.add_service_member(self.ctx.session, netview_default.id, m2_member.member_id, const.SERVICE_TYPE_DNS) dhcp_member = infoblox_db.get_next_dhcp_member(self.ctx.session, self.grid_id) # only available dhcp member should be 'm3' self.assertEqual(m3_member.member_id, dhcp_member.member_id)
def test_service_member(self): # prepare grid self._create_default_grid() # prepare grid members member_list = [{ 'member_id': 'm1', 'member_name': 'm1.com', 'member_ip': '10.10.1.1', 'member_ipv6': None, 'member_type': const.MEMBER_TYPE_GRID_MASTER, 'member_status': 'ON' }, { 'member_id': 'm2', 'member_name': 'm2.com', 'member_ip': '10.10.1.2', 'member_ipv6': 'fd44:acb:5df6:1083::22', 'member_type': const.MEMBER_TYPE_CP_MEMBER, 'member_status': 'ON' }] self._create_members(member_list, self.grid_id) db_members = infoblox_db.get_members(self.ctx.session, grid_id=self.grid_id) gm_member = utils.find_one_in_list('member_type', 'GM', db_members) m2_member = utils.find_one_in_list('member_id', 'm2', db_members) # create network views netview_dict = {'default': gm_member.member_id} self._create_network_views(netview_dict) db_network_views = infoblox_db.get_network_views(self.ctx.session) netview_default = utils.find_one_in_list('network_view', 'default', db_network_views) # test addition infoblox_db.add_service_member(self.ctx.session, netview_default.id, gm_member.member_id, const.SERVICE_TYPE_DHCP) infoblox_db.add_service_member(self.ctx.session, netview_default.id, m2_member.member_id, const.SERVICE_TYPE_DHCP) db_service_members = infoblox_db.get_service_members( self.ctx.session, netview_default.id) gm_dhcp_member = utils.find_one_in_list('member_id', gm_member.member_id, db_service_members) self.assertIsNotNone(gm_dhcp_member) m2_dhcp_member = utils.find_one_in_list('member_id', m2_member.member_id, db_service_members) self.assertIsNotNone(m2_dhcp_member) # test removal infoblox_db.remove_service_member(self.ctx.session, netview_default.id) db_service_members = infoblox_db.get_service_members( self.ctx.session, netview_default.id) gm_dhcp_member = utils.find_one_in_list('member_id', gm_member.member_id, db_service_members) self.assertIsNone(gm_dhcp_member) m2m_dhcp_member = utils.find_one_in_list('member_id', m2_member.member_id, db_service_members) self.assertIsNone(m2m_dhcp_member)
def test_get_next_dhcp_member(self): # prepare grid self._create_default_grid() # prepare grid members member_list = [{ 'member_id': 'm1', 'member_name': 'm1.com', 'member_ip': '10.10.1.1', 'member_ipv6': None, 'member_type': const.MEMBER_TYPE_GRID_MASTER, 'member_status': 'ON' }, { 'member_id': 'm2', 'member_name': 'm2.com', 'member_ip': '10.10.1.2', 'member_ipv6': 'fd44:acb:5df6:1083::22', 'member_type': const.MEMBER_TYPE_CP_MEMBER, 'member_status': 'ON' }, { 'member_id': 'm3', 'member_name': 'm3.com', 'member_ip': '10.10.1.3', 'member_ipv6': None, 'member_type': const.MEMBER_TYPE_CP_MEMBER, 'member_status': 'ON' }, { 'member_id': 'm4', 'member_name': 'm4.com', 'member_ip': '10.10.1.4', 'member_ipv6': None, 'member_type': const.MEMBER_TYPE_REGULAR_MEMBER, 'member_status': 'ON' }] self._create_members(member_list, self.grid_id) db_members = infoblox_db.get_members(self.ctx.session, grid_id=self.grid_id) gm_member = utils.find_one_in_list('member_type', 'GM', db_members) m2_member = utils.find_one_in_list('member_id', 'm2', db_members) m3_member = utils.find_one_in_list('member_id', 'm3', db_members) m4_member = utils.find_one_in_list('member_id', 'm4', db_members) # create network views netview_dict = { 'default': gm_member.member_id, 'testview': m2_member.member_id } self._create_network_views(netview_dict) db_network_views = infoblox_db.get_network_views(self.ctx.session) netview_default = utils.find_one_in_list('network_view', 'default', db_network_views) netview_testview = utils.find_one_in_list('network_view', 'testview', db_network_views) # add mapping members # - m1 (gm) is the authority member for 'default' view # - m2 is the authority member for 'testview' view as CPM infoblox_db.add_mapping_member(self.ctx.session, netview_default.id, gm_member.member_id, const.MAPPING_RELATION_GM_OWNED) infoblox_db.add_mapping_member(self.ctx.session, netview_testview.id, m2_member.member_id, const.MAPPING_RELATION_DELEGATED) # add service members # - m4 is a member that serves DHCP/DNS for a network under # the non-delegated view, 'default' # - m2 is the delegated member to the network view, 'testview' infoblox_db.add_service_member(self.ctx.session, netview_default.id, m4_member.member_id, const.SERVICE_TYPE_DHCP) infoblox_db.add_service_member(self.ctx.session, netview_default.id, m4_member.member_id, const.SERVICE_TYPE_DNS) infoblox_db.add_service_member(self.ctx.session, netview_default.id, m2_member.member_id, const.SERVICE_TYPE_DHCP) infoblox_db.add_service_member(self.ctx.session, netview_default.id, m2_member.member_id, const.SERVICE_TYPE_DNS) dhcp_member = infoblox_db.get_next_dhcp_member(self.ctx.session, self.grid_id) # only available dhcp member should be 'm3' self.assertEqual(m3_member.member_id, dhcp_member.member_id)