diff options
| author | Galyna Zholtkevych <gzholtkevych@mirantis.com> | 2017-01-25 13:38:32 +0200 |
|---|---|---|
| committer | Vladyslav Drok <vdrok@mirantis.com> | 2017-01-25 19:15:24 +0200 |
| commit | be5c99cd11b5690bec1b34e9edc5b4bc496d7158 (patch) | |
| tree | f7209663cb86f575ee7a5038fb7a608648244b7f | |
| parent | 6702a7de1feafe455752148d2b14ec01e4aaf157 (diff) | |
| download | python-ironicclient-be5c99cd11b5690bec1b34e9edc5b4bc496d7158.tar.gz | |
Support --os-baremetal-api-version latest
For OSC commands, the --os-baremetal-api-version
optional argument does not have the value 'latest'.
Unlike the ironic CLI osc_lib does not support passing
'latest', so this is replaced by latest known API version
which is used in requests to the Bare Metal service.
Change-Id: Id98d1ab4b6dd1fdfb41d3a4fdd384baf75e69d6f
Closes-Bug: #1657502
| -rw-r--r-- | ironicclient/osc/plugin.py | 21 | ||||
| -rw-r--r-- | ironicclient/tests/unit/osc/test_plugin.py | 38 | ||||
| -rw-r--r-- | releasenotes/notes/latest-baremetal-api-version-a20e3099e3b97a1b.yaml | 6 |
3 files changed, 63 insertions, 2 deletions
diff --git a/ironicclient/osc/plugin.py b/ironicclient/osc/plugin.py index d23cf68..ce5c764 100644 --- a/ironicclient/osc/plugin.py +++ b/ironicclient/osc/plugin.py @@ -16,6 +16,7 @@ """OpenStackClient plugin for Bare Metal service.""" +import argparse import logging from ironicclient.common import http @@ -53,7 +54,6 @@ def make_client(instance): region_name=instance._region_name ) ) - return client @@ -65,7 +65,24 @@ def build_option_parser(parser): default=utils.env( 'OS_BAREMETAL_API_VERSION', default=http.DEFAULT_VER), + choices=sorted( + API_VERSIONS, + key=lambda k: [int(x) for x in k.split('.')]) + ['latest'], + action=ReplaceLatestVersion, help='Baremetal API version, default=' + http.DEFAULT_VER + - ' (Env: OS_BAREMETAL_API_VERSION)') + ' (Env: OS_BAREMETAL_API_VERSION). ' + '"latest" is the latest known API version', + ) return parser + + +class ReplaceLatestVersion(argparse.Action): + """Replaces `latest` keyword by last known version.""" + def __call__(self, parser, namespace, values, option_string=None): + latest = values == 'latest' + if latest: + values = '1.%d' % LAST_KNOWN_API_VERSION + LOG.debug("Replacing 'latest' API version with the " + "latest known version '%s'", values) + setattr(namespace, self.dest, values) diff --git a/ironicclient/tests/unit/osc/test_plugin.py b/ironicclient/tests/unit/osc/test_plugin.py index a08e35f..a86f5fe 100644 --- a/ironicclient/tests/unit/osc/test_plugin.py +++ b/ironicclient/tests/unit/osc/test_plugin.py @@ -10,9 +10,12 @@ # License for the specific language governing permissions and limitations # under the License. +import argparse + import mock import testtools +from ironicclient.common import http from ironicclient.osc import plugin from ironicclient.tests.unit.osc import fakes from ironicclient.v1 import client @@ -33,3 +36,38 @@ class MakeClientTest(testtools.TestCase): instance.get_endpoint_for_service_type.assert_called_once_with( 'baremetal', region_name=instance._region_name, interface=instance.interface) + + +class BuildOptionParserTest(testtools.TestCase): + + @mock.patch.object(argparse.ArgumentParser, 'add_argument') + def test_build_option_parser(self, mock_add_argument): + parser = argparse.ArgumentParser() + mock_add_argument.reset_mock() + plugin.build_option_parser(parser) + version_list = ['1'] + ['1.%d' % i for i in range( + 1, plugin.LAST_KNOWN_API_VERSION + 1)] + ['latest'] + mock_add_argument.assert_called_once_with( + '--os-baremetal-api-version', action=plugin.ReplaceLatestVersion, + choices=version_list, default=http.DEFAULT_VER, help=mock.ANY, + metavar='<baremetal-api-version>') + + +class ReplaceLatestVersionTest(testtools.TestCase): + + def test___call___latest(self): + parser = argparse.ArgumentParser() + plugin.build_option_parser(parser) + namespace = argparse.Namespace() + parser.parse_known_args(['--os-baremetal-api-version', 'latest'], + namespace) + self.assertEqual('1.%d' % plugin.LAST_KNOWN_API_VERSION, + namespace.os_baremetal_api_version) + + def test___call___specific_version(self): + parser = argparse.ArgumentParser() + plugin.build_option_parser(parser) + namespace = argparse.Namespace() + parser.parse_known_args(['--os-baremetal-api-version', '1.4'], + namespace) + self.assertEqual('1.4', namespace.os_baremetal_api_version) diff --git a/releasenotes/notes/latest-baremetal-api-version-a20e3099e3b97a1b.yaml b/releasenotes/notes/latest-baremetal-api-version-a20e3099e3b97a1b.yaml new file mode 100644 index 0000000..edd3f7b --- /dev/null +++ b/releasenotes/notes/latest-baremetal-api-version-a20e3099e3b97a1b.yaml @@ -0,0 +1,6 @@ +--- +features: + - For OSC commands, the --os-baremetal-api-version optional argument + (or OS_BAREMETAL_API_VERSION environment variable) can have the + value 'latest'. If set to 'latest', the latest API version known by client + will be used in requests to the Bare Metal service. |
