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 pod
            pod_filters = [{
                'key': 'region_name',
                'comparator': 'eq',
                'value': region
            }]
            pod_list = api.list_pods(cxt, pod_filters)
            # skip region/pod not registered in cascade service
            if len(pod_list) != 1:
                continue
            for service in endpoint_map[region]:
                pod_id = pod_list[0]['pod_id']
                config_filters = [{
                    'key': 'pod_id',
                    'comparator': 'eq',
                    'value': pod_id
                }, {
                    'key': 'service_type',
                    'comparator': 'eq',
                    'value': service
                }]
                config_list = api.list_cached_endpoints(cxt, config_filters)

                if len(config_list) > 1:
                    continue
                if len(config_list) == 1:
                    config_id = config_list[0]['service_id']
                    update_dict = {
                        'service_url': endpoint_map[region][service]
                    }
                    api.update_cached_endpoints(cxt, config_id, update_dict)
                else:
                    config_dict = {
                        'service_id': str(uuid.uuid4()),
                        'pod_id': pod_id,
                        'service_type': service,
                        'service_url': endpoint_map[region][service]
                    }
                    api.create_cached_endpoints(cxt, config_dict)
Ejemplo n.º 2
0
 def _get_config_with_retry(self, cxt, filters, pod, service, retry):
     conf_list = api.list_cached_endpoints(cxt, filters)
     if len(conf_list) == 0:
         if not retry:
             raise exceptions.EndpointNotFound(pod, service)
         self._update_endpoint_from_keystone(cxt, True)
         return self._get_config_with_retry(cxt, filters, pod, service,
                                            False)
     return conf_list
Ejemplo n.º 3
0
 def _get_config_with_retry(self, cxt, filters, pod, service, retry):
     conf_list = api.list_cached_endpoints(cxt, filters)
     if len(conf_list) == 0:
         if not retry:
             raise exceptions.EndpointNotFound(pod, service)
         self._update_endpoint_from_keystone(cxt, True)
         return self._get_config_with_retry(cxt,
                                            filters, pod, service, False)
     return conf_list
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.get_admin_context()
            self._ensure_token_for_admin(admin_context)
            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 pod
            pod_filters = [{'key': 'region_name', 'comparator': 'eq',
                            'value': region}]
            pod_list = api.list_pods(cxt, pod_filters)
            # skip region/pod not registered in cascade service
            if len(pod_list) != 1:
                continue
            for service in endpoint_map[region]:
                pod_id = pod_list[0]['pod_id']
                config_filters = [{'key': 'pod_id', 'comparator': 'eq',
                                   'value': pod_id},
                                  {'key': 'service_type', 'comparator': 'eq',
                                   'value': service}]
                config_list = api.list_cached_endpoints(
                    cxt, config_filters)

                if len(config_list) > 1:
                    continue
                if len(config_list) == 1:
                    config_id = config_list[0]['service_id']
                    update_dict = {
                        'service_url': endpoint_map[region][service]}
                    api.update_cached_endpoints(
                        cxt, config_id, update_dict)
                else:
                    config_dict = {
                        'service_id': uuidutils.generate_uuid(),
                        'pod_id': pod_id,
                        'service_type': service,
                        'service_url': endpoint_map[region][service]
                    }
                    api.create_cached_endpoints(
                        cxt, config_dict)
Ejemplo n.º 5
0
def fake_get_pod_service_endpoint(ctx, region_name, st):

    pod = api.get_pod_by_name(ctx, region_name)
    if pod:
        f = [{
            'key': 'pod_id',
            'comparator': 'eq',
            'value': pod['pod_id']
        }, {
            'key': 'service_type',
            'comparator': 'eq',
            'value': st
        }]
        pod_services = api.list_cached_endpoints(ctx, filters=f, sorts=[])

        if len(pod_services) != 1:
            return ''

        return pod_services[0]['service_url']

    return ''