|
4 | 4 | The hope is that it will be refactored into something more standardized. |
5 | 5 | """ |
6 | 6 |
|
| 7 | +from typing import Protocol |
| 8 | + |
7 | 9 |
|
8 | 10 | try: |
9 | 11 | # pylint: disable-next=unused-import |
@@ -36,4 +38,63 @@ class ManagedCredentialType: # type: ignore[no-redef] # noqa: WPS440 |
36 | 38 | """Flag for whether this plugin instance is managed.""" |
37 | 39 |
|
38 | 40 |
|
| 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 | + |
39 | 100 | __all__ = () # noqa: WPS410 |
0 commit comments