diff --git a/vitrageclient/shell.py b/vitrageclient/shell.py index bace1d1..38ba32b 100644 --- a/vitrageclient/shell.py +++ b/vitrageclient/shell.py @@ -28,6 +28,8 @@ from keystoneauth1 import loading import client import noauth + +from v1.cli import resource from v1.cli import topology from vitrageclient import __version__ @@ -35,6 +37,8 @@ from vitrageclient import __version__ class VitrageCommandManager(commandmanager.CommandManager): COMMANDS = { "topology show": topology.TopologyShow, + "resource show": resource.ResourceShow, + "resource list": resource.ResourceList, } def load_commands(self, namespace): diff --git a/vitrageclient/v1/cli/resource.py b/vitrageclient/v1/cli/resource.py new file mode 100644 index 0000000..138e359 --- /dev/null +++ b/vitrageclient/v1/cli/resource.py @@ -0,0 +1,46 @@ +# 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 + + +# noinspection PyAbstractClass +class ResourceShow(show.ShowOne): + """Show a resource""" + + def get_parser(self, prog_name): + parser = super(ResourceShow, self).get_parser(prog_name) + parser.add_argument("resource_id", help="ID of a resource") + return parser + + def take_action(self, parsed_args): + resource_id = parsed_args.resource_id + resource = self.app.client.resource.get(resource_id=resource_id) + return self.dict2columns(resource) + + +class ResourceList(lister.Lister): + """List resources""" + + def get_parser(self, prog_name): + parser = super(ResourceList, self).get_parser(prog_name) + parser.add_argument("--type", dest='resource_type', + metavar="", + help="Type of resource") + + return parser + + def take_action(self, parsed_args): + resource_type = parsed_args.resource_type + resources = self.app.client.resource.list(resource_type=resource_type) + return [], resources diff --git a/vitrageclient/v1/client.py b/vitrageclient/v1/client.py index 7813fbe..f303f42 100644 --- a/vitrageclient/v1/client.py +++ b/vitrageclient/v1/client.py @@ -11,6 +11,8 @@ # under the License. from vitrageclient import client + +from vitrageclient.v1 import resource from vitrageclient.v1 import topology @@ -20,3 +22,4 @@ class Client(object): self._api = client.VitrageClient(session, service_type=service_type, **kwargs) self.topology = topology.Topology(self._api) + self.resource = resource.Resource(self._api) diff --git a/vitrageclient/v1/resource.py b/vitrageclient/v1/resource.py new file mode 100644 index 0000000..1ff3d89 --- /dev/null +++ b/vitrageclient/v1/resource.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. + + +class Resource(object): + url = "v1/resources/" + + def __init__(self, api): + self.api = api + + def list(self, resource_type=None): + """Get a all resources + + :param resource_type: the type for the resources + """ + params = dict(resource_type=resource_type) + return self.api.get(self.url, params=params).json() + + def get(self, resource_id): + """Get a resource + + :param resource_id: the id of the resource + """ + url = self.url + resource_id + return self.api.get(url).json()