Debian Codegraph @

ansible_collections/cisco/meraki/plugins/action/devices_switch_routing_interfaces_info.py

raw
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # Copyright (c) 2021, Cisco Systems
5 # GNU General Public License v3.0+ (see LICENSE or
6 # https://www.gnu.org/licenses/gpl-3.0.txt)
7
8 from __future__ import (absolute_import, division, print_function)
9 __metaclass__ = type
10 from ansible.plugins.action import ActionBase
11 try:
12 from ansible_collections.ansible.utils.plugins.module_utils.common.argspec_validate import (
13 AnsibleArgSpecValidator, )
14 except ImportError:
15 ANSIBLE_UTILS_IS_INSTALLED = False
16 else:
17 ANSIBLE_UTILS_IS_INSTALLED = True
18 from ansible.errors import AnsibleActionFail
19 from ansible_collections.cisco.meraki.plugins.plugin_utils.meraki import (
20 MERAKI,
21 meraki_argument_spec,
22 )
23
24 # Get common arguments specification
25 argument_spec = meraki_argument_spec()
26 # Add arguments specific for this module
27 argument_spec.update(dict(
28 serial=dict(type="str"),
29 mode=dict(type="str"),
30 protocol=dict(type="str"),
31 interfaceId=dict(type="str"),
32 ))
33
34 required_if = []
35 required_one_of = []
36 mutually_exclusive = []
37 required_together = []
38
39
40 class ActionModule(ActionBase):
41 def __init__(self, *args, **kwargs):
42 if not ANSIBLE_UTILS_IS_INSTALLED:
43 raise AnsibleActionFail(
44 "ansible.utils is not installed. Execute 'ansible-galaxy collection install ansible.utils'")
45 super(ActionModule, self).__init__(*args, **kwargs)
46 self._supports_async = False
47 self._supports_check_mode = True
48 self._result = None
49
50 # Checks the supplied parameters against the argument spec for this module
51 def _check_argspec(self):
52 aav = AnsibleArgSpecValidator(
53 data=self._task.args,
54 schema=dict(argument_spec=argument_spec),
55 schema_format="argspec",
56 schema_conditionals=dict(
57 required_if=required_if,
58 required_one_of=required_one_of,
59 mutually_exclusive=mutually_exclusive,
60 required_together=required_together,
61 ),
62 name=self._task.action,
63 )
64 valid, errors, self._task.args = aav.validate()
65 if not valid:
66 raise AnsibleActionFail(errors)
67
68 def get_object(self, params):
69 new_object = {}
70 if params.get("serial") is not None:
71 new_object["serial"] = params.get(
72 "serial")
73 if params.get("interfaceId") is not None:
74 new_object["interfaceId"] = params.get(
75 "interfaceId")
76 return new_object
77
78 def get_all(self, params):
79 new_object = {}
80 if params.get("serial") is not None:
81 new_object["serial"] = params.get(
82 "serial")
83 if params.get("mode") is not None:
84 new_object["mode"] = params.get(
85 "mode")
86 if params.get("protocol") is not None:
87 new_object["protocol"] = params.get(
88 "protocol")
89
90 return new_object
91
92 def run(self, tmp=None, task_vars=None):
93 self._task.diff = False
94 self._result = super(ActionModule, self).run(tmp, task_vars)
95 self._result["changed"] = False
96 self._check_argspec()
97
98 self._result.update(dict(meraki_response={}))
99
100 meraki = MERAKI(params=self._task.args)
101
102 id = self._task.args.get("interfaceId")
103 if id:
104 response = meraki.exec_meraki(
105 family="switch",
106 function='getDeviceSwitchRoutingInterface',
107 params=self.get_object(self._task.args),
108 )
109 self._result.update(dict(meraki_response=response))
110 self._result.update(meraki.exit_json())
111 return self._result
112 if not id:
113 response = meraki.exec_meraki(
114 family="switch",
115 function='getDeviceSwitchRoutingInterfaces',
116 params=self.get_all(self._task.args),
117 )
118 self._result.update(dict(meraki_response=response))
119 self._result.update(meraki.exit_json())
120 return self._result