add more api

added topology show and topology list

Implements: blueprint vitrage-cli
Change-Id: Id630e19c240fef95692260235e67e36b970120c6
This commit is contained in:
Eyal 2015-11-19 15:32:54 +02:00
parent 3a20796127
commit ae3935d7a5
7 changed files with 97 additions and 15 deletions

View File

@ -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

View File

@ -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

View File

@ -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()

View File

View File

@ -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)

View File

@ -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

View File

@ -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()