diff --git a/plugins/module_utils/role_utils.py b/plugins/module_utils/role_utils.py index 888fbae9..37bd9923 100644 --- a/plugins/module_utils/role_utils.py +++ b/plugins/module_utils/role_utils.py @@ -40,6 +40,7 @@ RoleConfigGroupsResourceApi, RolesResourceApi, MgmtRolesResourceApi, + MgmtServiceResourceApi, ) @@ -296,6 +297,42 @@ def create_role( return role +def create_mgmt_role_model( + api_client: ApiClient, + role_type: str, + hostname: str = None, + host_id: str = None, + config: dict = None, +) -> ApiRole: + if ( + role_type.upper() + not in MgmtServiceResourceApi(api_client).list_role_types().items + ): + raise InvalidRoleTypeException( + f"Invalid role type '{role_type}' for Cloudera Management Service" + ) + + # Set up the role type + mgmt_role = ApiRole(type=str(role_type).upper()) + + # Host assignment + host_ref = get_host_ref(api_client, hostname, 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 + + # Role override configurations + if config: + mgmt_role.config = ApiConfigList( + items=[ApiConfig(name=k, value=v) for k, v in config.items()] + ) + + return mgmt_role + + def provision_service_role( api_client: ApiClient, cluster_name: str, service_name: str, role: ApiRole ) -> ApiRole: diff --git a/plugins/modules/cm_service.py b/plugins/modules/cm_service.py index 1ae5da6f..36cae830 100644 --- a/plugins/modules/cm_service.py +++ b/plugins/modules/cm_service.py @@ -580,7 +580,7 @@ get_mgmt_base_role_config_group, ) from ansible_collections.cloudera.cluster.plugins.module_utils.role_utils import ( - create_role, + create_mgmt_role_model, ) @@ -867,7 +867,7 @@ def process(self): else {c.name: c.value for c in existing_role.config.items} ) - new_role = create_role( + new_role = create_mgmt_role_model( api_client=self.api_client, role_type=existing_role.type, hostname=incoming_hostname, @@ -919,7 +919,7 @@ def process(self): incoming_role = incoming_roles_map[role_type] - new_role = create_role( + new_role = create_mgmt_role_model( api_client=self.api_client, role_type=incoming_role.get("type"), hostname=incoming_role.get("cluster_hostname"), diff --git a/plugins/modules/cm_service_role.py b/plugins/modules/cm_service_role.py index bb3c98d6..c2bd914d 100644 --- a/plugins/modules/cm_service_role.py +++ b/plugins/modules/cm_service_role.py @@ -313,7 +313,6 @@ from collections.abc import Callable from cm_client import ( - ApiBulkCommandList, ApiCommand, ApiRole, ApiRoleList, @@ -332,7 +331,7 @@ ConfigListUpdates, ) from ansible_collections.cloudera.cluster.plugins.module_utils.role_utils import ( - create_role, + create_mgmt_role_model, parse_role_result, read_cm_role, ) @@ -393,7 +392,7 @@ def process(self): elif self.state in ["present", "restarted", "started", "stopped"]: # If it is a new role if not current: - new_role = create_role( + new_role = create_mgmt_role_model( api_client=self.api_client, role_type=self.type, hostname=self.cluster_hostname, @@ -419,7 +418,7 @@ def process(self): else: new_config = {c.name: c.value for c in current.config.items} - new_role = create_role( + new_role = create_mgmt_role_model( api_client=self.api_client, role_type=current.type, hostname=self.cluster_hostname,