Esempio n. 1
0
    def _preprocess_logs(self, ctx, logs):
        resetting_roots = set()
        log_by_root = {}
        resource_paths = ('resource', 'service_graph', 'infra', 'tree',
                          'status')
        for log in logs:
            if log.action == aim_tree.ActionLog.RESET:
                resetting_roots.add(log.root_rn)
            action = log.action
            aim_res = None
            for path in resource_paths:
                try:
                    klass = importutils.import_class('aim.api.' + path +
                                                     '.%s' % log.object_type)
                    aim_res = klass(**utils.json_loads(log.object_dict))
                except ImportError:
                    pass
            if not aim_res:
                LOG.warn('Aim resource for event %s not found' % log)
                continue
            # REVISIT: We currently only query the DB for
            # SecurityGroupRule resources, but should treat all
            # resource types uniformly, and therefore should do this
            # for all resource types. This will also allow elimination
            # of the epoch bumping when modifying list attributes of
            # other resource types. But we probably will want to
            # optimize this quering to avoid a roundtrip to the DB
            # server for each action log item, by making one find()
            # call for each resource type, filtering with the
            # identities from all the action log items being
            # processed.
            if isinstance(aim_res, resource.SecurityGroupRule):
                db_aim_res = self.aim_manager.get(ctx, aim_res)
                if db_aim_res:
                    if action == aim_tree.ActionLog.DELETE:
                        LOG.warn("AIM resource %s exists in DB for delete "
                                 "action" % db_aim_res)
                    else:
                        # Use current resource from DB so that list
                        # attributes do no need to be protected from
                        # concurrent updates by bumping the resource's
                        # epoch.
                        aim_res = db_aim_res

                        # We will remove this SG rule from AIM tree to
                        # prevent it from showing up in APIC because its
                        # a block-all rule.
                        if (aim_cfg.CONF.aim.
                                remove_remote_group_sg_rule_if_block_all
                                and aim_res.remote_group_id
                                and not aim_res.remote_ips):
                            action = aim_tree.ActionLog.DELETE
                else:
                    if action != aim_tree.ActionLog.DELETE:
                        LOG.warn("AIM resource %s does not exist in DB "
                                 "for create/update action" % aim_res)
            log_by_root.setdefault(log.root_rn, []).append(
                (action, aim_res, log))
        return log_by_root, resetting_roots
 def subscribe(self, urls):
     resp = self._subscribe(urls)
     if resp is not None:
         if resp.ok:
             return utils.json_loads(resp.text)['imdata']
         else:
             if resp.status_code in [405, 598]:
                 self.establish_ws_session()
             raise WebSocketSubscriptionFailed(urls=urls,
                                               code=resp.status_code,
                                               text=resp.text)
 def subscribe(self, urls):
     if urls == self.EMPTY_URLS:
         raise WebSocketSubscriptionFailed(urls=urls,
                                           code=400,
                                           text="Empty URLS")
     resp = self._subscribe(urls)
     if resp is not None:
         if resp.ok:
             return utils.json_loads(resp.text)['imdata']
         else:
             if resp.status_code in [405, 598, 500]:
                 self.establish_ws_session()
             raise WebSocketSubscriptionFailed(urls=urls,
                                               code=resp.status_code,
                                               text=resp.text)
Esempio n. 4
0
 def _preprocess_logs(self, logs):
     resetting_roots = set()
     log_by_root = {}
     resource_paths = ('resource', 'service_graph', 'infra', 'tree',
                       'status')
     for log in logs:
         if log.action == aim_tree.ActionLog.RESET:
             resetting_roots.add(log.root_rn)
         action = log.action
         aim_res = None
         for path in resource_paths:
             try:
                 klass = importutils.import_class(
                     'aim.api.' + path + '.%s' % log.object_type)
                 aim_res = klass(**utils.json_loads(log.object_dict))
             except ImportError:
                 pass
         if not aim_res:
             LOG.warn('Aim resource for event %s not found' % log)
             continue
         log_by_root.setdefault(log.root_rn, []).append(
             (action, aim_res, log))
     return log_by_root, resetting_roots
Esempio n. 5
0
 def from_string(string, root_key=None):
     to_dict = utils.json_loads(string)
     return (StructuredHashTree(StructuredHashTree._build_tree(to_dict)) if
             to_dict else StructuredHashTree(root_key=root_key))
 def from_string(string, root_key=None):
     to_dict = utils.json_loads(string)
     return (StructuredHashTree(StructuredHashTree._build_tree(to_dict)) if
             to_dict else StructuredHashTree(root_key=root_key))
    def _exec_rest_operation_with_http_info(self, k8s_klass, verb, **kwargs):
        params = locals()
        for key, val in iteritems(params['kwargs']):
            params[key] = val
        del params['kwargs']

        # Path looks like
        # /api(s)?/<api_version>(/namespaces/{namespace})?/<kind>(/{name})?'

        path = '/api'
        if k8s_klass.api_version != K8S_API_VERSION_CORE_V1:
            path += 's'
        path += '/' + k8s_klass.api_version
        path_params = {}
        if getattr(k8s_klass, 'namespaced', True) and params['namespace']:
            path += '/namespaces/{namespace}'
            path_params['namespace'] = params['namespace']
        path += ('/%s' % k8s_klass.kind.lower())
        if not k8s_klass.kind.endswith('s'):
            path += 's'
        # When name is passed, use it as a path parameter
        if 'name' in params:
            path += '/{name}'
            path_params['name'] = params['name']

        resource_path = path.replace('{format}', 'json')
        query_params = {}
        # Set query params for the verb
        for query in self.query_params:
            if query in params:
                query_params[utils.snake_to_lower_camel(query)] = params[query]

        header_params = {
            'Accept': self.api_client.select_header_accept(
                self.verb_accept_headers[verb]),
            'Content-Type': self.api_client.select_header_content_type(
                self.verb_content_type[verb])
        }

        # Authentication setting
        auth_settings = ['BearerToken']
        if verb == 'POST' and 'body' in params:
            params['body']['kind'] = k8s_klass.kind
            params['body']['apiVersion'] = k8s_klass.api_version

        result = self.api_client.call_api(
            resource_path, verb, path_params, query_params, header_params,
            body=params.get('body'), post_params=[], files={},
            response_type='str', auth_settings=auth_settings,
            callback=params.get('callback'), collection_formats={},
            _return_http_data_only=params.get('_return_http_data_only'),
            _preload_content=params.get('_preload_content', True),
            _request_timeout=params.get('_request_timeout'))
        if result and isinstance(result, str):
            try:
                return utils.json_loads(
                    result.replace(": u'", "'").replace("'", '"'))
            except ValueError:
                try:
                    return ast.literal_eval(result)
                except ValueError:
                    pass

        return result
 def from_string(string, root_key=None, has_populated=False):
     to_dict = utils.json_loads(string)
     return (StructuredHashTree(StructuredHashTree._build_tree(to_dict),
                                has_populated=has_populated) if to_dict else
             StructuredHashTree(root_key=root_key,
                                has_populated=has_populated))
    def _exec_rest_operation_with_http_info(self, k8s_klass, verb, **kwargs):
        params = locals()
        for key, val in iteritems(params['kwargs']):
            params[key] = val
        del params['kwargs']

        # Path looks like
        # /api(s)?/<api_version>(/namespaces/{namespace})?/<kind>(/{name})?'

        path = '/api'
        if k8s_klass.api_version != K8S_API_VERSION_CORE_V1:
            path += 's'
        path += '/' + k8s_klass.api_version
        path_params = {}
        if getattr(k8s_klass, 'namespaced', True) and params['namespace']:
            path += '/namespaces/{namespace}'
            path_params['namespace'] = params['namespace']
        path += ('/%s' % k8s_klass.kind.lower())
        if not k8s_klass.kind.endswith('s'):
            path += 's'
        # When name is passed, use it as a path parameter
        if 'name' in params:
            path += '/{name}'
            path_params['name'] = params['name']

        resource_path = path.replace('{format}', 'json')
        query_params = {}
        # Set query params for the verb
        for query in self.query_params:
            if query in params:
                query_params[utils.snake_to_lower_camel(query)] = params[query]

        header_params = {
            'Accept':
            self.api_client.select_header_accept(
                self.verb_accept_headers[verb]),
            'Content-Type':
            self.api_client.select_header_content_type(
                self.verb_content_type[verb])
        }

        # Authentication setting
        auth_settings = ['BearerToken']
        if verb == 'POST' and 'body' in params:
            params['body']['kind'] = k8s_klass.kind
            params['body']['apiVersion'] = k8s_klass.api_version

        result = self.api_client.call_api(
            resource_path,
            verb,
            path_params,
            query_params,
            header_params,
            body=params.get('body'),
            post_params=[],
            files={},
            response_type='str',
            auth_settings=auth_settings,
            callback=params.get('callback'),
            collection_formats={},
            _return_http_data_only=params.get('_return_http_data_only'),
            _preload_content=params.get('_preload_content', True),
            _request_timeout=params.get('_request_timeout'))
        if result and isinstance(result, str):
            try:
                return utils.json_loads(
                    result.replace(": u'", "'").replace("'", '"'))
            except ValueError:
                try:
                    return ast.literal_eval(result)
                except ValueError:
                    pass

        return result