Debian Codegraph @

ansible_collections/community/general/plugins/modules/lxca_cmms.py

raw
1 #!/usr/bin/python
2 # Copyright (c) Ansible project
3 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 # SPDX-License-Identifier: GPL-3.0-or-later
5 #
6 from __future__ import annotations
7
8 DOCUMENTATION = r"""
9 author:
10 - Naval Patel (@navalkp)
11 - Prashant Bhosale (@prabhosa)
12 module: lxca_cmms
13 short_description: Custom module for lxca cmms inventory utility
14 description:
15 - This module returns/displays a inventory details of cmms.
16 attributes:
17 check_mode:
18 support: none
19 diff_mode:
20 support: none
21
22 options:
23 uuid:
24 description: UUID of device, this is string with length greater than 16.
25 type: str
26
27 command_options:
28 description: Options to filter nodes information.
29 default: cmms
30 choices:
31 - cmms
32 - cmms_by_uuid
33 - cmms_by_chassis_uuid
34 type: str
35
36 chassis:
37 description: UUID of chassis, this is string with length greater than 16.
38 type: str
39
40 extends_documentation_fragment:
41 - community.general._lxca_common
42 - community.general._attributes
43 """
44
45 EXAMPLES = r"""
46 # get all cmms info
47 - name: Get nodes data from LXCA
48 community.general.lxca_cmms:
49 login_user: USERID
50 login_password: Password
51 auth_url: "https://10.243.15.168"
52
53 # get specific cmms info by uuid
54 - name: Get nodes data from LXCA
55 community.general.lxca_cmms:
56 login_user: USERID
57 login_password: Password
58 auth_url: "https://10.243.15.168"
59 uuid: "3C737AA5E31640CE949B10C129A8B01F"
60 command_options: cmms_by_uuid
61
62 # get specific cmms info by chassis uuid
63 - name: Get nodes data from LXCA
64 community.general.lxca_cmms:
65 login_user: USERID
66 login_password: Password
67 auth_url: "https://10.243.15.168"
68 chassis: "3C737AA5E31640CE949B10C129A8B01F"
69 command_options: cmms_by_chassis_uuid
70 """
71
72 RETURN = r"""
73 result:
74 description: Cmms detail from lxca.
75 returned: success
76 type: dict
77 sample:
78 cmmList:
79 - machineType: ''
80 model: ''
81 type: 'CMM'
82 uuid: '118D2C88C8FD11E4947B6EAE8B4BDCDF'
83 # bunch of properties
84 - machineType: ''
85 model: ''
86 type: 'CMM'
87 uuid: '223D2C88C8FD11E4947B6EAE8B4BDCDF'
88 # bunch of properties
89 # Multiple cmms details
90 """
91
92 import traceback
93
94 from ansible.module_utils.basic import AnsibleModule
95
96 from ansible_collections.community.general.plugins.module_utils._lxca_common import (
97 LXCA_COMMON_ARGS,
98 connection_object,
99 has_pylxca,
100 )
101
102 try:
103 from pylxca import cmms
104 except ImportError:
105 pass
106
107
108 UUID_REQUIRED = "UUID of device is required for cmms_by_uuid command."
109 CHASSIS_UUID_REQUIRED = "UUID of chassis is required for cmms_by_chassis_uuid command."
110
111
112 def _cmms(module, lxca_con):
113 return cmms(lxca_con)
114
115
116 def _cmms_by_uuid(module, lxca_con):
117 if not module.params["uuid"]:
118 module.fail_json(msg=UUID_REQUIRED)
119 return cmms(lxca_con, module.params["uuid"])
120
121
122 def _cmms_by_chassis_uuid(module, lxca_con):
123 if not module.params["chassis"]:
124 module.fail_json(msg=CHASSIS_UUID_REQUIRED)
125 return cmms(lxca_con, chassis=module.params["chassis"])
126
127
128 def setup_module_object():
129 """
130 this function merge argument spec and create ansible module object
131 :return:
132 """
133 args_spec = dict(LXCA_COMMON_ARGS)
134 args_spec.update(INPUT_ARG_SPEC)
135 module = AnsibleModule(argument_spec=args_spec, supports_check_mode=False)
136
137 return module
138
139
140 FUNC_DICT = {
141 "cmms": _cmms,
142 "cmms_by_uuid": _cmms_by_uuid,
143 "cmms_by_chassis_uuid": _cmms_by_chassis_uuid,
144 }
145
146
147 INPUT_ARG_SPEC = dict(
148 command_options=dict(default="cmms", choices=["cmms", "cmms_by_uuid", "cmms_by_chassis_uuid"]),
149 uuid=dict(),
150 chassis=dict(),
151 )
152
153
154 def execute_module(module):
155 """
156 This function invoke commands
157 :param module: Ansible module object
158 """
159 try:
160 with connection_object(module) as lxca_con:
161 result = FUNC_DICT[module.params["command_options"]](module, lxca_con)
162 module.exit_json(changed=False, msg=f"Success {module.params['command_options']} result", result=result)
163 except Exception as exception:
164 error_msg = "; ".join((e) for e in exception.args)
165 module.fail_json(msg=error_msg, exception=traceback.format_exc())
166
167
168 def main():
169 module = setup_module_object()
170 has_pylxca(module)
171 execute_module(module)
172
173
174 if __name__ == "__main__":
175 main()