Blackify openstack.cloud

Black used with the '-l 79 -S' flags.

A future change will ignore this commit in git-blame history by adding a
'git-blame-ignore-revs' file.

Change-Id: Ib58bb45ce8c29e5347ffc36d40d6f5d52b140c6b
Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
This commit is contained in:
Stephen Finucane 2023-05-05 11:22:56 +01:00
parent c2ff7336ce
commit 004c7352d0
128 changed files with 26600 additions and 16255 deletions

View File

@ -70,7 +70,7 @@ class AcceleratorCloudMixin:
"""
device_profile = self.accelerator.get_device_profile(
name_or_id,
filters
filters,
)
if device_profile is None:
self.log.debug(
@ -104,7 +104,7 @@ class AcceleratorCloudMixin:
"""
accelerator_request = self.accelerator.get_accelerator_request(
name_or_id,
filters
filters,
)
if accelerator_request is None:
self.log.debug(

View File

@ -39,7 +39,8 @@ def _normalize_port_list(nics):
except KeyError:
raise TypeError(
"Either 'address' or 'mac' must be provided "
"for port %s" % row)
"for port %s" % row
)
ports.append(dict(row, address=address))
return ports
@ -136,32 +137,34 @@ class BaremetalCloudMixin:
raise exc.OpenStackCloudException(
"Refusing to inspect available machine %(node)s "
"which is associated with an instance "
"(instance_uuid %(inst)s)" %
{'node': node.id, 'inst': node.instance_id})
"(instance_uuid %(inst)s)"
% {'node': node.id, 'inst': node.instance_id}
)
return_to_available = True
# NOTE(TheJulia): Changing available machine to managedable state
# and due to state transitions we need to until that transition has
# completed.
node = self.baremetal.set_node_provision_state(node, 'manage',
wait=True,
timeout=timeout)
node = self.baremetal.set_node_provision_state(
node, 'manage', wait=True, timeout=timeout
)
if node.provision_state not in ('manageable', 'inspect failed'):
raise exc.OpenStackCloudException(
"Machine %(node)s must be in 'manageable', 'inspect failed' "
"or 'available' provision state to start inspection, the "
"current state is %(state)s" %
{'node': node.id, 'state': node.provision_state})
"current state is %(state)s"
% {'node': node.id, 'state': node.provision_state}
)
node = self.baremetal.set_node_provision_state(node, 'inspect',
wait=True,
timeout=timeout)
node = self.baremetal.set_node_provision_state(
node, 'inspect', wait=True, timeout=timeout
)
if return_to_available:
node = self.baremetal.set_node_provision_state(node, 'provide',
wait=True,
timeout=timeout)
node = self.baremetal.set_node_provision_state(
node, 'provide', wait=True, timeout=timeout
)
return node
@ -170,19 +173,27 @@ class BaremetalCloudMixin:
try:
yield
except Exception as exc:
self.log.debug("cleaning up node %s because of an error: %s",
node.id, exc)
self.log.debug(
"cleaning up node %s because of an error: %s", node.id, exc
)
tb = sys.exc_info()[2]
try:
self.baremetal.delete_node(node)
except Exception:
self.log.debug("could not remove node %s", node.id,
exc_info=True)
self.log.debug(
"could not remove node %s", node.id, exc_info=True
)
raise exc.with_traceback(tb)
def register_machine(self, nics, wait=False, timeout=3600,
lock_timeout=600, provision_state='available',
**kwargs):
def register_machine(
self,
nics,
wait=False,
timeout=3600,
lock_timeout=600,
provision_state='available',
**kwargs
):
"""Register Baremetal with Ironic
Allows for the registration of Baremetal nodes with Ironic
@ -233,9 +244,10 @@ class BaremetalCloudMixin:
:returns: Current state of the node.
"""
if provision_state not in ('enroll', 'manageable', 'available'):
raise ValueError('Initial provision state must be enroll, '
'manageable or available, got %s'
% provision_state)
raise ValueError(
'Initial provision state must be enroll, '
'manageable or available, got %s' % provision_state
)
# Available is tricky: it cannot be directly requested on newer API
# versions, we need to go through cleaning. But we cannot go through
@ -246,19 +258,24 @@ class BaremetalCloudMixin:
with self._delete_node_on_error(machine):
# Making a node at least manageable
if (machine.provision_state == 'enroll'
and provision_state != 'enroll'):
if (
machine.provision_state == 'enroll'
and provision_state != 'enroll'
):
machine = self.baremetal.set_node_provision_state(
machine, 'manage', wait=True, timeout=timeout)
machine, 'manage', wait=True, timeout=timeout
)
machine = self.baremetal.wait_for_node_reservation(
machine, timeout=lock_timeout)
machine, timeout=lock_timeout
)
# Create NICs before trying to run cleaning
created_nics = []
try:
for port in _normalize_port_list(nics):
nic = self.baremetal.create_port(node_id=machine.id,
**port)
nic = self.baremetal.create_port(
node_id=machine.id, **port
)
created_nics.append(nic.id)
except Exception:
@ -269,10 +286,13 @@ class BaremetalCloudMixin:
pass
raise
if (machine.provision_state != 'available'
and provision_state == 'available'):
if (
machine.provision_state != 'available'
and provision_state == 'available'
):
machine = self.baremetal.set_node_provision_state(
machine, 'provide', wait=wait, timeout=timeout)
machine, 'provide', wait=wait, timeout=timeout
)
return machine
@ -295,15 +315,18 @@ class BaremetalCloudMixin:
:raises: OpenStackCloudException on operation failure.
"""
if wait is not None:
warnings.warn("wait argument is deprecated and has no effect",
DeprecationWarning)
warnings.warn(
"wait argument is deprecated and has no effect",
DeprecationWarning,
)
machine = self.get_machine(uuid)
invalid_states = ['active', 'cleaning', 'clean wait', 'clean failed']
if machine['provision_state'] in invalid_states:
raise exc.OpenStackCloudException(
"Error unregistering node '%s' due to current provision "
"state '%s'" % (uuid, machine['provision_state']))
"state '%s'" % (uuid, machine['provision_state'])
)
# NOTE(TheJulia) There is a high possibility of a lock being present
# if the machine was just moved through the state machine. This was
@ -314,7 +337,8 @@ class BaremetalCloudMixin:
except exc.OpenStackCloudException as e:
raise exc.OpenStackCloudException(
"Error unregistering node '%s': Exception occured while"
" waiting to be able to proceed: %s" % (machine['uuid'], e))
" waiting to be able to proceed: %s" % (machine['uuid'], e)
)
for nic in _normalize_port_list(nics):
try:
@ -381,32 +405,28 @@ class BaremetalCloudMixin:
machine = self.get_machine(name_or_id)
if not machine:
raise exc.OpenStackCloudException(
"Machine update failed to find Machine: %s. " % name_or_id)
"Machine update failed to find Machine: %s. " % name_or_id
)
new_config = dict(machine._to_munch(), **attrs)
try:
patch = jsonpatch.JsonPatch.from_diff(
machine._to_munch(),
new_config)
machine._to_munch(), new_config
)
except Exception as e:
raise exc.OpenStackCloudException(
"Machine update failed - Error generating JSON patch object "
"for submission to the API. Machine: %s Error: %s"
% (name_or_id, e))
% (name_or_id, e)
)
if not patch:
return dict(
node=machine,
changes=None
)
return dict(node=machine, changes=None)
change_list = [change['path'] for change in patch]
node = self.baremetal.update_node(machine, **attrs)
return dict(
node=node,
changes=change_list
)
return dict(node=node, changes=change_list)
def attach_port_to_machine(self, name_or_id, port_name_or_id):
"""Attach a virtual port to the bare metal machine.
@ -459,16 +479,16 @@ class BaremetalCloudMixin:
self.baremetal.validate_node(name_or_id, required=ifaces)
def validate_node(self, uuid):
warnings.warn('validate_node is deprecated, please use '
'validate_machine instead', DeprecationWarning)
warnings.warn(
'validate_node is deprecated, please use '
'validate_machine instead',
DeprecationWarning,
)
self.baremetal.validate_node(uuid)
def node_set_provision_state(self,
name_or_id,
state,
configdrive=None,
wait=False,
timeout=3600):
def node_set_provision_state(
self, name_or_id, state, configdrive=None, wait=False, timeout=3600
):
"""Set Node Provision State
Enables a user to provision a Machine and optionally define a
@ -495,15 +515,17 @@ class BaremetalCloudMixin:
:rtype: :class:`~openstack.baremetal.v1.node.Node`.
"""
node = self.baremetal.set_node_provision_state(
name_or_id, target=state, config_drive=configdrive,
wait=wait, timeout=timeout)
name_or_id,
target=state,
config_drive=configdrive,
wait=wait,
timeout=timeout,
)
return node
def set_machine_maintenance_state(
self,
name_or_id,
state=True,
reason=None):
self, name_or_id, state=True, reason=None
):
"""Set Baremetal Machine Maintenance State
Sets Baremetal maintenance state and maintenance reason.
@ -587,28 +609,33 @@ class BaremetalCloudMixin:
"""
self.baremetal.set_node_power_state(name_or_id, 'rebooting')
def activate_node(self, uuid, configdrive=None,
wait=False, timeout=1200):
def activate_node(self, uuid, configdrive=None, wait=False, timeout=1200):
self.node_set_provision_state(
uuid, 'active', configdrive, wait=wait, timeout=timeout)
uuid, 'active', configdrive, wait=wait, timeout=timeout
)
def deactivate_node(self, uuid, wait=False,
timeout=1200):
def deactivate_node(self, uuid, wait=False, timeout=1200):
self.node_set_provision_state(
uuid, 'deleted', wait=wait, timeout=timeout)
uuid, 'deleted', wait=wait, timeout=timeout
)
def set_node_instance_info(self, uuid, patch):
warnings.warn("The set_node_instance_info call is deprecated, "
"use patch_machine or update_machine instead",
DeprecationWarning)
warnings.warn(
"The set_node_instance_info call is deprecated, "
"use patch_machine or update_machine instead",
DeprecationWarning,
)
return self.patch_machine(uuid, patch)
def purge_node_instance_info(self, uuid):
warnings.warn("The purge_node_instance_info call is deprecated, "
"use patch_machine or update_machine instead",
DeprecationWarning)
return self.patch_machine(uuid,
dict(path='/instance_info', op='remove'))
warnings.warn(
"The purge_node_instance_info call is deprecated, "
"use patch_machine or update_machine instead",
DeprecationWarning,
)
return self.patch_machine(
uuid, dict(path='/instance_info', op='remove')
)
def wait_for_baremetal_node_lock(self, node, timeout=30):
"""Wait for a baremetal node to have no lock.
@ -618,7 +645,10 @@ class BaremetalCloudMixin:
:raises: OpenStackCloudException upon client failure.
:returns: None
"""
warnings.warn("The wait_for_baremetal_node_lock call is deprecated "
"in favor of wait_for_node_reservation on the baremetal "
"proxy", DeprecationWarning)
warnings.warn(
"The wait_for_baremetal_node_lock call is deprecated "
"in favor of wait_for_node_reservation on the baremetal "
"proxy",
DeprecationWarning,
)
self.baremetal.wait_for_node_reservation(node, timeout)

View File

@ -127,8 +127,7 @@ class BlockStorageCloudMixin:
:returns: A volume ``Type`` object if found, else None.
"""
return _utils._get_entity(
self, 'volume_type', name_or_id, filters)
return _utils._get_entity(self, 'volume_type', name_or_id, filters)
def create_volume(
self,
@ -162,7 +161,9 @@ class BlockStorageCloudMixin:
raise exc.OpenStackCloudException(
"Image {image} was requested as the basis for a new"
" volume, but was not found on the cloud".format(
image=image))
image=image
)
)
kwargs['imageRef'] = image_obj['id']
kwargs = self._get_volume_kwargs(kwargs)
kwargs['size'] = size
@ -193,10 +194,10 @@ class BlockStorageCloudMixin:
volume = self.get_volume(name_or_id)
if not volume:
raise exc.OpenStackCloudException(
"Volume %s not found." % name_or_id)
"Volume %s not found." % name_or_id
)
volume = self.block_storage.update_volume(
volume, **kwargs)
volume = self.block_storage.update_volume(volume, **kwargs)
self.list_volumes.invalidate(self)
@ -219,7 +220,9 @@ class BlockStorageCloudMixin:
if not volume:
raise exc.OpenStackCloudException(
"Volume {name_or_id} does not exist".format(
name_or_id=name_or_id))
name_or_id=name_or_id
)
)
self.block_storage.set_volume_bootable_status(volume, bootable)
@ -249,7 +252,8 @@ class BlockStorageCloudMixin:
self.log.debug(
"Volume %(name_or_id)s does not exist",
{'name_or_id': name_or_id},
exc_info=True)
exc_info=True,
)
return False
try:
self.block_storage.delete_volume(volume, force=force)
@ -297,10 +301,12 @@ class BlockStorageCloudMixin:
project_id = proj.id
params['tenant_id'] = project_id
error_msg = "{msg} for the project: {project} ".format(
msg=error_msg, project=name_or_id)
msg=error_msg, project=name_or_id
)
data = proxy._json_response(
self.block_storage.get('/limits', params=params))
self.block_storage.get('/limits', params=params)
)
limits = self._get_and_munchify('limits', data)
return limits
@ -413,22 +419,23 @@ class BlockStorageCloudMixin:
# If we got volume as dict we need to re-fetch it to be able to
# use wait_for_status.
volume = self.block_storage.get_volume(volume['id'])
self.block_storage.wait_for_status(
volume, 'in-use', wait=timeout)
self.block_storage.wait_for_status(volume, 'in-use', wait=timeout)
return attachment
def _get_volume_kwargs(self, kwargs):
name = kwargs.pop('name', kwargs.pop('display_name', None))
description = kwargs.pop('description',
kwargs.pop('display_description', None))
description = kwargs.pop(
'description', kwargs.pop('display_description', None)
)
if name:
kwargs['name'] = name
if description:
kwargs['description'] = description
return kwargs
@_utils.valid_kwargs('name', 'display_name',
'description', 'display_description')
@_utils.valid_kwargs(
'name', 'display_name', 'description', 'display_description'
)
def create_volume_snapshot(
self,
volume_id,
@ -459,7 +466,8 @@ class BlockStorageCloudMixin:
snapshot = self.block_storage.create_snapshot(**payload)
if wait:
snapshot = self.block_storage.wait_for_status(
snapshot, wait=timeout)
snapshot, wait=timeout
)
return snapshot
@ -499,8 +507,7 @@ class BlockStorageCloudMixin:
:returns: A volume ``Snapshot`` object if found, else None.
"""
return _utils._get_entity(self, 'volume_snapshot', name_or_id,
filters)
return _utils._get_entity(self, 'volume_snapshot', name_or_id, filters)
def create_volume_backup(
self,
@ -572,8 +579,7 @@ class BlockStorageCloudMixin:
:returns: A volume ``Backup`` object if found, else None.
"""
return _utils._get_entity(self, 'volume_backup', name_or_id,
filters)
return _utils._get_entity(self, 'volume_backup', name_or_id, filters)
def list_volume_snapshots(self, detailed=True, filters=None):
"""List all volume snapshots.
@ -615,8 +621,9 @@ class BlockStorageCloudMixin:
return list(self.block_storage.backups(details=detailed, **filters))
def delete_volume_backup(self, name_or_id=None, force=False, wait=False,
timeout=None):
def delete_volume_backup(
self, name_or_id=None, force=False, wait=False, timeout=None
):
"""Delete a volume backup.
:param name_or_id: Name or unique ID of the volume backup.
@ -635,7 +642,8 @@ class BlockStorageCloudMixin:
return False
self.block_storage.delete_backup(
volume_backup, ignore_missing=False, force=force)
volume_backup, ignore_missing=False, force=force
)
if wait:
self.block_storage.wait_for_delete(volume_backup, wait=timeout)
@ -663,7 +671,8 @@ class BlockStorageCloudMixin:
return False
self.block_storage.delete_snapshot(
volumesnapshot, ignore_missing=False)
volumesnapshot, ignore_missing=False
)
if wait:
self.block_storage.wait_for_delete(volumesnapshot, wait=timeout)
@ -695,8 +704,7 @@ class BlockStorageCloudMixin:
:returns: A list of volume ``Volume`` objects, if any are found.
"""
volumes = self.list_volumes()
return _utils._filter_list(
volumes, name_or_id, filters)
return _utils._filter_list(volumes, name_or_id, filters)
def search_volume_snapshots(self, name_or_id=None, filters=None):
"""Search for one or more volume snapshots.
@ -723,8 +731,7 @@ class BlockStorageCloudMixin:
:returns: A list of volume ``Snapshot`` objects, if any are found.
"""
volumesnapshots = self.list_volume_snapshots()
return _utils._filter_list(
volumesnapshots, name_or_id, filters)
return _utils._filter_list(volumesnapshots, name_or_id, filters)
def search_volume_backups(self, name_or_id=None, filters=None):
"""Search for one or more volume backups.
@ -751,8 +758,7 @@ class BlockStorageCloudMixin:
:returns: A list of volume ``Backup`` objects, if any are found.
"""
volume_backups = self.list_volume_backups()
return _utils._filter_list(
volume_backups, name_or_id, filters)
return _utils._filter_list(volume_backups, name_or_id, filters)
# TODO(stephenfin): Remove 'get_extra' in a future major version
def search_volume_types(
@ -797,7 +803,8 @@ class BlockStorageCloudMixin:
volume_type = self.get_volume_type(name_or_id)
if not volume_type:
raise exc.OpenStackCloudException(
"VolumeType not found: %s" % name_or_id)
"VolumeType not found: %s" % name_or_id
)
return self.block_storage.get_type_access(volume_type)
@ -814,7 +821,8 @@ class BlockStorageCloudMixin:
volume_type = self.get_volume_type(name_or_id)
if not volume_type:
raise exc.OpenStackCloudException(
"VolumeType not found: %s" % name_or_id)
"VolumeType not found: %s" % name_or_id
)
self.block_storage.add_type_access(volume_type, project_id)
@ -829,7 +837,8 @@ class BlockStorageCloudMixin:
volume_type = self.get_volume_type(name_or_id)
if not volume_type:
raise exc.OpenStackCloudException(
"VolumeType not found: %s" % name_or_id)
"VolumeType not found: %s" % name_or_id
)
self.block_storage.remove_type_access(volume_type, project_id)
def set_volume_quotas(self, name_or_id, **kwargs):
@ -842,12 +851,11 @@ class BlockStorageCloudMixin:
quota does not exist.
"""
proj = self.identity.find_project(
name_or_id, ignore_missing=False)
proj = self.identity.find_project(name_or_id, ignore_missing=False)
self.block_storage.update_quota_set(
_qs.QuotaSet(project_id=proj.id),
**kwargs)
_qs.QuotaSet(project_id=proj.id), **kwargs
)
def get_volume_quotas(self, name_or_id):
"""Get volume quotas for a project

View File

@ -23,10 +23,12 @@ class ClusteringCloudMixin:
def _clustering_client(self):
if 'clustering' not in self._raw_clients:
clustering_client = self._get_versioned_client(
'clustering', min_version=1, max_version='1.latest')
'clustering', min_version=1, max_version='1.latest'
)
self._raw_clients['clustering'] = clustering_client
return self._raw_clients['clustering']
# NOTE(gtema): work on getting rid of direct API calls showed that this
# implementation never worked properly and tests in reality verifying wrong
# things. Unless someone is really interested in this piece of code this will

View File

@ -19,7 +19,6 @@ from openstack.cloud import exc
class CoeCloudMixin:
@_utils.cache_on_arguments()
def list_coe_clusters(self):
"""List COE (Container Orchestration Engine) cluster.
@ -72,7 +71,10 @@ class CoeCloudMixin:
return _utils._get_entity(self, 'coe_cluster', name_or_id, filters)
def create_coe_cluster(
self, name, cluster_template_id, **kwargs,
self,
name,
cluster_template_id,
**kwargs,
):
"""Create a COE cluster based on given cluster template.
@ -133,11 +135,11 @@ class CoeCloudMixin:
cluster = self.get_coe_cluster(name_or_id)
if not cluster:
raise exc.OpenStackCloudException(
"COE cluster %s not found." % name_or_id)
"COE cluster %s not found." % name_or_id
)
cluster = self.container_infrastructure_management.update_cluster(
cluster,
**kwargs
cluster, **kwargs
)
return cluster
@ -149,8 +151,11 @@ class CoeCloudMixin:
:returns: Details about the CA certificate for the given cluster.
"""
return self.container_infrastructure_management\
.get_cluster_certificate(cluster_id)
return (
self.container_infrastructure_management.get_cluster_certificate(
cluster_id
)
)
def sign_coe_cluster_certificate(self, cluster_id, csr):
"""Sign client key and generate the CA certificate for a cluster
@ -164,10 +169,9 @@ class CoeCloudMixin:
:raises: OpenStackCloudException on operation error.
"""
return self.container_infrastructure_management\
.create_cluster_certificate(
cluster_uuid=cluster_id,
csr=csr)
return self.container_infrastructure_management.create_cluster_certificate( # noqa: E501
cluster_uuid=cluster_id, csr=csr
)
@_utils.cache_on_arguments()
def list_cluster_templates(self, detail=False):
@ -182,10 +186,12 @@ class CoeCloudMixin:
the OpenStack API call.
"""
return list(
self.container_infrastructure_management.cluster_templates())
self.container_infrastructure_management.cluster_templates()
)
def search_cluster_templates(
self, name_or_id=None, filters=None, detail=False):
self, name_or_id=None, filters=None, detail=False
):
"""Search cluster templates.
:param name_or_id: cluster template name or ID.
@ -199,8 +205,7 @@ class CoeCloudMixin:
the OpenStack API call.
"""
cluster_templates = self.list_cluster_templates(detail=detail)
return _utils._filter_list(
cluster_templates, name_or_id, filters)
return _utils._filter_list(cluster_templates, name_or_id, filters)
def get_cluster_template(self, name_or_id, filters=None, detail=False):
"""Get a cluster template by name or ID.
@ -225,11 +230,16 @@ class CoeCloudMixin:
cluster template is found.
"""
return _utils._get_entity(
self, 'cluster_template', name_or_id,
filters=filters, detail=detail)
self,
'cluster_template',
name_or_id,
filters=filters,
detail=detail,
)
def create_cluster_template(
self, name, image_id=None, keypair_id=None, coe=None, **kwargs):
self, name, image_id=None, keypair_id=None, coe=None, **kwargs
):
"""Create a cluster template.
:param string name: Name of the cluster template.
@ -243,14 +253,15 @@ class CoeCloudMixin:
:raises: ``OpenStackCloudException`` if something goes wrong during
the OpenStack API call
"""
cluster_template = self.container_infrastructure_management \
.create_cluster_template(
cluster_template = (
self.container_infrastructure_management.create_cluster_template(
name=name,
image_id=image_id,
keypair_id=keypair_id,
coe=coe,
**kwargs,
)
)
return cluster_template
@ -270,11 +281,13 @@ class CoeCloudMixin:
self.log.debug(
"Cluster template %(name_or_id)s does not exist",
{'name_or_id': name_or_id},
exc_info=True)
exc_info=True,
)
return False
self.container_infrastructure_management.delete_cluster_template(
cluster_template)
cluster_template
)
return True
def update_cluster_template(self, name_or_id, **kwargs):
@ -289,14 +302,15 @@ class CoeCloudMixin:
cluster_template = self.get_cluster_template(name_or_id)
if not cluster_template:
raise exc.OpenStackCloudException(
"Cluster template %s not found." % name_or_id)
cluster_template = self.container_infrastructure_management \
.update_cluster_template(
cluster_template,
**kwargs
"Cluster template %s not found." % name_or_id
)
cluster_template = (
self.container_infrastructure_management.update_cluster_template(
cluster_template, **kwargs
)
)
return cluster_template
def list_magnum_services(self):

View File

</
@ -114,12 +114,15 @@ class ComputeCloudMixin:
"""
flavors = self.list_flavors(get_extra=get_extra)
for flavor in sorted(flavors, key=operator.itemgetter('ram')):
if (flavor['ram'] >= ram
and (not include or include in flavor['name'])):
if flavor['ram'] >= ram and (
not include or include in flavor['name']
):
return flavor
raise exc.OpenStackCloudException(
"Could not find a flavor with {ram} and '{include}'".format(
ram=ram, include=include))
ram=ram, include=include
)
)
@_utils.cache_on_arguments()
def _nova_extensions(self):
@ -155,8 +158,12 @@ class ComputeCloudMixin:
return _utils._filter_list(flavors, name_or_id, filters)
def search_servers(
self, name_or_id=None, filters=None, detailed=False,
all_projects=False, bare=False,
self,
name_or_id=None,
filters=None,
detailed=False,
all_projects=False,
bare=False,
):
"""Search servers.
@ -169,7 +176,8 @@ class ComputeCloudMixin:
criteria.
"""
servers = self.list_servers(
detailed=detailed, all_projects=all_projects, bare=bare)
detailed=detailed, all_projects=all_projects, bare=bare
)
return _utils._filter_list(servers, name_or_id, filters)
def search_server_groups(self, name_or_id=None, filters=None):
@ -213,8 +221,8 @@ class ComputeCloudMixin:
return ret
except exceptions.SDKException:
self.log.debug(
"Availability zone list could not be fetched",
exc_info=True)
"Availability zone list could not be fetched", exc_info=True
)
return []
@_utils.cache_on_arguments()
@ -226,8 +234,9 @@ class ComputeCloudMixin:
clouds.yaml by setting openstack.cloud.get_extra_specs to False.
:returns: A list of compute ``Flavor`` objects.
"""
return list(self.compute.flavors(
details=True, get_extra_specs=get_extra))
return list(
self.compute.flavors(details=True, get_extra_specs=get_extra)
)
def list_server_security_groups(self, server):
"""List all security groups associated with the given server.
@ -268,8 +277,9 @@ class ComputeCloudMixin:
sg = self.get_security_group(sg)
if sg is None:
self.log.debug('Security group %s not found for adding',
sg)
self.log.debug(
'Security group %s not found for adding', sg
)
return None, None
@ -288,7 +298,8 @@ class ComputeCloudMixin:
:raises: ``OpenStackCloudException``, on operation error.
"""
server, security_groups = self._get_server_security_groups(
server, security_groups)
server, security_groups
)
if not (server and security_groups):
return False
@ -310,7 +321,8 @@ class ComputeCloudMixin:
:raises: ``OpenStackCloudException``, on operation error.
"""
server, security_groups = self._get_server_security_groups(
server, security_groups)
server, security_groups
)
if not (server and security_groups):
return False
@ -327,7 +339,10 @@ class ComputeCloudMixin:
# error? Nova returns ok if you try to add a group twice.
self.log.debug(
"The security group %s was not present on server %s so "
"no action was performed", sg.name, server.name)
"no action was performed",
sg.name,
server.name,
)
ret = False
return ret
@ -377,7 +392,8 @@ class ComputeCloudMixin:
self._servers = self._list_servers(
detailed=detailed,
all_projects=all_projects,
bare=bare)
bare=bare,
)
self._servers_time = time.time()
finally:
self._servers_lock.release()
@ -386,14 +402,15 @@ class ComputeCloudMixin:
# list from the cloud, we still return a filtered list.
return _utils._filter_list(self._servers, None, filters)
def _list_servers(self, detailed=False, all_projects=False, bare=False,
filters=None):
def _list_servers(
self, detailed=False, all_projects=False, bare=False, filters=None
):
filters = filters or {}
return [
self._expand_server(server, detailed, bare)
for server in self.compute.servers(
all_projects=all_projects,
**filters)
all_projects=all_projects, **filters
)
]
def list_server_groups(self):
@ -472,12 +489,15 @@ class ComputeCloudMixin:
if not filters:
filters = {}
flavor = self.compute.find_flavor(
name_or_id, get_extra_specs=get_extra,
ignore_missing=True, **filters)
name_or_id,
get_extra_specs=get_extra,
ignore_missing=True,
**filters,
)
return flavor
def get_flavor_by_id(self, id, get_extra=False):
""" Get a flavor by ID
"""Get a flavor by ID
:param id: ID of the flavor.
:param get_extra: Whether or not the list_flavors call should get the
@ -505,7 +525,8 @@ class ComputeCloudMixin:
if not server:
raise exc.OpenStackCloudException(
"Console log requested for invalid server")
"Console log requested for invalid server"
)
try:
return self._get_server_console_output(server['id'], length)
@ -514,8 +535,7 @@ class ComputeCloudMixin:
def _get_server_console_output(self, server_id, length=None):
output = self.compute.get_server_console_output(
server=server_id,
length=length
server=server_id, length=length
)
if 'output' in output:
return output['output']
@ -555,9 +575,12 @@ class ComputeCloudMixin:
the current auth scoped project.
:returns: A compute ``Server`` object if found, else None.
"""
searchfunc = functools.partial(self.search_servers,
detailed=detailed, bare=True,
all_projects=all_projects)
searchfunc = functools.partial(
self.search_servers,
detailed=detailed,
bare=True,
all_projects=all_projects,
)
server = _utils._get_entity(self, searchfunc, name_or_id, filters)
return self._expand_server(server, detailed, bare)
@ -600,8 +623,7 @@ class ComputeCloudMixin:
:returns: A compute ``ServerGroup`` object if found, else None.
"""
return _utils._get_entity(self, 'server_group', name_or_id,
filters)
return _utils._get_entity(self, 'server_group', name_or_id, filters)
def create_keypair(self, name, public_key=None):
"""Create a new keypair.
@ -664,10 +686,12 @@ class ComputeCloudMixin:
if not server_obj:
raise exc.OpenStackCloudException(
"Server {server} could not be found and therefore"
" could not be snapshotted.".format(server=server))
" could not be snapshotted.".format(server=server)
)
server = server_obj
image = self.compute.create_server_image(
server, name=name, metadata=metadata, wait=wait, timeout=timeout)
server, name=name, metadata=metadata, wait=wait, timeout=timeout
)
return image
def get_server_id(self, name_or_id):
@ -709,12 +733,25 @@ class ComputeCloudMixin:
return dict(server_vars=server_vars, groups=groups)
@_utils.valid_kwargs(
'meta', 'files', 'userdata', 'description',
'reservation_id', 'return_raw', 'min_count',
'max_count', 'security_groups', 'key_name',
'availability_zone', 'block_device_mapping',
'block_device_mapping_v2', 'nics', 'scheduler_hints',
'config_drive', 'admin_pass', 'disk_config')
'meta',
'files',
'userdata',
'description',
'reservation_id',
'return_raw',
'min_count',
'max_count',
'security_groups',
'key_name',
'availability_zone',
'block_device_mapping',
'block_device_mapping_v2',
'nics',
'scheduler_hints',
'config_drive',
'admin_pass',
'disk_config',
)
def create_server(
self,
name,
@ -818,10 +855,12 @@ class ComputeCloudMixin:
# after image in the argument list. Doh.
if not flavor:
raise TypeError(
"create_server() missing 1 required argument: 'flavor'")
"create_server() missing 1 required argument: 'flavor'"
)
if not image and not boot_volume:
raise TypeError(
"create_server() requires either 'image' or 'boot_volume'")
"create_server() requires either 'image' or 'boot_volume'"
)
# TODO(mordred) Add support for description starting in 2.19
security_groups = kwargs.get('security_groups', [])
@ -836,11 +875,12 @@ class ComputeCloudMixin:
if user_data:
kwargs['user_data'] = self._encode_server_userdata(user_data)
for (desired, given) in (
('OS-DCF:diskConfig', 'disk_config'),
('config_drive', 'config_drive'),
('key_name', 'key_name'),
('metadata', 'meta'),
('adminPass', 'admin_pass')):
('OS-DCF:diskConfig', 'disk_config'),
('config_drive', 'config_drive'),
('key_name', 'key_name'),
('metadata', 'meta'),
('adminPass', 'admin_pass'),
):
value = kwargs.pop(given, None)
if value:
kwargs[desired] = value
@ -850,7 +890,8 @@ class ComputeCloudMixin:
if not group_obj:
raise exc.OpenStackCloudException(
"Server Group {group} was requested but was not found"
" on the cloud".format(group=group))
" on the cloud".format(group=group)
)
if 'scheduler_hints' not in kwargs:
kwargs['scheduler_hints'] = {}
kwargs['scheduler_hints']['group'] = group_obj['id']
@ -865,7 +906,8 @@ class ComputeCloudMixin:
else:
raise exc.OpenStackCloudException(
'nics parameter to create_server takes a list of dicts.'
' Got: {nics}'.format(nics=kwargs['nics']))
' Got: {nics}'.format(nics=kwargs['nics'])
)
if network and ('nics' not in kwargs or not kwargs['nics']):
nics = []
@ -881,7 +923,10 @@ class ComputeCloudMixin:
'Network {network} is not a valid network in'
' {cloud}:{region}'.format(
network=network,
cloud=self.name, region=self._compute_region))
cloud=self.name,
region=self._compute_region,
)
)
nics.append({'net-id': network_obj['id']})
kwargs['nics'] = nics
@ -904,14 +949,17 @@ class ComputeCloudMixin:
if not nic_net:
raise exc.OpenStackCloudException(
"Requested network {net} could not be found.".format(
net=net_name))
net=net_name
)
)
net['uuid'] = nic_net['id']
for ip_key in ('v4-fixed-ip', 'v6-fixed-ip', 'fixed_ip'):
fixed_ip = nic.pop(ip_key, None)
if fixed_ip and net.get('fixed_ip'):
raise exc.OpenStackCloudException(
"Only one of v4-fixed-ip, v6-fixed-ip or fixed_ip"
" may be given")
" may be given"
)
if fixed_ip:
net['fixed_ip'] = fixed_ip
for key in ('port', 'port-id'):
@ -920,13 +968,13 @@ class ComputeCloudMixin:
# A tag supported only in server microversion 2.32-2.36 or >= 2.42
# Bumping the version to 2.42 to support the 'tag' implementation
if 'tag' in nic:
utils.require_microversion(
self.compute, '2.42')
utils.require_microversion(self.compute, '2.42')
net['tag'] = nic.pop('tag')
if nic:
raise exc.OpenStackCloudException(
"Additional unsupported keys given for server network"
" creation: {keys}".format(keys=nic.keys()))
" creation: {keys}".format(keys=nic.keys())
)
networks.append(net)
if networks:
kwargs['networks'] = networks
@ -954,10 +1002,14 @@ class ComputeCloudMixin:
boot_volume = root_volume
kwargs = self._get_boot_from_volume_kwargs(
image=image, boot_from_volume=boot_from_volume,
boot_volume=boot_volume, volume_size=str(volume_size),
image=image,
boot_from_volume=boot_from_volume,
boot_volume=boot_volume,
volume_size=str(volume_size),
terminate_volume=terminate_volume,
volumes=volumes, kwargs=kwargs)
volumes=volumes,
kwargs=kwargs,
)
kwargs['name'] = name
@ -977,14 +1029,18 @@ class ComputeCloudMixin:
server = self.compute.get_server(server.id)
if server.status == 'ERROR':
raise exc.OpenStackCloudCreateException(
resource='server', resource_id=server.id)
resource='server', resource_id=server.id
)
server = meta.add_server_interfaces(self, server)
else:
server = self.wait_for_server(
server,
auto_ip=auto_ip, ips=ips, ip_pool=ip_pool,
reuse=reuse_ips, timeout=timeout,
auto_ip=auto_ip,
ips=ips,
ip_pool=ip_pool,
reuse=reuse_ips,
timeout=timeout,
nat_destination=nat_destination,
)
@ -992,8 +1048,15 @@ class ComputeCloudMixin:
return server
def _get_boot_from_volume_kwargs(
self, image, boot_from_volume, boot_volume, volume_size,
terminate_volume, volumes, kwargs):
self,
image,
boot_from_volume,
boot_volume,
volume_size,
terminate_volume,
volumes,
kwargs,
):
"""Return block device mappings
:param image: Image dict, name or id to boot with.
@ -1015,7 +1078,10 @@ class ComputeCloudMixin:
'Volume {boot_volume} is not a valid volume'
' in {cloud}:{region}'.format(
boot_volume=boot_volume,
cloud=self.name, region=self._compute_region))
cloud=self.name,
region=self._compute_region,
)
)
block_mapping = {
'boot_index': '0',
'delete_on_termination': terminate_volume,
@ -1036,7 +1102,10 @@ class ComputeCloudMixin:
'Image {image} is not a valid image in'
' {cloud}:{region}'.format(
image=image,
cloud=self.name, region=self._compute_region))
cloud=self.name,
region=self._compute_region,
)
)
block_mapping = {
'boot_index': '0',
@ -1066,7 +1135,10 @@ class ComputeCloudMixin:
'Volume {volume} is not a valid volume'
' in {cloud}:{region}'.format(
volume=volume,
cloud=self.name, region=self._compute_region))
cloud=self.name,
region=self._compute_region,
)
)
block_mapping = {
'boot_index': '-1',
'delete_on_termination': False,
@ -1080,8 +1152,15 @@ class ComputeCloudMixin:
return kwargs
def wait_for_server(
self, server, auto_ip=True, ips=None, ip_pool=None,
reuse=True, timeout=180, nat_destination=None):
self,
server,
auto_ip=True,
ips=None,
ip_pool=None,
reuse=True,
timeout=180,
nat_destination=None,
):
"""
Wait for a server to reach ACTIVE status.
"""
@ -1094,11 +1173,12 @@ class ComputeCloudMixin:
# There is no point in iterating faster than the list_servers cache
for count in utils.iterate_timeout(
timeout,
timeout_message,
# if _SERVER_AGE is 0 we still want to wait a bit
# to be friendly with the server.
wait=self._SERVER_AGE or 2):
timeout,
timeout_message,