Exemplo n.º 1
0
 def _get_mapping_conditions(self, netview_dict):
     conditions = dict()
     for object_name in self.mapping_neutron_object_names:
         object_value = utils.get_ea_value(object_name, netview_dict, True)
         if object_value:
             conditions[object_name] = object_value
     return conditions
Exemplo n.º 2
0
 def _get_mapping_conditions(self, netview_dict):
     conditions = dict()
     for object_name in self.mapping_neutron_object_names:
         object_value = utils.get_ea_value(object_name, netview_dict, True)
         if object_value:
             conditions[object_name] = object_value
     return conditions
Exemplo n.º 3
0
    def test_get_ea_value(self):
        # test EAs with scalar
        ea = {
            "extattrs": {
                "Cloud API Owned": {
                    "value": "True"
                },
                "CMP Type": {
                    "value": "Openstack"
                },
                "Subnet ID": {
                    "value": "subnet-22222222"
                }
            }
        }
        cloud_api_owned_ea = utils.get_ea_value("Cloud API Owned", ea)
        self.assertEqual('True', cloud_api_owned_ea)
        cmp_type_ea = utils.get_ea_value("CMP Type", ea)
        self.assertEqual('Openstack', cmp_type_ea)
        subnet_id_ea = utils.get_ea_value("Subnet ID", ea)
        self.assertEqual('subnet-22222222', subnet_id_ea)

        # test EA with a list
        ea = {
            "extattrs": {
                "Tenant CIDR Mapping": {
                    "value": ["11.11.1.0/24", "11.11.2.0/24"]
                },
                "Tenant ID Mapping": {
                    "value": "1234567890"
                }
            }
        }
        tenant_cidr_mapping = utils.get_ea_value("Tenant CIDR Mapping", ea)
        expected = ["11.11.1.0/24", "11.11.2.0/24"]
        self.assertEqual(expected, tenant_cidr_mapping)
        tenant_id_mapping = utils.get_ea_value("Tenant ID Mapping", ea)
        expected = "1234567890"
        self.assertEqual(expected, tenant_id_mapping)
        # use 'should_return_list_value' parameter
        tenant_id_mapping = utils.get_ea_value("Tenant ID Mapping", ea, True)
        expected = ["1234567890"]
        self.assertEqual(expected, tenant_id_mapping)

        # negative tests
        invalid_ea = utils.get_ea_value("Invalid", ea)
        self.assertEqual(None, invalid_ea)
        invalid_ea = utils.get_ea_value(None, ea)
        self.assertEqual(None, invalid_ea)
        invalid_ea = utils.get_ea_value(None, None)
        self.assertEqual(None, invalid_ea)
Exemplo n.º 4
0
    def test_get_ea_value(self):
        # test EAs with scalar
        ea = {"extattrs": {"Cloud API Owned": {"value": "True"},
                           "CMP Type": {"value": "Openstack"},
                           "Subnet ID": {"value": "subnet-22222222"}}}
        cloud_api_owned_ea = utils.get_ea_value("Cloud API Owned", ea)
        self.assertEqual('True', cloud_api_owned_ea)
        cmp_type_ea = utils.get_ea_value("CMP Type", ea)
        self.assertEqual('Openstack', cmp_type_ea)
        subnet_id_ea = utils.get_ea_value("Subnet ID", ea)
        self.assertEqual('subnet-22222222', subnet_id_ea)

        # test EA with a list
        ea = {
            "extattrs": {
                "Tenant CIDR Mapping": {
                    "value": [
                        "11.11.1.0/24",
                        "11.11.2.0/24"
                    ]
                },
                "Tenant ID Mapping": {
                    "value": "1234567890"
                }
            }
        }
        tenant_cidr_mapping = utils.get_ea_value("Tenant CIDR Mapping", ea)
        expected = ["11.11.1.0/24", "11.11.2.0/24"]
        self.assertEqual(expected, tenant_cidr_mapping)
        tenant_id_mapping = utils.get_ea_value("Tenant ID Mapping", ea)
        expected = "1234567890"
        self.assertEqual(expected, tenant_id_mapping)
        # use 'should_return_list_value' parameter
        tenant_id_mapping = utils.get_ea_value("Tenant ID Mapping", ea, True)
        expected = ["1234567890"]
        self.assertEqual(expected, tenant_id_mapping)

        # negative tests
        invalid_ea = utils.get_ea_value("Invalid", ea)
        self.assertEqual(None, invalid_ea)
        invalid_ea = utils.get_ea_value(None, ea)
        self.assertEqual(None, invalid_ea)
        invalid_ea = utils.get_ea_value(None, None)
        self.assertEqual(None, invalid_ea)
Exemplo n.º 5
0
    def test_grid_configuration_with_grid_member(self):
        # prepare grid members.
        self._prepare_grid_member()

        # check if GM exists
        db_members = dbi.get_members(self.ctx.session,
                                     grid_id=self.test_grid_config.grid_id,
                                     member_type='GM')
        self.assertEqual('GM', db_members[0]['member_type'])

        # get grid config from GM
        config_json = self.connector_fixture.get_object(
            base.FixtureResourceMap.FAKE_GRID_MASTER_GRID_CONFIGURATION)
        self.test_grid_config._discover_config = mock.Mock()
        self.test_grid_config._discover_config.return_value = config_json
        self.test_grid_config.sync()

        # verify if grid config object fields are set correctly
        expected = utils.get_ea_value('Default Network View Scope',
                                      config_json)
        self.assertEqual(expected,
                         self.test_grid_config.default_network_view_scope)
        expected = utils.get_ea_value('Default Network View', config_json)
        self.assertEqual(expected,
                         self.test_grid_config.default_network_view)
        expected = utils.get_ea_value('IP Allocation Strategy', config_json)
        self.assertEqual(expected,
                         self.test_grid_config.ip_allocation_strategy)
        expected = utils.get_ea_value('Default Domain Name Pattern',
                                      config_json)
        self.assertEqual(expected,
                         self.test_grid_config.default_domain_name_pattern)
        expected = utils.get_ea_value('Default Host Name Pattern',
                                      config_json)
        self.assertEqual(expected,
                         self.test_grid_config.default_host_name_pattern)
        expected = utils.get_ea_value('DNS Record Binding Types',
                                      config_json)
        self.assertEqual(expected,
                         self.test_grid_config.dns_record_binding_types)
Exemplo n.º 6
0
    def test_grid_configuration_with_grid_member(self):
        # prepare grid members.
        self._prepare_grid_member()

        # check if GM exists
        db_members = dbi.get_members(self.ctx.session,
                                     grid_id=self.test_grid_config.grid_id,
                                     member_type='GM')
        self.assertEqual('GM', db_members[0]['member_type'])

        # get grid config from GM
        config_json = self.connector_fixture.get_object(
            base.FixtureResourceMap.FAKE_GRID_MASTER_GRID_CONFIGURATION)
        self.test_grid_config._discover_config = mock.Mock()
        self.test_grid_config._discover_config.return_value = config_json
        self.test_grid_config.sync()

        # verify if grid config object fields are set correctly
        expected = utils.get_ea_value('Default Network View Scope',
                                      config_json)
        self.assertEqual(expected,
                         self.test_grid_config.default_network_view_scope)
        expected = utils.get_ea_value('Default Network View', config_json)
        self.assertEqual(expected, self.test_grid_config.default_network_view)
        expected = utils.get_ea_value('IP Allocation Strategy', config_json)
        self.assertEqual(expected,
                         self.test_grid_config.ip_allocation_strategy)
        expected = utils.get_ea_value('Default Domain Name Pattern',
                                      config_json)
        self.assertEqual(expected,
                         self.test_grid_config.default_domain_name_pattern)
        expected = utils.get_ea_value('Default Host Name Pattern', config_json)
        self.assertEqual(expected,
                         self.test_grid_config.default_host_name_pattern)
        expected = utils.get_ea_value('DNS Record Binding Types', config_json)
        self.assertEqual(expected,
                         self.test_grid_config.dns_record_binding_types)
Exemplo n.º 7
0
 def _update_from_ea(self, field, ea_name, extattrs):
     value = utils.get_ea_value(ea_name, extattrs, ea_name
                                in const.EA_MULTI_VALUES)
     if value:
         setattr(self, field, self._value_to_bool(value))
Exemplo n.º 8
0
    def _sync_network_views(self, discovered_netviews, dns_views):
        """Discover network views and sync with db.

        The discovered network view json contains the following data:
        - network view
        - cloud_info for delegated member if cloud platform is supported
        - mapping conditional EAs

        So discovered information will be updated in tables such as
        infoblox_network_views and infoblox_mapping_conditions.

        :param discovered_netviews: discovered network view json
        :return: None
        """
        session = self._context.session
        self._load_persisted_mappings()
        discovered_delegations = dict()

        persisted_netview_ids = utils.get_values_from_records(
            'id', self.db_network_views)
        discovered_netview_ids = []

        for netview in discovered_netviews:
            netview_name = netview['name']
            is_default = netview[const.IS_DEFAULT]
            netview_id = utils.get_network_view_id(self._grid_id,
                                                   netview['_ref'])

            cloud_adapter_id_vals = utils.get_ea_value(
                const.EA_CLOUD_ADAPTER_ID, netview, True)
            if cloud_adapter_id_vals is None:
                participated = False
            else:
                cloud_adapter_ids = [
                    gid for gid in cloud_adapter_id_vals
                    if int(gid) == self._grid_id
                ]
                participated = True if cloud_adapter_ids else False

            if not participated:
                continue

            shared_val = utils.get_ea_value(const.EA_IS_SHARED, netview)
            is_shared = types.Boolean()(shared_val) if shared_val else False

            # authority member is default to GM
            gm_row = utils.find_one_in_list('member_type',
                                            const.MEMBER_TYPE_GRID_MASTER,
                                            self.db_members)
            authority_member_id = gm_row.member_id

            # get delegation member if cloud platform is supported
            delegated_member = self._get_delegated_member(netview)
            if delegated_member:
                authority_member_id = delegated_member.member_id
                discovered_delegations[netview_name] = (
                    delegated_member.member_id)

            dns_view = (dns_views[netview_name]
                        if dns_views.get(netview_name) else None)

            # see if the network view already exists in db
            netview_row = utils.find_one_in_list('id', netview_id,
                                                 self.db_network_views)
            if netview_row:
                dbi.update_network_view(session, netview_id, netview_name,
                                        authority_member_id, is_shared,
                                        dns_view, participated, is_default)
            else:
                internal_netview = (const.DEFAULT_NETWORK_VIEW
                                    if is_default else netview_name)
                internal_dnsview = (const.DEFAULT_DNS_VIEW
                                    if is_default else dns_view)
                dbi.add_network_view(session, netview_id, netview_name,
                                     self._grid_id, authority_member_id,
                                     is_shared, dns_view, internal_netview,
                                     internal_dnsview, participated,
                                     is_default)

            discovered_netview_ids.append(netview_id)

            # update mapping conditions for the current network view
            self._update_mapping_conditions(netview, netview_id, participated)

        # we have added new network views. now let's remove persisted
        # network views not found from discovery
        persisted_set = set(persisted_netview_ids)
        removable_set = persisted_set.difference(discovered_netview_ids)
        removable_netviews = list(removable_set)
        if removable_netviews:
            dbi.remove_network_views(session, removable_netviews)
        session.flush()
        return discovered_delegations
Exemplo n.º 9
0
 def _update_from_ea(self, field, ea_name, extattrs):
     value = utils.get_ea_value(ea_name, extattrs,
                                ea_name in const.EA_MULTI_VALUES)
     if value:
         setattr(self, field, self._value_to_bool(value))
Exemplo n.º 10
0
    def sync_members(self):
        """Synchronizes grid members.

        Members in the active grid are discovered from NIOS backend and
        grid members are in sync in neutron db. The members who are no longer
        in used are set to 'OFF' status.
        """
        session = self._context.session
        grid_id = self._grid_config.grid_id

        db_grids = dbi.get_grids(session)
        db_grid = utils.find_one_in_list('grid_id', grid_id, db_grids)
        gm_member_id = db_grid.gm_id

        db_members = dbi.get_members(session, grid_id=grid_id)
        gm_member = utils.find_one_in_list('member_id', gm_member_id,
                                           db_members)

        discovered_members = self._discover_members()
        if not discovered_members:
            return

        dns_member_settings = self._discover_dns_settings()
        dhcp_member_settings = self._discover_dhcp_settings()

        discovered_licenses = self._discover_member_licenses()

        discovered_member_ids = []

        for member in discovered_members:
            member_name = member['host_name']
            member_ip, member_ipv6 = self._get_lan1_ips(member)
            member_wapi = member_ip if member_ip else member_ipv6
            member_hwid = member['node_info'][0].get('hwid')
            member_status = self._get_member_status(
                member['node_info'][0]['service_status'])
            member_type = self._get_member_type(discovered_licenses,
                                                member_name, member_hwid)

            require_db_update = False
            if member_type == const.MEMBER_TYPE_GRID_MASTER:
                if gm_member:
                    require_db_update = True
                member_id = gm_member_id
                member_wapi = self._grid_config.grid_master_host
            else:
                # no need to process 'Is Cloud Member' flag for non GM members
                ea_is_cloud_member = utils.get_ea_value(
                    const.EA_IS_CLOUD_MEMBER, member)
                is_cloud_member = (types.Boolean()(ea_is_cloud_member)
                                   if ea_is_cloud_member else False)
                if not is_cloud_member:
                    continue

                db_member = utils.find_one_in_list('member_name', member_name,
                                                   db_members)
                if db_member:
                    require_db_update = True
                    member_id = db_member.member_id
                else:
                    member_id = utils.get_hash(str(grid_id) + member_name)

            member_dhcp_ip, member_dhcp_ipv6 = self._get_dhcp_ips(
                member, dhcp_member_settings)
            member_dns_ip, member_dns_ipv6 = self._get_dns_ips(
                member, dns_member_settings)

            if require_db_update:
                dbi.update_member(session, member_id, grid_id, member_name,
                                  member_ip, member_ipv6, member_type,
                                  member_status, member_dhcp_ip,
                                  member_dhcp_ipv6, member_dns_ip,
                                  member_dns_ipv6, member_wapi)
            else:
                dbi.add_member(session, member_id, grid_id, member_name,
                               member_ip, member_ipv6, member_type,
                               member_status, member_dhcp_ip, member_dhcp_ipv6,
                               member_dns_ip, member_dns_ipv6, member_wapi)

            discovered_member_ids.append(member_id)

        # deleting members are delicate operation so we won't allow it
        # but we will set member status to OFF to unused members.
        db_member_ids = utils.get_values_from_records('member_id', db_members)
        persisted_set = set(db_member_ids)
        discovered_set = set(discovered_member_ids)
        disable_set = persisted_set.difference(discovered_set)
        disabling_member_ids = list(disable_set)
        for member_id in disabling_member_ids:
            dbi.update_member(session,
                              member_id,
                              grid_id,
                              member_status=const.MEMBER_STATUS_OFF)
        session.flush()
Exemplo n.º 11
0
    def _sync_network_views(self, discovered_netviews, dns_views):
        """Discover network views and sync with db.

        The discovered network view json contains the following data:
        - network view
        - cloud_info for delegated member if cloud platform is supported
        - mapping conditional EAs

        So discovered information will be updated in tables such as
        infoblox_network_views and infoblox_mapping_conditions.

        :param discovered_netviews: discovered network view json
        :return: None
        """
        session = self._context.session
        self._load_persisted_mappings()
        discovered_delegations = dict()

        persisted_netview_ids = utils.get_values_from_records(
            'id', self.db_network_views)
        discovered_netview_ids = []

        for netview in discovered_netviews:
            netview_name = netview['name']
            is_default = netview[const.IS_DEFAULT]
            netview_id = utils.get_network_view_id(self._grid_id,
                                                   netview['_ref'])

            cloud_adapter_id_vals = utils.get_ea_value(
                const.EA_CLOUD_ADAPTER_ID, netview, True)
            if cloud_adapter_id_vals is None:
                participated = False
            else:
                cloud_adapter_ids = [gid for gid in cloud_adapter_id_vals
                                     if int(gid) == self._grid_id]
                participated = True if cloud_adapter_ids else False

            if not participated:
                continue

            shared_val = utils.get_ea_value(const.EA_IS_SHARED, netview)
            is_shared = types.Boolean()(shared_val) if shared_val else False

            # authority member is default to GM
            gm_row = utils.find_one_in_list('member_type',
                                            const.MEMBER_TYPE_GRID_MASTER,
                                            self.db_members)
            authority_member_id = gm_row.member_id

            # get delegation member if cloud platform is supported
            delegated_member = self._get_delegated_member(netview)
            if delegated_member:
                authority_member_id = delegated_member.member_id
                discovered_delegations[netview_name] = (
                    delegated_member.member_id)

            dns_view = (dns_views[netview_name] if dns_views.get(netview_name)
                        else None)

            # see if the network view already exists in db
            netview_row = utils.find_one_in_list('id',
                                                 netview_id,
                                                 self.db_network_views)
            if netview_row:
                dbi.update_network_view(session, netview_id, netview_name,
                                        authority_member_id, is_shared,
                                        dns_view, participated, is_default)
            else:
                internal_netview = (const.DEFAULT_NETWORK_VIEW if is_default
                                    else netview_name)
                internal_dnsview = (const.DEFAULT_DNS_VIEW if is_default
                                    else dns_view)
                dbi.add_network_view(session,
                                     netview_id,
                                     netview_name,
                                     self._grid_id,
                                     authority_member_id,
                                     is_shared,
                                     dns_view,
                                     internal_netview,
                                     internal_dnsview,
                                     participated,
                                     is_default)

            discovered_netview_ids.append(netview_id)

            # update mapping conditions for the current network view
            self._update_mapping_conditions(netview, netview_id, participated)

        # we have added new network views. now let's remove persisted
        # network views not found from discovery
        persisted_set = set(persisted_netview_ids)
        removable_set = persisted_set.difference(discovered_netview_ids)
        removable_netviews = list(removable_set)
        if removable_netviews:
            dbi.remove_network_views(session, removable_netviews)
        session.flush()
        return discovered_delegations
Exemplo n.º 12
0
    def sync_members(self):
        """Synchronizes grid members.

        Members in the active grid are discovered from NIOS backend and
        grid members are in sync in neutron db. The members who are no longer
        in used are set to 'OFF' status.
        """
        session = self._context.session
        grid_id = self._grid_config.grid_id

        db_grids = dbi.get_grids(session)
        db_grid = utils.find_one_in_list('grid_id', grid_id, db_grids)
        gm_member_id = db_grid.gm_id

        db_members = dbi.get_members(session, grid_id=grid_id)
        gm_member = utils.find_one_in_list('member_id', gm_member_id,
                                           db_members)

        discovered_members = self._discover_members()
        if not discovered_members:
            return

        dns_member_settings = self._discover_dns_settings()
        dhcp_member_settings = self._discover_dhcp_settings()

        discovered_licenses = self._discover_member_licenses()

        discovered_member_ids = []

        for member in discovered_members:
            member_name = member['host_name']
            member_ip, member_ipv6 = self._get_lan1_ips(member)
            member_wapi = member_ip if member_ip else member_ipv6
            member_hwid = member['node_info'][0].get('hwid')
            member_status = self._get_member_status(
                member['node_info'][0]['service_status'])
            member_type = self._get_member_type(discovered_licenses,
                                                member_name,
                                                member_hwid)

            require_db_update = False
            if member_type == const.MEMBER_TYPE_GRID_MASTER:
                if gm_member:
                    require_db_update = True
                member_id = gm_member_id
                member_wapi = self._grid_config.grid_master_host
            else:
                # no need to process 'Is Cloud Member' flag for non GM members
                ea_is_cloud_member = utils.get_ea_value(
                    const.EA_IS_CLOUD_MEMBER, member)
                is_cloud_member = (types.Boolean()(ea_is_cloud_member)
                                   if ea_is_cloud_member else False)
                if not is_cloud_member:
                    continue

                db_member = utils.find_one_in_list('member_name', member_name,
                                                   db_members)
                if db_member:
                    require_db_update = True
                    member_id = db_member.member_id
                else:
                    member_id = utils.get_hash(str(grid_id) + member_name)

            member_dhcp_ip, member_dhcp_ipv6 = self._get_dhcp_ips(
                member, dhcp_member_settings)
            member_dns_ip, member_dns_ipv6 = self._get_dns_ips(
                member, dns_member_settings)

            if require_db_update:
                dbi.update_member(session,
                                  member_id,
                                  grid_id,
                                  member_name,
                                  member_ip,
                                  member_ipv6,
                                  member_type,
                                  member_status,
                                  member_dhcp_ip,
                                  member_dhcp_ipv6,
                                  member_dns_ip,
                                  member_dns_ipv6,
                                  member_wapi)
            else:
                dbi.add_member(session,
                               member_id,
                               grid_id,
                               member_name,
                               member_ip,
                               member_ipv6,
                               member_type,
                               member_status,
                               member_dhcp_ip,
                               member_dhcp_ipv6,
                               member_dns_ip,
                               member_dns_ipv6,
                               member_wapi)

            discovered_member_ids.append(member_id)

        # deleting members are delicate operation so we won't allow it
        # but we will set member status to OFF to unused members.
        db_member_ids = utils.get_values_from_records('member_id', db_members)
        persisted_set = set(db_member_ids)
        discovered_set = set(discovered_member_ids)
        disable_set = persisted_set.difference(discovered_set)
        disabling_member_ids = list(disable_set)
        for member_id in disabling_member_ids:
            dbi.update_member(session,
                              member_id,
                              grid_id,
                              member_status=const.MEMBER_STATUS_OFF)
        session.flush()