mypy: cinder/volume/volume_types.py

Change-Id: I89fc0f75d4839fedc4fc771e6d23b9526109cf4a
This commit is contained in:
Eric Harney 2022-03-30 12:36:21 -04:00
parent be71720bb9
commit 04b6700f48
3 changed files with 66 additions and 36 deletions

View File

@ -216,7 +216,7 @@ class API(base.Base):
def create(self, def create(self,
context: context.RequestContext, context: context.RequestContext,
size: Optional[Union[str, int]], size: Union[str, int],
name: Optional[str], name: Optional[str],
description: Optional[str], description: Optional[str],
snapshot: Optional[objects.Snapshot] = None, snapshot: Optional[objects.Snapshot] = None,

View File

@ -21,7 +21,7 @@
from __future__ import annotations from __future__ import annotations
from typing import Optional from typing import Any, Iterable, Optional, Union # noqa: H301
from oslo_config import cfg from oslo_config import cfg
from oslo_db import exception as db_exc from oslo_db import exception as db_exc
@ -48,12 +48,12 @@ MIN_SIZE_KEY = "provisioning:min_vol_size"
MAX_SIZE_KEY = "provisioning:max_vol_size" MAX_SIZE_KEY = "provisioning:max_vol_size"
def create(context, def create(context: context.RequestContext,
name, name: str,
extra_specs=None, extra_specs: Optional[dict[str, Any]] = None,
is_public=True, is_public: bool = True,
projects=None, projects: Optional[list[str]] = None,
description=None): description: Optional[str] = None):
"""Creates volume types.""" """Creates volume types."""
extra_specs = extra_specs or {} extra_specs = extra_specs or {}
projects = projects or [] projects = projects or []
@ -72,7 +72,11 @@ def create(context,
return type_ref return type_ref
def update(context, id, name, description, is_public=None): def update(context: context.RequestContext,
id: Optional[str],
name: Optional[str],
description: Optional[str],
is_public: Optional[bool] = None) -> None:
"""Update volume type by id.""" """Update volume type by id."""
if id is None: if id is None:
msg = _("id cannot be None") msg = _("id cannot be None")
@ -105,7 +109,7 @@ def update(context, id, name, description, is_public=None):
raise exception.VolumeTypeUpdateFailed(id=id) raise exception.VolumeTypeUpdateFailed(id=id)
def destroy(context, id): def destroy(context: context.RequestContext, id: str) -> dict[str, Any]:
"""Marks volume types as deleted. """Marks volume types as deleted.
There must exist at least one volume type (i.e. the default type) in There must exist at least one volume type (i.e. the default type) in
@ -145,9 +149,15 @@ def destroy(context, id):
return db.volume_type_destroy(elevated, id) return db.volume_type_destroy(elevated, id)
def get_all_types(context, inactive=0, filters=None, marker=None, def get_all_types(context: context.RequestContext,
limit=None, sort_keys=None, sort_dirs=None, inactive: int = 0,
offset=None, list_result=False): filters: Optional[dict] = None,
marker: Optional[dict[str, Any]] = None,
limit: Optional[int] = None,
sort_keys: Optional[list[str]] = None,
sort_dirs: Optional[list[str]] = None,
offset: Optional[int] = None,
list_result: bool = False) -> Union[dict[str, Any], list]:
"""Get all non-deleted volume_types. """Get all non-deleted volume_types.
Pass true as argument if you want deleted volume types returned also. Pass true as argument if you want deleted volume types returned also.
@ -161,7 +171,10 @@ def get_all_types(context, inactive=0, filters=None, marker=None,
return vol_types return vol_types
def get_volume_type(ctxt, id, expected_fields=None): def get_volume_type(
ctxt: Optional[context.RequestContext],
id: Optional[str],
expected_fields: Optional[Iterable[str]] = None) -> dict[str, Any]:
"""Retrieves single volume type by id.""" """Retrieves single volume type by id."""
if id is None: if id is None:
msg = _("id cannot be None") msg = _("id cannot be None")
@ -173,7 +186,8 @@ def get_volume_type(ctxt, id, expected_fields=None):
return db.volume_type_get(ctxt, id, expected_fields=expected_fields) return db.volume_type_get(ctxt, id, expected_fields=expected_fields)
def get_by_name_or_id(context, identity): def get_by_name_or_id(context: context.RequestContext,
identity: str) -> dict[str, Any]:
"""Retrieves volume type by id or name""" """Retrieves volume type by id or name"""
if uuidutils.is_uuid_like(identity): if uuidutils.is_uuid_like(identity):
# both name and id can be in uuid format # both name and id can be in uuid format
@ -189,7 +203,8 @@ def get_by_name_or_id(context, identity):
return get_volume_type_by_name(context, identity) return get_volume_type_by_name(context, identity)
def get_volume_type_by_name(context, name): def get_volume_type_by_name(context: context.RequestContext,
name: Optional[str]) -> dict[str, Any]:
"""Retrieves single volume type by name.""" """Retrieves single volume type by name."""
if name is None: if name is None:
msg = _("name cannot be None") msg = _("name cannot be None")
@ -198,7 +213,8 @@ def get_volume_type_by_name(context, name):
return db.volume_type_get_by_name(context, name) return db.volume_type_get_by_name(context, name)
def get_default_volume_type(contxt=None): def get_default_volume_type(
contxt: Optional[context.RequestContext] = None) -> dict[str, Any]:
"""Get the default volume type. """Get the default volume type.
:raises VolumeTypeDefaultMisconfiguredError: when the configured default :raises VolumeTypeDefaultMisconfiguredError: when the configured default
@ -226,7 +242,9 @@ def get_default_volume_type(contxt=None):
return vol_type return vol_type
def get_volume_type_extra_specs(volume_type_id, key=False): def get_volume_type_extra_specs(
volume_type_id: str,
key: Union[str, bool] = False) -> Union[dict, bool]:
volume_type = get_volume_type(context.get_admin_context(), volume_type = get_volume_type(context.get_admin_context(),
volume_type_id) volume_type_id)
extra_specs = volume_type['extra_specs'] extra_specs = volume_type['extra_specs']
@ -239,18 +257,19 @@ def get_volume_type_extra_specs(volume_type_id, key=False):
return extra_specs return extra_specs
def is_public_volume_type(context, volume_type_id): def is_public_volume_type(context: context.RequestContext,
volume_type_id: str) -> bool:
"""Return is_public boolean value of volume type""" """Return is_public boolean value of volume type"""
volume_type = db.volume_type_get(context, volume_type_id) volume_type = db.volume_type_get(context, volume_type_id)
return volume_type['is_public'] return volume_type['is_public']
@utils.if_notifications_enabled @utils.if_notifications_enabled
def notify_about_volume_type_access_usage(context, def notify_about_volume_type_access_usage(context: context.RequestContext,
volume_type_id, volume_type_id: str,
project_id, project_id: str,
event_suffix, event_suffix: str,
host=None): host: Optional[str] = None) -> None:
"""Notify about successful usage type-access-(add/remove) command. """Notify about successful usage type-access-(add/remove) command.
:param context: security context :param context: security context
@ -271,7 +290,9 @@ def notify_about_volume_type_access_usage(context,
notifier_info) notifier_info)
def add_volume_type_access(context, volume_type_id, project_id): def add_volume_type_access(context: context.RequestContext,
volume_type_id: Optional[str],
project_id: str) -> None:
"""Add access to volume type for project_id.""" """Add access to volume type for project_id."""
if volume_type_id is None: if volume_type_id is None:
msg = _("volume_type_id cannot be None") msg = _("volume_type_id cannot be None")
@ -290,7 +311,9 @@ def add_volume_type_access(context, volume_type_id, project_id):
'access.add') 'access.add')
def remove_volume_type_access(context, volume_type_id, project_id): def remove_volume_type_access(context: context.RequestContext,
volume_type_id: Optional[str],
project_id: str) -> None:
"""Remove access to volume type for project_id.""" """Remove access to volume type for project_id."""
if volume_type_id is None: if volume_type_id is None:
msg = _("volume_type_id cannot be None") msg = _("volume_type_id cannot be None")
@ -314,7 +337,9 @@ def is_encrypted(context: context.RequestContext,
return get_volume_type_encryption(context, volume_type_id) is not None return get_volume_type_encryption(context, volume_type_id) is not None
def get_volume_type_encryption(context, volume_type_id): def get_volume_type_encryption(
context: context.RequestContext,
volume_type_id: Optional[str]) -> Optional[dict]:
if volume_type_id is None: if volume_type_id is None:
return None return None
@ -322,7 +347,7 @@ def get_volume_type_encryption(context, volume_type_id):
return encryption return encryption
def get_volume_type_qos_specs(volume_type_id): def get_volume_type_qos_specs(volume_type_id: str) -> dict[str, Any]:
"""Get all qos specs for given volume type.""" """Get all qos specs for given volume type."""
ctxt = context.get_admin_context() ctxt = context.get_admin_context()
res = db.volume_type_qos_specs_get(ctxt, res = db.volume_type_qos_specs_get(ctxt,
@ -331,8 +356,8 @@ def get_volume_type_qos_specs(volume_type_id):
def volume_types_diff(context: context.RequestContext, def volume_types_diff(context: context.RequestContext,
vol_type_id1, vol_type_id1: str,
vol_type_id2) -> tuple[dict, bool]: vol_type_id2: str) -> tuple[dict[str, Any], bool]:
"""Returns a 'diff' of two volume types and whether they are equal. """Returns a 'diff' of two volume types and whether they are equal.
Returns a tuple of (diff, equal), where 'equal' is a boolean indicating Returns a tuple of (diff, equal), where 'equal' is a boolean indicating
@ -359,13 +384,13 @@ def volume_types_diff(context: context.RequestContext,
{...}} {...}}
} }
""" """
def _fix_qos_specs(qos_specs): def _fix_qos_specs(qos_specs: Optional[dict]) -> None:
if qos_specs: if qos_specs:
for key in QOS_IGNORED_FIELDS: for key in QOS_IGNORED_FIELDS:
qos_specs.pop(key, None) qos_specs.pop(key, None)
qos_specs.update(qos_specs.pop('specs', {})) qos_specs.update(qos_specs.pop('specs', {}))
def _fix_encryption_specs(encryption): def _fix_encryption_specs(encryption: Optional[dict]) -> Optional[dict]:
if encryption: if encryption:
encryption = dict(encryption) encryption = dict(encryption)
for param in ENCRYPTION_IGNORED_FIELDS: for param in ENCRYPTION_IGNORED_FIELDS:
@ -373,7 +398,7 @@ def volume_types_diff(context: context.RequestContext,
return encryption return encryption
def _dict_diff(dict1: Optional[dict], def _dict_diff(dict1: Optional[dict],
dict2: Optional[dict]) -> tuple[dict, bool]: dict2: Optional[dict]) -> tuple[dict[str, Any], bool]:
res = {} res = {}
equal = True equal = True
if dict1 is None: if dict1 is None:
@ -426,9 +451,11 @@ def volume_types_diff(context: context.RequestContext,
return (diff, all_equal) return (diff, all_equal)
def volume_types_encryption_changed(context, vol_type_id1, vol_type_id2): def volume_types_encryption_changed(
context: context.RequestContext,
vol_type_id1: str, vol_type_id2: str) -> bool:
"""Return whether encryptions of two volume types are same.""" """Return whether encryptions of two volume types are same."""
def _get_encryption(enc): def _get_encryption(enc: dict) -> dict:
enc = dict(enc) enc = dict(enc)
for param in ENCRYPTION_IGNORED_FIELDS: for param in ENCRYPTION_IGNORED_FIELDS:
enc.pop(param, None) enc.pop(param, None)
@ -442,7 +469,9 @@ def volume_types_encryption_changed(context, vol_type_id1, vol_type_id2):
return enc1_filtered != enc2_filtered return enc1_filtered != enc2_filtered
def provision_filter_on_size(context, volume_type, size): def provision_filter_on_size(context: context.RequestContext,
volume_type: Optional[dict[str, Any]],
size: Union[str, int]) -> None:
"""This function filters volume provisioning requests on size limits. """This function filters volume provisioning requests on size limits.
If a volume type has provisioning size min/max set, this filter If a volume type has provisioning size min/max set, this filter

View File

@ -1,3 +1,4 @@
cinder/api/v3/types.py
cinder/backup/api.py cinder/backup/api.py
cinder/backup/manager.py cinder/backup/manager.py
cinder/backup/drivers/ceph.py cinder/backup/drivers/ceph.py