Debian Codegraph @

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