Skip to content

Commit f84a3ae

Browse files
committed
Move in OpenStack credential input helper
This should help decouple credential and inventory plugins.
1 parent ba94be9 commit f84a3ae

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

src/awx_plugins/interfaces/_temporary_private_api.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
The hope is that it will be refactored into something more standardized.
55
"""
66

7+
from typing import Protocol
8+
79

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

3840

41+
class _CredentialInput(Protocol):
42+
def get_input(
43+
self: '_CredentialInput',
44+
name: str,
45+
default: object = None,
46+
) -> bool | str:
47+
"""Get an input from this credential.
48+
49+
:param name: Input name to check.
50+
:type name: str
51+
:param default: Fallback for a missing input.
52+
:type default: object
53+
"""
54+
55+
def has_input(self: '_CredentialInput', name: str) -> bool:
56+
"""Check if an input is present.
57+
58+
:param name: Input name to check.
59+
:type name: str
60+
"""
61+
62+
63+
def _retrieve_openstack_data_from_credential( # noqa: WPS234, WPS320
64+
cred: _CredentialInput,
65+
) -> dict[
66+
str,
67+
dict[str, dict[str, dict[str, bool | str] | bool | str]], # noqa: WPS221
68+
]:
69+
openstack_auth = {
70+
'auth_url': cred.get_input('host', default=''),
71+
'username': cred.get_input('username', default=''),
72+
'password': cred.get_input('password', default=''),
73+
'project_name': cred.get_input('project', default=''),
74+
}
75+
if cred.has_input('project_domain_name'):
76+
openstack_auth['project_domain_name'] = cred.get_input(
77+
'project_domain_name', default='',
78+
)
79+
if cred.has_input('domain'):
80+
openstack_auth['domain_name'] = cred.get_input('domain', default='')
81+
verify_state = cred.get_input('verify_ssl', default=True)
82+
83+
openstack_data = {
84+
'clouds': {
85+
'devstack': {
86+
'auth': openstack_auth,
87+
'verify': verify_state,
88+
},
89+
},
90+
}
91+
92+
if cred.has_input('region'):
93+
openstack_data['clouds']['devstack']['region_name'] = cred.get_input(
94+
'region', default='',
95+
)
96+
97+
return openstack_data
98+
99+
39100
__all__ = () # noqa: WPS410

0 commit comments

Comments
 (0)