Add availability_zone CLI

This patch adds the availability_zone support for CLI.

Change-Id: I13faf6d90e84f401f07376c517c643ba9eda2295
Co-Authored-By: IWAMOTO Toshihiro <iwamoto@valinux.co.jp>
Partially-implements: blueprint add-availability-zone
This commit is contained in:
Hirofumi Ichihara 2015-12-08 22:36:58 +09:00
parent 88010add39
commit 709928b550
8 changed files with 115 additions and 0 deletions

View File

@ -0,0 +1,38 @@
# 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 neutronclient.i18n import _
from neutronclient.neutron import v2_0 as neutronv20
def add_az_hint_argument(parser, resource):
parser.add_argument(
'--availability-zone-hint', metavar='AVAILABILITY_ZONE',
action='append', dest='availability_zone_hints',
help=_('Availability Zone for the %s '
'(requires availability zone extension, '
'this option can be repeated).') % resource)
def args2body_az_hint(parsed_args, resource):
if parsed_args.availability_zone_hints:
resource['availability_zone_hints'] = (
parsed_args.availability_zone_hints)
class ListAvailabilityZone(neutronv20.ListCommand):
"""List availability zones."""
resource = 'availability_zone'
list_columns = ['name', 'resource', 'state']
pagination_support = True
sorting_support = True

View File

@ -20,6 +20,7 @@ from neutronclient.common import exceptions
from neutronclient.common import utils
from neutronclient.i18n import _
from neutronclient.neutron import v2_0 as neutronV20
from neutronclient.neutron.v2_0 import availability_zone
from neutronclient.neutron.v2_0.qos import policy as qos_policy
@ -146,6 +147,7 @@ class CreateNetwork(neutronV20.CreateCommand, qos_policy.CreateQosPolicyMixin):
help=_('Name of network to create.'))
self.add_arguments_qos_policy(parser)
availability_zone.add_az_hint_argument(parser, self.resource)
def args2body(self, parsed_args):
body = {'name': parsed_args.name,
@ -158,6 +160,7 @@ class CreateNetwork(neutronV20.CreateCommand, qos_policy.CreateQosPolicyMixin):
'provider:segmentation_id'])
self.args2body_qos_policy(parsed_args, body)
availability_zone.args2body_az_hint(parsed_args, body)
return {'network': body}

View File

@ -24,6 +24,7 @@ from neutronclient.common import exceptions
from neutronclient.common import utils
from neutronclient.i18n import _
from neutronclient.neutron import v2_0 as neutronV20
from neutronclient.neutron.v2_0 import availability_zone
def _format_external_gateway_info(router):
@ -74,10 +75,13 @@ class CreateRouter(neutronV20.CreateCommand):
parser, '--ha', dest='ha',
help=_('Create a highly available router.'))
availability_zone.add_az_hint_argument(parser, self.resource)
def args2body(self, parsed_args):
body = {'admin_state_up': parsed_args.admin_state}
neutronV20.update_dict(parsed_args, body,
['name', 'tenant_id', 'distributed', 'ha'])
availability_zone.args2body_az_hint(parsed_args, body)
return {self.resource: body}

View File

@ -43,6 +43,7 @@ from neutronclient.i18n import _
from neutronclient.neutron.v2_0 import address_scope
from neutronclient.neutron.v2_0 import agent
from neutronclient.neutron.v2_0 import agentscheduler
from neutronclient.neutron.v2_0 import availability_zone
from neutronclient.neutron.v2_0 import extension
from neutronclient.neutron.v2_0.flavor import flavor
from neutronclient.neutron.v2_0.flavor import flavor_profile
@ -390,6 +391,7 @@ COMMAND_V2 = {
'flavor-profile-create': flavor_profile.CreateFlavorProfile,
'flavor-profile-delete': flavor_profile.DeleteFlavorProfile,
'flavor-profile-update': flavor_profile.UpdateFlavorProfile,
'availability-zone-list': availability_zone.ListAvailabilityZone,
}
COMMANDS = {'2.0': COMMAND_V2}

View File

@ -0,0 +1,31 @@
# 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 sys
from neutronclient.neutron.v2_0 import availability_zone as az
from neutronclient.tests.unit import test_cli20
class CLITestV20Agent(test_cli20.CLITestV20Base):
def test_list_agents(self):
contents = {'availability_zones': [{'name': 'zone1',
'resource': 'network',
'state': 'available'},
{'name': 'zone2',
'resource': 'router',
'state': 'unavailable'}]}
args = ['-f', 'json']
resources = "availability_zones"
cmd = az.ListAvailabilityZone(test_cli20.MyApp(sys.stdout), None)
self._test_list_columns(cmd, resources, contents, args)

View File

@ -149,6 +149,21 @@ class CLITestV20NetworkJSON(test_cli20.CLITestV20Base):
self._test_create_resource(resource, cmd, name, myid, args,
position_names, position_values)
def test_create_network_with_az_hint(self):
"""Create net: --availability-zone-hint zone1
--availability-zone-hint zone2.
"""
resource = 'network'
cmd = network.CreateNetwork(test_cli20.MyApp(sys.stdout), None)
name = 'myname'
myid = 'myid'
args = ['--availability-zone-hint', 'zone1',
'--availability-zone-hint', 'zone2', name]
position_names = ['availability_zone_hints', 'name']
position_values = [['zone1', 'zone2'], name]
self._test_create_resource(resource, cmd, name, myid, args,
position_names, position_values)
def test_list_nets_empty_with_column(self):
resources = "networks"
cmd = network.ListNetwork(test_cli20.MyApp(sys.stdout), None)

View File

@ -111,6 +111,21 @@ class CLITestV20RouterJSON(test_cli20.CLITestV20Base):
"""Create router: --distributed=false."""
self._create_router_distributed_or_ha(distributed='false')
def test_create_router_with_az_hint(self):
"""Create router: --availability-zone-hint zone1
--availability-zone-hint zone2.
"""
resource = 'router'
cmd = router.CreateRouter(test_cli20.MyApp(sys.stdout), None)
name = 'myname'
myid = 'myid'
args = ['--availability-zone-hint', 'zone1',
'--availability-zone-hint', 'zone2', name]
position_names = ['availability_zone_hints', 'name']
position_values = [['zone1', 'zone2'], name]
self._test_create_resource(resource, cmd, name, myid, args,
position_names, position_values)
def test_list_routers_detail(self):
"""list routers: -D."""
resources = "routers"

View File

@ -435,6 +435,7 @@ class Client(ClientBase):
service_profile_path = "/service_profiles/%s"
flavor_profile_bindings_path = flavor_path + service_profiles_path
flavor_profile_binding_path = flavor_path + service_profile_path
availability_zones_path = "/availability_zones"
# API has no way to report plurals, so we have to hard code them
EXTED_PLURALS = {'routers': 'router',
@ -1729,6 +1730,12 @@ class Client(ClientBase):
return self.put(self.service_profile_path % (service_profile),
body=body)
@APIParamsCall
def list_availability_zones(self, retrieve_all=True, **_params):
"""Fetches a list of all availability zones."""
return self.list('availability_zones', self.availability_zones_path,
retrieve_all, **_params)
def __init__(self, **kwargs):
"""Initialize a new client for the Neutron v2.0 API."""
super(Client, self).__init__(**kwargs)