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
This commit is contained in:
Galyna Zholtkevych 2017-01-25 13:38:32 +02:00 committed by Vladyslav Drok
parent 6702a7de1f
commit be5c99cd11
3 changed files with 63 additions and 2 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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.