Browse Source

Allow OS_BAREMETAL_API_VERSION=latest to work

Allow OS_BAREMETAL_API_VERSION=latest to work. If
OS_BAREMETAL_API_VERSION=latest then convert to the latest version and
set that as the 'default' value for the os_baremetal_api_version
value.

Remove the LOG.debug() message about setting to latest as the message
never appears in the logfile. As tested with:
  $ openstack --debug --log-file log.txt baremetal node list --os-baremetal-api-version latest

Also it was set to always log the message.

Change-Id: Idc3de8ae55e8267342633668a06d18b45c9e5f0a
Closes-Bug: #1712935
(cherry picked from commit 9d8afb38e8)
John L. Villalovos 1 year ago
parent
commit
9e718ab9ee

+ 13
- 8
ironicclient/osc/plugin.py View File

@@ -27,6 +27,7 @@ LOG = logging.getLogger(__name__)
27 27
 API_VERSION_OPTION = 'os_baremetal_api_version'
28 28
 API_NAME = 'baremetal'
29 29
 LAST_KNOWN_API_VERSION = 34
30
+LATEST_VERSION = "1.{}".format(LAST_KNOWN_API_VERSION)
30 31
 API_VERSIONS = {
31 32
     '1.%d' % i: 'ironicclient.v1.client.Client'
32 33
     for i in range(1, LAST_KNOWN_API_VERSION + 1)
@@ -75,9 +76,7 @@ def build_option_parser(parser):
75 76
     parser.add_argument(
76 77
         '--os-baremetal-api-version',
77 78
         metavar='<baremetal-api-version>',
78
-        default=utils.env(
79
-            'OS_BAREMETAL_API_VERSION',
80
-            default=http.DEFAULT_VER),
79
+        default=_get_environment_version(http.DEFAULT_VER),
81 80
         choices=sorted(
82 81
             API_VERSIONS,
83 82
             key=lambda k: [int(x) for x in k.split('.')]) + ['latest'],
@@ -92,14 +91,20 @@ def build_option_parser(parser):
92 91
     return parser
93 92
 
94 93
 
94
+def _get_environment_version(default):
95
+    env_value = utils.env('OS_BAREMETAL_API_VERSION')
96
+    if not env_value:
97
+        return default
98
+    if env_value == 'latest':
99
+        env_value = LATEST_VERSION
100
+    return env_value
101
+
102
+
95 103
 class ReplaceLatestVersion(argparse.Action):
96 104
     """Replaces `latest` keyword by last known version."""
97 105
     def __call__(self, parser, namespace, values, option_string=None):
98 106
         global OS_BAREMETAL_API_VERSION_SPECIFIED
99 107
         OS_BAREMETAL_API_VERSION_SPECIFIED = True
100
-        latest = values == 'latest'
101
-        if latest:
102
-            values = '1.%d' % LAST_KNOWN_API_VERSION
103
-        LOG.debug("Replacing 'latest' API version with the "
104
-                  "latest known version '%s'", values)
108
+        if values == 'latest':
109
+            values = LATEST_VERSION
105 110
         setattr(namespace, self.dest, values)

+ 33
- 1
ironicclient/tests/unit/osc/test_plugin.py View File

@@ -23,6 +23,7 @@ from ironicclient.v1 import client
23 23
 
24 24
 class MakeClientTest(testtools.TestCase):
25 25
 
26
+    @mock.patch.object(plugin.utils, 'env', lambda x: None)
26 27
     @mock.patch.object(plugin, 'OS_BAREMETAL_API_VERSION_SPECIFIED', new=True)
27 28
     @mock.patch.object(plugin.LOG, 'warning', autospec=True)
28 29
     @mock.patch.object(client, 'Client', autospec=True)
@@ -40,6 +41,7 @@ class MakeClientTest(testtools.TestCase):
40 41
             'baremetal', region_name=instance._region_name,
41 42
             interface=instance.interface)
42 43
 
44
+    @mock.patch.object(plugin.utils, 'env', lambda x: None)
43 45
     @mock.patch.object(plugin, 'OS_BAREMETAL_API_VERSION_SPECIFIED', new=False)
44 46
     @mock.patch.object(plugin.LOG, 'warning', autospec=True)
45 47
     @mock.patch.object(client, 'Client', autospec=True)
@@ -75,6 +77,7 @@ class MakeClientTest(testtools.TestCase):
75 77
 
76 78
 class BuildOptionParserTest(testtools.TestCase):
77 79
 
80
+    @mock.patch.object(plugin.utils, 'env', lambda x: None)
78 81
     @mock.patch.object(argparse.ArgumentParser, 'add_argument')
79 82
     def test_build_option_parser(self, mock_add_argument):
80 83
         parser = argparse.ArgumentParser()
@@ -87,19 +90,48 @@ class BuildOptionParserTest(testtools.TestCase):
87 90
             choices=version_list, default=http.DEFAULT_VER, help=mock.ANY,
88 91
             metavar='<baremetal-api-version>')
89 92
 
93
+    @mock.patch.object(plugin.utils, 'env', lambda x: "latest")
94
+    @mock.patch.object(argparse.ArgumentParser, 'add_argument')
95
+    def test_build_option_parser_env_latest(self, mock_add_argument):
96
+        parser = argparse.ArgumentParser()
97
+        mock_add_argument.reset_mock()
98
+        plugin.build_option_parser(parser)
99
+        version_list = ['1'] + ['1.%d' % i for i in range(
100
+            1, plugin.LAST_KNOWN_API_VERSION + 1)] + ['latest']
101
+        mock_add_argument.assert_called_once_with(
102
+            '--os-baremetal-api-version', action=plugin.ReplaceLatestVersion,
103
+            choices=version_list, default=plugin.LATEST_VERSION, help=mock.ANY,
104
+            metavar='<baremetal-api-version>')
105
+
106
+    @mock.patch.object(plugin.utils, 'env')
107
+    def test__get_environment_version(self, mock_utils_env):
108
+        mock_utils_env.return_value = 'latest'
109
+        result = plugin._get_environment_version(None)
110
+        self.assertEqual(plugin.LATEST_VERSION, result)
111
+
112
+        mock_utils_env.return_value = None
113
+        result = plugin._get_environment_version('1.22')
114
+        self.assertEqual("1.22", result)
115
+
116
+        mock_utils_env.return_value = "1.23"
117
+        result = plugin._get_environment_version('1.9')
118
+        self.assertEqual("1.23", result)
119
+
90 120
 
91 121
 class ReplaceLatestVersionTest(testtools.TestCase):
92 122
 
123
+    @mock.patch.object(plugin.utils, 'env', lambda x: None)
93 124
     def test___call___latest(self):
94 125
         parser = argparse.ArgumentParser()
95 126
         plugin.build_option_parser(parser)
96 127
         namespace = argparse.Namespace()
97 128
         parser.parse_known_args(['--os-baremetal-api-version', 'latest'],
98 129
                                 namespace)
99
-        self.assertEqual('1.%d' % plugin.LAST_KNOWN_API_VERSION,
130
+        self.assertEqual(plugin.LATEST_VERSION,
100 131
                          namespace.os_baremetal_api_version)
101 132
         self.assertTrue(plugin.OS_BAREMETAL_API_VERSION_SPECIFIED)
102 133
 
134
+    @mock.patch.object(plugin.utils, 'env', lambda x: None)
103 135
     def test___call___specific_version(self):
104 136
         parser = argparse.ArgumentParser()
105 137
         plugin.build_option_parser(parser)

+ 6
- 0
releasenotes/notes/bug-1712935-allow-os_baremetal_api_version_env_var_to_be_latest-28c8eed24f389673.yaml View File

@@ -0,0 +1,6 @@
1
+---
2
+fixes:
3
+  - |
4
+    Fix issue where doing ``export OS_BAREMETAL_API_VERSION=latest`` would
5
+    cause the ``openstack baremetal`` commands to fail.  See `bug 1712935
6
+    <https://bugs.launchpad.net/ironic/+bug/1712935>`_.

Loading…
Cancel
Save