summaryrefslogtreecommitdiff
path: root/openstackclient/compute
diff options
context:
space:
mode:
Diffstat (limited to 'openstackclient/compute')
-rw-r--r--openstackclient/compute/v2/console.py54
-rw-r--r--openstackclient/compute/v2/flavor.py59
-rw-r--r--openstackclient/compute/v2/keypair.py109
-rw-r--r--openstackclient/compute/v2/server.py510
-rw-r--r--openstackclient/compute/v2/server_group.py25
5 files changed, 560 insertions, 197 deletions
diff --git a/openstackclient/compute/v2/console.py b/openstackclient/compute/v2/console.py
index 110b21b8..0ab5c8a2 100644
--- a/openstackclient/compute/v2/console.py
+++ b/openstackclient/compute/v2/console.py
@@ -22,6 +22,15 @@ from osc_lib import utils
from openstackclient.i18n import _
+def _get_console_columns(item):
+ # To maintain backwards compatibility we need to rename sdk props to
+ # whatever OSC was using before
+ column_map = {}
+ hidden_columns = ['id', 'links', 'location', 'name']
+ return utils.get_osc_show_columns_for_sdk_resource(
+ item, column_map, hidden_columns)
+
+
class ShowConsoleLog(command.Command):
_description = _("Show server's console output")
@@ -44,19 +53,18 @@ class ShowConsoleLog(command.Command):
return parser
def take_action(self, parsed_args):
- compute_client = self.app.client_manager.compute
+ compute_client = self.app.client_manager.sdk_connection.compute
- server = utils.find_resource(
- compute_client.servers,
- parsed_args.server,
+ server = compute_client.find_server(
+ name_or_id=parsed_args.server,
+ ignore_missing=False
)
- length = parsed_args.lines
- if length:
- # NOTE(dtroyer): get_console_output() appears to shortchange the
- # output by one line
- length += 1
- data = server.get_console_output(length=length)
+ output = compute_client.get_server_console_output(
+ server.id, length=parsed_args.lines)
+ data = None
+ if output:
+ data = output.get('output', None)
if data and data[-1] != '\n':
data += '\n'
@@ -120,21 +128,15 @@ class ShowConsoleURL(command.ShowOne):
return parser
def take_action(self, parsed_args):
- compute_client = self.app.client_manager.compute
- server = utils.find_resource(
- compute_client.servers,
+ compute_client = self.app.client_manager.sdk_connection.compute
+ server = compute_client.find_server(
parsed_args.server,
- )
+ ignore_missing=False)
+
+ data = compute_client.create_console(server.id,
+ console_type=parsed_args.url_type)
+
+ display_columns, columns = _get_console_columns(data)
+ data = utils.get_dict_properties(data, columns)
- data = server.get_console_url(parsed_args.url_type)
- if not data:
- return ({}, {})
-
- info = {}
- # NOTE(Rui Chen): Return 'remote_console' in compute microversion API
- # 2.6 and later, return 'console' in compute
- # microversion API from 2.0 to 2.5, do compatibility
- # handle for different microversion API.
- console_data = data.get('remote_console', data.get('console'))
- info.update(console_data)
- return zip(*sorted(info.items()))
+ return (display_columns, data)
diff --git a/openstackclient/compute/v2/flavor.py b/openstackclient/compute/v2/flavor.py
index 805e919e..00431b7b 100644
--- a/openstackclient/compute/v2/flavor.py
+++ b/openstackclient/compute/v2/flavor.py
@@ -18,6 +18,7 @@
import logging
from novaclient import api_versions
+from osc_lib.cli import format_columns
from osc_lib.cli import parseractions
from osc_lib.command import command
from osc_lib import exceptions
@@ -30,6 +31,31 @@ from openstackclient.identity import common as identity_common
LOG = logging.getLogger(__name__)
+_formatters = {
+ 'extra_specs': format_columns.DictColumn,
+ # Unless we finish switch to use SDK resources this need to be doubled this
+ # way
+ 'properties': format_columns.DictColumn,
+ 'Properties': format_columns.DictColumn
+}
+
+
+def _get_flavor_columns(item):
+ # To maintain backwards compatibility we need to rename sdk props to
+ # whatever OSC was using before
+ column_map = {
+ 'extra_specs': 'properties',
+ 'ephemeral': 'OS-FLV-EXT-DATA:ephemeral',
+ 'is_disabled': 'OS-FLV-DISABLED:disabled',
+ 'is_public': 'os-flavor-access:is_public'
+
+ }
+ hidden_columns = ['links', 'location']
+
+ return utils.get_osc_show_columns_for_sdk_resource(
+ item, column_map, hidden_columns)
+
+
def _find_flavor(compute_client, flavor):
try:
return compute_client.flavors.get(flavor)
@@ -191,10 +217,16 @@ class CreateFlavor(command.ShowOne):
LOG.error(_("Failed to set flavor property: %s"), e)
flavor_info = flavor._info.copy()
- flavor_info.pop("links")
- flavor_info['properties'] = utils.format_dict(flavor.get_keys())
+ flavor_info['properties'] = flavor.get_keys()
- return zip(*sorted(flavor_info.items()))
+ display_columns, columns = _get_flavor_columns(flavor_info)
+ data = utils.get_dict_properties(
+ flavor_info, columns,
+ formatters=_formatters,
+ mixed_case_fields=['OS-FLV-DISABLED:disabled',
+ 'OS-FLV-EXT-DATA:ephemeral'])
+
+ return (display_columns, data)
class DeleteFlavor(command.Command):
@@ -309,7 +341,7 @@ class ListFlavor(command.Lister):
return (column_headers,
(utils.get_item_properties(
- s, columns, formatters={'Properties': utils.format_dict},
+ s, columns, formatters=_formatters,
) for s in data))
@@ -428,11 +460,8 @@ class ShowFlavor(command.ShowOne):
try:
flavor_access = compute_client.flavor_access.list(
flavor=resource_flavor.id)
- projects = [utils.get_field(access, 'tenant_id')
- for access in flavor_access]
- # TODO(Huanxuan Ao): This format case can be removed after
- # patch https://review.opendev.org/#/c/330223/ merged.
- access_projects = utils.format_list(projects)
+ access_projects = [utils.get_field(access, 'tenant_id')
+ for access in flavor_access]
except Exception as e:
msg = _("Failed to get access projects list "
"for flavor '%(flavor)s': %(e)s")
@@ -442,11 +471,17 @@ class ShowFlavor(command.ShowOne):
flavor.update({
'access_project_ids': access_projects
})
- flavor.pop("links", None)
- flavor['properties'] = utils.format_dict(resource_flavor.get_keys())
+ flavor['properties'] = resource_flavor.get_keys()
+
+ display_columns, columns = _get_flavor_columns(flavor)
+ data = utils.get_dict_properties(
+ flavor, columns,
+ formatters=_formatters,
+ mixed_case_fields=['OS-FLV-DISABLED:disabled',
+ 'OS-FLV-EXT-DATA:ephemeral'])
- return zip(*sorted(flavor.items()))
+ return (display_columns, data)
class UnsetFlavor(command.Command):
diff --git a/openstackclient/compute/v2/keypair.py b/openstackclient/compute/v2/keypair.py
index 2b365ceb..8c365cf0 100644
--- a/openstackclient/compute/v2/keypair.py
+++ b/openstackclient/compute/v2/keypair.py
@@ -20,11 +20,13 @@ import logging
import os
import sys
+from novaclient import api_versions
from osc_lib.command import command
from osc_lib import exceptions
from osc_lib import utils
from openstackclient.i18n import _
+from openstackclient.identity import common as identity_common
LOG = logging.getLogger(__name__)
@@ -53,6 +55,15 @@ class CreateKeypair(command.ShowOne):
help=_("Filename for private key to save. If not used, "
"print private key in console.")
)
+ parser.add_argument(
+ '--type',
+ metavar='<type>',
+ choices=['ssh', 'x509'],
+ help=_(
+ "Keypair type. Can be ssh or x509. "
+ "(Supported by API versions '2.2' - '2.latest')"
+ ),
+ )
return parser
def take_action(self, parsed_args):
@@ -70,17 +81,28 @@ class CreateKeypair(command.ShowOne):
"exception": e}
)
- keypair = compute_client.keypairs.create(
- parsed_args.name,
- public_key=public_key,
- )
+ kwargs = {
+ 'name': parsed_args.name,
+ 'public_key': public_key,
+ }
+ if parsed_args.type:
+ if compute_client.api_version < api_versions.APIVersion('2.2'):
+ msg = _(
+ '--os-compute-api-version 2.2 or greater is required to '
+ 'support the --type option.'
+ )
+ raise exceptions.CommandError(msg)
+
+ kwargs['key_type'] = parsed_args.type
+
+ keypair = compute_client.keypairs.create(**kwargs)
private_key = parsed_args.private_key
# Save private key into specified file
if private_key:
try:
with io.open(
- os.path.expanduser(parsed_args.private_key), 'w+'
+ os.path.expanduser(parsed_args.private_key), 'w+'
) as p:
p.write(keypair.private_key)
except IOError as e:
@@ -142,18 +164,85 @@ class DeleteKeypair(command.Command):
class ListKeypair(command.Lister):
_description = _("List key fingerprints")
+ def get_parser(self, prog_name):
+ parser = super().get_parser(prog_name)
+ user_group = parser.add_mutually_exclusive_group()
+ user_group.add_argument(
+ '--user',
+ metavar='<user>',
+ help=_(
+ 'Show keypairs for another user (admin only) (name or ID). '
+ 'Requires ``--os-compute-api-version`` 2.10 or greater.'
+ ),
+ )
+ identity_common.add_user_domain_option_to_parser(parser)
+ user_group.add_argument(
+ '--project',
+ metavar='<project>',
+ help=_(
+ 'Show keypairs for all users associated with project '
+ '(admin only) (name or ID). '
+ 'Requires ``--os-compute-api-version`` 2.10 or greater.'
+ ),
+ )
+ identity_common.add_project_domain_option_to_parser(parser)
+ return parser
+
def take_action(self, parsed_args):
compute_client = self.app.client_manager.compute
+ identity_client = self.app.client_manager.identity
+
+ if parsed_args.project:
+ if compute_client.api_version < api_versions.APIVersion('2.10'):
+ msg = _(
+ '--os-compute-api-version 2.10 or greater is required to '
+ 'support the --project option'
+ )
+ raise exceptions.CommandError(msg)
+
+ # NOTE(stephenfin): This is done client side because nova doesn't
+ # currently support doing so server-side. If this is slow, we can
+ # think about spinning up a threadpool or similar.
+ project = identity_common.find_project(
+ identity_client,
+ parsed_args.project,
+ parsed_args.project_domain,
+ ).id
+ users = identity_client.users.list(tenant_id=project)
+
+ data = []
+ for user in users:
+ data.extend(compute_client.keypairs.list(user_id=user.id))
+ elif parsed_args.user:
+ if compute_client.api_version < api_versions.APIVersion('2.10'):
+ msg = _(
+ '--os-compute-api-version 2.10 or greater is required to '
+ 'support the --user option'
+ )
+ raise exceptions.CommandError(msg)
+
+ user = identity_common.find_user(
+ identity_client,
+ parsed_args.user,
+ parsed_args.user_domain,
+ )
+
+ data = compute_client.keypairs.list(user_id=user.id)
+ else:
+ data = compute_client.keypairs.list()
+
columns = (
"Name",
"Fingerprint"
)
- data = compute_client.keypairs.list()
- return (columns,
- (utils.get_item_properties(
- s, columns,
- ) for s in data))
+ if compute_client.api_version >= api_versions.APIVersion('2.2'):
+ columns += ("Type", )
+
+ return (
+ columns,
+ (utils.get_item_properties(s, columns) for s in data),
+ )
class ShowKeypair(command.ShowOne):
diff --git a/openstackclient/compute/v2/server.py b/openstackclient/compute/v2/server.py
index dec6eb62..fddafaee 100644
--- a/openstackclient/compute/v2/server.py
+++ b/openstackclient/compute/v2/server.py
@@ -236,6 +236,14 @@ class AddFixedIP(command.Command):
metavar="<ip-address>",
help=_("Requested fixed IP address"),
)
+ parser.add_argument(
+ '--tag',
+ metavar='<tag>',
+ help=_(
+ 'Tag for the attached interface. '
+ '(supported by --os-compute-api-version 2.52 or above)'
+ )
+ )
return parser
def take_action(self, parsed_args):
@@ -246,11 +254,23 @@ class AddFixedIP(command.Command):
network = compute_client.api.network_find(parsed_args.network)
- server.interface_attach(
- port_id=None,
- net_id=network['id'],
- fixed_ip=parsed_args.fixed_ip_address,
- )
+ kwargs = {
+ 'port_id': None,
+ 'net_id': network['id'],
+ 'fixed_ip': parsed_args.fixed_ip_address,
+ }
+
+ if parsed_args.tag:
+ if compute_client.api_version < api_versions.APIVersion('2.49'):
+ msg = _(
+ '--os-compute-api-version 2.49 or greater is required to '
+ 'support the --tag option'
+ )
+ raise exceptions.CommandError(msg)
+
+ kwargs['tag'] = parsed_args.tag
+
+ server.interface_attach(**kwargs)
class AddFloatingIP(network_common.NetworkAndComputeCommand):
@@ -510,20 +530,32 @@ class AddServerVolume(command.Command):
metavar='<device>',
help=_('Server internal device name for volume'),
)
+ parser.add_argument(
+ '--tag',
+ metavar='<tag>',
+ help=_(
+ "Tag for the attached volume. "
+ "(Supported by API versions '2.49' - '2.latest')"
+ ),
+ )
termination_group = parser.add_mutually_exclusive_group()
termination_group.add_argument(
'--enable-delete-on-termination',
action='store_true',
- help=_("Specify if the attached volume should be deleted when "
- "the server is destroyed. (Supported with "
- "``--os-compute-api-version`` 2.79 or greater.)"),
+ help=_(
+ "Specify if the attached volume should be deleted when the "
+ "server is destroyed. "
+ "(Supported by API versions '2.79' - '2.latest')"
+ ),
)
termination_group.add_argument(
'--disable-delete-on-termination',
action='store_true',
- help=_("Specify if the attached volume should not be deleted "
- "when the server is destroyed. (Supported with "
- "``--os-compute-api-version`` 2.79 or greater.)"),
+ help=_(
+ "Specify if the attached volume should not be deleted when "
+ "the server is destroyed. "
+ "(Supported by API versions '2.79' - '2.latest')"
+ ),
)
return parser
@@ -540,28 +572,38 @@ class AddServerVolume(command.Command):
parsed_args.volume,
)
- support_set_delete_on_termination = (compute_client.api_version >=
- api_versions.APIVersion('2.79'))
-
- if not support_set_delete_on_termination:
- if parsed_args.enable_delete_on_termination:
- msg = _('--os-compute-api-version 2.79 or greater '
- 'is required to support the '
- '--enable-delete-on-termination option.')
- raise exceptions.CommandError(msg)
- if parsed_args.disable_delete_on_termination:
- msg = _('--os-compute-api-version 2.79 or greater '
- 'is required to support the '
- '--disable-delete-on-termination option.')
- raise exceptions.CommandError(msg)
-
kwargs = {
"device": parsed_args.device
}
+ if parsed_args.tag:
+ if compute_client.api_version < api_versions.APIVersion('2.49'):
+ msg = _(
+ '--os-compute-api-version 2.49 or greater is required to '
+ 'support the --tag option'
+ )
+ raise exceptions.CommandError(msg)
+
+ kwargs['tag'] = parsed_args.tag
+
if parsed_args.enable_delete_on_termination:
+ if compute_client.api_version < api_versions.APIVersion('2.79'):
+ msg = _(
+ '--os-compute-api-version 2.79 or greater is required to '
+ 'support the --enable-delete-on-termination option.'
+ )
+ raise exceptions.CommandError(msg)
+
kwargs['delete_on_termination'] = True
+
if parsed_args.disable_delete_on_termination:
+ if compute_client.api_version < api_versions.APIVersion('2.79'):
+ msg = _(
+ '--os-compute-api-version 2.79 or greater is required to '
+ 'support the --disable-delete-on-termination option.'
+ )
+ raise exceptions.CommandError(msg)
+
kwargs['delete_on_termination'] = False
compute_client.volumes.create_server_volume(
@@ -598,13 +640,20 @@ class CreateServer(command.ShowOne):
disk_group.add_argument(
'--volume',
metavar='<volume>',
- help=_('Create server using this volume as the boot disk (name '
- 'or ID).\n'
- 'This option automatically creates a block device mapping '
- 'with a boot index of 0. On many hypervisors (libvirt/kvm '
- 'for example) this will be device vda. Do not create a '
- 'duplicate mapping using --block-device-mapping for this '
- 'volume.'),
+ help=_(
+ 'Create server using this volume as the boot disk (name or ID)'
+ '\n'
+ 'This option automatically creates a block device mapping '
+ 'with a boot index of 0. On many hypervisors (libvirt/kvm '
+ 'for example) this will be device vda. Do not create a '
+ 'duplicate mapping using --block-device-mapping for this '
+ 'volume.'
+ ),
+ )
+ parser.add_argument(
+ '--password',
+ metavar='<password>',
+ help=_("Set the password to this server"),
)
parser.add_argument(
'--flavor',
@@ -617,28 +666,34 @@ class CreateServer(command.ShowOne):
metavar='<security-group>',
action='append',
default=[],
- help=_('Security group to assign to this server (name or ID) '
- '(repeat option to set multiple groups)'),
+ help=_(
+ 'Security group to assign to this server (name or ID) '
+ '(repeat option to set multiple groups)'
+ ),
)
parser.add_argument(
'--key-name',
metavar='<key-name>',
- help=_('Keypair to inject into this server (optional extension)'),
+ help=_('Keypair to inject into this server'),
)
parser.add_argument(
'--property',
metavar='<key=value>',
action=parseractions.KeyValueAction,
- help=_('Set a property on this server '
- '(repeat option to set multiple values)'),
+ help=_(
+ 'Set a property on this server '
+ '(repeat option to set multiple values)'
+ ),
)
parser.add_argument(
'--file',
metavar='<dest-filename=source-filename>',
action='append',
default=[],
- help=_('File to inject into image before boot '
- '(repeat option to set multiple files)'),
+ help=_(
+ 'File to inject into image before boot '
+ '(repeat option to set multiple files)'
+ ),
)
parser.add_argument(
'--user-data',
@@ -648,8 +703,10 @@ class CreateServer(command.ShowOne):
parser.add_argument(
'--description',
metavar='<description>',
- help=_('Set description for the server (supported by '
- '--os-compute-api-version 2.19 or above)'),
+ help=_(
+ 'Set description for the server '
+ '(supported by --os-compute-api-version 2.19 or above)'
+ ),
)
parser.add_argument(
'--availability-zone',
@@ -659,29 +716,35 @@ class CreateServer(command.ShowOne):
parser.add_argument(
'--host',
metavar='<host>',
- help=_('Requested host to create servers. Admin only '
- 'by default. (supported by --os-compute-api-version 2.74 '
- 'or above)'),
+ help=_(
+ 'Requested host to create servers. '
+ '(admin only) '
+ '(supported by --os-compute-api-version 2.74 or above)'
+ ),
)
parser.add_argument(
'--hypervisor-hostname',
metavar='<hypervisor-hostname>',
- help=_('Requested hypervisor hostname to create servers. Admin '
- 'only by default. (supported by --os-compute-api-version '
- '2.74 or above)'),
+ help=_(
+ 'Requested hypervisor hostname to create servers. '
+ '(admin only) '
+ '(supported by --os-compute-api-version 2.74 or above)'
+ ),
)
parser.add_argument(
'--boot-from-volume',
metavar='<volume-size>',
type=int,
- help=_('When used in conjunction with the ``--image`` or '
- '``--image-property`` option, this option automatically '
- 'creates a block device mapping with a boot index of 0 '
- 'and tells the compute service to create a volume of the '
- 'given size (in GB) from the specified image and use it '
- 'as the root disk of the server. The root volume will not '
- 'be deleted when the server is deleted. This option is '
- 'mutually exclusive with the ``--volume`` option.')
+ help=_(
+ 'When used in conjunction with the ``--image`` or '
+ '``--image-property`` option, this option automatically '
+ 'creates a block device mapping with a boot index of 0 '
+ 'and tells the compute service to create a volume of the '
+ 'given size (in GB) from the specified image and use it '
+ 'as the root disk of the server. The root volume will not '
+ 'be deleted when the server is deleted. This option is '
+ 'mutually exclusive with the ``--volume`` option.'
+ )
)
parser.add_argument(
'--block-device-mapping',
@@ -691,37 +754,40 @@ class CreateServer(command.ShowOne):
# NOTE(RuiChen): Add '\n' at the end of line to put each item in
# the separated line, avoid the help message looks
# messy, see _SmartHelpFormatter in cliff.
- help=_('Create a block device on the server.\n'
- 'Block device mapping in the format\n'
- '<dev-name>=<id>:<type>:<size(GB)>:<delete-on-terminate>\n'
- '<dev-name>: block device name, like: vdb, xvdc '
- '(required)\n'
- '<id>: Name or ID of the volume, volume snapshot or image '
- '(required)\n'
- '<type>: volume, snapshot or image; default: volume '
- '(optional)\n'
- '<size(GB)>: volume size if create from image or snapshot '
- '(optional)\n'
- '<delete-on-terminate>: true or false; default: false '
- '(optional)\n'
- '(optional extension)'),
+ help=_(
+ 'Create a block device on the server.\n'
+ 'Block device mapping in the format\n'
+ '<dev-name>=<id>:<type>:<size(GB)>:<delete-on-terminate>\n'
+ '<dev-name>: block device name, like: vdb, xvdc '
+ '(required)\n'
+ '<id>: Name or ID of the volume, volume snapshot or image '
+ '(required)\n'
+ '<type>: volume, snapshot or image; default: volume '
+ '(optional)\n'
+ '<size(GB)>: volume size if create from image or snapshot '
+ '(optional)\n'
+ '<delete-on-terminate>: true or false; default: false '
+ '(optional)\n'
+ ),
)
parser.add_argument(
'--nic',
metavar="<net-id=net-uuid,v4-fixed-ip=ip-addr,v6-fixed-ip=ip-addr,"
"port-id=port-uuid,auto,none>",
action='append',
- help=_("Create a NIC on the server. "
- "Specify option multiple times to create multiple NICs. "
- "Either net-id or port-id must be provided, but not both. "
- "net-id: attach NIC to network with this UUID, "
- "port-id: attach NIC to port with this UUID, "
- "v4-fixed-ip: IPv4 fixed address for NIC (optional), "
- "v6-fixed-ip: IPv6 fixed address for NIC (optional), "
- "none: (v2.37+) no network is attached, "
- "auto: (v2.37+) the compute service will automatically "
- "allocate a network. Specifying a --nic of auto or none "
- "cannot be used with any other --nic value."),
+ help=_(
+ "Create a NIC on the server. "
+ "Specify option multiple times to create multiple NICs. "
+ "Either net-id or port-id must be provided, but not both. "
+ "net-id: attach NIC to network with this UUID, "
+ "port-id: attach NIC to port with this UUID, "
+ "v4-fixed-ip: IPv4 fixed address for NIC (optional), "
+ "v6-fixed-ip: IPv6 fixed address for NIC (optional), "
+ "none: (v2.37+) no network is attached, "
+ "auto: (v2.37+) the compute service will automatically "
+ "allocate a network. Specifying a --nic of auto or none "
+ "cannot be used with any other --nic value."
+ ),
)
parser.add_argument(
'--network',
@@ -729,13 +795,15 @@ class CreateServer(command.ShowOne):
action='append',
dest='nic',
type=_prefix_checked_value('net-id='),
- help=_("Create a NIC on the server and connect it to network. "
- "Specify option multiple times to create multiple NICs. "
- "This is a wrapper for the '--nic net-id=<network>' "
- "parameter that provides simple syntax for the standard "
- "use case of connecting a new server to a given network. "
- "For more advanced use cases, refer to the '--nic' "
- "parameter."),
+ help=_(
+ "Create a NIC on the server and connect it to network. "
+ "Specify option multiple times to create multiple NICs. "
+ "This is a wrapper for the '--nic net-id=<network>' "
+ "parameter that provides simple syntax for the standard "
+ "use case of connecting a new server to a given network. "
+ "For more advanced use cases, refer to the '--nic' "
+ "parameter."
+ ),
)
parser.add_argument(
'--port',
@@ -743,12 +811,14 @@ class CreateServer(command.ShowOne):
action='append',
dest='nic',
type=_prefix_checked_value('port-id='),
- help=_("Create a NIC on the server and connect it to port. "
- "Specify option multiple times to create multiple NICs. "
- "This is a wrapper for the '--nic port-id=<port>' "
- "parameter that provides simple syntax for the standard "
- "use case of connecting a new server to a given port. For "
- "more advanced use cases, refer to the '--nic' parameter."),
+ help=_(
+ "Create a NIC on the server and connect it to port. "
+ "Specify option multiple times to create multiple NICs. "
+ "This is a wrapper for the '--nic port-id=<port>' "
+ "parameter that provides simple syntax for the standard "
+ "use case of connecting a new server to a given port. For "
+ "more advanced use cases, refer to the '--nic' parameter."
+ ),
)
parser.add_argument(
'--hint',
@@ -801,6 +871,18 @@ class CreateServer(command.ShowOne):
action='store_true',
help=_('Wait for build to complete'),
)
+ parser.add_argument(
+ '--tag',
+ metavar='<tag>',
+ action='append',
+ default=[],
+ dest='tags',
+ help=_(
+ 'Tags for the server. '
+ 'Specify multiple times to add multiple tags. '
+ '(supported by --os-compute-api-version 2.52 or above)'
+ ),
+ )
return parser
def take_action(self, parsed_args):
@@ -823,10 +905,13 @@ class CreateServer(command.ShowOne):
if not image and parsed_args.image_property:
def emit_duplicated_warning(img, image_property):
img_uuid_list = [str(image.id) for image in img]
- LOG.warning(_('Multiple matching images: %(img_uuid_list)s\n'
- 'Using image: %(chosen_one)s') %
- {'img_uuid_list': img_uuid_list,
- 'chosen_one': img_uuid_list[0]})
+ LOG.warning(
+ 'Multiple matching images: %(img_uuid_list)s\n'
+ 'Using image: %(chosen_one)s',
+ {
+ 'img_uuid_list': img_uuid_list,
+ 'chosen_one': img_uuid_list[0],
+ })
def _match_image(image_api, wanted_properties):
image_list = image_api.images()
@@ -843,45 +928,52 @@ class CreateServer(command.ShowOne):
set([key, value])
except TypeError:
if key != 'properties':
- LOG.debug('Skipped the \'%s\' attribute. '
- 'That cannot be compared. '
- '(image: %s, value: %s)',
- key, img.id, value)
+ LOG.debug(
+ 'Skipped the \'%s\' attribute. '
+ 'That cannot be compared. '
+ '(image: %s, value: %s)',
+ key, img.id, value,
+ )
pass
else:
img_dict[key] = value
- if all(k in img_dict and img_dict[k] == v
- for k, v in wanted_properties.items()):
+ if all(
+ k in img_dict and img_dict[k] == v
+ for k, v in wanted_properties.items()
+ ):
images_matched.append(img)
+
return images_matched
images = _match_image(image_client, parsed_args.image_property)
if len(images) > 1:
- emit_duplicated_warning(images,
- parsed_args.image_property)
+ emit_duplicated_warning(images, parsed_args.image_property)
if images:
image = images[0]
else:
- raise exceptions.CommandError(_("No images match the "
- "property expected by "
- "--image-property"))
+ msg = _(
+ 'No images match the property expected by '
+ '--image-property'
+ )
+ raise exceptions.CommandError(msg)
# Lookup parsed_args.volume
volume = None
if parsed_args.volume:
# --volume and --boot-from-volume are mutually exclusive.
if parsed_args.boot_from_volume:
- raise exceptions.CommandError(
- _('--volume is not allowed with --boot-from-volume'))
+ msg = _('--volume is not allowed with --boot-from-volume')
+ raise exceptions.CommandError(msg)
+
volume = utils.find_resource(
volume_client.volumes,
parsed_args.volume,
).id
# Lookup parsed_args.flavor
- flavor = utils.find_resource(compute_client.flavors,
- parsed_args.flavor)
+ flavor = utils.find_resource(
+ compute_client.flavors, parsed_args.flavor)
files = {}
for f in parsed_args.file:
@@ -891,16 +983,17 @@ class CreateServer(command.ShowOne):
except IOError as e:
msg = _("Can't open '%(source)s': %(exception)s")
raise exceptions.CommandError(
- msg % {"source": src,
- "exception": e}
+ msg % {'source': src, 'exception': e}
)
if parsed_args.min > parsed_args.max:
msg = _("min instances should be <= max instances")
raise exceptions.CommandError(msg)
+
if parsed_args.min < 1:
msg = _("min instances should be > 0")
raise exceptions.CommandError(msg)
+
if parsed_args.max < 1:
msg = _("max instances should be > 0")
raise exceptions.CommandError(msg)
@@ -912,8 +1005,7 @@ class CreateServer(command.ShowOne):
except IOError as e:
msg = _("Can't open '%(data)s': %(exception)s")
raise exceptions.CommandError(
- msg % {"data": parsed_args.user_data,
- "exception": e}
+ msg % {'data': parsed_args.user_data, 'exception': e}
)
if parsed_args.description:
@@ -924,11 +1016,12 @@ class CreateServer(command.ShowOne):
block_device_mapping_v2 = []
if volume:
- block_device_mapping_v2 = [{'uuid': volume,
- 'boot_index': '0',
- 'source_type': 'volume',
- 'destination_type': 'volume'
- }]
+ block_device_mapping_v2 = [{
+ 'uuid': volume,
+ 'boot_index': '0',
+ 'source_type': 'volume',
+ 'destination_type': 'volume'
+ }]
elif parsed_args.boot_from_volume:
# Tell nova to create a root volume from the image provided.
block_device_mapping_v2 = [{
@@ -949,13 +1042,16 @@ class CreateServer(command.ShowOne):
dev_map = dev_map.split(':')
if dev_map[0]:
mapping = {'device_name': dev_name}
+
# 1. decide source and destination type
if (len(dev_map) > 1 and
dev_map[1] in ('volume', 'snapshot', 'image')):
mapping['source_type'] = dev_map[1]
else:
mapping['source_type'] = 'volume'
+
mapping['destination_type'] = 'volume'
+
# 2. check target exist, update target uuid according by
# source type
if mapping['source_type'] == 'volume':
@@ -981,14 +1077,18 @@ class CreateServer(command.ShowOne):
image_id = image_client.find_image(dev_map[0],
ignore_missing=False).id
mapping['uuid'] = image_id
+
# 3. append size and delete_on_termination if exist
if len(dev_map) > 2 and dev_map[2]:
mapping['volume_size'] = dev_map[2]
+
if len(dev_map) > 3 and dev_map[3]:
mapping['delete_on_termination'] = dev_map[3]
else:
- msg = _("Volume, volume snapshot or image (name or ID) must "
- "be specified if --block-device-mapping is specified")
+ msg = _(
+ 'Volume, volume snapshot or image (name or ID) must '
+ 'be specified if --block-device-mapping is specified'
+ )
raise exceptions.CommandError(msg)
block_device_mapping_v2.append(mapping)
@@ -1002,22 +1102,32 @@ class CreateServer(command.ShowOne):
auto_or_none = True
nics.append(nic_str)
else:
- nic_info = {"net-id": "", "v4-fixed-ip": "",
- "v6-fixed-ip": "", "port-id": ""}
+ nic_info = {
+ 'net-id': '',
+ 'v4-fixed-ip': '',
+ 'v6-fixed-ip': '',
+ 'port-id': '',
+ }
for kv_str in nic_str.split(","):
k, sep, v = kv_str.partition("=")
if k in nic_info and v:
nic_info[k] = v
else:
- msg = (_("Invalid nic argument '%s'. Nic arguments "
- "must be of the form --nic <net-id=net-uuid"
- ",v4-fixed-ip=ip-addr,v6-fixed-ip=ip-addr,"
- "port-id=port-uuid>."))
+ msg = _(
+ "Invalid nic argument '%s'. Nic arguments "
+ "must be of the form --nic <net-id=net-uuid"
+ ",v4-fixed-ip=ip-addr,v6-fixed-ip=ip-addr,"
+ "port-id=port-uuid>."
+ )
raise exceptions.CommandError(msg % k)
+
if bool(nic_info["net-id"]) == bool(nic_info["port-id"]):
- msg = _("either network or port should be specified "
- "but not both")
+ msg = _(
+ 'Either network or port should be specified '
+ 'but not both'
+ )
raise exceptions.CommandError(msg)
+
if self.app.client_manager.is_network_endpoint_enabled():
network_client = self.app.client_manager.network
if nic_info["net-id"]:
@@ -1034,17 +1144,22 @@ class CreateServer(command.ShowOne):
nic_info["net-id"]
)['id']
if nic_info["port-id"]:
- msg = _("can't create server with port specified "
- "since network endpoint not enabled")
+ msg = _(
+ "Can't create server with port specified "
+ "since network endpoint not enabled"
+ )
raise exceptions.CommandError(msg)
+
nics.append(nic_info)
if nics:
if auto_or_none:
if len(nics) > 1:
- msg = _('Specifying a --nic of auto or none cannot '
- 'be used with any other --nic, --network '
- 'or --port value.')
+ msg = _(
+ 'Specifying a --nic of auto or none cannot '
+ 'be used with any other --nic, --network '
+ 'or --port value.'
+ )
raise exceptions.CommandError(msg)
nics = nics[0]
else:
@@ -1105,6 +1220,7 @@ class CreateServer(command.ShowOne):
userdata=userdata,
key_name=parsed_args.key_name,
availability_zone=parsed_args.availability_zone,
+ admin_pass=parsed_args.password,
block_device_mapping_v2=block_device_mapping_v2,
nics=nics,
scheduler_hints=hints,
@@ -1113,18 +1229,34 @@ class CreateServer(command.ShowOne):
if parsed_args.description:
boot_kwargs['description'] = parsed_args.description
+ if parsed_args.tags:
+ if compute_client.api_version < api_versions.APIVersion('2.52'):
+ msg = _(
+ '--os-compute-api-version 2.52 or greater is required to '
+ 'support the --tag option'
+ )
+ raise exceptions.CommandError(msg)
+
+ boot_kwargs['tags'] = parsed_args.tags
+
if parsed_args.host:
if compute_client.api_version < api_versions.APIVersion("2.74"):
- msg = _("Specifying --host is not supported for "
- "--os-compute-api-version less than 2.74")
+ msg = _(
+ '--os-compute-api-version 2.74 or greater is required to '
+ 'support the --host option'
+ )
raise exceptions.CommandError(msg)
+
boot_kwargs['host'] = parsed_args.host
if parsed_args.hypervisor_hostname:
if compute_client.api_version < api_versions.APIVersion("2.74"):
- msg = _("Specifying --hypervisor-hostname is not supported "
- "for --os-compute-api-version less than 2.74")
+ msg = _(
+ '--os-compute-api-version 2.74 or greater is required to '
+ 'support the --hypervisor-hostname option'
+ )
raise exceptions.CommandError(msg)
+
boot_kwargs['hypervisor_hostname'] = (
parsed_args.hypervisor_hostname)
@@ -1150,8 +1282,7 @@ class CreateServer(command.ShowOne):
):
self.app.stdout.write('\n')
else:
- LOG.error(_('Error creating server: %s'),
- parsed_args.server_name)
+ LOG.error('Error creating server: %s', parsed_args.server_name)
self.app.stdout.write(_('Error creating server\n'))
raise SystemExit
@@ -1380,6 +1511,30 @@ class ListServer(command.Lister):
help=_('Only display unlocked servers. '
'Requires ``--os-compute-api-version`` 2.73 or greater.'),
)
+ parser.add_argument(
+ '--tags',
+ metavar='<tag>',
+ action='append',
+ default=[],
+ dest='tags',
+ help=_(
+ 'Only list servers with the specified tag. '
+ 'Specify multiple times to filter on multiple tags. '
+ '(supported by --os-compute-api-version 2.26 or above)'
+ ),
+ )
+ parser.add_argument(
+ '--not-tags',
+ metavar='<tag>',
+ action='append',
+ default=[],
+ dest='not_tags',
+ help=_(
+ 'Only list servers without the specified tag. '
+ 'Specify multiple times to filter on multiple tags. '
+ '(supported by --os-compute-api-version 2.26 or above)'
+ ),
+ )
return parser
def take_action(self, parsed_args):
@@ -1435,6 +1590,27 @@ class ListServer(command.Lister):
'changes-before': parsed_args.changes_before,
'changes-since': parsed_args.changes_since,
}
+
+ if parsed_args.tags:
+ if compute_client.api_version < api_versions.APIVersion('2.26'):
+ msg = _(
+ '--os-compute-api-version 2.26 or greater is required to '
+ 'support the --tag option'
+ )
+ raise exceptions.CommandError(msg)
+
+ search_opts['tags'] = parsed_args.tags
+
+ if parsed_args.not_tags:
+ if compute_client.api_version < api_versions.APIVersion('2.26'):
+ msg = _(
+ '--os-compute-api-version 2.26 or greater is required to '
+ 'support the --not-tag option'
+ )
+ raise exceptions.CommandError(msg)
+
+ search_opts['not-tags'] = parsed_args.not_tags
+
support_locked = (compute_client.api_version >=
api_versions.APIVersion('2.73'))
if not support_locked and (parsed_args.locked or parsed_args.unlocked):
@@ -2803,6 +2979,18 @@ class SetServer(command.Command):
help=_('New server description (supported by '
'--os-compute-api-version 2.19 or above)'),
)
+ parser.add_argument(
+ '--tag',
+ metavar='<tag>',
+ action='append',
+ default=[],
+ dest='tags',
+ help=_(
+ 'Tag for the server. '
+ 'Specify multiple times to add multiple tags. '
+ '(supported by --os-compute-api-version 2.26 or above)'
+ ),
+ )
return parser
def take_action(self, parsed_args):
@@ -2841,6 +3029,17 @@ class SetServer(command.Command):
raise exceptions.CommandError(msg)
server.update(description=parsed_args.description)
+ if parsed_args.tags:
+ if server.api_version < api_versions.APIVersion('2.26'):
+ msg = _(
+ '--os-compute-api-version 2.26 or greater is required to '
+ 'support the --tag option'
+ )
+ raise exceptions.CommandError(msg)
+
+ for tag in parsed_args.tags:
+ server.add_tag(tag=tag)
+
class ShelveServer(command.Command):
_description = _("Shelve server(s)")
@@ -3182,7 +3381,7 @@ class UnrescueServer(command.Command):
class UnsetServer(command.Command):
- _description = _("Unset server properties")
+ _description = _("Unset server properties and tags")
def get_parser(self, prog_name):
parser = super(UnsetServer, self).get_parser(prog_name)
@@ -3206,6 +3405,18 @@ class UnsetServer(command.Command):
help=_('Unset server description (supported by '
'--os-compute-api-version 2.19 or above)'),
)
+ parser.add_argument(
+ '--tag',
+ metavar='<tag>',
+ action='append',
+ default=[],
+ dest='tags',
+ help=_(
+ 'Tag to remove from the server. '
+ 'Specify multiple times to remove multiple tags. '
+ '(supported by --os-compute-api-version 2.26 or later'
+ ),
+ )
return parser
def take_action(self, parsed_args):
@@ -3231,6 +3442,17 @@ class UnsetServer(command.Command):
description="",
)
+ if parsed_args.tags:
+ if compute_client.api_version < api_versions.APIVersion('2.26'):
+ msg = _(
+ '--os-compute-api-version 2.26 or greater is required to '
+ 'support the --tag option'
+ )
+ raise exceptions.CommandError(msg)
+
+ for tag in parsed_args.tags:
+ compute_client.servers.delete_tag(server, tag=tag)
+
class UnshelveServer(command.Command):
_description = _("Unshelve server(s)")
diff --git a/openstackclient/compute/v2/server_group.py b/openstackclient/compute/v2/server_group.py
index 1af6e28d..a3363244 100644
--- a/openstackclient/compute/v2/server_group.py
+++ b/openstackclient/compute/v2/server_group.py
@@ -56,12 +56,18 @@ class CreateServerGroup(command.ShowOne):
parser.add_argument(
'--policy',
metavar='<policy>',
+ choices=[
+ 'affinity',
+ 'anti-affinity',
+ 'soft-affinity',
+ 'soft-anti-affinity',
+ ],
default='affinity',
- help=_("Add a policy to <name> "
- "('affinity' or 'anti-affinity', "
- "defaults to 'affinity'). Specify --os-compute-api-version "
- "2.15 or higher for the 'soft-affinity' or "
- "'soft-anti-affinity' policy.")
+ help=_(
+ "Add a policy to <name> "
+ "Specify --os-compute-api-version 2.15 or higher for the "
+ "'soft-affinity' or 'soft-anti-affinity' policy."
+ )
)
return parser
@@ -69,9 +75,18 @@ class CreateServerGroup(command.ShowOne):
compute_client = self.app.client_manager.compute
info = {}
+ if parsed_args.policy in ('soft-affinity', 'soft-anti-affinity'):
+ if compute_client.api_version < api_versions.APIVersion('2.15'):
+ msg = _(
+ '--os-compute-api-version 2.15 or greater is required to '
+ 'support the %s policy'
+ )
+ raise exceptions.CommandError(msg % parsed_args.policy)
+
policy_arg = {'policies': [parsed_args.policy]}
if compute_client.api_version >= api_versions.APIVersion("2.64"):
policy_arg = {'policy': parsed_args.policy}
+
server_group = compute_client.server_groups.create(
name=parsed_args.name, **policy_arg)