Skip to content

Commit

Permalink
Move in OpenStack credential input helper
Browse files Browse the repository at this point in the history
This should help decouple credential and inventory plugins.
  • Loading branch information
webknjaz committed Oct 17, 2024
1 parent 887c4bc commit c305b46
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/awx_plugins/interfaces/_temporary_private_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
The hope is that it will be refactored into something more standardized.
"""

from typing import Protocol


try:
# pylint: disable-next=unused-import
Expand Down Expand Up @@ -36,4 +38,63 @@ class ManagedCredentialType: # type: ignore[no-redef] # noqa: WPS440
"""Flag for whether this plugin instance is managed."""


class _CredentialInput(Protocol):
def get_input(
self: '_CredentialInput',
name: str,
default: object = None,
) -> bool | str:
"""Get an input from this credential.
:param name: Input name to check.
:type name: str
:param default: Fallback for a missing input.
:type default: object
"""

def has_input(self: '_CredentialInput', name: str) -> bool:
"""Check if an input is present.
:param name: Input name to check.
:type name: str
"""


def _retrieve_openstack_data_from_credential( # noqa: WPS234, WPS320
cred: _CredentialInput,
) -> dict[
str,
dict[str, dict[str, dict[str, bool | str] | bool | str]], # noqa: WPS221
]:
openstack_auth = {
'auth_url': cred.get_input('host', default=''),
'username': cred.get_input('username', default=''),
'password': cred.get_input('password', default=''),
'project_name': cred.get_input('project', default=''),
}
if cred.has_input('project_domain_name'):
openstack_auth['project_domain_name'] = cred.get_input(
'project_domain_name', default='',
)
if cred.has_input('domain'):
openstack_auth['domain_name'] = cred.get_input('domain', default='')
verify_state = cred.get_input('verify_ssl', default=True)

openstack_data = {
'clouds': {
'devstack': {
'auth': openstack_auth,
'verify': verify_state,
},
},
}

if cred.has_input('region'):
openstack_data['clouds']['devstack']['region_name'] = cred.get_input(
'region', default='',
)

return openstack_data


__all__ = () # noqa: WPS410

0 comments on commit c305b46

Please sign in to comment.