|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +# Copyright 2025 Cloudera, Inc. All Rights Reserved. |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | + |
| 18 | +DOCUMENTATION = r""" |
| 19 | +module: iam_machine_user_info |
| 20 | +short_description: Gather information about CDP Public IAM machine users |
| 21 | +description: |
| 22 | + - Gather information about CDP Public IAM machine user or machine users |
| 23 | +author: |
| 24 | + - "Ronald Suplina (@rsuplina)" |
| 25 | +version_added: "3.2.0" |
| 26 | +options: |
| 27 | + name: |
| 28 | + description: |
| 29 | + - A list of machine user names or CRNs or a single machine user name/CRN. |
| 30 | + - If no machine user name or CRN is provided, all machine users are returned. |
| 31 | + type: list |
| 32 | + elements: str |
| 33 | + required: False |
| 34 | + aliases: |
| 35 | + - machine_user_name |
| 36 | +extends_documentation_fragment: |
| 37 | + - cloudera.cloud.cdp_client |
| 38 | +""" |
| 39 | + |
| 40 | +EXAMPLES = r""" |
| 41 | +# Note: These examples do not set authentication details. |
| 42 | +
|
| 43 | + - name: Gather information about all Machine Users |
| 44 | + cloudera.cloud.iam_machine_user_info: |
| 45 | +
|
| 46 | + - name: Gather information about a named Machine User |
| 47 | + cloudera.cloud.iam_machine_user_info: |
| 48 | + name: example-machine-user-01 |
| 49 | +
|
| 50 | + - name: Gather information about several named Machine Users |
| 51 | + cloudera.cloud.iam_machine_user_info: |
| 52 | + name: |
| 53 | + - example-machine-user-01 |
| 54 | + - example-machine-user-02 |
| 55 | + - example-machine-user-03 |
| 56 | +""" |
| 57 | + |
| 58 | +RETURN = r""" |
| 59 | +machine_users: |
| 60 | + description: |
| 61 | + - Returns a list of machine user records. |
| 62 | + - Each record represents a CDP IAM machine user and its details. |
| 63 | + type: list |
| 64 | + returned: always |
| 65 | + elements: dict |
| 66 | + contains: |
| 67 | + creation_date: |
| 68 | + description: The date when this machine user record was created. |
| 69 | + returned: on success |
| 70 | + type: str |
| 71 | + sample: 2020-07-06T12:24:05.531000+00:00 |
| 72 | + crn: |
| 73 | + description: The CRN of the machine user. |
| 74 | + returned: on success |
| 75 | + type: str |
| 76 | + machine_user_name: |
| 77 | + description: The machine user name. |
| 78 | + returned: on success |
| 79 | + type: str |
| 80 | + sample: example-machine-user-01 |
| 81 | + status: |
| 82 | + description: The current status of the machine user (ACTIVE or CONTROL_PLANE_LOCKED_OUT). |
| 83 | + returned: when supported |
| 84 | + type: str |
| 85 | + sample: ACTIVE |
| 86 | + workload_password_details: |
| 87 | + description: Information about the workload password for the machine user. |
| 88 | + returned: when supported |
| 89 | + type: dict |
| 90 | + workload_username: |
| 91 | + description: The username used in all the workload clusters of the machine user. |
| 92 | + returned: when supported |
| 93 | + type: str |
| 94 | +sdk_out: |
| 95 | + description: Returns the captured API HTTP log. |
| 96 | + returned: when supported |
| 97 | + type: str |
| 98 | +sdk_out_lines: |
| 99 | + description: Returns a list of each line of the captured API HTTP log. |
| 100 | + returned: when supported |
| 101 | + type: list |
| 102 | + elements: str |
| 103 | +""" |
| 104 | + |
| 105 | +from typing import Any |
| 106 | + |
| 107 | +from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 108 | + |
| 109 | +from ansible_collections.cloudera.cloud.plugins.module_utils.common import ( |
| 110 | + ServicesModule, |
| 111 | +) |
| 112 | +from ansible_collections.cloudera.cloud.plugins.module_utils.cdp_iam import ( |
| 113 | + CdpIamClient, |
| 114 | +) |
| 115 | + |
| 116 | + |
| 117 | +class IAMMachineUserInfo(ServicesModule): |
| 118 | + def __init__(self): |
| 119 | + super().__init__( |
| 120 | + argument_spec=dict( |
| 121 | + name=dict( |
| 122 | + required=False, |
| 123 | + type="list", |
| 124 | + elements="str", |
| 125 | + aliases=["machine_user_name"], |
| 126 | + ), |
| 127 | + ), |
| 128 | + supports_check_mode=True, |
| 129 | + ) |
| 130 | + |
| 131 | + # Set parameters |
| 132 | + self.name = self.get_param("name") |
| 133 | + |
| 134 | + # Initialize the return values |
| 135 | + self.machine_users = [] |
| 136 | + |
| 137 | + def process(self): |
| 138 | + client = CdpIamClient(api_client=self.api_client) |
| 139 | + result = client.list_machine_users(machine_user_names=self.name) |
| 140 | + self.machine_users = [ |
| 141 | + camel_dict_to_snake_dict(machine_user) |
| 142 | + for machine_user in result.get("machineUsers", []) |
| 143 | + ] |
| 144 | + |
| 145 | + |
| 146 | +def main(): |
| 147 | + result = IAMMachineUserInfo() |
| 148 | + |
| 149 | + output: dict[str, Any] = dict( |
| 150 | + changed=False, |
| 151 | + machine_users=result.machine_users, |
| 152 | + ) |
| 153 | + |
| 154 | + if result.debug_log: |
| 155 | + output.update( |
| 156 | + sdk_out=result.log_out, |
| 157 | + sdk_out_lines=result.log_lines, |
| 158 | + ) |
| 159 | + |
| 160 | + result.module.exit_json(**output) |
| 161 | + |
| 162 | + |
| 163 | +if __name__ == "__main__": |
| 164 | + main() |
0 commit comments