|
175 | 175 | """ |
176 | 176 |
|
177 | 177 | from ansible.module_utils.basic import AnsibleModule |
178 | | -from ansible_collections.cisco.nd.plugins.module_utils.nd import nd_argument_spec, NDModule |
179 | | -from ansible_collections.cisco.nd.plugins.module_utils.nd_network_resources import NDNetworkResourceModule |
180 | | -from ansible_collections.cisco.nd.plugins.module_utils.constants import USER_ROLES_MAPPING |
| 178 | +# TODO: To be replaced with: |
| 179 | +# from ansible_collections.cisco.nd.plugins.module_utils.nd import nd_argument_spec |
| 180 | +# from ansible_collections.cisco.nd.plugins.module_utils.nd_network_resource_module import NDNetworkResourceModule |
| 181 | +# from ansible_collections.cisco.nd.plugins.module_utils.models.local_user import LocalUserModel |
| 182 | +# from ansible_collections.cisco.nd.plugins.module_utils.constants import USER_ROLES_MAPPING |
| 183 | +from module_utils.nd import nd_argument_spec |
| 184 | +from module_utils.nd_network_resources import NDNetworkResourceModule |
| 185 | +from module_utils.models.local_user import LocalUserModel |
| 186 | +from module_utils.constants import USER_ROLES_MAPPING |
181 | 187 |
|
182 | 188 |
|
183 | | -# Actions overwrite functions |
184 | | -def query_all_local_users(nd): |
185 | | - return nd.query_obj(nd.path).get("localusers") |
| 189 | +# NOTE: Maybe Add the overwrite action in the LocalUserModel |
| 190 | +def query_all_local_users(nd_module): |
| 191 | + """ |
| 192 | + Custom query_all action to extract 'localusers' from response. |
| 193 | + """ |
| 194 | + response = nd_module.query_obj(nd_module.path) |
| 195 | + return response.get("localusers", []) |
186 | 196 |
|
187 | 197 |
|
188 | | -# TODO: Adapt to Pydantic Model |
| 198 | +# NOTE: Maybe Add More aliases like in the LocalUserModel / Revisit the argmument_spec |
189 | 199 | def main(): |
190 | 200 | argument_spec = nd_argument_spec() |
191 | 201 | argument_spec.update( |
192 | 202 | config=dict( |
193 | 203 | type="list", |
194 | 204 | elements="dict", |
| 205 | + required=True, |
195 | 206 | options=dict( |
196 | 207 | email=dict(type="str"), |
197 | 208 | login_id=dict(type="str", required=True), |
@@ -221,49 +232,33 @@ def main(): |
221 | 232 | argument_spec=argument_spec, |
222 | 233 | supports_check_mode=True, |
223 | 234 | ) |
224 | | - |
225 | | - path = "/api/v1/infra/aaa/localUsers" |
226 | | - identifier_keys = ["loginID"] |
227 | | - actions_overwrite_map = {"query_all": query_all_local_users} |
228 | | - |
229 | | - nd = NDNetworkResourceModule(module, path, identifier_keys, actions_overwrite_map=actions_overwrite_map) |
230 | | - |
231 | | - state = nd.params.get("state") |
232 | | - config = nd.params.get("config") |
233 | | - override_exceptions = nd.params.get("override_exceptions") |
234 | | - new_config = [] |
235 | | - for object in config: |
236 | | - payload = { |
237 | | - "email": object.get("email"), |
238 | | - "firstName": object.get("first_name"), |
239 | | - "lastName": object.get("last_name"), |
240 | | - "loginID": object.get("login_id"), |
241 | | - "password": object.get("user_password"), |
242 | | - "remoteIDClaim": object.get("remote_id_claim"), |
243 | | - "xLaunch": object.get("remote_user_authorization"), |
244 | | - } |
245 | | - |
246 | | - if object.get("security_domains"): |
247 | | - payload["rbac"] = { |
248 | | - "domains": { |
249 | | - security_domain.get("name"): { |
250 | | - "roles": ( |
251 | | - [USER_ROLES_MAPPING.get(role) for role in security_domain["roles"]] if isinstance(security_domain.get("roles"), list) else [] |
252 | | - ) |
253 | | - } |
254 | | - for security_domain in object["security_domains"] |
255 | | - }, |
256 | | - } |
257 | | - if object.get("reuse_limitation") or object.get("time_interval_limitation"): |
258 | | - payload["passwordPolicy"] = { |
259 | | - "reuseLimitation": object.get("reuse_limitation"), |
260 | | - "timeIntervalLimitation": object.get("time_interval_limitation"), |
| 235 | + |
| 236 | + try: |
| 237 | + # Create NDNetworkResourceModule with LocalUserModel |
| 238 | + nd_module = NDNetworkResourceModule( |
| 239 | + module=module, |
| 240 | + path="/api/v1/infra/aaa/localUsers", |
| 241 | + model_class=LocalUserModel, |
| 242 | + actions_overwrite_map={ |
| 243 | + "query_all": query_all_local_users |
261 | 244 | } |
262 | | - new_config.append(payload) |
263 | | - |
264 | | - nd.manage_state(state=state, new_configs=new_config, unwanted_keys=[["passwordPolicy", "passwordChangeTime"], ["userID"]], override_exceptions=override_exceptions) |
| 245 | + ) |
| 246 | + |
| 247 | + # Manage state |
| 248 | + nd_module.manage_state( |
| 249 | + state=module.params["state"], |
| 250 | + new_configs=module.params["config"], |
| 251 | + unwanted_keys=[ |
| 252 | + ["passwordPolicy", "passwordChangeTime"], # Nested path |
| 253 | + ["userID"] # Simple key |
| 254 | + ], |
| 255 | + override_exceptions=module.params.get("override_exceptions") |
| 256 | + ) |
265 | 257 |
|
266 | | - nd.exit_json() |
| 258 | + nd_module.exit_json() |
| 259 | + |
| 260 | + except Exception as e: |
| 261 | + module.fail_json(msg=f"Module execution failed: {str(e)}") |
267 | 262 |
|
268 | 263 |
|
269 | 264 | if __name__ == "__main__": |
|
0 commit comments