diff --git a/vitrageclient/client.py b/vitrageclient/client.py index 1ea1da6..8f3c4db 100644 --- a/vitrageclient/client.py +++ b/vitrageclient/client.py @@ -10,11 +10,27 @@ # License for the specific language governing permissions and limitations # under the License. +import exc +from keystoneauth1 import adapter from vitrageclient.common import utils -def get_client(version, *args, **kwargs): +# noinspection PyPep8Naming +def Client(version, *args, **kwargs): module = utils.import_versioned_module(version, 'client') client_class = getattr(module, 'Client') return client_class(*args, **kwargs) + + +class VitrageClient(adapter.Adapter): + def request(self, url, method, **kwargs): + raise_exc = kwargs.pop('raise_exc', True) + resp = super(VitrageClient, self).request(url, + method, + raise_exc=False, + **kwargs) + + if raise_exc and resp.status_code >= 400: + raise exc.from_response(resp, url, method) + return resp diff --git a/vitrageclient/exc.py b/vitrageclient/exc.py index 942764d..8af2cc2 100644 --- a/vitrageclient/exc.py +++ b/vitrageclient/exc.py @@ -30,3 +30,7 @@ class ClientException(Exception): formatted_string += " (Request-ID: %s)" % self.request_id return formatted_string + + +def from_response(resp, url, method): + return None diff --git a/vitrageclient/shell.py b/vitrageclient/shell.py index c07ef1d..6e84376 100644 --- a/vitrageclient/shell.py +++ b/vitrageclient/shell.py @@ -16,9 +16,8 @@ Vitrage command line interface """ from __future__ import print_function -import client + import logging -import noauth import os import sys import warnings @@ -27,7 +26,10 @@ from cliff import app from cliff import commandmanager from keystoneauth1 import exceptions from keystoneauth1 import loading -from v1 import topology + +import client +import noauth +from v1.cli import topology from vitrageclient import __version__ @@ -115,7 +117,7 @@ class VitrageShell(app.App): self.options, auth=auth_plugin) # noinspection PyAttributeOutsideInit - self._client = client.get_client( + self._client = client.Client( self.options.vitrage_api_version, session=session, interface=self.options.interface, @@ -135,7 +137,6 @@ class VitrageShell(app.App): def configure_logging(self): if self.options.debug: - # Set this here so cliff.app.configure_logging() can work self._set_debug_logging_messages() super(VitrageShell, self).configure_logging() diff --git a/vitrageclient/v1/cli/__init__.py b/vitrageclient/v1/cli/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/vitrageclient/v1/cli/topology.py b/vitrageclient/v1/cli/topology.py new file mode 100644 index 0000000..180656a --- /dev/null +++ b/vitrageclient/v1/cli/topology.py @@ -0,0 +1,34 @@ +# 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 cliff import lister +from cliff import show + + +class TopologyList(lister.Lister): + + COLS = ('id', 'name', 'description') + + def take_action(self, parsed_args): + topologies = self.app.client.topology.list() + return self.COLS, [tuple([topologies[col] for col in self.COLS])] + + +class TopologyShow(show.ShowOne): + def get_parser(self, uuid): + parser = super(TopologyShow, self).get_parser(uuid) + parser.add_argument("id", help="Id of the topology") + return parser + + def take_action(self, parsed_args): + topology = self.app.client.topology.get(id=parsed_args.id) + return self.dict2columns(topology) diff --git a/vitrageclient/v1/client.py b/vitrageclient/v1/client.py index d5bcf39..9e29dae 100644 --- a/vitrageclient/v1/client.py +++ b/vitrageclient/v1/client.py @@ -10,6 +10,25 @@ # License for the specific language governing permissions and limitations # under the License. +from v1 import topology +from vitrageclient import client + class Client(object): - pass + DEFAULT_HEADERS = { + "Accept": "application/json", + } + + def __init__(self, session=None, service_type='rca', **kwargs): + self._set_default_headers(kwargs) + self._api = client.VitrageClient(session, service_type=service_type, + **kwargs) + self.topology = topology.Topology(self._api) + + def _set_default_headers(self, kwargs): + headers = kwargs.get('headers', {}) + for k, v in self.DEFAULT_HEADERS.items(): + if k not in headers: + headers[k] = v + kwargs['headers'] = headers + return kwargs diff --git a/vitrageclient/v1/topology.py b/vitrageclient/v1/topology.py index c7ae7aa..7ee6c7e 100644 --- a/vitrageclient/v1/topology.py +++ b/vitrageclient/v1/topology.py @@ -10,15 +10,23 @@ # License for the specific language governing permissions and limitations # under the License. -from cliff import lister -from cliff import show +class Topology(object): + URL = "v1/topologies/" -class TopologyList(lister.Lister): - def take_action(self, parsed_args): - pass + def __init__(self, api): + self.api = api + def list(self): + """List topologies""" -class TopologyShow(show.ShowOne): - def take_action(self, parsed_args): - pass + return self.api.get(self.URL).json() + + def get(self, uuid): + """Get a topology + + :param uuid: Id of topology + :type uuid: str + """ + + return self.api.get(self.URL + uuid).json()