-
Notifications
You must be signed in to change notification settings - Fork 52
Add User Module #252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add User Module #252
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,296 @@ | ||
| # Copyright 2024 Cloudera, Inc. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from ansible_collections.cloudera.cluster.plugins.module_utils.cm_utils import ( | ||
| ClouderaManagerModule, | ||
| ) | ||
| from cm_client.rest import ApiException | ||
| from cm_client import ( | ||
| UsersResourceApi, | ||
| ApiUser2, | ||
| ApiAuthRoleRef, | ||
| ApiUser2List, | ||
| AuthRolesResourceApi, | ||
| ) | ||
|
|
||
| ANSIBLE_METADATA = { | ||
| "metadata_version": "1.1", | ||
| "status": ["preview"], | ||
| "supported_by": "community", | ||
| } | ||
|
|
||
| DOCUMENTATION = r""" | ||
| --- | ||
| module: user | ||
| short_description: Create, delete or update users within Cloudera Manager | ||
| description: | ||
| - Creates a user with specified authorization roles in Cloudera Manager, or updates roles for an existing user. | ||
| - Supports purging roles or adding new roles to the existing list. | ||
| - Enables the deletion of a user along with its associated roles if desired. | ||
| author: | ||
| - "Ronald Suplina (@rsuplina)" | ||
| requirements: | ||
| - cm_client | ||
| options: | ||
| account_name: | ||
| description: | ||
| - The name of the user account to be managed. | ||
| type: str | ||
| required: true | ||
| account_password: | ||
| description: | ||
| - The password for the account. | ||
| - Required when creating a new account. | ||
| type: str | ||
| required: false | ||
| roles: | ||
| description: | ||
| - A list of authentication roles associated with the account. | ||
| - Existing roles are preserved unless C(purge) is set to True. | ||
| type: list | ||
| required: false | ||
| aliases: | ||
| - auth_roles | ||
| purge: | ||
| description: | ||
| - When set to True, ensures that roles not listed in C(roles) are removed from the account. | ||
| type: bool | ||
| default: false | ||
| state: | ||
| description: | ||
| - Controls the desired state of the account. | ||
| - C(present) ensures the account exists with the specified parameters. | ||
| - C(absent) deletes the account and its associated roles. | ||
| type: str | ||
| default: present | ||
| choices: | ||
| - present | ||
| - absent | ||
| """ | ||
|
|
||
| EXAMPLES = r""" | ||
| --- | ||
| - name: Create new Administrator user | ||
| cloudera.cluster.user: | ||
| host: example.cloudera.com | ||
| port: "7180" | ||
| username: "jane_smith" | ||
| password: "S&peR4Ec*re" | ||
| account_name: "admin_user" | ||
| account_password: "Password123" | ||
| roles: ["Full Administrator"] | ||
| state: "Present" | ||
| purge: false | ||
|
|
||
| - name: Add additional roles to user | ||
| cloudera.cluster.user: | ||
| host: example.cloudera.com | ||
| port: "7180" | ||
| username: "jane_smith" | ||
| password: "S&peR4Ec*re" | ||
| account_name: "john" | ||
| account_password: "Password123" | ||
| roles: ["Configurator","Dashboard User","Limited Operator"] | ||
| state: "Present" | ||
|
|
||
| - name: Reduce permissions on user to a single role | ||
| cloudera.cluster.user: | ||
| host: example.cloudera.com | ||
| port: "7180" | ||
| username: "jane_smith" | ||
| password: "S&peR4Ec*re" | ||
| account_name: "john" | ||
| account_password: "Password123" | ||
| roles: ["Dashboard User"] | ||
| state: "Present" | ||
| purge: true | ||
|
|
||
| - name: Remove specified user | ||
| cloudera.cluster.user: | ||
| host: example.cloudera.com | ||
| port: "7180" | ||
| username: "jane_smith" | ||
| password: "S&peR4Ec*re" | ||
| account_name: "john" | ||
| roles: ["Dashboard User"] | ||
| state: "absent" | ||
|
|
||
| """ | ||
|
|
||
| RETURN = r""" | ||
| --- | ||
| user: | ||
rsuplina marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| description: List of users within the cluster | ||
| type: dict | ||
| elements: dict | ||
rsuplina marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| returned: always | ||
| contains: | ||
| name: | ||
| description: The username, which is unique within a Cloudera Manager installation. | ||
| type: str | ||
| returned: always | ||
| auth_roles: | ||
| description: A list of ApiAuthRole objects representing the authentication roles assigned to the user. | ||
rsuplina marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| type: list | ||
| returned: optional | ||
| """ | ||
|
|
||
|
|
||
| class ClouderaUserInfo(ClouderaManagerModule): | ||
| def __init__(self, module): | ||
| super(ClouderaUserInfo, self).__init__(module) | ||
|
|
||
| # Set the parameters | ||
| self.account_name = self.get_param("account_name") | ||
| self.account_password = self.get_param("account_password") | ||
| self.roles = self.get_param("roles") | ||
| self.state = self.get_param("state") | ||
| self.purge = self.get_param("purge") | ||
|
|
||
| # Initialize the return values | ||
| self.user_output = [] | ||
| self.changed = False | ||
| self.diff = {} | ||
|
|
||
| # Execute the logic | ||
| self.process() | ||
|
|
||
| @ClouderaManagerModule.handle_process | ||
| def process(self): | ||
| api_instance = UsersResourceApi(self.api_client) | ||
| auth_role_api_instance = AuthRolesResourceApi(self.api_client) | ||
| all_roles = auth_role_api_instance.read_auth_roles().to_dict()["items"] | ||
| existing = [] | ||
|
|
||
| try: | ||
| existing = api_instance.read_user2(self.account_name).to_dict() | ||
| except ApiException as ex: | ||
| if ex.status == 404: | ||
| pass | ||
| else: | ||
| raise ex | ||
|
|
||
| if self.state == "present": | ||
|
|
||
| if existing: | ||
| incoming_roles = [ | ||
| role["uuid"] | ||
| for role in all_roles | ||
| if role["display_name"] in self.roles | ||
| ] | ||
| existing_roles = [role["uuid"] for role in existing["auth_roles"] or []] | ||
|
|
||
| if self.module._diff: | ||
| current_roles = set(existing_roles) | ||
| incoming_roles_set = set(incoming_roles) | ||
| self.diff.update( | ||
| before=list(current_roles - incoming_roles_set), | ||
| after=list(incoming_roles_set - current_roles), | ||
| ) | ||
| if self.purge: | ||
| roles_to_add = incoming_roles | ||
| else: | ||
| roles_to_add = list(set(existing_roles) | set(incoming_roles)) | ||
|
|
||
| if roles_to_add: | ||
| auth_roles = [ | ||
| ApiAuthRoleRef(uuid=role_uuid) for role_uuid in roles_to_add | ||
| ] | ||
| self.user_output = api_instance.update_user2( | ||
| self.account_name, | ||
| body=ApiUser2( | ||
| name=self.account_name, | ||
| auth_roles=auth_roles, | ||
| password=self.account_password, | ||
| ), | ||
| ).to_dict() | ||
| self.changed = True | ||
| else: | ||
| auth_roles = [] | ||
| self.user_output = api_instance.update_user2( | ||
| self.account_name, | ||
| body=ApiUser2( | ||
| name=self.account_name, | ||
| auth_roles=auth_roles, | ||
| password=self.account_password, | ||
| ), | ||
| ).to_dict() | ||
| self.changed = True | ||
|
|
||
| else: | ||
| incoming_roles = [ | ||
| role["uuid"] | ||
| for role in all_roles | ||
| if role["display_name"] in self.roles | ||
| ] | ||
| auth_roles = [ | ||
| ApiAuthRoleRef(uuid=role_uuid) for role_uuid in incoming_roles | ||
| ] | ||
| api_instance.create_users2( | ||
| body=ApiUser2List( | ||
| items=[ | ||
| ApiUser2( | ||
| name=self.account_name, | ||
| auth_roles=auth_roles, | ||
| password=self.account_password, | ||
| ) | ||
| ] | ||
| ) | ||
| ) | ||
| self.user_output = api_instance.read_user2(self.account_name).to_dict() | ||
|
|
||
| self.changed = True | ||
|
|
||
| if self.state == "absent": | ||
| if existing: | ||
| self.user_output = api_instance.delete_user2( | ||
| self.account_name | ||
| ).to_dict() | ||
| self.changed = True | ||
|
|
||
|
|
||
| def main(): | ||
| module = ClouderaManagerModule.ansible_module( | ||
| argument_spec=dict( | ||
| account_name=dict(required=True, type="str"), | ||
| account_password=dict(required=False, type="str"), | ||
| roles=dict(required=False, type="list", aliases=["auth_roles"]), | ||
| purge=dict(type="bool", default=False), | ||
| state=dict( | ||
| type="str", | ||
| default="present", | ||
| choices=["present", "absent"], | ||
| ), | ||
| ), | ||
| supports_check_mode=False, | ||
| ) | ||
|
|
||
| result = ClouderaUserInfo(module) | ||
|
|
||
| output = dict( | ||
| changed=False, | ||
| user_output=result.user_output, | ||
| ) | ||
| if module._diff: | ||
| output.update(diff=result.diff) | ||
|
|
||
| if result.debug: | ||
| log = result.log_capture.getvalue() | ||
| output.update(debug=log, debug_lines=log.split("\n")) | ||
|
|
||
| module.exit_json(**output) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.