From 24032199edf9e189ebad95264eb38a58a7ee71b1 Mon Sep 17 00:00:00 2001 From: Ruby Loo Date: Wed, 31 Aug 2016 22:57:53 -0400 Subject: [PATCH] Move duplicated info to new v1/utils.py This moves duplicated information to a new v1/utils.py file: - list of HTTP codes - list of boot devices - provision-related information Change-Id: I4a11627359213770dbc8aa037fcf95ed7bfd0870 --- ironicclient/osc/v1/baremetal_driver.py | 6 +-- ironicclient/osc/v1/baremetal_node.py | 13 ++--- .../tests/unit/osc/v1/test_baremetal_node.py | 3 +- ironicclient/tests/unit/v1/test_node_shell.py | 3 +- ironicclient/v1/driver_shell.py | 9 ++-- ironicclient/v1/node_shell.py | 49 ++++--------------- ironicclient/v1/utils.py | 48 ++++++++++++++++++ 7 files changed, 75 insertions(+), 56 deletions(-) create mode 100644 ironicclient/v1/utils.py diff --git a/ironicclient/osc/v1/baremetal_driver.py b/ironicclient/osc/v1/baremetal_driver.py index 07cb72ba1..c6deac82b 100644 --- a/ironicclient/osc/v1/baremetal_driver.py +++ b/ironicclient/osc/v1/baremetal_driver.py @@ -21,6 +21,7 @@ from osc_lib import utils as oscutils from ironicclient.common import utils from ironicclient.v1 import resource_fields as res_fields +from ironicclient.v1 import utils as v1_utils class ListBaremetalDriver(command.Lister): @@ -52,7 +53,6 @@ class PassthruCallBaremetalDriver(command.ShowOne): """Call a vendor passthru method for a driver.""" log = logging.getLogger(__name__ + ".PassthruCallBaremetalDriver") - http_methods = ['POST', 'PUT', 'GET', 'DELETE', 'PATCH'] def get_parser(self, prog_name): parser = super(PassthruCallBaremetalDriver, self).get_parser(prog_name) @@ -77,11 +77,11 @@ class PassthruCallBaremetalDriver(command.ShowOne): '--http-method', dest='http_method', metavar='', - choices=self.http_methods, + choices=v1_utils.HTTP_METHODS, default='POST', help="The HTTP method to use in the passthru request. One of " "%s. Defaults to 'POST'." % - oscutils.format_list(self.http_methods) + oscutils.format_list(v1_utils.HTTP_METHODS) ) return parser diff --git a/ironicclient/osc/v1/baremetal_node.py b/ironicclient/osc/v1/baremetal_node.py index 13ab2e01a..eb0565a49 100644 --- a/ironicclient/osc/v1/baremetal_node.py +++ b/ironicclient/osc/v1/baremetal_node.py @@ -25,6 +25,7 @@ from ironicclient.common.i18n import _ from ironicclient.common import utils from ironicclient import exc from ironicclient.v1 import resource_fields as res_fields +from ironicclient.v1 import utils as v1_utils class ProvisionStateBaremetalNode(command.Command): @@ -77,8 +78,6 @@ class BootdeviceSetBaremetalNode(command.Command): log = logging.getLogger(__name__ + ".BootdeviceSetBaremetalNode") - BOOT_DEVICES = ['pxe', 'disk', 'cdrom', 'bios', 'safe'] - def get_parser(self, prog_name): parser = super(BootdeviceSetBaremetalNode, self).get_parser(prog_name) @@ -90,8 +89,8 @@ class BootdeviceSetBaremetalNode(command.Command): parser.add_argument( 'device', metavar='', - choices=self.BOOT_DEVICES, - help="One of %s" % (oscutils.format_list(self.BOOT_DEVICES)) + choices=v1_utils.BOOT_DEVICES, + help="One of %s" % (oscutils.format_list(v1_utils.BOOT_DEVICES)) ) parser.add_argument( '--persistent', @@ -623,8 +622,6 @@ class PassthruCallBaremetalNode(command.Command): log = logging.getLogger(__name__ + ".PassthuCallBaremetalNode") - HTTP_METHODS = ['POST', 'PUT', 'GET', 'DELETE', 'PATCH'] - def get_parser(self, prog_name): parser = super(PassthruCallBaremetalNode, self).get_parser( prog_name) @@ -649,11 +646,11 @@ class PassthruCallBaremetalNode(command.Command): parser.add_argument( '--http-method', metavar='', - choices=self.HTTP_METHODS, + choices=v1_utils.HTTP_METHODS, default='POST', help="The HTTP method to use in the passthru request. One of " "%s. Defaults to POST." % - oscutils.format_list(self.HTTP_METHODS) + oscutils.format_list(v1_utils.HTTP_METHODS) ) return parser diff --git a/ironicclient/tests/unit/osc/v1/test_baremetal_node.py b/ironicclient/tests/unit/osc/v1/test_baremetal_node.py index ab7b73808..60d4e70f1 100644 --- a/ironicclient/tests/unit/osc/v1/test_baremetal_node.py +++ b/ironicclient/tests/unit/osc/v1/test_baremetal_node.py @@ -23,6 +23,7 @@ from ironicclient.common import utils as commonutils from ironicclient import exc from ironicclient.osc.v1 import baremetal_node from ironicclient.tests.unit.osc.v1 import fakes as baremetal_fakes +from ironicclient.v1 import utils as v1_utils class TestBaremetal(baremetal_fakes.TestBaremetal): @@ -118,7 +119,7 @@ class TestBootdeviceShow(TestBaremetal): "boot_device": "pxe", "persistent": False} self.baremetal_mock.node.get_supported_boot_devices.return_value = { - "supported_boot_devices": ["cdrom", "bios", "safe", "disk", "pxe"]} + "supported_boot_devices": v1_utils.BOOT_DEVICES} def test_bootdevice_show(self): arglist = ['node_uuid'] diff --git a/ironicclient/tests/unit/v1/test_node_shell.py b/ironicclient/tests/unit/v1/test_node_shell.py index 590a97608..d4db010de 100644 --- a/ironicclient/tests/unit/v1/test_node_shell.py +++ b/ironicclient/tests/unit/v1/test_node_shell.py @@ -23,6 +23,7 @@ from ironicclient.common import utils as commonutils from ironicclient import exc from ironicclient.tests.unit import utils import ironicclient.v1.node_shell as n_shell +import ironicclient.v1.utils as v1_utils class NodeShellTest(utils.BaseTestCase): @@ -507,7 +508,7 @@ class NodeShellTest(utils.BaseTestCase): 'node_uuid', 'active', configdrive='foo', cleansteps=None) client_mock.node.wait_for_provision_state.assert_called_once_with( 'node_uuid', expected_state='active', timeout=0, - poll_interval=n_shell._LONG_ACTION_POLL_INTERVAL) + poll_interval=v1_utils._LONG_ACTION_POLL_INTERVAL) def test_do_node_set_provision_state_deleted(self): client_mock = mock.MagicMock() diff --git a/ironicclient/v1/driver_shell.py b/ironicclient/v1/driver_shell.py index 7422af88e..a6ac546a8 100644 --- a/ironicclient/v1/driver_shell.py +++ b/ironicclient/v1/driver_shell.py @@ -18,6 +18,7 @@ import argparse from ironicclient.common import cliutils from ironicclient.common import utils from ironicclient.v1 import resource_fields as res_fields +from ironicclient.v1 import utils as v1_utils def _print_driver_show(driver, json=False): @@ -95,10 +96,10 @@ def do_driver_raid_logical_disk_properties(cc, args): "Can be specified multiple times.") @cliutils.arg('--http-method', metavar='', - choices=['POST', 'PUT', 'GET', 'DELETE', 'PATCH'], - help="The HTTP method to use in the request. Valid HTTP " - "methods are: 'POST', 'PUT', 'GET', 'DELETE', and 'PATCH'. " - "Defaults to 'POST'.") + choices=v1_utils.HTTP_METHODS, + help=("The HTTP method to use in the request. Valid HTTP " + "methods are: %s. Defaults to 'POST'." % + ', '.join(v1_utils.HTTP_METHODS))) @cliutils.arg('--http_method', help=argparse.SUPPRESS) def do_driver_vendor_passthru(cc, args): diff --git a/ironicclient/v1/node_shell.py b/ironicclient/v1/node_shell.py index 82e97dc3f..6b7fb933b 100644 --- a/ironicclient/v1/node_shell.py +++ b/ironicclient/v1/node_shell.py @@ -21,35 +21,7 @@ from ironicclient.common.i18n import _ from ironicclient.common import utils from ironicclient import exc from ironicclient.v1 import resource_fields as res_fields - - -# Polling intervals in seconds. -_LONG_ACTION_POLL_INTERVAL = 10 -_SHORT_ACTION_POLL_INTERVAL = 2 -# This dict acts as both list of possible provision actions and arguments for -# wait_for_provision_state invocation. -PROVISION_ACTIONS = { - 'active': {'expected_state': 'active', - 'poll_interval': _LONG_ACTION_POLL_INTERVAL}, - 'deleted': {'expected_state': 'available', - 'poll_interval': _LONG_ACTION_POLL_INTERVAL}, - 'rebuild': {'expected_state': 'active', - 'poll_interval': _LONG_ACTION_POLL_INTERVAL}, - 'inspect': {'expected_state': 'manageable', - # This is suboptimal for in-band inspection, but it's probably - # not worth making people wait 10 seconds for OOB inspection - 'poll_interval': _SHORT_ACTION_POLL_INTERVAL}, - 'provide': {'expected_state': 'available', - # This assumes cleaning is in place - 'poll_interval': _LONG_ACTION_POLL_INTERVAL}, - 'manage': {'expected_state': 'manageable', - 'poll_interval': _SHORT_ACTION_POLL_INTERVAL}, - 'clean': {'expected_state': 'manageable', - 'poll_interval': _LONG_ACTION_POLL_INTERVAL}, - 'adopt': {'expected_state': 'active', - 'poll_interval': _SHORT_ACTION_POLL_INTERVAL}, - 'abort': None, # no support for --wait in abort -} +from ironicclient.v1 import utils as v1_utils def _print_node_show(node, fields=None, json=False): @@ -323,10 +295,10 @@ def do_node_update(cc, args): "be specified multiple times.")) @cliutils.arg('--http-method', metavar='', - choices=['POST', 'PUT', 'GET', 'DELETE', 'PATCH'], - help="The HTTP method to use in the request. Valid HTTP " - "methods are: 'POST', 'PUT', 'GET', 'DELETE', and 'PATCH'. " - "Defaults to 'POST'.") + choices=v1_utils.HTTP_METHODS, + help=("The HTTP method to use in the request. Valid HTTP " + "methods are: %s. Defaults to 'POST'." % + ', '.join(v1_utils.HTTP_METHODS))) @cliutils.arg('--http_method', help=argparse.SUPPRESS) def do_node_vendor_passthru(cc, args): @@ -468,9 +440,8 @@ def do_node_set_target_raid_config(cc, args): @cliutils.arg( 'provision_state', metavar='', - choices=list(PROVISION_ACTIONS), - help="Supported states: %s." % ', '.join("'%s'" % state - for state in PROVISION_ACTIONS)) + choices=v1_utils.PROVISION_STATES, + help="Supported states: %s." % ', '.join(v1_utils.PROVISION_STATES)) @cliutils.arg( '--config-drive', metavar='', @@ -516,7 +487,7 @@ def do_node_set_provision_state(cc, args): 'setting provision state to "clean"')) if args.wait_timeout is not None: - wait_args = PROVISION_ACTIONS.get(args.provision_state) + wait_args = v1_utils.PROVISION_ACTIONS.get(args.provision_state) if wait_args is None: raise exceptions.CommandError( _("--wait is not supported for provision state '%s'") @@ -574,8 +545,8 @@ def do_node_set_console_mode(cc, args): @cliutils.arg( 'device', metavar='', - choices=['pxe', 'disk', 'cdrom', 'bios', 'safe'], - help="'pxe', 'disk', 'cdrom', 'bios', or 'safe'.") + choices=v1_utils.BOOT_DEVICES, + help="One of %s." % ', '.join(v1_utils.BOOT_DEVICES)) @cliutils.arg( '--persistent', dest='persistent', diff --git a/ironicclient/v1/utils.py b/ironicclient/v1/utils.py new file mode 100644 index 000000000..7c18e60d8 --- /dev/null +++ b/ironicclient/v1/utils.py @@ -0,0 +1,48 @@ +# Copyright 2016 Intel Corporation +# 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. + +HTTP_METHODS = ['POST', 'PUT', 'GET', 'DELETE', 'PATCH'] + +BOOT_DEVICES = ['pxe', 'disk', 'cdrom', 'bios', 'safe'] + +# Polling intervals in seconds. +_LONG_ACTION_POLL_INTERVAL = 10 +_SHORT_ACTION_POLL_INTERVAL = 2 +# This dict acts as both list of possible provision actions and arguments for +# wait_for_provision_state invocation. +PROVISION_ACTIONS = { + 'active': {'expected_state': 'active', + 'poll_interval': _LONG_ACTION_POLL_INTERVAL}, + 'deleted': {'expected_state': 'available', + 'poll_interval': _LONG_ACTION_POLL_INTERVAL}, + 'rebuild': {'expected_state': 'active', + 'poll_interval': _LONG_ACTION_POLL_INTERVAL}, + 'inspect': {'expected_state': 'manageable', + # This is suboptimal for in-band inspection, but it's probably + # not worth making people wait 10 seconds for OOB inspection + 'poll_interval': _SHORT_ACTION_POLL_INTERVAL}, + 'provide': {'expected_state': 'available', + # This assumes cleaning is in place + 'poll_interval': _LONG_ACTION_POLL_INTERVAL}, + 'manage': {'expected_state': 'manageable', + 'poll_interval': _SHORT_ACTION_POLL_INTERVAL}, + 'clean': {'expected_state': 'manageable', + 'poll_interval': _LONG_ACTION_POLL_INTERVAL}, + 'adopt': {'expected_state': 'active', + 'poll_interval': _SHORT_ACTION_POLL_INTERVAL}, + 'abort': None, # no support for --wait in abort +} + +PROVISION_STATES = list(PROVISION_ACTIONS)