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