Skip to content

Commit

Permalink
typing: Adapt to typed keystoneauth
Browse files Browse the repository at this point in the history
Change-Id: I0a1bc9e660e8265f19b6a0ba862f5ee6a6c3a8d6
Signed-off-by: Stephen Finucane <[email protected]>
  • Loading branch information
stephenfin committed Feb 19, 2025
1 parent c5a2e3d commit b3e1951
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 8 deletions.
2 changes: 1 addition & 1 deletion openstack/block_storage/v3/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def failover(
body['backend_id'] = backend_id

action = 'failover_host'
if utils.supports_microversion(self, '3.26'):
if utils.supports_microversion(session, '3.26'):
action = 'failover'

return self._action(session, action, body)
12 changes: 10 additions & 2 deletions openstack/config/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@
from openstack import exceptions
from openstack import warnings as os_warnings

if ty.TYPE_CHECKING:
from keystoneauth1.loading._plugins.identity import v3 as v3_loaders

PLATFORMDIRS = platformdirs.PlatformDirs(
'openstack', 'OpenStack', multipath='/etc'
'openstack', 'OpenStack', multipath=True
)
CONFIG_HOME = PLATFORMDIRS.user_config_dir
CACHE_PATH = PLATFORMDIRS.user_cache_dir
Expand Down Expand Up @@ -1061,7 +1064,9 @@ def _get_auth_loader(self, config):
# doing what they want causes them sorrow.
config['auth_type'] = 'admin_token'

loader = loading.get_plugin_loader(config['auth_type'])
loader: loading.BaseLoader[ty.Any] = loading.get_plugin_loader(
config['auth_type']
)

# As the name would suggest, v3multifactor uses multiple factors for
# authentication. As a result, we need to register the configuration
Expand All @@ -1074,6 +1079,9 @@ def _get_auth_loader(self, config):
# options in keystoneauth1.loading._plugins.identity.v3.MultiAuth
# without calling 'load_from_options'.
if config['auth_type'] == 'v3multifactor':
if ty.TYPE_CHECKING:
# narrow types
assert isinstance(loader, v3_loaders.MultiFactor)
# We use '.get' since we can't be sure this key is set yet -
# validation happens later, in _validate_auth
loader._methods = config.get('auth_methods')
Expand Down
2 changes: 1 addition & 1 deletion openstack/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ def authorize(self):
try:
return self.session.get_token()
except keystoneauth1.exceptions.ClientException as e:
raise exceptions.SDKException(e)
raise exceptions.SDKException(str(e))

def connect_as(self, **kwargs):
"""Make a new Connection object with new auth context.
Expand Down
3 changes: 2 additions & 1 deletion openstack/identity/v3/_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,8 @@ def find_endpoint(self, name_or_id, ignore_missing=True):
_endpoint.Endpoint, name_or_id, ignore_missing=ignore_missing
)

def get_endpoint(self, endpoint):
# TODO(stephenfin): This conflicts with Adapter.get_endpoint
def get_endpoint(self, endpoint): # type: ignore[override]
"""Get a single endpoint
:param endpoint: The value can be the ID of an endpoint or a
Expand Down
8 changes: 7 additions & 1 deletion openstack/object_store/v1/_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,13 @@ def generate_form_signature(

res = self._get_resource(_container.Container, container)
endpoint = parse.urlparse(self.get_endpoint())
path = '/'.join([endpoint.path, res.name, object_prefix])
if isinstance(endpoint.path, bytes):
# To keep mypy happy: the output type will be the same as the input
# type
path = endpoint.path.decode()
else:
path = endpoint.path
path = '/'.join([path, res.name, object_prefix])

data = f'{path}\n{redirect_url}\n{max_file_size}\n{max_upload_count}\n{expires}'
sig = hmac.new(temp_url_key, data.encode(), sha1).hexdigest()
Expand Down
13 changes: 12 additions & 1 deletion openstack/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
from openstack import utils
from openstack import warnings as os_warnings

if ty.TYPE_CHECKING:
from openstack import connection


ResourceType = ty.TypeVar('ResourceType', bound=resource.Resource)

Expand Down Expand Up @@ -89,6 +92,8 @@ class Proxy(adapter.Adapter):
Dictionary of resource names (key) types (value).
"""

_connection: 'connection.Connection'

def __init__(
self,
session,
Expand All @@ -106,7 +111,9 @@ def __init__(
kwargs.setdefault(
'retriable_status_codes', self.retriable_status_codes
)
super().__init__(session=session, *args, **kwargs)
# TODO(stephenfin): Resolve this by copying the signature of
# adapter.Adapter.__init__
super().__init__(session=session, *args, **kwargs) # type: ignore
self._statsd_client = statsd_client
self._statsd_prefix = statsd_prefix
self._prometheus_counter = prometheus_counter
Expand Down Expand Up @@ -889,6 +896,10 @@ def should_skip_resource_cleanup(self, resource=None, skip_resources=None):
if resource is None or skip_resources is None:
return False

if self.service_type is None:
# to keep mypy happy - this should never happen
return False

resource_name = f"{self.service_type.replace('-', '_')}.{resource}"

if resource_name in skip_resources:
Expand Down
5 changes: 4 additions & 1 deletion openstack/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -2235,9 +2235,12 @@ def find(

# Try to short-circuit by looking directly for a matching ID.
try:
# TODO(stephenfin): Our types say we accept a ksa Adapter, but this
# requires an SDK Proxy. Do we update the types or rework this to
# support use of an adapter.
match = cls.existing(
id=name_or_id,
connection=session._get_connection(),
connection=session._get_connection(), # type: ignore
**params,
)
return match.fetch(session, microversion=microversion, **params)
Expand Down

0 comments on commit b3e1951

Please sign in to comment.