Skip to content

Commit d0112a8

Browse files
committed
compute: Add missing options for 'server list'
This accepts a large number of options that we weren't exposing. Add the following options: '--availability-zone', '--key-name', '--config-drive' and '--no-config-drive', '--progress', '--vm-state', '--task-state' and '--power-state'. In addition, refine the 'openstack server list --status' parameter to restrict users to the actual choices supported by the server. Change-Id: Ieeb1f22df7092e66a411b6a36eafb3e16efc2fc2 Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
1 parent 03776d8 commit d0112a8

3 files changed

Lines changed: 361 additions & 12 deletions

File tree

openstackclient/compute/v2/server.py

Lines changed: 207 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,18 +1344,18 @@ def _show_progress(progress):
13441344
raise SystemExit
13451345

13461346

1347+
def percent_type(x):
1348+
x = int(x)
1349+
if not 0 < x <= 100:
1350+
raise argparse.ArgumentTypeError("Must be between 0 and 100")
1351+
return x
1352+
1353+
13471354
class ListServer(command.Lister):
13481355
_description = _("List servers")
13491356

13501357
def get_parser(self, prog_name):
13511358
parser = super(ListServer, self).get_parser(prog_name)
1352-
parser.add_argument(
1353-
'--availability-zone',
1354-
metavar='<availability-zone>',
1355-
help=_('Only return instances that match the availability zone. '
1356-
'Note that this option will be ignored for non-admin users '
1357-
'when using ``--os-compute-api-version`` prior to 2.83.'),
1358-
)
13591359
parser.add_argument(
13601360
'--reservation-id',
13611361
metavar='<reservation-id>',
@@ -1385,10 +1385,35 @@ def get_parser(self, prog_name):
13851385
metavar='<server-name>',
13861386
help=_('Regular expression to match instance name (admin only)'),
13871387
)
1388+
# taken from 'task_and_vm_state_from_status' function in nova
1389+
# the API sadly reports these in upper case and while it would be
1390+
# wonderful to plaster over this ugliness client-side, there are
1391+
# already users in the wild doing this in upper case that we need to
1392+
# support
13881393
parser.add_argument(
13891394
'--status',
13901395
metavar='<status>',
1391-
# FIXME(dhellmann): Add choices?
1396+
choices=(
1397+
'ACTIVE',
1398+
'BUILD',
1399+
'DELETED',
1400+
'ERROR',
1401+
'HARD_REBOOT',
1402+
'MIGRATING',
1403+
'PASSWORD',
1404+
'PAUSED',
1405+
'REBOOT',
1406+
'REBUILD',
1407+
'RESCUE',
1408+
'RESIZE',
1409+
'REVERT_RESIZE',
1410+
'SHELVED',
1411+
'SHELVED_OFFLOADED',
1412+
'SHUTOFF',
1413+
'SOFT_DELETED',
1414+
'SUSPENDED',
1415+
'VERIFY_RESIZE'
1416+
),
13921417
help=_('Search by server status'),
13931418
)
13941419
parser.add_argument(
@@ -1421,7 +1446,10 @@ def get_parser(self, prog_name):
14211446
parser.add_argument(
14221447
'--user',
14231448
metavar='<user>',
1424-
help=_('Search by user (admin only) (name or ID)'),
1449+
help=_(
1450+
'Search by user (name or ID) '
1451+
'(admin only before microversion 2.83)'
1452+
),
14251453
)
14261454
identity_common.add_user_domain_option_to_parser(parser)
14271455
parser.add_argument(
@@ -1430,6 +1458,146 @@ def get_parser(self, prog_name):
14301458
default=False,
14311459
help=_('Only display deleted servers (admin only)'),
14321460
)
1461+
parser.add_argument(
1462+
'--availability-zone',
1463+
default=None,
1464+
help=_(
1465+
'Search by availability zone '
1466+
'(admin only before microversion 2.83)'
1467+
),
1468+
)
1469+
parser.add_argument(
1470+
'--key-name',
1471+
help=_(
1472+
'Search by keypair name '
1473+
'(admin only before microversion 2.83)'
1474+
),
1475+
)
1476+
config_drive_group = parser.add_mutually_exclusive_group()
1477+
config_drive_group.add_argument(
1478+
'--config-drive',
1479+
action='store_true',
1480+
dest='has_config_drive',
1481+
default=None,
1482+
help=_(
1483+
'Only display servers with a config drive attached '
1484+
'(admin only before microversion 2.83)'
1485+
),
1486+
)
1487+
# NOTE(gibi): this won't actually do anything until bug 1871409 is
1488+
# fixed and the REST API is cleaned up regarding the values of
1489+
# config_drive
1490+
config_drive_group.add_argument(
1491+
'--no-config-drive',
1492+
action='store_false',
1493+
dest='has_config_drive',
1494+
help=_(
1495+
'Only display servers without a config drive attached '
1496+
'(admin only before microversion 2.83)'
1497+
),
1498+
)
1499+
parser.add_argument(
1500+
'--progress',
1501+
type=percent_type,
1502+
default=None,
1503+
help=_(
1504+
'Search by progress value (%%) '
1505+
'(admin only before microversion 2.83)'
1506+
),
1507+
)
1508+
parser.add_argument(
1509+
'--vm-state',
1510+
metavar='<state>',
1511+
# taken from 'InstanceState' object field in nova
1512+
choices=(
1513+
'active',
1514+
'building',
1515+
'deleted',
1516+
'error',
1517+
'paused',
1518+
'stopped',
1519+
'suspended',
1520+
'rescued',
1521+
'resized',
1522+
'shelved',
1523+
'shelved_offloaded',
1524+
'soft-delete',
1525+
),
1526+
help=_(
1527+
'Search by vm_state value '
1528+
'(admin only before microversion 2.83)'
1529+
),
1530+
)
1531+
parser.add_argument(
1532+
'--task-state',
1533+
metavar='<state>',
1534+
# taken from 'InstanceTaskState' object field in nova
1535+
choices=(
1536+
'block_device_mapping',
1537+
'deleting',
1538+
'image_backup',
1539+
'image_pending_upload',
1540+
'image_snapshot',
1541+
'image_snapshot_pending',
1542+
'image_uploading',
1543+
'migrating',
1544+
'networking',
1545+
'pausing',
1546+
'powering-off',
1547+
'powering-on',
1548+
'rebooting',
1549+
'reboot_pending',
1550+
'reboot_started',
1551+
'reboot_pending_hard',
1552+
'reboot_started_hard',
1553+
'rebooting_hard',
1554+
'rebuilding',
1555+
'rebuild_block_device_mapping',
1556+
'rebuild_spawning',
1557+
'rescuing',
1558+
'resize_confirming',
1559+
'resize_finish',
1560+
'resize_migrated',
1561+
'resize_migrating',
1562+
'resize_prep',
1563+
'resize_reverting',
1564+
'restoring',
1565+
'resuming',
1566+
'scheduling',
1567+
'shelving',
1568+
'shelving_image_pending_upload',
1569+
'shelving_image_uploading',
1570+
'shelving_offloading',
1571+
'soft-deleting',
1572+
'spawning',
1573+
'suspending',
1574+
'updating_password',
1575+
'unpausing',
1576+
'unrescuing',
1577+
'unshelving',
1578+
),
1579+
help=_(
1580+
'Search by task_state value '
1581+
'(admin only before microversion 2.83)'
1582+
),
1583+
)
1584+
parser.add_argument(
1585+
'--power-state',
1586+
metavar='<state>',
1587+
# taken from 'InstancePowerState' object field in nova
1588+
choices=(
1589+
'pending',
1590+
'running',
1591+
'paused',
1592+
'shutdown',
1593+
'crashed',
1594+
'suspended',
1595+
),
1596+
help=_(
1597+
'Search by power_state value '
1598+
'(admin only before microversion 2.83)'
1599+
),
1600+
)
14331601
parser.add_argument(
14341602
'--long',
14351603
action='store_true',
@@ -1582,7 +1750,6 @@ def take_action(self, parsed_args):
15821750
ignore_missing=False).id
15831751

15841752
search_opts = {
1585-
'availability_zone': parsed_args.availability_zone,
15861753
'reservation_id': parsed_args.reservation_id,
15871754
'ip': parsed_args.ip,
15881755
'ip6': parsed_args.ip6,
@@ -1600,6 +1767,36 @@ def take_action(self, parsed_args):
16001767
'changes-since': parsed_args.changes_since,
16011768
}
16021769

1770+
if parsed_args.availability_zone:
1771+
search_opts['availability_zone'] = parsed_args.availability_zone
1772+
1773+
if parsed_args.key_name:
1774+
search_opts['key_name'] = parsed_args.key_name
1775+
1776+
if parsed_args.has_config_drive is not None:
1777+
search_opts['config_drive'] = parsed_args.has_config_drive
1778+
1779+
if parsed_args.progress is not None:
1780+
search_opts['progress'] = str(parsed_args.progress)
1781+
1782+
if parsed_args.vm_state:
1783+
search_opts['vm_state'] = parsed_args.vm_state
1784+
1785+
if parsed_args.task_state:
1786+
search_opts['task_state'] = parsed_args.task_state
1787+
1788+
if parsed_args.power_state:
1789+
# taken from 'InstancePowerState' object field in nova
1790+
power_state = {
1791+
'pending': 0,
1792+
'running': 1,
1793+
'paused': 3,
1794+
'shutdown': 4,
1795+
'crashed': 6,
1796+
'suspended': 7,
1797+
}[parsed_args.power_state]
1798+
search_opts['power_state'] = power_state
1799+
16031800
if parsed_args.tags:
16041801
if compute_client.api_version < api_versions.APIVersion('2.26'):
16051802
msg = _(

0 commit comments

Comments
 (0)