Use nargs for parsing multilple values in fuel2
That allows to do more control on command line arguments by configuring argparse instead of doing that manually. Closes-bug: #1441130 Change-Id: Ia89bb3f2d164d977b52f7201748053314ae135f3
This commit is contained in:
@@ -63,8 +63,11 @@ class BaseListCommand(lister.Lister, BaseCommand):
|
||||
group.add_argument('-s',
|
||||
'--sort-columns',
|
||||
type=str,
|
||||
default='id',
|
||||
help='Comma separated list of keys for sorting '
|
||||
nargs='+',
|
||||
choices=self.columns,
|
||||
metavar='SORT_COLUMN',
|
||||
default=['id'],
|
||||
help='Space separated list of keys for sorting '
|
||||
'the data. Defaults to id. Wrong values '
|
||||
'are ignored.')
|
||||
|
||||
@@ -74,11 +77,8 @@ class BaseListCommand(lister.Lister, BaseCommand):
|
||||
data = self.client.get_all()
|
||||
data = data_utils.get_display_data_multi(self.columns, data)
|
||||
|
||||
sort_columns = parsed_args.sort_columns.split(',')
|
||||
|
||||
scolumn_ids = [self.columns.index(col)
|
||||
for col in sort_columns
|
||||
if col in self.columns]
|
||||
for col in parsed_args.sort_columns]
|
||||
|
||||
data.sort(key=lambda x: [x[scolumn_id] for scolumn_id in scolumn_ids])
|
||||
|
||||
|
||||
@@ -14,7 +14,6 @@
|
||||
|
||||
from cliff import show
|
||||
|
||||
from fuelclient.cli import error
|
||||
from fuelclient.commands import base
|
||||
from fuelclient.common import data_utils
|
||||
|
||||
@@ -182,13 +181,15 @@ class EnvAddNodes(EnvMixIn, base.BaseCommand):
|
||||
|
||||
parser.add_argument('-n',
|
||||
'--nodes',
|
||||
type=str,
|
||||
type=int,
|
||||
nargs='+',
|
||||
required=True,
|
||||
help='Ids of the nodes to add.')
|
||||
|
||||
parser.add_argument('-r',
|
||||
'--roles',
|
||||
type=str,
|
||||
nargs='+',
|
||||
required=True,
|
||||
help='Target roles of the nodes.')
|
||||
|
||||
@@ -196,15 +197,10 @@ class EnvAddNodes(EnvMixIn, base.BaseCommand):
|
||||
|
||||
def take_action(self, parsed_args):
|
||||
env_id = parsed_args.env
|
||||
try:
|
||||
nodes = [int(n) for n in parsed_args.nodes.split(',')]
|
||||
except ValueError:
|
||||
raise error.ArgumentException('All node ids should be '
|
||||
'integer numbers.')
|
||||
|
||||
roles = parsed_args.roles.split(',')
|
||||
|
||||
self.client.add_nodes(environment_id=env_id, nodes=nodes, roles=roles)
|
||||
self.client.add_nodes(environment_id=env_id,
|
||||
nodes=parsed_args.nodes,
|
||||
roles=parsed_args.roles)
|
||||
|
||||
msg = 'Nodes {n} were added to the environment {e} with roles {r}\n'
|
||||
self.app.stdout.write(msg.format(n=parsed_args.nodes,
|
||||
|
||||
@@ -20,6 +20,7 @@ import mock
|
||||
|
||||
import fuelclient
|
||||
from fuelclient.cli import error
|
||||
from fuelclient.commands import environment as env
|
||||
from fuelclient import main as main_mod
|
||||
from fuelclient.tests import base
|
||||
|
||||
@@ -89,3 +90,40 @@ class BaseCLITest(base.UnitTestCase):
|
||||
|
||||
self.exec_command_interactive(commands)
|
||||
m_find_command.assert_called_once_with(commands)
|
||||
|
||||
@mock.patch('cliff.formatters.table.TableFormatter.emit_list')
|
||||
def test_lister_sorting(self, m_emit_list):
|
||||
cmd = 'env list -s status net_provider'
|
||||
|
||||
raw_data = [{'id': 43,
|
||||
'status': 'STATUS 2',
|
||||
'name': 'Test env 2',
|
||||
'mode': 'ha_compact',
|
||||
'release_id': 2,
|
||||
'net_provider': 'nova'},
|
||||
|
||||
{'id': 42,
|
||||
'status': 'STATUS 1',
|
||||
'name': 'Test env 1',
|
||||
'mode': 'ha_compact',
|
||||
'release_id': 2,
|
||||
'net_provider': 'nova'},
|
||||
|
||||
{'id': 44,
|
||||
'status': 'STATUS 2',
|
||||
'name': 'Test env 3',
|
||||
'mode': 'ha_compact',
|
||||
'release_id': 2,
|
||||
'net_provider': 'neutron'}]
|
||||
|
||||
expected_order = [1, 2, 0]
|
||||
expected_data = [[raw_data[i][prop] for prop in env.EnvList.columns]
|
||||
for i in expected_order]
|
||||
|
||||
self.m_client.get_all.return_value = raw_data
|
||||
|
||||
self.exec_command(cmd)
|
||||
m_emit_list.assert_called_once_with(mock.ANY,
|
||||
expected_data,
|
||||
mock.ANY,
|
||||
mock.ANY)
|
||||
|
||||
@@ -64,7 +64,7 @@ class TestEnvCommand(test_engine.BaseCLITest):
|
||||
self.m_client.deploy_changes.assert_called_once_with(42)
|
||||
|
||||
def test_env_add_nodes(self):
|
||||
args = 'env add nodes -e 42 -n 24,25 -r compute,cinder'
|
||||
args = 'env add nodes -e 42 -n 24 25 -r compute cinder'
|
||||
self.exec_command(args)
|
||||
|
||||
self.m_get_client.assert_called_once_with('environment', mock.ANY)
|
||||
|
||||
Reference in New Issue
Block a user