From ea28ff1d0d859636e72f64a7008368459840bdf2 Mon Sep 17 00:00:00 2001 From: Webster Mudge Date: Fri, 9 May 2025 17:53:51 -0400 Subject: [PATCH] Remove reference to circular import of get_host_ref for create_mgmt_role_model() Signed-off-by: Webster Mudge --- plugins/module_utils/role_utils.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/plugins/module_utils/role_utils.py b/plugins/module_utils/role_utils.py index 0992b027..1f95b863 100644 --- a/plugins/module_utils/role_utils.py +++ b/plugins/module_utils/role_utils.py @@ -337,13 +337,34 @@ def create_mgmt_role_model( mgmt_role = ApiRole(type=str(role_type).upper()) # Host assignment - host_ref = get_host_ref(api_client, hostname, host_id) + if hostname: + host_ref = next( + ( + h + for h in HostsResourceApi(api_client).read_hosts().items + if h.hostname == hostname + ), + None, + ) + elif host_id: + try: + host_ref = HostsResourceApi(api_client).read_host(host_id) + except ApiException as ex: + if ex.status != 404: + raise ex + else: + host_ref = None + else: + raise RoleException("Specify either 'hostname' or 'host_id'") + if host_ref is None: raise RoleHostNotFoundException( f"Host not found: hostname='{hostname}', host_id='{host_id}'" ) else: - mgmt_role.host_ref = host_ref + mgmt_role.host_ref = ApiHostRef( + host_id=host_ref.host_id, hostname=host_ref.hostname + ) # Role override configurations if config: