add show resource and list resources

Implements: blueprint vitrage-cli
Change-Id: I7b41f60321d4f3c119245e99b2c80ce4fc508f30
This commit is contained in:
Eyal
2016-01-20 15:41:59 +02:00
parent b3bd630f95
commit 6db6113566
4 changed files with 87 additions and 0 deletions

View File

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

View File

@@ -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="<resource type>",
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

View File

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

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