Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions plugins/module_utils/role_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
RoleConfigGroupsResourceApi,
RolesResourceApi,
MgmtRolesResourceApi,
MgmtServiceResourceApi,
)


Expand Down Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions plugins/modules/cm_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)


Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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"),
Expand Down
7 changes: 3 additions & 4 deletions plugins/modules/cm_service_role.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,6 @@
from collections.abc import Callable

from cm_client import (
ApiBulkCommandList,
ApiCommand,
ApiRole,
ApiRoleList,
Expand All @@ -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,
)
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
Loading