Ejemplo n.º 1
0
    def _update_endpoint_from_keystone(self, cxt, is_internal):
        """Update the database by querying service endpoint url from Keystone

        :param cxt: context object
        :param is_internal: if True, this method utilizes pre-configured admin
        username and password to apply an new admin token, this happens only
        when auto_refresh_endpoint is set to True. if False, token in cxt is
        directly used, users should prepare admin token themselves
        :return: None
        """
        if is_internal:
            admin_context = tricircle_context.Context()
            admin_context.auth_token = self._get_admin_token()
            endpoint_map = self._get_endpoint_from_keystone(admin_context)
        else:
            endpoint_map = self._get_endpoint_from_keystone(cxt)
        for region in endpoint_map:
            # use region name to query site
            site_filters = [{'key': 'site_name', 'comparator': 'eq',
                             'value': region}]
            site_list = models.list_sites(cxt, site_filters)

            # skip region/site not registered in cascade service
            if len(site_list) != 1:
                continue
            for service in endpoint_map[region]:
                site_id = site_list[0]['site_id']
                config_filters = [{'key': 'site_id', 'comparator': 'eq',
                                   'value': site_id},
                                  {'key': 'service_type', 'comparator': 'eq',
                                   'value': service}]
                config_list = models.list_site_service_configuration(
                    cxt, config_filters)

                if len(config_list) > 1:
                    raise exception.EndpointNotUnique(site_id, service)
                if len(config_list) == 1:
                    config_id = config_list[0]['service_id']
                    update_dict = {
                        'service_url': endpoint_map[region][service]}
                    models.update_site_service_configuration(
                        cxt, config_id, update_dict)
                else:
                    config_dict = {
                        'service_id': str(uuid.uuid4()),
                        'site_id': site_id,
                        'service_name': '%s_%s' % (region, service),
                        'service_type': service,
                        'service_url': endpoint_map[region][service]
                    }
                    models.create_site_service_configuration(
                        cxt, config_dict)
Ejemplo n.º 2
0
    def test_list_endpoint_not_valid(self):
        cfg.CONF.set_override(name='auto_refresh_endpoint', override=False,
                              group='client')
        update_dict = {'service_url': FAKE_URL_INVALID}
        # update url to an invalid one
        models.update_site_service_configuration(self.context,
                                                 FAKE_SERVICE_ID,
                                                 update_dict)

        # auto refresh set to False, directly raise exception
        self.assertRaises(exception.EndpointNotAvailable,
                          self.client.list_resources,
                          FAKE_RESOURCE, self.context, [])
Ejemplo n.º 3
0
    def test_list_endpoint_not_valid(self):
        cfg.CONF.set_override(name='auto_refresh_endpoint',
                              override=False,
                              group='client')
        update_dict = {'service_url': FAKE_URL_INVALID}
        # update url to an invalid one
        models.update_site_service_configuration(self.context, FAKE_SERVICE_ID,
                                                 update_dict)

        # auto refresh set to False, directly raise exception
        self.assertRaises(exception.EndpointNotAvailable,
                          self.client.list_resources, FAKE_RESOURCE,
                          self.context, [])
Ejemplo n.º 4
0
    def _update_endpoint_from_keystone(self, cxt, is_internal):
        """Update the database by querying service endpoint url from Keystone

        :param cxt: context object
        :param is_internal: if True, this method utilizes pre-configured admin
        username and password to apply an new admin token, this happens only
        when auto_refresh_endpoint is set to True. if False, token in cxt is
        directly used, users should prepare admin token themselves
        :return: None
        """
        if is_internal:
            admin_context = tricircle_context.Context()
            admin_context.auth_token = self._get_admin_token()
            endpoint_map = self._get_endpoint_from_keystone(admin_context)
        else:
            endpoint_map = self._get_endpoint_from_keystone(cxt)

        for region in endpoint_map:
            # use region name to query site
            site_filters = [{'key': 'site_name', 'comparator': 'eq',
                             'value': region}]
            site_list = models.list_sites(cxt, site_filters)
            # skip region/site not registered in cascade service
            if len(site_list) != 1:
                continue
            for service in endpoint_map[region]:
                site_id = site_list[0]['site_id']
                config_filters = [{'key': 'site_id', 'comparator': 'eq',
                                   'value': site_id},
                                  {'key': 'service_type', 'comparator': 'eq',
                                   'value': service}]
                config_list = models.list_site_service_configurations(
                    cxt, config_filters)

                if len(config_list) > 1:
                    raise exception.EndpointNotUnique(site_id, service)
                if len(config_list) == 1:
                    config_id = config_list[0]['service_id']
                    update_dict = {
                        'service_url': endpoint_map[region][service]}
                    models.update_site_service_configuration(
                        cxt, config_id, update_dict)
                else:
                    config_dict = {
                        'service_id': str(uuid.uuid4()),
                        'site_id': site_id,
                        'service_type': service,
                        'service_url': endpoint_map[region][service]
                    }
                    models.create_site_service_configuration(
                        cxt, config_dict)
Ejemplo n.º 5
0
    def test_list_endpoint_not_valid_retry(self):
        cfg.CONF.set_override(name='auto_refresh_endpoint', override=True,
                              group='client')
        update_dict = {'service_url': FAKE_URL_INVALID}
        # update url to an invalid one
        models.update_site_service_configuration(self.context,
                                                 FAKE_SERVICE_ID,
                                                 update_dict)

        self.client._get_admin_token = mock.Mock()
        self.client._get_endpoint_from_keystone = mock.Mock()
        self.client._get_endpoint_from_keystone.return_value = {
            FAKE_SITE_NAME: {FAKE_TYPE: FAKE_URL}
        }

        resources = self.client.list_resources(
            FAKE_RESOURCE, self.context, [])
        self.assertEqual(resources, [{'name': 'res1'}, {'name': 'res2'}])
Ejemplo n.º 6
0
    def test_list_endpoint_not_valid_retry(self):
        cfg.CONF.set_override(name='auto_refresh_endpoint',
                              override=True,
                              group='client')
        update_dict = {'service_url': FAKE_URL_INVALID}
        # update url to an invalid one
        models.update_site_service_configuration(self.context, FAKE_SERVICE_ID,
                                                 update_dict)

        self.client._get_admin_token = mock.Mock()
        self.client._get_endpoint_from_keystone = mock.Mock()
        self.client._get_endpoint_from_keystone.return_value = {
            FAKE_SITE_NAME: {
                FAKE_TYPE: FAKE_URL
            }
        }

        resources = self.client.list_resources(FAKE_RESOURCE, self.context, [])
        self.assertEqual(resources, [{'name': 'res1'}, {'name': 'res2'}])