| 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 |
meraki_compare_equality2, |
| 23 |
get_dict_result, |
| 24 |
) |
| 25 |
from ansible_collections.cisco.meraki.plugins.plugin_utils.exceptions import ( |
| 26 |
InconsistentParameters, |
| 27 |
) |
| 28 |
|
| 29 |
|
| 30 |
argument_spec = meraki_argument_spec() |
| 31 |
|
| 32 |
argument_spec.update(dict( |
| 33 |
state=dict(type="str", default="present", choices=["present"]), |
| 34 |
enabledNetworks=dict(type="list"), |
| 35 |
organizationId=dict(type="str"), |
| 36 |
)) |
| 37 |
|
| 38 |
required_if = [ |
| 39 |
("state", "present", ["organizationId"], True), |
| 40 |
] |
| 41 |
required_one_of = [] |
| 42 |
mutually_exclusive = [] |
| 43 |
required_together = [] |
| 44 |
|
| 45 |
|
| 46 |
class OrganizationsAdaptivePolicySettings(object): |
| 47 |
def __init__(self, params, meraki): |
| 48 |
self.meraki = meraki |
| 49 |
self.new_object = dict( |
| 50 |
enabledNetworks=params.get("enabledNetworks"), |
| 51 |
organization_id=params.get("organizationId"), |
| 52 |
) |
| 53 |
|
| 54 |
def get_all_params(self, name=None, id=None): |
| 55 |
new_object_params = {} |
| 56 |
if self.new_object.get('organizationId') is not None or self.new_object.get( |
| 57 |
'organization_id') is not None: |
| 58 |
new_object_params['organizationId'] = self.new_object.get( |
| 59 |
'organizationId') or self.new_object.get('organization_id') |
| 60 |
return new_object_params |
| 61 |
|
| 62 |
def update_all_params(self): |
| 63 |
new_object_params = {} |
| 64 |
if self.new_object.get('enabledNetworks') is not None or self.new_object.get( |
| 65 |
'enabled_networks') is not None: |
| 66 |
new_object_params['enabledNetworks'] = self.new_object.get( |
| 67 |
'enabledNetworks') or self.new_object.get('enabled_networks') |
| 68 |
if self.new_object.get('organizationId') is not None or self.new_object.get( |
| 69 |
'organization_id') is not None: |
| 70 |
new_object_params['organizationId'] = self.new_object.get( |
| 71 |
'organizationId') or self.new_object.get('organization_id') |
| 72 |
return new_object_params |
| 73 |
|
| 74 |
def get_object_by_name(self, name): |
| 75 |
result = None |
| 76 |
|
| 77 |
try: |
| 78 |
items = self.meraki.exec_meraki( |
| 79 |
family="organizations", |
| 80 |
function="getOrganizationAdaptivePolicySettings", |
| 81 |
params=self.get_all_params(name=name), |
| 82 |
) |
| 83 |
if isinstance(items, dict): |
| 84 |
if 'enabledNetworks' in items: |
| 85 |
items = items.get('enabledNetworks') |
| 86 |
result = get_dict_result(items, 'name', name) |
| 87 |
if result is None: |
| 88 |
result = items |
| 89 |
except Exception as e: |
| 90 |
print("Error: ", e) |
| 91 |
result = None |
| 92 |
return result |
| 93 |
|
| 94 |
def get_object_by_id(self, id): |
| 95 |
result = None |
| 96 |
|
| 97 |
return result |
| 98 |
|
| 99 |
def exists(self): |
| 100 |
prev_obj = None |
| 101 |
id_exists = False |
| 102 |
name_exists = False |
| 103 |
o_id = self.new_object.get( |
| 104 |
"organizationId") or self.new_object.get("organization_id") |
| 105 |
name = self.new_object.get("name") |
| 106 |
if o_id: |
| 107 |
prev_obj = self.get_object_by_name(o_id) |
| 108 |
id_exists = prev_obj is not None and isinstance(prev_obj, dict) |
| 109 |
if not id_exists and name: |
| 110 |
prev_obj = self.get_object_by_name(name) |
| 111 |
name_exists = prev_obj is not None and isinstance(prev_obj, dict) |
| 112 |
if name_exists: |
| 113 |
_id = prev_obj.get("id") |
| 114 |
if id_exists and name_exists and o_id != _id: |
| 115 |
raise InconsistentParameters( |
| 116 |
"The 'id' and 'name' params don't refer to the same object") |
| 117 |
if _id: |
| 118 |
self.new_object.update(dict(id=_id)) |
| 119 |
it_exists = prev_obj is not None and isinstance(prev_obj, dict) |
| 120 |
return (it_exists, prev_obj) |
| 121 |
|
| 122 |
def requires_update(self, current_obj): |
| 123 |
requested_obj = self.new_object |
| 124 |
|
| 125 |
obj_params = [ |
| 126 |
("enabledNetworks", "enabledNetworks"), |
| 127 |
("organizationId", "organizationId"), |
| 128 |
] |
| 129 |
|
| 130 |
|
| 131 |
return any( |
| 132 |
not meraki_compare_equality2( |
| 133 |
current_obj.get(meraki_param), |
| 134 |
requested_obj.get(ansible_param)) for ( |
| 135 |
meraki_param, |
| 136 |
ansible_param) in obj_params) |
| 137 |
|
| 138 |
def update(self): |
| 139 |
id = self.new_object.get("id") |
| 140 |
name = self.new_object.get("name") |
| 141 |
result = None |
| 142 |
result = self.meraki.exec_meraki( |
| 143 |
family="organizations", |
| 144 |
function="updateOrganizationAdaptivePolicySettings", |
| 145 |
params=self.update_all_params(), |
| 146 |
op_modifies=True, |
| 147 |
) |
| 148 |
return result |
| 149 |
|
| 150 |
|
| 151 |
class ActionModule(ActionBase): |
| 152 |
def __init__(self, *args, **kwargs): |
| 153 |
if not ANSIBLE_UTILS_IS_INSTALLED: |
| 154 |
raise AnsibleActionFail( |
| 155 |
"ansible.utils is not installed. Execute 'ansible-galaxy collection install ansible.utils'") |
| 156 |
super(ActionModule, self).__init__(*args, **kwargs) |
| 157 |
self._supports_async = False |
| 158 |
self._supports_check_mode = False |
| 159 |
self._result = None |
| 160 |
|
| 161 |
|
| 162 |
def _check_argspec(self): |
| 163 |
aav = AnsibleArgSpecValidator( |
| 164 |
data=self._task.args, |
| 165 |
schema=dict(argument_spec=argument_spec), |
| 166 |
schema_format="argspec", |
| 167 |
schema_conditionals=dict( |
| 168 |
required_if=required_if, |
| 169 |
required_one_of=required_one_of, |
| 170 |
mutually_exclusive=mutually_exclusive, |
| 171 |
required_together=required_together, |
| 172 |
), |
| 173 |
name=self._task.action, |
| 174 |
) |
| 175 |
valid, errors, self._task.args = aav.validate() |
| 176 |
if not valid: |
| 177 |
raise AnsibleActionFail(errors) |
| 178 |
|
| 179 |
def run(self, tmp=None, task_vars=None): |
| 180 |
self._task.diff = False |
| 181 |
self._result = super(ActionModule, self).run(tmp, task_vars) |
| 182 |
self._result["changed"] = False |
| 183 |
self._check_argspec() |
| 184 |
|
| 185 |
meraki = MERAKI(self._task.args) |
| 186 |
obj = OrganizationsAdaptivePolicySettings(self._task.args, meraki) |
| 187 |
|
| 188 |
state = self._task.args.get("state") |
| 189 |
|
| 190 |
response = None |
| 191 |
if state == "present": |
| 192 |
(obj_exists, prev_obj) = obj.exists() |
| 193 |
if obj_exists: |
| 194 |
if obj.requires_update(prev_obj): |
| 195 |
response = obj.update() |
| 196 |
meraki.object_updated() |
| 197 |
else: |
| 198 |
response = prev_obj |
| 199 |
meraki.object_already_present() |
| 200 |
else: |
| 201 |
meraki.fail_json( |
| 202 |
"Object does not exists, plugin only has update") |
| 203 |
|
| 204 |
self._result.update(dict(meraki_response=response)) |
| 205 |
self._result.update(meraki.exit_json()) |
| 206 |
return self._result |