| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 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 |
|
| 25 |
argument_spec = meraki_argument_spec() |
| 26 |
|
| 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 |
sortOrder=dict(type="str"), |
| 35 |
networkId=dict(type="str"), |
| 36 |
severity=dict(type="str"), |
| 37 |
types=dict(type="list"), |
| 38 |
tsStart=dict(type="str"), |
| 39 |
tsEnd=dict(type="str"), |
| 40 |
category=dict(type="str"), |
| 41 |
sortBy=dict(type="str"), |
| 42 |
serials=dict(type="list"), |
| 43 |
deviceTypes=dict(type="list"), |
| 44 |
deviceTags=dict(type="list"), |
| 45 |
active=dict(type="bool"), |
| 46 |
dismissed=dict(type="bool"), |
| 47 |
resolved=dict(type="bool"), |
| 48 |
suppressAlertsForOfflineNodes=dict(type="bool"), |
| 49 |
id=dict(type="str"), |
| 50 |
)) |
| 51 |
|
| 52 |
required_if = [] |
| 53 |
required_one_of = [] |
| 54 |
mutually_exclusive = [] |
| 55 |
required_together = [] |
| 56 |
|
| 57 |
|
| 58 |
class ActionModule(ActionBase): |
| 59 |
def __init__(self, *args, **kwargs): |
| 60 |
if not ANSIBLE_UTILS_IS_INSTALLED: |
| 61 |
raise AnsibleActionFail( |
| 62 |
"ansible.utils is not installed. Execute 'ansible-galaxy collection install ansible.utils'") |
| 63 |
super(ActionModule, self).__init__(*args, **kwargs) |
| 64 |
self._supports_async = False |
| 65 |
self._supports_check_mode = True |
| 66 |
self._result = None |
| 67 |
|
| 68 |
|
| 69 |
def _check_argspec(self): |
| 70 |
aav = AnsibleArgSpecValidator( |
| 71 |
data=self._task.args, |
| 72 |
schema=dict(argument_spec=argument_spec), |
| 73 |
schema_format="argspec", |
| 74 |
schema_conditionals=dict( |
| 75 |
required_if=required_if, |
| 76 |
required_one_of=required_one_of, |
| 77 |
mutually_exclusive=mutually_exclusive, |
| 78 |
required_together=required_together, |
| 79 |
), |
| 80 |
name=self._task.action, |
| 81 |
) |
| 82 |
valid, errors, self._task.args = aav.validate() |
| 83 |
if not valid: |
| 84 |
raise AnsibleActionFail(errors) |
| 85 |
|
| 86 |
def get_object(self, params): |
| 87 |
new_object = {} |
| 88 |
if params.get("organizationId") is not None: |
| 89 |
new_object["organizationId"] = params.get( |
| 90 |
"organizationId") |
| 91 |
if params.get("id") is not None: |
| 92 |
new_object["id"] = params.get( |
| 93 |
"id") |
| 94 |
return new_object |
| 95 |
|
| 96 |
def get_all(self, params): |
| 97 |
new_object = {} |
| 98 |
if params.get("organizationId") is not None: |
| 99 |
new_object["organizationId"] = params.get( |
| 100 |
"organizationId") |
| 101 |
if params.get("perPage") is not None: |
| 102 |
new_object["perPage"] = params.get( |
| 103 |
"perPage") |
| 104 |
new_object['total_pages'] = params.get( |
| 105 |
"total_pages") or 1 |
| 106 |
new_object['direction'] = params.get( |
| 107 |
"direction") or "next" |
| 108 |
if params.get("startingAfter") is not None: |
| 109 |
new_object["startingAfter"] = params.get( |
| 110 |
"startingAfter") |
| 111 |
if params.get("endingBefore") is not None: |
| 112 |
new_object["endingBefore"] = params.get( |
| 113 |
"endingBefore") |
| 114 |
if params.get("sortOrder") is not None: |
| 115 |
new_object["sortOrder"] = params.get( |
| 116 |
"sortOrder") |
| 117 |
if params.get("networkId") is not None: |
| 118 |
new_object["networkId"] = params.get( |
| 119 |
"networkId") |
| 120 |
if params.get("severity") is not None: |
| 121 |
new_object["severity"] = params.get( |
| 122 |
"severity") |
| 123 |
if params.get("types") is not None: |
| 124 |
new_object["types"] = params.get( |
| 125 |
"types") |
| 126 |
if params.get("tsStart") is not None: |
| 127 |
new_object["tsStart"] = params.get( |
| 128 |
"tsStart") |
| 129 |
if params.get("tsEnd") is not None: |
| 130 |
new_object["tsEnd"] = params.get( |
| 131 |
"tsEnd") |
| 132 |
if params.get("category") is not None: |
| 133 |
new_object["category"] = params.get( |
| 134 |
"category") |
| 135 |
if params.get("sortBy") is not None: |
| 136 |
new_object["sortBy"] = params.get( |
| 137 |
"sortBy") |
| 138 |
if params.get("serials") is not None: |
| 139 |
new_object["serials"] = params.get( |
| 140 |
"serials") |
| 141 |
if params.get("deviceTypes") is not None: |
| 142 |
new_object["deviceTypes"] = params.get( |
| 143 |
"deviceTypes") |
| 144 |
if params.get("deviceTags") is not None: |
| 145 |
new_object["deviceTags"] = params.get( |
| 146 |
"deviceTags") |
| 147 |
if params.get("active") is not None: |
| 148 |
new_object["active"] = params.get( |
| 149 |
"active") |
| 150 |
if params.get("dismissed") is not None: |
| 151 |
new_object["dismissed"] = params.get( |
| 152 |
"dismissed") |
| 153 |
if params.get("resolved") is not None: |
| 154 |
new_object["resolved"] = params.get( |
| 155 |
"resolved") |
| 156 |
if params.get("suppressAlertsForOfflineNodes") is not None: |
| 157 |
new_object["suppressAlertsForOfflineNodes"] = params.get( |
| 158 |
"suppressAlertsForOfflineNodes") |
| 159 |
|
| 160 |
return new_object |
| 161 |
|
| 162 |
def run(self, tmp=None, task_vars=None): |
| 163 |
self._task.diff = False |
| 164 |
self._result = super(ActionModule, self).run(tmp, task_vars) |
| 165 |
self._result["changed"] = False |
| 166 |
self._check_argspec() |
| 167 |
|
| 168 |
self._result.update(dict(meraki_response={})) |
| 169 |
|
| 170 |
meraki = MERAKI(params=self._task.args) |
| 171 |
|
| 172 |
id = self._task.args.get("id") |
| 173 |
if id: |
| 174 |
response = meraki.exec_meraki( |
| 175 |
family="organizations", |
| 176 |
function='getOrganizationAssuranceAlert', |
| 177 |
params=self.get_object(self._task.args), |
| 178 |
) |
| 179 |
self._result.update(dict(meraki_response=response)) |
| 180 |
self._result.update(meraki.exit_json()) |
| 181 |
return self._result |
| 182 |
if not id: |
| 183 |
response = meraki.exec_meraki( |
| 184 |
family="organizations", |
| 185 |
function='getOrganizationAssuranceAlerts', |
| 186 |
params=self.get_all(self._task.args), |
| 187 |
) |
| 188 |
self._result.update(dict(meraki_response=response)) |
| 189 |
self._result.update(meraki.exit_json()) |
| 190 |
return self._result |