diff --git a/venusclient/common/utils.py b/venusclient/common/utils.py index ad70eb7..b30de5f 100644 --- a/venusclient/common/utils.py +++ b/venusclient/common/utils.py @@ -13,6 +13,7 @@ # under the License. # import logging +from urllib import parse from oslo_serialization import jsonutils @@ -157,3 +158,19 @@ def args_array_to_patch(op, attributes): else: raise exc.CommandError(_('Unknown PATCH operation: %s') % op) return patch + + +def prepare_query_string(params): + """Convert dict params to query string""" + # Transform the dict to a sequence of two-element tuples in fixed + # order, then the encoded string will be consistent in Python 2&3. + if not params: + return '' + params = sorted(params.items(), key=lambda x: x[0]) + return '?%s' % parse.urlencode(params) if params else '' + + +def get_url_with_filter(url, filters): + query_string = prepare_query_string(filters) + url = "%s%s" % (url, query_string) + return url diff --git a/venusclient/v1/client.py b/venusclient/v1/client.py index a81aaf1..5a8fff0 100644 --- a/venusclient/v1/client.py +++ b/venusclient/v1/client.py @@ -1,17 +1,16 @@ -# Copyright 2014 -# The Cloudscaling Group, Inc. +# Copyright 2020 Inspur # -# Licensed under the Apache License, Version 2.0 (the "License"); you may not -# use this file except in compliance with the License. You may obtain a copy -# of the License at +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at # -# http://www.apache.org/licenses/LICENSE-2.0 +# http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. import os_client_config @@ -19,6 +18,7 @@ from keystoneauth1 import session as ksa_session from oslo_utils import importutils from venusclient.common import httpclient from venusclient.v1 import config +from venusclient.v1 import search profiler = importutils.try_import("osprofiler.profiler") @@ -181,6 +181,7 @@ class Client(object): ) self.config = config.ConfigManager(self.http_client) + self.search = search.SearchManager(self.http_client) profile = kwargs.pop("profile", None) if profiler and profile: diff --git a/venusclient/v1/config.py b/venusclient/v1/config.py index 0bbc129..39ba866 100644 --- a/venusclient/v1/config.py +++ b/venusclient/v1/config.py @@ -34,12 +34,3 @@ class ConfigManager(basemodels.BaseModelManager): return body except Exception as e: raise RuntimeError(str(e)) - - def get_logs(self, args): - print(args) - url = '/v1/search/logs' - try: - resp, body = self.api.json_request('GET', url) - return body - except Exception as e: - raise RuntimeError(str(e)) diff --git a/venusclient/v1/search.py b/venusclient/v1/search.py new file mode 100644 index 0000000..5c5a5c2 --- /dev/null +++ b/venusclient/v1/search.py @@ -0,0 +1,41 @@ +# Copyright 2020 Inspur +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from venusclient.common import utils +from venusclient.v1 import basemodels + + +class SearchManager(basemodels.BaseModelManager): + api_name = "search" + base_url = "search" + + def get_logs(self, start_time=0, end_time=20, page_size=15, page_num=1): + url = '/v1/search/logs' + + params = { + 'start_time': start_time, + 'end_time': end_time, + 'page_size': page_size, + 'page_num': page_num + } + url += utils.prepare_query_string(params) + + print('123123123') + print(url) + + try: + resp, body = self.api.json_request('GET', url) + return body + except Exception as e: + raise RuntimeError(str(e))