Debian Codegraph @

ansible_collections/cisco/meraki/plugins/action/networks_sm_target_groups_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 networkId=dict(type="str"),
29 withDetails=dict(type="bool"),
30 targetGroupId=dict(type="str"),
31 ))
32
33 required_if = []
34 required_one_of = []
35 mutually_exclusive = []
36 required_together = []
37
38
39 class ActionModule(ActionBase):
40 def __init__(self, *args, **kwargs):
41 if not ANSIBLE_UTILS_IS_INSTALLED:
42 raise AnsibleActionFail(
43 "ansible.utils is not installed. Execute 'ansible-galaxy collection install ansible.utils'")
44 super(ActionModule, self).__init__(*args, **kwargs)
45 self._supports_async = False
46 self._supports_check_mode = True
47 self._result = None
48
49 # Checks the supplied parameters against the argument spec for this module
50 def _check_argspec(self):
51 aav = AnsibleArgSpecValidator(
52 data=self._task.args,
53 schema=dict(argument_spec=argument_spec),
54 schema_format="argspec",
55 schema_conditionals=dict(
56 required_if=required_if,
57 required_one_of=required_one_of,
58 mutually_exclusive=mutually_exclusive,
59 required_together=required_together,
60 ),
61 name=self._task.action,
62 )
63 valid, errors, self._task.args = aav.validate()
64 if not valid:
65 raise AnsibleActionFail(errors)
66
67 def get_object(self, params):
68 new_object = {}
69 if params.get("networkId") is not None:
70 new_object["networkId"] = params.get(
71 "networkId")
72 if params.get("targetGroupId") is not None:
73 new_object["targetGroupId"] = params.get(
74 "targetGroupId")
75 if params.get("withDetails") is not None:
76 new_object["withDetails"] = params.get(
77 "withDetails")
78 return new_object
79
80 def get_all(self, params):
81 new_object = {}
82 if params.get("networkId") is not None:
83 new_object["networkId"] = params.get(
84 "networkId")
85 if params.get("withDetails") is not None:
86 new_object["withDetails"] = params.get(
87 "withDetails")
88
89 return new_object
90
91 def run(self, tmp=None, task_vars=None):
92 self._task.diff = False
93 self._result = super(ActionModule, self).run(tmp, task_vars)
94 self._result["changed"] = False
95 self._check_argspec()
96
97 self._result.update(dict(meraki_response={}))
98
99 meraki = MERAKI(params=self._task.args)
100
101 id = self._task.args.get("targetGroupId")
102 if id:
103 response = meraki.exec_meraki(
104 family="sm",
105 function='getNetworkSmTargetGroup',
106 params=self.get_object(self._task.args),
107 )
108 self._result.update(dict(meraki_response=response))
109 self._result.update(meraki.exit_json())
110 return self._result
111 if not id:
112 response = meraki.exec_meraki(
113 family="sm",
114 function='getNetworkSmTargetGroups',
115 params=self.get_all(self._task.args),
116 )
117 self._result.update(dict(meraki_response=response))
118 self._result.update(meraki.exit_json())
119 return self._result