Add support of Availability Zones API

Change-Id: Ie7ff8234b5cd4a20d94148c7512b2d5cd621e190
Closes-Bug: #1622540
This commit is contained in:
Valeriy Ponomaryov 2016-09-12 12:50:54 +03:00
parent c67f7d56d4
commit 44ae492fed
8 changed files with 211 additions and 0 deletions

View File

@ -141,6 +141,20 @@ class ManilaCLIClient(base.CLIClient):
raise exceptions.ResourceReleaseFailed(
res_type=res_type, res_id=res_id)
def list_availability_zones(self, columns=None, microversion=None):
"""List availability zones.
:param columns: comma separated string of columns.
Example, "--columns id,name"
:param microversion: API microversion that should be used.
"""
cmd = 'availability-zone-list'
if columns is not None:
cmd += ' --columns ' + columns
azs_raw = self.manila(cmd, microversion=microversion)
azs = output_parser.listing(azs_raw)
return azs
# Share types
def create_share_type(self, name=None, driver_handles_share_servers=True,

View File

@ -0,0 +1,59 @@
# Copyright 2016 Mirantis Inc.
# 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 ddt
from oslo_utils import uuidutils
from manilaclient.tests.functional import base
@ddt.ddt
class ManilaClientTestAvailabilityZonesReadOnly(base.BaseTestCase):
@ddt.data("2.6", "2.7", "2.22")
def test_availability_zone_list(self, microversion):
self.skip_if_microversion_not_supported(microversion)
azs = self.user_client.list_availability_zones(
microversion=microversion)
for az in azs:
self.assertEqual(4, len(az))
for key in ('Id', 'Name', 'Created_At', 'Updated_At'):
self.assertIn(key, az)
self.assertTrue(uuidutils.is_uuid_like(az['Id']))
self.assertIsNotNone(az['Name'])
self.assertIsNotNone(az['Created_At'])
@ddt.data(
('name', ['Name']),
('name,id', ['Name', 'Id']),
('name,created_at', ['Name', 'Created_At']),
('name,id,created_at', ['Name', 'Id', 'Created_At']),
)
@ddt.unpack
def test_availability_zone_list_with_columns(self, columns_arg, expected):
azs = self.user_client.list_availability_zones(columns=columns_arg)
for az in azs:
self.assertEqual(len(expected), len(az))
for key in expected:
self.assertIn(key, az)
if 'Id' in expected:
self.assertTrue(uuidutils.is_uuid_like(az['Id']))
if 'Name' in expected:
self.assertIsNotNone(az['Name'])
if 'Created_At' in expected:
self.assertIsNotNone(az['Created_At'])

View File

@ -86,6 +86,21 @@ class FakeHTTPClient(fakes.FakeHTTPClient):
}
return (200, {}, body)
def get_availability_zones(self):
availability_zones = {
"availability_zones": [
{"id": "368c5780-ad72-4bcf-a8b6-19e45f4fafoo",
"name": "foo",
"created_at": "2016-07-08T14:13:12.000000",
"updated_at": "2016-07-08T15:14:13.000000"},
{"id": "368c5780-ad72-4bcf-a8b6-19e45f4fabar",
"name": "bar",
"created_at": "2016-07-08T14:13:12.000000",
"updated_at": "2016-07-08T15:14:13.000000"},
]
}
return (200, {}, availability_zones)
def get_os_services(self, **kw):
services = {
"services": [

View File

@ -0,0 +1,49 @@
# Copyright 2016 Mirantis, Inc.
# 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 ddt
import mock
from manilaclient import api_versions
from manilaclient.tests.unit import utils
from manilaclient.v2 import availability_zones
@ddt.ddt
class AvailabilityZoneTest(utils.TestCase):
def _get_manager(self, microversion):
version = api_versions.APIVersion(microversion)
mock_microversion = mock.Mock(api_version=version)
return availability_zones.AvailabilityZoneManager(
api=mock_microversion)
def _get_resource_path(self, microversion):
if (api_versions.APIVersion(microversion) >
api_versions.APIVersion("2.6")):
return availability_zones.RESOURCE_PATH
return availability_zones.RESOURCE_PATH_LEGACY
@ddt.data("2.6", "2.7", api_versions.MIN_VERSION, api_versions.MAX_VERSION)
def test_list(self, microversion):
manager = self._get_manager(microversion)
resource_path = self._get_resource_path(microversion)
self.mock_object(manager, "_list")
result = manager.list()
manager._list.assert_called_once_with(
resource_path, availability_zones.RESOURCE_NAME)
self.assertEqual(manager._list.return_value, result)

View File

@ -100,6 +100,17 @@ class ShellTest(test_utils.TestCase):
return self.shell.cs.assert_called_anytime(
method, url, body, clear_callstack=clear_callstack)
def test_availability_zone_list(self):
self.run_command('availability-zone-list')
self.assert_called('GET', '/availability-zones')
@mock.patch.object(cliutils, 'print_list', mock.Mock())
def test_availability_zone_list_select_column(self):
self.run_command('availability-zone-list --columns id,name')
self.assert_called('GET', '/availability-zones')
cliutils.print_list.assert_called_once_with(
mock.ANY, fields=['Id', 'Name'])
def test_service_list(self):
self.run_command('service-list')
self.assert_called('GET', '/services')

View File

@ -0,0 +1,41 @@
# Copyright 2016 Mirantis, Inc.
# 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.
from manilaclient import api_versions
from manilaclient import base
from manilaclient.openstack.common.apiclient import base as common_base
RESOURCE_PATH_LEGACY = '/os-availability-zone'
RESOURCE_PATH = '/availability-zones'
RESOURCE_NAME = 'availability_zones'
class AvailabilityZone(common_base.Resource):
def __repr__(self):
return "<AvailabilityZone: %s>" % self.id
class AvailabilityZoneManager(base.Manager):
"""Manage :class:`Service` resources."""
resource_class = AvailabilityZone
@api_versions.wraps("1.0", "2.6")
def list(self):
return self._list(RESOURCE_PATH_LEGACY, RESOURCE_NAME)
@api_versions.wraps("2.7") # noqa
def list(self):
return self._list(RESOURCE_PATH, RESOURCE_NAME)

View File

@ -22,6 +22,7 @@ import manilaclient
from manilaclient.common import constants
from manilaclient.common import httpclient
from manilaclient import exceptions
from manilaclient.v2 import availability_zones
from manilaclient.v2 import consistency_group_snapshots as cg_snapshots
from manilaclient.v2 import consistency_groups
from manilaclient.v2 import limits
@ -205,6 +206,8 @@ class Client(object):
http_log_debug=http_log_debug,
api_version=self.api_version)
self.availability_zones = availability_zones.AvailabilityZoneManager(
self)
self.limits = limits.LimitsManager(self)
self.services = services.ServiceManager(self)
self.security_services = security_services.SecurityServiceManager(self)

View File

@ -2620,6 +2620,25 @@ def do_share_server_delete(cs, args):
"share servers.")
@cliutils.arg(
'--columns',
metavar='<columns>',
type=str,
default=None,
help='Comma separated list of columns to be displayed '
'e.g. --columns "id,name"')
def do_availability_zone_list(cs, args):
"""List all availability zones."""
if args.columns is not None:
fields = _split_columns(columns=args.columns)
else:
fields = ("Id", "Name", "Created_At", "Updated_At")
availability_zones = cs.availability_zones.list()
cliutils.print_list(availability_zones, fields=fields)
@cliutils.arg(
'--host',
metavar='<hostname>',