From ed1ebaa36cb4a40723631b7de330e385082d5e7d Mon Sep 17 00:00:00 2001 From: melanie witt Date: Mon, 12 Dec 2016 23:15:43 +0000 Subject: [PATCH] Let nova-manage cell_v2 commands use transport_url from CONF The nova-manage cell_v2 simple_cell_setup command currently requires a --transport-url to be passed on the command line. It should be able to use the [DEFAULT]/transport_url setting from nova.conf if present. This makes --transport-url optional and uses it if specified. If not specified, it looks at [DEFAULT]/transport_url, and finally raises an error if neither were found. Change-Id: Idfda66d161545a20f3102c083a7198720bf54e6b (cherry picked from commit 4560d11365add93b6aea6f3451d2a1e513a4e03e) --- nova/cmd/manage.py | 21 +++++++++++++-------- nova/tests/unit/test_nova_manage.py | 20 ++++++++++++++++++++ 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/nova/cmd/manage.py b/nova/cmd/manage.py index 83e523935108..c799e55104f1 100644 --- a/nova/cmd/manage.py +++ b/nova/cmd/manage.py @@ -1201,12 +1201,16 @@ class CellCommands(object): class CellV2Commands(object): """Commands for managing cells v2.""" - # TODO(melwitt): Remove this when the oslo.messaging function - # for assembling a transport url from ConfigOpts is available - @args('--transport-url', metavar='', required=True, - dest='transport_url', + def _validate_transport_url(self, transport_url): + transport_url = transport_url or CONF.transport_url + if not transport_url: + print('Must specify --transport-url if [DEFAULT]/transport_url ' + 'is not set in the configuration file.') + return transport_url + + @args('--transport-url', metavar='', dest='transport_url', help='The transport url for the cell message queue') - def simple_cell_setup(self, transport_url): + def simple_cell_setup(self, transport_url=None): """Simple cellsv2 setup. This simplified command is for use by existing non-cells users to @@ -1218,6 +1222,9 @@ class CellV2Commands(object): if CONF.cells.enable: print('CellsV1 users cannot use this simplified setup command') return 2 + transport_url = self._validate_transport_url(transport_url) + if not transport_url: + return 1 ctxt = context.RequestContext() try: cell0_mapping = self.map_cell0() @@ -1430,10 +1437,8 @@ class CellV2Commands(object): nova-manage cell_v2 map_cell_and_hosts --config-file """ - transport_url = CONF.transport_url or transport_url + transport_url = self._validate_transport_url(transport_url) if not transport_url: - print('Must specify --transport-url if [DEFAULT]/transport_url ' - 'is not set in the configuration file.') return 1 self._map_cell_and_hosts(transport_url, name, verbose) # online_data_migrations established a pattern of 0 meaning everything diff --git a/nova/tests/unit/test_nova_manage.py b/nova/tests/unit/test_nova_manage.py index ddb5bc010b46..f1f01c728eed 100644 --- a/nova/tests/unit/test_nova_manage.py +++ b/nova/tests/unit/test_nova_manage.py @@ -1348,3 +1348,23 @@ class CellV2CommandsTestCase(test.TestCase): mock_cell_mapping_get_by_uuid.assert_not_called() mock_cell_mapping_get_all.assert_called_once_with( test.MatchType(context.RequestContext)) + + def test_validate_transport_url_in_conf(self): + from_conf = 'fake://user:pass@host:port/' + self.flags(transport_url=from_conf) + self.assertEqual(from_conf, + self.commands._validate_transport_url(None)) + + def test_validate_transport_url_on_command_line(self): + from_cli = 'fake://user:pass@host:port/' + self.assertEqual(from_cli, + self.commands._validate_transport_url(from_cli)) + + def test_validate_transport_url_missing(self): + self.assertIsNone(self.commands._validate_transport_url(None)) + + def test_validate_transport_url_favors_command_line(self): + self.flags(transport_url='fake://user:pass@host:port/') + from_cli = 'fake://otheruser:otherpass@otherhost:otherport' + self.assertEqual(from_cli, + self.commands._validate_transport_url(from_cli))