|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +# Copyright (c) 2024 Cisco Systems, Inc. and its affiliates |
| 5 | +# |
| 6 | +# Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 7 | +# this software and associated documentation files (the "Software"), to deal in |
| 8 | +# the Software without restriction, including without limitation the rights to |
| 9 | +# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 10 | +# the Software, and to permit persons to whom the Software is furnished to do so, |
| 11 | +# subject to the following conditions: |
| 12 | +# |
| 13 | +# The above copyright notice and this permission notice shall be included in all |
| 14 | +# copies or substantial portions of the Software. |
| 15 | +# |
| 16 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 18 | +# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 19 | +# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 20 | +# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 21 | +# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | +# |
| 23 | +# SPDX-License-Identifier: MIT |
| 24 | + |
| 25 | +from __future__ import absolute_import, division, print_function |
| 26 | + |
| 27 | +__metaclass__ = type |
| 28 | + |
| 29 | +from ansible.utils.display import Display |
| 30 | +from ansible.plugins.action import ActionBase |
| 31 | + |
| 32 | + |
| 33 | +display = Display() |
| 34 | + |
| 35 | + |
| 36 | +class ActionModule(ActionBase): |
| 37 | + """ |
| 38 | + Action plugin to dynamically select between cisco.nac_dc_vxlan.dtc.rest_selector and cisco.dcnm.dcnm_rest |
| 39 | + based on the ansible_network_os variable. |
| 40 | + """ |
| 41 | + |
| 42 | + def run(self, tmp=None, task_vars=None): |
| 43 | + """ |
| 44 | + Execute the action plugin. |
| 45 | +
|
| 46 | + Args: |
| 47 | + tmp (str, optional): Temporary directory. Defaults to None. |
| 48 | + task_vars (dict, optional): Dictionary of task variables. Defaults to None. |
| 49 | +
|
| 50 | + Returns: |
| 51 | + dict: Results of the REST API call |
| 52 | + """ |
| 53 | + # Initialize results dictionary |
| 54 | + results = super(ActionModule, self).run(tmp, task_vars) |
| 55 | + results['changed'] = False |
| 56 | + results['failed'] = False |
| 57 | + |
| 58 | + # Get task arguments |
| 59 | + module_args = self._task.args.copy() |
| 60 | + |
| 61 | + # Get the network OS from task variables or module arguments |
| 62 | + network_os = task_vars.get('ansible_network_os') \ |
| 63 | + or module_args.pop('ansible_network_os', None) |
| 64 | + |
| 65 | + # Determine which module to use based on network_os |
| 66 | + if network_os == 'cisco.nd.nd': |
| 67 | + rest_module = 'cisco.nac_dc_vxlan.dtc.rest_selector' |
| 68 | + elif network_os == 'cisco.dcnm.dcnm': |
| 69 | + rest_module = 'cisco.dcnm.dcnm_rest' |
| 70 | + else: |
| 71 | + results['failed'] = True |
| 72 | + results['msg'] = ( |
| 73 | + f"Unsupported network_os: {network_os}. " |
| 74 | + "Must be 'cisco.nd.nd' or 'cisco.dcnm.dcnm'" |
| 75 | + ) |
| 76 | + return results |
| 77 | + |
| 78 | + display.vvvv(f"Using REST module: {rest_module}") |
| 79 | + |
| 80 | + try: |
| 81 | + # Execute the appropriate REST module |
| 82 | + result = self._execute_module( |
| 83 | + module_name=rest_module, |
| 84 | + module_args=module_args, |
| 85 | + task_vars=task_vars, |
| 86 | + tmp=tmp |
| 87 | + ) |
| 88 | + |
| 89 | + # Update results with the module's results |
| 90 | + if result.get('failed'): |
| 91 | + results.update(result) |
| 92 | + else: |
| 93 | + results['changed'] = result.get('changed', False) |
| 94 | + results.update(result) |
| 95 | + |
| 96 | + except Exception as e: |
| 97 | + results['failed'] = True |
| 98 | + results['msg'] = f"Failed to execute {rest_module}: {str(e)}" |
| 99 | + display.error(results['msg'], wrap_text=False) |
| 100 | + |
| 101 | + return results |
0 commit comments