Example #1
0
    def test_network_view_management(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)

        # should be no network views
        db_network_views = infoblox_db.get_network_views(self.ctx.session)
        self.assertEqual(0, len(db_network_views))

        # test network view additions
        netview_dict = {
            'default': gm_member.member_id,
            'hs-view-1': gm_member.member_id,
            'hs-view-2': gm_member.member_id,
            'hs-view-3': gm_member.member_id
        }
        self._create_network_views(netview_dict)

        db_network_views = infoblox_db.get_network_views(self.ctx.session)
        actual_rows = utils.get_values_from_records('network_view',
                                                    db_network_views)
        self.assertEqual(netview_dict.keys(), actual_rows)

        # test network view removals
        # - remove 'hs-view-1', 'hs-view-2'
        removing_list = [netview_dict['hs-view-1'], netview_dict['hs-view-2']]
        infoblox_db.remove_network_views_by_names(self.ctx.session,
                                                  removing_list, self.grid_id)

        db_network_views = infoblox_db.get_network_views(self.ctx.session)
        actual_rows = utils.get_values_from_records('network_view',
                                                    db_network_views)
        actual_set = set(actual_rows)
        expected_set = set(netview_dict.keys()).difference(removing_list)
        self.assertEqual(expected_set, actual_set)

        # - remove 'hs-view-3'
        removing_netview_name = 'hs-view-3'
        removing_netview = utils.find_one_in_list('network_view',
                                                  removing_netview_name,
                                                  db_network_views)
        removing_netview_id = removing_netview.id
        infoblox_db.remove_network_views(self.ctx.session,
                                         [removing_netview_id])

        actual_network_views = infoblox_db.get_network_views(
            self.ctx.session, network_view=removing_netview_name)
        self.assertEqual([], actual_network_views)
    def test_network_view_management(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)

        # should be no network views
        db_network_views = infoblox_db.get_network_views(self.ctx.session)
        self.assertEqual(0, len(db_network_views))

        # test network view additions
        netview_dict = {'default': gm_member.member_id,
                        'hs-view-1': gm_member.member_id,
                        'hs-view-2': gm_member.member_id,
                        'hs-view-3': gm_member.member_id}
        self._create_network_views(netview_dict)

        db_network_views = infoblox_db.get_network_views(self.ctx.session)
        actual_rows = utils.get_values_from_records('network_view',
                                                    db_network_views)
        self.assertEqual(netview_dict.keys(), actual_rows)

        # test network view removals
        # - remove 'hs-view-1', 'hs-view-2'
        removing_list = [netview_dict['hs-view-1'], netview_dict['hs-view-2']]
        infoblox_db.remove_network_views_by_names(self.ctx.session,
                                                  removing_list,
                                                  self.grid_id)

        db_network_views = infoblox_db.get_network_views(self.ctx.session)
        actual_rows = utils.get_values_from_records('network_view',
                                                    db_network_views)
        actual_set = set(actual_rows)
        expected_set = set(netview_dict.keys()).difference(removing_list)
        self.assertEqual(expected_set, actual_set)

        # - remove 'hs-view-3'
        removing_netview_name = 'hs-view-3'
        removing_netview = utils.find_one_in_list('network_view',
                                                  removing_netview_name,
                                                  db_network_views)
        removing_netview_id = removing_netview.id
        infoblox_db.remove_network_views(self.ctx.session,
                                         [removing_netview_id])

        actual_network_views = infoblox_db.get_network_views(
            self.ctx.session, network_view=removing_netview_name)
        self.assertEqual([], actual_network_views)
Example #3
0
    def delete_dns_zones(self, dns_zone=None, ib_network=None):
        if self.grid_config.dns_support is False:
            return

        session = self.ib_cxt.context.session
        dns_view = self.ib_cxt.mapping.dns_view
        cidr = self.ib_cxt.subnet['cidr']
        dns_zone = dns_zone if dns_zone else self.dns_zone

        db_network_views = dbi.get_network_views(
            session,
            network_view_id=self.ib_cxt.mapping.network_view_id,
            participated=True)
        if not db_network_views:
            LOG.info("Network view has been removed so dns zone is already"
                     "removed.")
            return

        zone_removable = (not self.ib_cxt.network_is_shared_or_external or
                          self.grid_config.admin_network_deletion)
        if zone_removable:
            # delete forward zone
            if self._is_forward_zone_removable():
                self.ib_cxt.ibom.delete_dns_zone(dns_view, dns_zone)

            # delete reverse zone
            self.ib_cxt.ibom.delete_dns_zone(dns_view, cidr)
    def _validate_mapping_conditions(self, network_view_json):
        db_network_views = dbi.get_network_views(self.ctx.session)
        db_mapping_conditions = dbi.get_mapping_conditions(self.ctx.session)

        expected_conditions = dict((nv['name'], nv['extattrs'])
                                   for nv in network_view_json
                                   if nv['extattrs'])
        expected_condition_rows = []
        for netview in expected_conditions:
            netview_row = utils.find_one_in_list('network_view', netview,
                                                 db_network_views)
            netview_id = netview_row.id
            for condition_name in expected_conditions[netview]:
                if 'Mapping' not in condition_name:
                    continue
                values = expected_conditions[netview][condition_name]['value']
                if not isinstance(values, list):
                    expected_condition_rows.append(netview_id + DELIMITER +
                                                   condition_name + DELIMITER +
                                                   values)
                    continue
                for value in values:
                    expected_condition_rows.append(netview_id + DELIMITER +
                                                   condition_name + DELIMITER +
                                                   value)

        actual_condition_rows = utils.get_composite_values_from_records(
            ['network_view_id', 'neutron_object_name', 'neutron_object_value'],
            db_mapping_conditions)
        self.assertEqual(set(expected_condition_rows),
                         set(actual_condition_rows))
 def _validate_network_views(self, network_view_json):
     db_network_views = dbi.get_network_views(self.ctx.session)
     self.assertEqual(len(network_view_json), len(db_network_views))
     expected = [nv['name'] for nv in network_view_json]
     actual = utils.get_values_from_records('network_view',
                                            db_network_views)
     self.assertEqual(expected, actual)
Example #6
0
 def _validate_network_views(self, network_view_json):
     db_network_views = dbi.get_network_views(self.ctx.session)
     self.assertEqual(len(network_view_json), len(db_network_views))
     expected = [nv['name'] for nv in network_view_json]
     actual = utils.get_values_from_records('network_view',
                                            db_network_views)
     self.assertEqual(expected, actual)
Example #7
0
    def _validate_mapping_conditions(self, network_view_json):
        db_network_views = dbi.get_network_views(self.ctx.session)
        db_mapping_conditions = dbi.get_mapping_conditions(self.ctx.session)

        expected_conditions = dict((nv['name'], nv['extattrs'])
                                   for nv in network_view_json
                                   if nv['extattrs'])
        expected_condition_rows = []
        for netview in expected_conditions:
            netview_row = utils.find_one_in_list('network_view', netview,
                                                 db_network_views)
            netview_id = netview_row.id
            for condition_name in expected_conditions[netview]:
                if 'Mapping' not in condition_name:
                    continue
                values = expected_conditions[netview][condition_name]['value']
                if not isinstance(values, list):
                    expected_condition_rows.append(netview_id + DELIMITER +
                                                   condition_name + DELIMITER +
                                                   values)
                    continue
                for value in values:
                    expected_condition_rows.append(netview_id + DELIMITER +
                                                   condition_name + DELIMITER +
                                                   value)

        actual_condition_rows = utils.get_composite_values_from_records(
            ['network_view_id', 'neutron_object_name', 'neutron_object_value'],
            db_mapping_conditions)
        self.assertEqual(set(expected_condition_rows),
                         set(actual_condition_rows))
Example #8
0
    def delete_dns_zones(self, dns_zone=None, ib_network=None):
        if self.grid_config.dns_support is False:
            return

        session = self.ib_cxt.context.session
        dns_view = self.ib_cxt.mapping.dns_view
        cidr = self.ib_cxt.subnet['cidr']
        dns_zone = dns_zone if dns_zone else self.dns_zone

        db_network_views = dbi.get_network_views(
            session,
            network_view_id=self.ib_cxt.mapping.network_view_id,
            participated=True)
        if not db_network_views:
            LOG.info("Network view has been removed so dns zone is already"
                     "removed.")
            return

        zone_removable = (not self.ib_cxt.network_is_shared_or_external
                          or self.grid_config.admin_network_deletion)
        if zone_removable:
            # delete forward zone
            if self._is_forward_zone_removable():
                self.ib_cxt.ibom.delete_dns_zone(dns_view, dns_zone)

            # delete reverse zone
            self.ib_cxt.ibom.delete_dns_zone(dns_view, cidr)
Example #9
0
    def _resync(self, force_sync=False):
        self.grid_mgr.sync(force_sync)

        self._cached_grid_members = dbi.get_members(
            self.context.session, grid_id=self.grid_id,
            member_status=const.MEMBER_STATUS_ON)
        self._cached_network_views = dbi.get_network_views(
            self.context.session, grid_id=self.grid_id)
        self._cached_mapping_conditions = dbi.get_mapping_conditions(
            self.context.session, grid_id=self.grid_id)
Example #10
0
 def _load_persisted_mappings(self):
     session = self._context.session
     self.db_network_views = dbi.get_network_views(session,
                                                   grid_id=self._grid_id)
     self.db_mapping_conditions = dbi.get_mapping_conditions(
         session, grid_id=self._grid_id)
     self.db_authority_members = dbi.get_mapping_members(
         session, grid_id=self._grid_id)
     self.db_service_members = dbi.get_service_members(
         session, grid_id=self._grid_id)
Example #11
0
 def _load_persisted_mappings(self):
     session = self._context.session
     self.db_network_views = dbi.get_network_views(
         session, grid_id=self._grid_id)
     self.db_mapping_conditions = dbi.get_mapping_conditions(
         session, grid_id=self._grid_id)
     self.db_authority_members = dbi.get_mapping_members(
         session, grid_id=self._grid_id)
     self.db_service_members = dbi.get_service_members(
         session, grid_id=self._grid_id)
    def test_network_view_mapping_conditions_with_tenant_id_condition(self):
        user_id = 'test user'
        tenant_id = '80afaaba012acb9c12888128d5123a09'

        # prepare network
        network_name = 'Test Network'
        network = self.plugin_stub.create_network(tenant_id, network_name)

        # prepare subnet with cidr tat is not used in mapping conditions
        subnet_name = 'Test Subnet'
        subnet_cidr = '10.0.0.0/24'
        subnet = self.plugin_stub.create_subnet(tenant_id, subnet_name,
                                                network['id'], subnet_cidr)

        # make sure that no subnet cidr is used in mapping conditions
        db_conditions = dbi.get_mapping_conditions(
            self.ctx.session,
            grid_id=self.grid_id,
            neutron_object_value=subnet_cidr)
        self.assertEqual([], db_conditions)

        # make sure that tenant id is used in mapping condition once
        db_conditions = dbi.get_mapping_conditions(
            self.ctx.session,
            grid_id=self.grid_id,
            neutron_object_value=tenant_id)
        self.assertEqual(1, len(db_conditions))

        # test mapping where tenant id mapping is found
        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 the mapping network view
        expected_netview_id = db_conditions[0].network_view_id
        db_netviews = dbi.get_network_views(self.ctx.session,
                                            grid_id=self.grid_id)
        netview_row = utils.find_one_in_list('id', expected_netview_id,
                                             db_netviews)
        expected_netview = netview_row.network_view

        db_mapping_members = dbi.get_mapping_members(self.ctx.session,
                                                     expected_netview_id,
                                                     grid_id=self.grid_id)
        expected_member_id = db_mapping_members[0].member_id

        self.assertEqual(expected_netview_id,
                         ib_cxt.mapping.network_view_id)
        self.assertEqual(expected_netview,
                         ib_cxt.mapping.network_view)
        self.assertEqual(expected_member_id,
                         ib_cxt.mapping.authority_member.member_id)
Example #13
0
    def test_network_view_mapping_conditions_with_tenant_id_condition(self):
        user_id = 'test user'
        tenant_id = '80afaaba012acb9c12888128d5123a09'

        # prepare network
        network_name = 'Test Network'
        network = self.plugin_stub.create_network(tenant_id, network_name)

        # prepare subnet with cidr tat is not used in mapping conditions
        subnet_name = 'Test Subnet'
        subnet_cidr = '10.0.0.0/24'
        subnet = self.plugin_stub.create_subnet(tenant_id, subnet_name,
                                                network['id'], subnet_cidr)

        # make sure that no subnet cidr is used in mapping conditions
        db_conditions = dbi.get_mapping_conditions(
            self.ctx.session,
            grid_id=self.grid_id,
            neutron_object_value=subnet_cidr)
        self.assertEqual([], db_conditions)

        # make sure that tenant id is used in mapping condition once
        db_conditions = dbi.get_mapping_conditions(
            self.ctx.session,
            grid_id=self.grid_id,
            neutron_object_value=tenant_id)
        self.assertEqual(1, len(db_conditions))

        # test mapping where tenant id mapping is found
        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 the mapping network view
        expected_netview_id = db_conditions[0].network_view_id
        db_netviews = dbi.get_network_views(self.ctx.session,
                                            grid_id=self.grid_id)
        netview_row = utils.find_one_in_list('id', expected_netview_id,
                                             db_netviews)
        expected_netview = netview_row.network_view

        db_mapping_members = dbi.get_mapping_members(self.ctx.session,
                                                     expected_netview_id,
                                                     grid_id=self.grid_id)
        expected_member_id = db_mapping_members[0].member_id

        self.assertEqual(expected_netview_id,
                         ib_cxt.mapping.network_view_id)
        self.assertEqual(expected_netview,
                         ib_cxt.mapping.network_view)
        self.assertEqual(expected_member_id,
                         ib_cxt.mapping.authority_member.member_id)
    def test_network_view_mapping_conditions_with_single_scope(self):
        user_id = 'test user'
        tenant_id = 'test-tenant'

        # prepare network
        network_name = 'Test Network'
        network = self.plugin_stub.create_network(tenant_id, network_name)

        # prepare subnet with cidr tat is not used in mapping conditions
        subnet_name = 'Test Subnet'
        subnet_cidr = '10.0.0.0/24'
        subnet = self.plugin_stub.create_subnet(tenant_id, subnet_name,
                                                network['id'], subnet_cidr)

        # make sure that no subnet cidr is used in mapping conditions
        db_conditions = dbi.get_mapping_conditions(
            self.ctx.session,
            grid_id=self.grid_id,
            neutron_object_value=subnet_cidr)
        self.assertEqual([], db_conditions)

        # check default network view when no mapping condition matches
        self.assertEqual('Single',
                         self.grid_config.default_network_view_scope)
        self.assertEqual('default', self.grid_config.default_network_view)

        # test default mapping as 'Single'
        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()

        # verify that 'default' view is used
        db_netviews = dbi.get_network_views(self.ctx.session,
                                            grid_id=self.grid_id)
        netview_row = utils.find_one_in_list('network_view', 'default',
                                             db_netviews)
        expected_netview_id = netview_row.id

        db_grid_members = dbi.get_members(self.ctx.session,
                                          grid_id=self.grid_id)
        member_row = utils.find_one_in_list('member_type', 'GM',
                                            db_grid_members)
        expected_member_name = member_row.member_name

        self.assertEqual(expected_netview_id,
                         ib_cxt.mapping.network_view_id)
        self.assertEqual('default',
                         ib_cxt.mapping.network_view)
        self.assertEqual(expected_member_name,
                         ib_cxt.mapping.authority_member.member_name)
Example #15
0
    def test_network_view_mapping_conditions_with_single_scope(self):
        user_id = 'test user'
        tenant_id = 'test-tenant'

        # prepare network
        network_name = 'Test Network'
        network = self.plugin_stub.create_network(tenant_id, network_name)

        # prepare subnet with cidr tat is not used in mapping conditions
        subnet_name = 'Test Subnet'
        subnet_cidr = '10.0.0.0/24'
        subnet = self.plugin_stub.create_subnet(tenant_id, subnet_name,
                                                network['id'], subnet_cidr)

        # make sure that no subnet cidr is used in mapping conditions
        db_conditions = dbi.get_mapping_conditions(
            self.ctx.session,
            grid_id=self.grid_id,
            neutron_object_value=subnet_cidr)
        self.assertEqual([], db_conditions)

        # check default network view when no mapping condition matches
        self.assertEqual('Single',
                         self.grid_config.default_network_view_scope)
        self.assertEqual('default', self.grid_config.default_network_view)

        # test default mapping as 'Single'
        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()

        # verify that 'default' view is used
        db_netviews = dbi.get_network_views(self.ctx.session,
                                            grid_id=self.grid_id)
        netview_row = utils.find_one_in_list('network_view', 'default',
                                             db_netviews)
        expected_netview_id = netview_row.id

        db_grid_members = dbi.get_members(self.ctx.session,
                                          grid_id=self.grid_id)
        member_row = utils.find_one_in_list('member_type', 'GM',
                                            db_grid_members)
        expected_member_name = member_row.member_name

        self.assertEqual(expected_netview_id,
                         ib_cxt.mapping.network_view_id)
        self.assertEqual('default',
                         ib_cxt.mapping.network_view)
        self.assertEqual(expected_member_name,
                         ib_cxt.mapping.authority_member.member_name)
    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)
Example #17
0
    def _get_dns_view(self):
        """Return dns view name.

        The following matrix describes all the dns view naming rule.

        | Network View Name | Grid Config DNS View Name | Final DNS View Name |
        | ----------------- | ------------------------- | ------------------- |
        | default           | default                   | default             |
        | default           | test_view                 | test_view           |
        | net_view_1        | default                   | default.net_view_1  |
        | net_view_2        | dns_view_2                | dns_view_2          |

        If 'default' name has been changed. the rule is slightly different.
        Assume that 'default' network view is changed to 'default_view' and
        'default' dns view is changed to 'default_dns'

        | Network View Name | Grid Config DNS View Name | Final DNS View Name |
        | ----------------- | ------------------------- | ------------------- |
        | default_view      | default_dns               | default_dns         |
        | default_view      | test_view                 | test_view           |
        | net_view_1        | default_dns               | default.net_view_1  |
        | net_view_2        | dns_view_2                | dns_view_2          |
        """
        if self.mapping.network_view_id:
            db_netview = dbi.get_network_views(
                self.context.session,
                network_view_id=self.mapping.network_view_id)
            if db_netview and db_netview[0].dns_view:
                return db_netview[0].dns_view

        # check if grid config dns view is 'default' dns view.
        is_default_view = False
        if self.grid_config.dns_view == const.DEFAULT_DNS_VIEW:
            is_default_view = True
        else:
            db_netview = utils.find_one_in_list('dns_view',
                                                self.grid_config.dns_view,
                                                self.discovered_network_views)
            if (db_netview and db_netview.internal_dns_view
                    == const.DEFAULT_DNS_VIEW):
                is_default_view = True

        if (is_default_view
                and self.mapping.network_view != const.DEFAULT_NETWORK_VIEW):
            return '.'.join(
                [const.DEFAULT_DNS_VIEW, self.mapping.network_view])
        return self.grid_config.dns_view
Example #18
0
    def _get_dns_view(self):
        """Return dns view name.

        The following matrix describes all the dns view naming rule.

        | Network View Name | Grid Config DNS View Name | Final DNS View Name |
        | ----------------- | ------------------------- | ------------------- |
        | default           | default                   | default             |
        | default           | test_view                 | test_view           |
        | net_view_1        | default                   | default.net_view_1  |
        | net_view_2        | dns_view_2                | dns_view_2          |

        If 'default' name has been changed. the rule is slightly different.
        Assume that 'default' network view is changed to 'default_view' and
        'default' dns view is changed to 'default_dns'

        | Network View Name | Grid Config DNS View Name | Final DNS View Name |
        | ----------------- | ------------------------- | ------------------- |
        | default_view      | default_dns               | default_dns         |
        | default_view      | test_view                 | test_view           |
        | net_view_1        | default_dns               | default.net_view_1  |
        | net_view_2        | dns_view_2                | dns_view_2          |
        """
        if self.mapping.network_view_id:
            db_netview = dbi.get_network_views(
                self.context.session,
                network_view_id=self.mapping.network_view_id)
            if db_netview and db_netview[0].dns_view:
                return db_netview[0].dns_view

        # check if grid config dns view is 'default' dns view.
        is_default_view = False
        if self.grid_config.dns_view == const.DEFAULT_DNS_VIEW:
            is_default_view = True
        else:
            db_netview = utils.find_one_in_list('dns_view',
                                                self.grid_config.dns_view,
                                                self.discovered_network_views)
            if (db_netview and
                    db_netview.internal_dns_view == const.DEFAULT_DNS_VIEW):
                is_default_view = True

        if (is_default_view and
                self.mapping.network_view != const.DEFAULT_NETWORK_VIEW):
            return '.'.join(
                [const.DEFAULT_DNS_VIEW, self.mapping.network_view])
        return self.grid_config.dns_view
Example #19
0
    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)
    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)
Example #22
0
 def discovered_network_views(self):
     if self._discovered_network_views is None:
         self._discovered_network_views = dbi.get_network_views(
             self.context.session, grid_id=self.grid_id)
     return self._discovered_network_views
Example #23
0
    def _get_network_view_by_scope(self, netview_scope, neutron_objs):
        netview_name = None

        if netview_scope == const.NETWORK_VIEW_SCOPE_SINGLE:
            db_netview = utils.find_one_in_list(
                'network_view', self.grid_config.default_network_view,
                self.discovered_network_views)
            if db_netview:
                if db_netview.participated:
                    netview_name = self.grid_config.default_network_view
                else:
                    raise exc.InfobloxNetworkViewNotParticipated(
                        network_view=self.grid_config.default_network_view)
            else:
                raise exc.InfobloxNetworkViewNotFound(
                    network_view=self.grid_config.default_network_view)
        else:
            object_id = None
            object_name = None

            if netview_scope == const.NETWORK_VIEW_SCOPE_SUBNET:
                object_id = neutron_objs['subnet_id']
                object_name = neutron_objs['subnet_name']
            elif netview_scope == const.NETWORK_VIEW_SCOPE_NETWORK:
                object_id = neutron_objs['network_id']
                object_name = neutron_objs['network_name']
            elif netview_scope == const.NETWORK_VIEW_SCOPE_TENANT:
                object_id = neutron_objs['tenant_id']
                object_name = neutron_objs['tenant_name']
            elif netview_scope == const.NETWORK_VIEW_SCOPE_ADDRESS_SCOPE:
                object_id = neutron_objs['address_scope_id']
                object_name = neutron_objs['address_scope_name']

            if object_id:
                netview_name = utils.generate_network_view_name(
                    object_id, object_name)

        # see if formulated netview name matches internal name in db
        # if matches then return the current netview name;
        # this is needed to support the netview name change
        db_netviews = dbi.get_network_views(self.context.session,
                                            grid_id=self.grid_id,
                                            internal_network_view=netview_name)
        if db_netviews:
            if db_netviews[0].participated:
                netview_name = db_netviews[0].network_view
            else:
                raise exc.InfobloxNetworkViewNotParticipated(
                    network_view=netview_name)

        # still no network view, then try to use default
        if not netview_name:
            default_netview = utils.find_one_in_list(
                'default', True, self.discovered_network_views)
            if default_netview:
                if default_netview.participated:
                    netview_name = default_netview.network_view
                else:
                    raise exc.InfobloxNetworkViewNotParticipated(
                        network_view=netview_name)
            else:
                raise exc.InfobloxDefaultNetworkViewNotFound()

        return netview_name
Example #24
0
    def test_mapping_management_mapping_conditions(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 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_row = utils.find_one_in_list('network_view', 'default',
                                                     db_network_views)
        netview_id = netview_default_row.id

        # should be no conditions
        db_conditions = infoblox_db.get_mapping_conditions(self.ctx.session)
        self.assertEqual(0, len(db_conditions))

        expected_rows = []

        # test mapping condition additions
        neutron_object_name = const.EA_MAPPING_TENANT_ID
        neutron_object_value = '90fbad5a098a4b7cb98826128d5b40b3'
        expected_rows.append(netview_id + ':' + neutron_object_name + ':' +
                             neutron_object_value)
        infoblox_db.add_mapping_condition(self.ctx.session, netview_id,
                                          neutron_object_name,
                                          neutron_object_value)

        neutron_object_name = const.EA_MAPPING_SUBNET_CIDR
        neutron_object_values = ["12.12.1.0/24", "13.13.1.0/24"]
        for value in neutron_object_values:
            expected_rows.append(netview_id + ':' + neutron_object_name + ':' +
                                 value)
        infoblox_db.add_mapping_conditions(self.ctx.session, netview_id,
                                           neutron_object_name,
                                           neutron_object_values)

        db_conditions = infoblox_db.get_mapping_conditions(self.ctx.session)
        actual_rows = utils.get_composite_values_from_records(
            ['network_view_id', 'neutron_object_name', 'neutron_object_value'],
            db_conditions, ':')
        self.assertEqual(expected_rows, actual_rows)

        # test mapping condition removals
        # - remove Tenant ID Mapping condition
        condition_1 = expected_rows[0].split(':')
        condition_neutron_object_name = condition_1[1]
        condition_neutron_object_value = condition_1[2]
        infoblox_db.remove_mapping_condition(self.ctx.session, netview_id,
                                             condition_neutron_object_name,
                                             condition_neutron_object_value)

        db_conditions = infoblox_db.get_mapping_conditions(
            self.ctx.session,
            network_view_id=netview_id,
            grid_id=self.grid_id,
            neutron_object_name=condition_neutron_object_name)
        self.assertEqual([], db_conditions)

        # - remove two Tenant CIDR Mapping conditions
        condition_2 = expected_rows[1].split(':')
        condition_3 = expected_rows[2].split(':')
        condition_neutron_object_name = condition_2[1]
        condition_neutron_object_values = [condition_2[2], condition_3[2]]
        infoblox_db.remove_mapping_conditions(self.ctx.session, netview_id,
                                              condition_neutron_object_name,
                                              condition_neutron_object_values)

        db_conditions = infoblox_db.get_mapping_conditions(
            self.ctx.session,
            network_view_id=netview_id,
            grid_id=self.grid_id,
            neutron_object_name=condition_neutron_object_name)
        self.assertEqual([], db_conditions)

        db_conditions = infoblox_db.get_mapping_conditions(self.ctx.session)
        self.assertEqual([], db_conditions)
Example #25
0
    def _validate_member_mapping(self, network_view_json, network_json):
        db_members = dbi.get_members(self.ctx.session,
                                     grid_id=self.test_grid_config.grid_id)
        db_network_views = dbi.get_network_views(self.ctx.session)
        db_mapping_members = dbi.get_mapping_members(self.ctx.session)
        db_service_members = dbi.get_service_members(self.ctx.session)

        gm_row = utils.find_one_in_list('member_type',
                                        const.MEMBER_TYPE_GRID_MASTER,
                                        db_members)
        gm_member_id = gm_row.member_id

        dedicated_delegation_members = dict()
        for netview in network_view_json:
            netview_name = netview['name']
            if (netview.get('cloud_info')
                    and netview.get('cloud_info').get('delegated_member')):
                delegated_member = utils.find_one_in_list(
                    'member_name',
                    netview['cloud_info']['delegated_member']['name'],
                    db_members)
                dedicated_delegation_members[netview_name] = (
                    delegated_member.member_id)

        expected_mapping_members = []
        expected_service_members = []

        # get delegated authority members from network views
        for netview in dedicated_delegation_members:
            netview_row = utils.find_one_in_list('network_view', netview,
                                                 db_network_views)
            netview_id = netview_row.id
            authority_member = dedicated_delegation_members[netview]
            mapping_relation = const.MAPPING_RELATION_DELEGATED
            mapping_row_info = (netview_id + DELIMITER + authority_member +
                                DELIMITER + mapping_relation)
            expected_mapping_members.append(mapping_row_info)

        # get authority members from networks
        for network in network_json:
            netview = network['network_view']
            netview_row = utils.find_one_in_list('network_view', netview,
                                                 db_network_views)
            netview_id = netview_row.id

            mapping_relation = const.MAPPING_RELATION_GM_OWNED
            authority_member = gm_member_id
            if netview in dedicated_delegation_members:
                authority_member = dedicated_delegation_members[netview]
                mapping_relation = const.MAPPING_RELATION_DELEGATED
            elif (network.get('cloud_info')
                  and network['cloud_info'].get('delegated_member')):
                delegated_member = utils.find_one_in_list(
                    'member_name',
                    network['cloud_info']['delegated_member']['name'],
                    db_members)
                authority_member = delegated_member.member_id
                mapping_relation = const.MAPPING_RELATION_DELEGATED

            mapping_row_info = (netview_id + DELIMITER + authority_member +
                                DELIMITER + mapping_relation)
            if mapping_row_info not in expected_mapping_members:
                expected_mapping_members.append(mapping_row_info)

            if network.get('members'):
                for m in network['members']:
                    if m['_struct'] == 'dhcpmember':
                        dhcp_member = utils.find_one_in_list(
                            'member_name', m['name'], db_members)
                        mapping_row_info = (netview_id + DELIMITER +
                                            dhcp_member.member_id + DELIMITER +
                                            const.SERVICE_TYPE_DHCP)
                        if mapping_row_info not in expected_service_members:
                            expected_service_members.append(mapping_row_info)

            if network.get('options'):
                dns_membe_ips = []
                for option in network['options']:
                    if option.get('name') == 'domain-name-servers':
                        option_values = option.get('value')
                        if option_values:
                            dns_membe_ips = option_values.split(',')
                            break
                for membe_ip in dns_membe_ips:
                    dns_member = utils.find_one_in_list(
                        'member_ip', membe_ip, db_members)
                    mapping_row_info = (netview_id + DELIMITER +
                                        dns_member.member_id + DELIMITER +
                                        const.SERVICE_TYPE_DNS)
                    if mapping_row_info not in expected_service_members:
                        expected_service_members.append(mapping_row_info)

        actual_mapping_members = utils.get_composite_values_from_records(
            ['network_view_id', 'member_id', 'mapping_relation'],
            db_mapping_members)
        self.assertEqual(set(expected_mapping_members),
                         set(actual_mapping_members))

        actual_service_members = utils.get_composite_values_from_records(
            ['network_view_id', 'member_id', 'service'], db_service_members)
        self.assertEqual(set(expected_service_members),
                         set(actual_service_members))
Example #26
0
 def discovered_network_views(self):
     if self._discovered_network_views is None:
         self._discovered_network_views = dbi.get_network_views(
             self.context.session, grid_id=self.grid_id)
     return self._discovered_network_views
    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_mapping_management_mapping_members(self):
        # prepare grid
        self._create_default_grid()

        # prepare grid 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 views
        netview_dict = {'default': gm_member.member_id}
        self._create_network_views(netview_dict)

        # get network view id
        db_network_views = infoblox_db.get_network_views(self.ctx.session)
        netview_default_row = utils.find_one_in_list('network_view',
                                                     'default',
                                                     db_network_views)
        netview_id = netview_default_row.id

        # should be no mapping members
        db_mapping_members = infoblox_db.get_mapping_members(self.ctx.session)
        self.assertEqual(0, len(db_mapping_members))

        # test mapping member additions
        expected_rows = []
        member_id = db_members[0]['member_id']
        relation = const.MAPPING_RELATION_DELEGATED
        infoblox_db.add_mapping_member(self.ctx.session, netview_id, member_id,
                                       relation)
        expected_rows.append(netview_id + ':' + member_id + ':' + relation)

        member_id = db_members[1]['member_id']
        relation = const.MAPPING_RELATION_DELEGATED
        infoblox_db.add_mapping_member(self.ctx.session, netview_id, member_id,
                                       relation)
        expected_rows.append(netview_id + ':' + member_id + ':' + relation)

        db_mapping_members = infoblox_db.get_mapping_members(self.ctx.session)
        actual_rows = utils.get_composite_values_from_records(
            ['network_view_id', 'member_id', 'mapping_relation'],
            db_mapping_members, ':')
        self.assertEqual(expected_rows, actual_rows)

        # test mapping member update
        member_id = db_members[0]['member_id']
        relation = const.MAPPING_RELATION_GM_OWNED
        infoblox_db.update_mapping_member(self.ctx.session, netview_id,
                                          member_id, relation)
        expected_rows[0] = netview_id + ':' + member_id + ':' + relation

        db_mapping_members = infoblox_db.get_mapping_members(self.ctx.session)
        actual_rows = utils.get_composite_values_from_records(
            ['network_view_id', 'member_id', 'mapping_relation'],
            db_mapping_members, ':')
        self.assertEqual(expected_rows, actual_rows)

        # test mapping member removals
        member_id = db_members[0]['member_id']
        infoblox_db.remove_mapping_member(self.ctx.session, netview_id,
                                          member_id)
        db_mapping_members = infoblox_db.get_mapping_members(
            self.ctx.session, member_id=member_id)
        self.assertEqual([], db_mapping_members)
Example #30
0
    def test_mapping_management_mapping_members(self):
        # prepare grid
        self._create_default_grid()

        # prepare grid 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 views
        netview_dict = {'default': gm_member.member_id}
        self._create_network_views(netview_dict)

        # get network view id
        db_network_views = infoblox_db.get_network_views(self.ctx.session)
        netview_default_row = utils.find_one_in_list('network_view', 'default',
                                                     db_network_views)
        netview_id = netview_default_row.id

        # should be no mapping members
        db_mapping_members = infoblox_db.get_mapping_members(self.ctx.session)
        self.assertEqual(0, len(db_mapping_members))

        # test mapping member additions
        expected_rows = []
        member_id = db_members[0]['member_id']
        relation = const.MAPPING_RELATION_DELEGATED
        infoblox_db.add_mapping_member(self.ctx.session, netview_id, member_id,
                                       relation)
        expected_rows.append(netview_id + ':' + member_id + ':' + relation)

        member_id = db_members[1]['member_id']
        relation = const.MAPPING_RELATION_DELEGATED
        infoblox_db.add_mapping_member(self.ctx.session, netview_id, member_id,
                                       relation)
        expected_rows.append(netview_id + ':' + member_id + ':' + relation)

        db_mapping_members = infoblox_db.get_mapping_members(self.ctx.session)
        actual_rows = utils.get_composite_values_from_records(
            ['network_view_id', 'member_id', 'mapping_relation'],
            db_mapping_members, ':')
        self.assertEqual(expected_rows, actual_rows)

        # test mapping member update
        member_id = db_members[0]['member_id']
        relation = const.MAPPING_RELATION_GM_OWNED
        infoblox_db.update_mapping_member(self.ctx.session, netview_id,
                                          member_id, relation)
        expected_rows[0] = netview_id + ':' + member_id + ':' + relation

        db_mapping_members = infoblox_db.get_mapping_members(self.ctx.session)
        actual_rows = utils.get_composite_values_from_records(
            ['network_view_id', 'member_id', 'mapping_relation'],
            db_mapping_members, ':')
        self.assertEqual(expected_rows, actual_rows)

        # test mapping member removals
        member_id = db_members[0]['member_id']
        infoblox_db.remove_mapping_member(self.ctx.session, netview_id,
                                          member_id)
        db_mapping_members = infoblox_db.get_mapping_members(
            self.ctx.session, member_id=member_id)
        self.assertEqual([], db_mapping_members)
    def _validate_member_mapping(self, network_view_json, network_json):
        db_members = dbi.get_members(self.ctx.session,
                                     grid_id=self.test_grid_config.grid_id)
        db_network_views = dbi.get_network_views(self.ctx.session)
        db_mapping_members = dbi.get_mapping_members(self.ctx.session)
        db_service_members = dbi.get_service_members(self.ctx.session)

        gm_row = utils.find_one_in_list('member_type',
                                        const.MEMBER_TYPE_GRID_MASTER,
                                        db_members)
        gm_member_id = gm_row.member_id

        dedicated_delegation_members = dict()
        for netview in network_view_json:
            netview_name = netview['name']
            if (netview.get('cloud_info') and
                    netview.get('cloud_info').get('delegated_member')):
                delegated_member = utils.find_one_in_list(
                    'member_name',
                    netview['cloud_info']['delegated_member']['name'],
                    db_members)
                dedicated_delegation_members[netview_name] = (
                    delegated_member.member_id)

        expected_mapping_members = []
        expected_service_members = []

        # get delegated authority members from network views
        for netview in dedicated_delegation_members:
            netview_row = utils.find_one_in_list('network_view', netview,
                                                 db_network_views)
            netview_id = netview_row.id
            authority_member = dedicated_delegation_members[netview]
            mapping_relation = const.MAPPING_RELATION_DELEGATED
            mapping_row_info = (netview_id + DELIMITER + authority_member +
                                DELIMITER + mapping_relation)
            expected_mapping_members.append(mapping_row_info)

        # get authority members from networks
        for network in network_json:
            netview = network['network_view']
            netview_row = utils.find_one_in_list('network_view', netview,
                                                 db_network_views)
            netview_id = netview_row.id

            mapping_relation = const.MAPPING_RELATION_GM_OWNED
            authority_member = gm_member_id
            if netview in dedicated_delegation_members:
                authority_member = dedicated_delegation_members[netview]
                mapping_relation = const.MAPPING_RELATION_DELEGATED
            elif (network.get('cloud_info') and
                    network['cloud_info'].get('delegated_member')):
                delegated_member = utils.find_one_in_list(
                    'member_name',
                    network['cloud_info']['delegated_member']['name'],
                    db_members)
                authority_member = delegated_member.member_id
                mapping_relation = const.MAPPING_RELATION_DELEGATED

            mapping_row_info = (netview_id + DELIMITER + authority_member +
                                DELIMITER + mapping_relation)
            if mapping_row_info not in expected_mapping_members:
                expected_mapping_members.append(mapping_row_info)

            if network.get('members'):
                for m in network['members']:
                    if m['_struct'] == 'dhcpmember':
                        dhcp_member = utils.find_one_in_list(
                            'member_name', m['name'], db_members)
                        mapping_row_info = (netview_id + DELIMITER +
                                            dhcp_member.member_id + DELIMITER +
                                            const.SERVICE_TYPE_DHCP)
                        if mapping_row_info not in expected_service_members:
                            expected_service_members.append(mapping_row_info)

            if network.get('options'):
                dns_membe_ips = []
                for option in network['options']:
                    if option.get('name') == 'domain-name-servers':
                        option_values = option.get('value')
                        if option_values:
                            dns_membe_ips = option_values.split(',')
                            break
                for membe_ip in dns_membe_ips:
                    dns_member = utils.find_one_in_list(
                        'member_ip', membe_ip, db_members)
                    mapping_row_info = (netview_id + DELIMITER +
                                        dns_member.member_id + DELIMITER +
                                        const.SERVICE_TYPE_DNS)
                    if mapping_row_info not in expected_service_members:
                        expected_service_members.append(mapping_row_info)

        actual_mapping_members = utils.get_composite_values_from_records(
            ['network_view_id', 'member_id', 'mapping_relation'],
            db_mapping_members)
        self.assertEqual(set(expected_mapping_members),
                         set(actual_mapping_members))

        actual_service_members = utils.get_composite_values_from_records(
            ['network_view_id', 'member_id', 'service'],
            db_service_members)
        self.assertEqual(set(expected_service_members),
                         set(actual_service_members))
Example #32
0
    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)
Example #33
0
    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)
Example #34
0
    def _get_network_view_by_scope(self, netview_scope, neutron_objs):
        netview_name = None

        if netview_scope == const.NETWORK_VIEW_SCOPE_SINGLE:
            db_netview = utils.find_one_in_list(
                'network_view',
                self.grid_config.default_network_view,
                self.discovered_network_views)
            if db_netview:
                if db_netview.participated:
                    netview_name = self.grid_config.default_network_view
                else:
                    raise exc.InfobloxNetworkViewNotParticipated(
                        network_view=self.grid_config.default_network_view)
            else:
                raise exc.InfobloxNetworkViewNotFound(
                    network_view=self.grid_config.default_network_view)
        else:
            object_id = None
            object_name = None

            if netview_scope == const.NETWORK_VIEW_SCOPE_SUBNET:
                object_id = neutron_objs['subnet_id']
                object_name = neutron_objs['subnet_name']
            elif netview_scope == const.NETWORK_VIEW_SCOPE_NETWORK:
                object_id = neutron_objs['network_id']
                object_name = neutron_objs['network_name']
            elif netview_scope == const.NETWORK_VIEW_SCOPE_TENANT:
                object_id = neutron_objs['tenant_id']
                object_name = neutron_objs['tenant_name']
            elif netview_scope == const.NETWORK_VIEW_SCOPE_ADDRESS_SCOPE:
                object_id = neutron_objs['address_scope_id']
                object_name = neutron_objs['address_scope_name']

            if object_id:
                netview_name = utils.generate_network_view_name(object_id,
                                                                object_name)

        # see if formulated netview name matches internal name in db
        # if matches then return the current netview name;
        # this is needed to support the netview name change
        db_netviews = dbi.get_network_views(
            self.context.session,
            grid_id=self.grid_id,
            internal_network_view=netview_name)
        if db_netviews:
            if db_netviews[0].participated:
                netview_name = db_netviews[0].network_view
            else:
                raise exc.InfobloxNetworkViewNotParticipated(
                    network_view=netview_name)

        # still no network view, then try to use default
        if not netview_name:
            default_netview = utils.find_one_in_list(
                'default', True, self.discovered_network_views)
            if default_netview:
                if default_netview.participated:
                    netview_name = default_netview.network_view
                else:
                    raise exc.InfobloxNetworkViewNotParticipated(
                        network_view=netview_name)
            else:
                raise exc.InfobloxDefaultNetworkViewNotFound()

        return netview_name
    def test_mapping_management_mapping_conditions(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 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_row = utils.find_one_in_list('network_view',
                                                     'default',
                                                     db_network_views)
        netview_id = netview_default_row.id

        # should be no conditions
        db_conditions = infoblox_db.get_mapping_conditions(self.ctx.session)
        self.assertEqual(0, len(db_conditions))

        expected_rows = []

        # test mapping condition additions
        neutron_object_name = const.EA_MAPPING_TENANT_ID
        neutron_object_value = '90fbad5a098a4b7cb98826128d5b40b3'
        expected_rows.append(netview_id + ':' + neutron_object_name +
                             ':' + neutron_object_value)
        infoblox_db.add_mapping_condition(self.ctx.session,
                                          netview_id,
                                          neutron_object_name,
                                          neutron_object_value)

        neutron_object_name = const.EA_MAPPING_SUBNET_CIDR
        neutron_object_values = ["12.12.1.0/24", "13.13.1.0/24"]
        for value in neutron_object_values:
            expected_rows.append(netview_id + ':' + neutron_object_name +
                                 ':' + value)
        infoblox_db.add_mapping_conditions(self.ctx.session,
                                           netview_id,
                                           neutron_object_name,
                                           neutron_object_values)

        db_conditions = infoblox_db.get_mapping_conditions(self.ctx.session)
        actual_rows = utils.get_composite_values_from_records(
            ['network_view_id', 'neutron_object_name', 'neutron_object_value'],
            db_conditions, ':')
        self.assertEqual(expected_rows, actual_rows)

        # test mapping condition removals
        # - remove Tenant ID Mapping condition
        condition_1 = expected_rows[0].split(':')
        condition_neutron_object_name = condition_1[1]
        condition_neutron_object_value = condition_1[2]
        infoblox_db.remove_mapping_condition(self.ctx.session,
                                             netview_id,
                                             condition_neutron_object_name,
                                             condition_neutron_object_value)

        db_conditions = infoblox_db.get_mapping_conditions(
            self.ctx.session,
            network_view_id=netview_id,
            grid_id=self.grid_id,
            neutron_object_name=condition_neutron_object_name)
        self.assertEqual([], db_conditions)

        # - remove two Tenant CIDR Mapping conditions
        condition_2 = expected_rows[1].split(':')
        condition_3 = expected_rows[2].split(':')
        condition_neutron_object_name = condition_2[1]
        condition_neutron_object_values = [condition_2[2], condition_3[2]]
        infoblox_db.remove_mapping_conditions(self.ctx.session,
                                              netview_id,
                                              condition_neutron_object_name,
                                              condition_neutron_object_values)

        db_conditions = infoblox_db.get_mapping_conditions(
            self.ctx.session,
            network_view_id=netview_id,
            grid_id=self.grid_id,
            neutron_object_name=condition_neutron_object_name)
        self.assertEqual([], db_conditions)

        db_conditions = infoblox_db.get_mapping_conditions(self.ctx.session)
        self.assertEqual([], db_conditions)