Beispiel #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': 'pod_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_pod_service_configurations(
                    cxt, config_filters)

                if len(config_list) > 1:
                    raise exceptions.EndpointNotUnique(pod_id, service)
                if len(config_list) == 1:
                    config_id = config_list[0]['service_id']
                    update_dict = {
                        'service_url': endpoint_map[region][service]
                    }
                    api.update_pod_service_configuration(
                        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_pod_service_configuration(cxt, config_dict)
Beispiel #2
0
 def _get_config_with_retry(self, cxt, filters, pod, service, retry):
     conf_list = api.list_pod_service_configurations(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
Beispiel #3
0
 def _get_config_with_retry(self, cxt, filters, pod, service, retry):
     conf_list = api.list_pod_service_configurations(cxt, filters)
     if len(conf_list) > 1:
         raise exceptions.EndpointNotUnique(pod, service)
     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
Beispiel #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 pod
            pod_filters = [{'key': 'pod_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_pod_service_configurations(
                    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_pod_service_configuration(
                        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_pod_service_configuration(
                        cxt, config_dict)
Beispiel #5
0
def fake_get_pod_service_endpoint(ctx, pod_name, st):

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

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

        return pod_services[0]['service_url']

    return ''
Beispiel #6
0
def fake_get_pod_service_endpoint(ctx, pod_name, st):

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

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

        return pod_services[0]['service_url']

    return ''