| 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 |
networkId=dict(type="str"), |
| 29 |
productType=dict(type="str"), |
| 30 |
includedEventTypes=dict(type="list"), |
| 31 |
excludedEventTypes=dict(type="list"), |
| 32 |
deviceMac=dict(type="str"), |
| 33 |
deviceSerial=dict(type="str"), |
| 34 |
deviceName=dict(type="str"), |
| 35 |
clientIp=dict(type="str"), |
| 36 |
clientMac=dict(type="str"), |
| 37 |
clientName=dict(type="str"), |
| 38 |
smDeviceMac=dict(type="str"), |
| 39 |
smDeviceName=dict(type="str"), |
| 40 |
eventDetails=dict(type="str"), |
| 41 |
eventSeverity=dict(type="str"), |
| 42 |
isCatalyst=dict(type="bool"), |
| 43 |
perPage=dict(type="int"), |
| 44 |
total_pages=dict(type="int"), |
| 45 |
direction=dict(type="str"), |
| 46 |
startingAfter=dict(type="str"), |
| 47 |
endingBefore=dict(type="str"), |
| 48 |
)) |
| 49 |
|
| 50 |
required_if = [] |
| 51 |
required_one_of = [] |
| 52 |
mutually_exclusive = [] |
| 53 |
required_together = [] |
| 54 |
|
| 55 |
|
| 56 |
class ActionModule(ActionBase): |
| 57 |
def __init__(self, *args, **kwargs): |
| 58 |
if not ANSIBLE_UTILS_IS_INSTALLED: |
| 59 |
raise AnsibleActionFail( |
| 60 |
"ansible.utils is not installed. Execute 'ansible-galaxy collection install ansible.utils'") |
| 61 |
super(ActionModule, self).__init__(*args, **kwargs) |
| 62 |
self._supports_async = False |
| 63 |
self._supports_check_mode = True |
| 64 |
self._result = None |
| 65 |
|
| 66 |
|
| 67 |
def _check_argspec(self): |
| 68 |
aav = AnsibleArgSpecValidator( |
| 69 |
data=self._task.args, |
| 70 |
schema=dict(argument_spec=argument_spec), |
| 71 |
schema_format="argspec", |
| 72 |
schema_conditionals=dict( |
| 73 |
required_if=required_if, |
| 74 |
required_one_of=required_one_of, |
| 75 |
mutually_exclusive=mutually_exclusive, |
| 76 |
required_together=required_together, |
| 77 |
), |
| 78 |
name=self._task.action, |
| 79 |
) |
| 80 |
valid, errors, self._task.args = aav.validate() |
| 81 |
if not valid: |
| 82 |
raise AnsibleActionFail(errors) |
| 83 |
|
| 84 |
def get_all(self, params): |
| 85 |
new_object = {} |
| 86 |
if params.get("networkId") is not None: |
| 87 |
new_object["networkId"] = params.get( |
| 88 |
"networkId") |
| 89 |
if params.get("productType") is not None: |
| 90 |
new_object["productType"] = params.get( |
| 91 |
"productType") |
| 92 |
if params.get("includedEventTypes") is not None: |
| 93 |
new_object["includedEventTypes"] = params.get( |
| 94 |
"includedEventTypes") |
| 95 |
if params.get("excludedEventTypes") is not None: |
| 96 |
new_object["excludedEventTypes"] = params.get( |
| 97 |
"excludedEventTypes") |
| 98 |
if params.get("deviceMac") is not None: |
| 99 |
new_object["deviceMac"] = params.get( |
| 100 |
"deviceMac") |
| 101 |
if params.get("deviceSerial") is not None: |
| 102 |
new_object["deviceSerial"] = params.get( |
| 103 |
"deviceSerial") |
| 104 |
if params.get("deviceName") is not None: |
| 105 |
new_object["deviceName"] = params.get( |
| 106 |
"deviceName") |
| 107 |
if params.get("clientIp") is not None: |
| 108 |
new_object["clientIp"] = params.get( |
| 109 |
"clientIp") |
| 110 |
if params.get("clientMac") is not None: |
| 111 |
new_object["clientMac"] = params.get( |
| 112 |
"clientMac") |
| 113 |
if params.get("clientName") is not None: |
| 114 |
new_object["clientName"] = params.get( |
| 115 |
"clientName") |
| 116 |
if params.get("smDeviceMac") is not None: |
| 117 |
new_object["smDeviceMac"] = params.get( |
| 118 |
"smDeviceMac") |
| 119 |
if params.get("smDeviceName") is not None: |
| 120 |
new_object["smDeviceName"] = params.get( |
| 121 |
"smDeviceName") |
| 122 |
if params.get("eventDetails") is not None: |
| 123 |
new_object["eventDetails"] = params.get( |
| 124 |
"eventDetails") |
| 125 |
if params.get("eventSeverity") is not None: |
| 126 |
new_object["eventSeverity"] = params.get( |
| 127 |
"eventSeverity") |
| 128 |
if params.get("isCatalyst") is not None: |
| 129 |
new_object["isCatalyst"] = params.get( |
| 130 |
"isCatalyst") |
| 131 |
if params.get("perPage") is not None: |
| 132 |
new_object["perPage"] = params.get( |
| 133 |
"perPage") |
| 134 |
new_object['total_pages'] = params.get( |
| 135 |
"total_pages") or 1 |
| 136 |
new_object['direction'] = params.get( |
| 137 |
"direction") or "next" |
| 138 |
if params.get("startingAfter") is not None: |
| 139 |
new_object["startingAfter"] = params.get( |
| 140 |
"startingAfter") |
| 141 |
if params.get("endingBefore") is not None: |
| 142 |
new_object["endingBefore"] = params.get( |
| 143 |
"endingBefore") |
| 144 |
|
| 145 |
return new_object |
| 146 |
|
| 147 |
def run(self, tmp=None, task_vars=None): |
| 148 |
self._task.diff = False |
| 149 |
self._result = super(ActionModule, self).run(tmp, task_vars) |
| 150 |
self._result["changed"] = False |
| 151 |
self._check_argspec() |
| 152 |
|
| 153 |
self._result.update(dict(meraki_response={})) |
| 154 |
|
| 155 |
meraki = MERAKI(params=self._task.args) |
| 156 |
|
| 157 |
response = meraki.exec_meraki( |
| 158 |
family="networks", |
| 159 |
function='getNetworkEvents', |
| 160 |
params=self.get_all(self._task.args), |
| 161 |
) |
| 162 |
self._result.update(dict(meraki_response=response)) |
| 163 |
self._result.update(meraki.exit_json()) |
| 164 |
return self._result |