def test_get_filter_obj(): filter_data = {"k1": "v1", "k2": "v2"} filter_obj = utils.get_filter_obj(filter_data, ["k1", "k2"]) assert filter_obj.k1 == "v1" assert filter_obj.k2 == "v2" # test filter data is empty filter_data = {} filter_obj = utils.get_filter_obj(filter_data, ["k1", "k2"]) assert filter_obj.k1 is None assert filter_obj.k2 is None
def _dispatch_search_instance(self, request, data, request_id): options = self._get_options(request) filter_obj = get_filter_obj(data.get("filter"), ["parent", "keyword"]) if filter_obj.keyword is None or len(filter_obj.keyword) < 2: raise KeywordTooShortException( "the length of keyword should be greater than or equals to 2") page_obj = get_page_obj(data.get("page")) provider = self._provider[data["type"]] pre_process = getattr(provider, "pre_search_instance", None) if pre_process and callable(pre_process): pre_process(filter_obj, page_obj, **options) search_function = getattr(provider, "search_instance", None) if not (search_function and callable(search_function)): return fail_response( 404, "resource type: {} not support search instance".format( data["type"]), request_id) result = provider.search_instance(filter_obj, page_obj, **options) return success_response(result.to_dict(), request_id)
def _dispatch_fetch_instance_info(self, request, data, request_id): options = self._get_options(request) filter_obj = get_filter_obj(data.get("filter"), ["ids", "attrs"]) provider = self._provider[data["type"]] pre_process = getattr(provider, "pre_fetch_instance_info", None) if pre_process and callable(pre_process): pre_process(filter_obj, **options) result = provider.fetch_instance_info(filter_obj, **options) return success_response(result.to_list(), request_id)
def _dispatch_list_instance_by_policy(self, request, data, request_id): options = self._get_options(request) filter_obj = get_filter_obj(data.get("filter"), ["expression"]) page_obj = get_page_obj(data.get("page")) provider = self._provider[data["type"]] pre_process = getattr(provider, "pre_list_instance_by_policy", None) if pre_process and callable(pre_process): pre_process(filter_obj, page_obj, **options) result = provider.list_instance_by_policy(filter_obj, page_obj, **options) return success_response(result.to_list(), request_id)
def _dispatch_list_instance(self, request, data, request_id): options = self._get_options(request) filter_obj = get_filter_obj( data.get("filter"), ["parent", "search", "resource_type_chain"]) page_obj = get_page_obj(data.get("page")) provider = self._provider[data["type"]] pre_process = getattr(provider, "pre_list_instance", None) if pre_process and callable(pre_process): pre_process(filter_obj, page_obj, **options) result = provider.list_instance(filter_obj, page_obj, **options) return success_response(result.to_dict(), request_id)
def _dispatch_list_attr_value(self, request, data, request_id): options = self._get_options(request) filter_obj = get_filter_obj(data.get("filter"), ["attr", "keyword", "ids"]) page_obj = get_page_obj(data.get("page")) provider = self._provider[data["type"]] pre_process = getattr(provider, "pre_list_attr_value", None) if pre_process and callable(pre_process): pre_process(filter_obj, page_obj, **options) result = provider.list_attr_value(filter_obj, page_obj, **options) return success_response(result.to_dict(), request_id)
def _parse_filter_and_page(self, data: Dict) -> (FancyDict, Page): filter_obj = get_filter_obj( data["filter"], ["ids", "parent", "search", "resource_type_chain", "keyword"]) page_obj = get_page_obj(data.get("page")) return filter_obj, page_obj
def _parse_filter_and_page(self, data): filter_obj = get_filter_obj( data["filter"], ["ids", "parent", "search", "resource_type_chain"]) page_obj = get_page_obj(data.get("page")) return filter_obj, page_obj