From 39e035fba11b7a599c2c1cd1228ff8019362dbb8 Mon Sep 17 00:00:00 2001 From: vponomaryov Date: Thu, 17 Apr 2014 06:45:00 -0400 Subject: [PATCH] Added service-list request Manila server has support of list with services. This commit adds possibility to use this API of Manila. Change-Id: Ib01168941713ceb7224618def20d8115656a21cd --- manilaclient/v1/client.py | 2 ++ manilaclient/v1/services.py | 47 +++++++++++++++++++++++++++++++++++++ manilaclient/v1/shell.py | 39 ++++++++++++++++++++++++++++++ tests/v1/test_services.py | 34 +++++++++++++++++++++++++++ 4 files changed, 122 insertions(+) create mode 100644 manilaclient/v1/services.py create mode 100644 tests/v1/test_services.py diff --git a/manilaclient/v1/client.py b/manilaclient/v1/client.py index e5d1b493b..fe3055c98 100644 --- a/manilaclient/v1/client.py +++ b/manilaclient/v1/client.py @@ -4,6 +4,7 @@ from manilaclient.v1 import share_networks from manilaclient.v1 import security_services from manilaclient.v1 import quota_classes from manilaclient.v1 import quotas +from manilaclient.v1 import services from manilaclient.v1 import shares from manilaclient.v1 import share_snapshots @@ -33,6 +34,7 @@ class Client(object): # know it's not being used as keyword argument password = api_key self.limits = limits.LimitsManager(self) + self.services = services.ServiceManager(self) self.security_services = security_services.SecurityServiceManager(self) self.share_networks = share_networks.ShareNetworkManager(self) diff --git a/manilaclient/v1/services.py b/manilaclient/v1/services.py new file mode 100644 index 000000000..ab4a80eba --- /dev/null +++ b/manilaclient/v1/services.py @@ -0,0 +1,47 @@ +# Copyright 2014 OpenStack LLC. +# All Rights Reserved. +# +# 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. + +import urllib + +from manilaclient import base + +RESOURCES_PATH = '/os-services' +RESOURCES_NAME = 'services' + + +class Service(base.Resource): + + def __repr__(self): + return "" % self.id + + +class ServiceManager(base.Manager): + """Manage :class:`Service` resources.""" + resource_class = Service + + def list(self, search_opts=None): + """Get a list of all services. + + :rtype: list of :class:`Service` + """ + query_string = '' + if search_opts: + query_string = urllib.urlencode([(key, value) + for (key, value) + in search_opts.items() + if value]) + if query_string: + query_string = "?%s" % query_string + return self._list(RESOURCES_PATH + query_string, RESOURCES_NAME) diff --git a/manilaclient/v1/shell.py b/manilaclient/v1/shell.py index 541792f0d..b8ec2978b 100644 --- a/manilaclient/v1/shell.py +++ b/manilaclient/v1/shell.py @@ -998,3 +998,42 @@ def do_security_service_list(cs, args): def do_security_service_delete(cs, args): """Delete security service""" cs.security_services.delete(args.security_service) + + +@utils.arg( + '--host', + metavar='', + default=None, + help='Name of host.') +@utils.arg( + '--binary', + metavar='', + default=None, + help='Service binary.') +@utils.arg( + '--status', + metavar='', + default=None, + help='Filter results by status') +@utils.arg( + '--state', + metavar='', + default=None, + help='Filter results by state') +@utils.arg( + '--zone', + metavar='', + default=None, + help='Availability zone.') +def do_service_list(cs, args): + """List all the services.""" + search_opts = { + 'status': args.status, + 'host': args.host, + 'binary': args.binary, + 'zone': args.zone, + 'state': args.state, + } + fields = ["Binary", "Host", "Zone", "Status", "State", "Updated_at"] + services = cs.services.list(search_opts=search_opts) + utils.print_list(services, fields=fields) diff --git a/tests/v1/test_services.py b/tests/v1/test_services.py new file mode 100644 index 000000000..65f756fed --- /dev/null +++ b/tests/v1/test_services.py @@ -0,0 +1,34 @@ +# Copyright 2013 OpenStack LLC. +# All Rights Reserved. +# +# 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. + +import mock +import unittest + +from manilaclient.v1 import services + + +class ServicesTest(unittest.TestCase): + + def setUp(self): + super(ServicesTest, self).setUp() + self.manager = services.ServiceManager(api=None) + + def test_list(self): + with mock.patch.object(self.manager, '_list', + mock.Mock(return_value=None)): + self.manager.list() + self.manager._list.assert_called_once_with( + services.RESOURCES_PATH, + services.RESOURCES_NAME)