| 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 |
organizationId=dict(type="str"), |
| 30 |
perPage=dict(type="int"), |
| 31 |
total_pages=dict(type="int"), |
| 32 |
direction=dict(type="str"), |
| 33 |
startingAfter=dict(type="str"), |
| 34 |
endingBefore=dict(type="str"), |
| 35 |
configurationUpdatedAfter=dict(type="str"), |
| 36 |
networkIds=dict(type="list"), |
| 37 |
productTypes=dict(type="list"), |
| 38 |
tags=dict(type="list"), |
| 39 |
tagsFilterType=dict(type="str"), |
| 40 |
name=dict(type="str"), |
| 41 |
mac=dict(type="str"), |
| 42 |
model=dict(type="str"), |
| 43 |
macs=dict(type="list"), |
| 44 |
serials=dict(type="list"), |
| 45 |
sensorMetrics=dict(type="list"), |
| 46 |
sensorAlertProfileIds=dict(type="list"), |
| 47 |
models=dict(type="list"), |
| 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 |
# Checks the supplied parameters against the argument spec for this module |
| 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_object(self, params): |
| 85 |
new_object = {} |
| 86 |
if params.get("serial") is not None: |
| 87 |
new_object["serial"] = params.get( |
| 88 |
"serial") |
| 89 |
return new_object |
| 90 |
|
| 91 |
def get_all(self, params): |
| 92 |
new_object = {} |
| 93 |
if params.get("organizationId") is not None: |
| 94 |
new_object["organizationId"] = params.get( |
| 95 |
"organizationId") |
| 96 |
if params.get("perPage") is not None: |
| 97 |
new_object["perPage"] = params.get( |
| 98 |
"perPage") |
| 99 |
new_object['total_pages'] = params.get( |
| 100 |
"total_pages") or 1 |
| 101 |
new_object['direction'] = params.get( |
| 102 |
"direction") or "next" |
| 103 |
if params.get("startingAfter") is not None: |
| 104 |
new_object["startingAfter"] = params.get( |
| 105 |
"startingAfter") |
| 106 |
if params.get("endingBefore") is not None: |
| 107 |
new_object["endingBefore"] = params.get( |
| 108 |
"endingBefore") |
| 109 |
if params.get("configurationUpdatedAfter") is not None: |
| 110 |
new_object["configurationUpdatedAfter"] = params.get( |
| 111 |
"configurationUpdatedAfter") |
| 112 |
if params.get("networkIds") is not None: |
| 113 |
new_object["networkIds"] = params.get( |
| 114 |
"networkIds") |
| 115 |
if params.get("productTypes") is not None: |
| 116 |
new_object["productTypes"] = params.get( |
| 117 |
"productTypes") |
| 118 |
if params.get("tags") is not None: |
| 119 |
new_object["tags"] = params.get( |
| 120 |
"tags") |
| 121 |
if params.get("tagsFilterType") is not None: |
| 122 |
new_object["tagsFilterType"] = params.get( |
| 123 |
"tagsFilterType") |
| 124 |
if params.get("name") is not None: |
| 125 |
new_object["name"] = params.get( |
| 126 |
"name") |
| 127 |
if params.get("mac") is not None: |
| 128 |
new_object["mac"] = params.get( |
| 129 |
"mac") |
| 130 |
if params.get("serial") is not None: |
| 131 |
new_object["serial"] = params.get( |
| 132 |
"serial") |
| 133 |
if params.get("model") is not None: |
| 134 |
new_object["model"] = params.get( |
| 135 |
"model") |
| 136 |
if params.get("macs") is not None: |
| 137 |
new_object["macs"] = params.get( |
| 138 |
"macs") |
| 139 |
if params.get("serials") is not None: |
| 140 |
new_object["serials"] = params.get( |
| 141 |
"serials") |
| 142 |
if params.get("sensorMetrics") is not None: |
| 143 |
new_object["sensorMetrics"] = params.get( |
| 144 |
"sensorMetrics") |
| 145 |
if params.get("sensorAlertProfileIds") is not None: |
| 146 |
new_object["sensorAlertProfileIds"] = params.get( |
| 147 |
"sensorAlertProfileIds") |
| 148 |
if params.get("models") is not None: |
| 149 |
new_object["models"] = params.get( |
| 150 |
"models") |
| 151 |
|
| 152 |
return new_object |
| 153 |
|
| 154 |
def run(self, tmp=None, task_vars=None): |
| 155 |
self._task.diff = False |
| 156 |
self._result = super(ActionModule, self).run(tmp, task_vars) |
| 157 |
self._result["changed"] = False |
| 158 |
self._check_argspec() |
| 159 |
|
| 160 |
self._result.update(dict(meraki_response={})) |
| 161 |
|
| 162 |
meraki = MERAKI(params=self._task.args) |
| 163 |
|
| 164 |
id = self._task.args.get("serial") |
| 165 |
if id: |
| 166 |
response = meraki.exec_meraki( |
| 167 |
family="devices", |
| 168 |
function='getDevice', |
| 169 |
params=self.get_object(self._task.args), |
| 170 |
) |
| 171 |
self._result.update(dict(meraki_response=response)) |
| 172 |
self._result.update(meraki.exit_json()) |
| 173 |
return self._result |
| 174 |
if not id: |
| 175 |
response = meraki.exec_meraki( |
| 176 |
family="organizations", |
| 177 |
function='getOrganizationDevices', |
| 178 |
params=self.get_all(self._task.args), |
| 179 |
) |
| 180 |
self._result.update(dict(meraki_response=response)) |
| 181 |
self._result.update(meraki.exit_json()) |
| 182 |
return self._result |