| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2021, Kevin Breit (@kbreit) <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
ANSIBLE_METADATA = { |
| 12 |
"metadata_version": "1.1", |
| 13 |
"status": ["deprecated"], |
| 14 |
"supported_by": "community", |
| 15 |
} |
| 16 |
|
| 17 |
DOCUMENTATION = r""" |
| 18 |
author: |
| 19 |
- Kevin Breit (@kbreit) |
| 20 |
deprecated: |
| 21 |
alternative: cisco.meraki.organizations_action_batches |
| 22 |
removed_in: 3.0.0 |
| 23 |
why: Updated modules released with increased functionality |
| 24 |
description: |
| 25 |
- Allows for management of Action Batch jobs for Meraki. |
| 26 |
extends_documentation_fragment: cisco.meraki.meraki |
| 27 |
module: meraki_action_batch |
| 28 |
notes: |
| 29 |
- This module is in active development and the interface may change. |
| 30 |
options: |
| 31 |
action_batch_id: |
| 32 |
description: |
| 33 |
- ID of an existing Action Batch job. |
| 34 |
type: str |
| 35 |
actions: |
| 36 |
description: |
| 37 |
- List of actions the job should execute. |
| 38 |
elements: dict |
| 39 |
suboptions: |
| 40 |
body: |
| 41 |
description: |
| 42 |
- Required body of action. |
| 43 |
type: raw |
| 44 |
operation: |
| 45 |
choices: |
| 46 |
- create |
| 47 |
- destroy |
| 48 |
- update |
| 49 |
- claim |
| 50 |
- bind |
| 51 |
- split |
| 52 |
- unbind |
| 53 |
- combine |
| 54 |
- update_order |
| 55 |
- cycle |
| 56 |
- swap |
| 57 |
- assignSeats |
| 58 |
- move |
| 59 |
- moveSeats |
| 60 |
- renewSeats |
| 61 |
description: |
| 62 |
- Operation type of action |
| 63 |
type: str |
| 64 |
resource: |
| 65 |
description: |
| 66 |
- Path to Action Batch resource. |
| 67 |
type: str |
| 68 |
type: list |
| 69 |
confirmed: |
| 70 |
default: false |
| 71 |
description: |
| 72 |
- Whether job is to be executed. |
| 73 |
type: bool |
| 74 |
net_id: |
| 75 |
description: |
| 76 |
- ID of network, if applicable. |
| 77 |
type: str |
| 78 |
net_name: |
| 79 |
description: |
| 80 |
- Name of network, if applicable. |
| 81 |
type: str |
| 82 |
state: |
| 83 |
choices: |
| 84 |
- query |
| 85 |
- present |
| 86 |
- absent |
| 87 |
default: present |
| 88 |
description: |
| 89 |
- Specifies whether to lookup, |
| 90 |
create, |
| 91 |
or delete an Action Batch job. |
| 92 |
type: str |
| 93 |
synchronous: |
| 94 |
default: true |
| 95 |
description: |
| 96 |
- Whether job is a synchronous or asynchronous job. |
| 97 |
type: bool |
| 98 |
short_description: Manage Action Batch jobs within the Meraki Dashboard. |
| 99 |
""" |
| 100 |
|
| 101 |
|
| 102 |
EXAMPLES = r""" |
| 103 |
- name: Query all Action Batches |
| 104 |
meraki_action_batch: |
| 105 |
auth_key: abc123 |
| 106 |
org_name: YourOrg |
| 107 |
state: query |
| 108 |
delegate_to: localhost |
| 109 |
- name: Query one Action Batch job |
| 110 |
meraki_action_batch: |
| 111 |
auth_key: abc123 |
| 112 |
org_name: YourOrg |
| 113 |
state: query |
| 114 |
action_batch_id: 12345 |
| 115 |
delegate_to: localhost |
| 116 |
- name: Create an Action Batch job |
| 117 |
meraki_action_batch: |
| 118 |
auth_key: abc123 |
| 119 |
org_name: YourOrg |
| 120 |
state: present |
| 121 |
actions: |
| 122 |
- resource: /organizations/org_123/networks |
| 123 |
operation: create |
| 124 |
body: |
| 125 |
name: AnsibleActionBatch1 |
| 126 |
productTypes: |
| 127 |
- switch |
| 128 |
delegate_to: localhost |
| 129 |
- name: Update Action Batch job |
| 130 |
meraki_action_batch: |
| 131 |
auth_key: abc123 |
| 132 |
org_name: YourOrg |
| 133 |
state: present |
| 134 |
action_batch_id: 12345 |
| 135 |
synchronous: false |
| 136 |
- name: Create an Action Batch job with multiple actions |
| 137 |
meraki_action_batch: |
| 138 |
auth_key: abc123 |
| 139 |
org_name: YourOrg |
| 140 |
state: present |
| 141 |
actions: |
| 142 |
- resource: /organizations/org_123/networks |
| 143 |
operation: create |
| 144 |
body: |
| 145 |
name: AnsibleActionBatch2 |
| 146 |
productTypes: |
| 147 |
- switch |
| 148 |
- resource: /organizations/org_123/networks |
| 149 |
operation: create |
| 150 |
body: |
| 151 |
name: AnsibleActionBatch3 |
| 152 |
productTypes: |
| 153 |
- switch |
| 154 |
delegate_to: localhost |
| 155 |
- name: Delete an Action Batch job |
| 156 |
meraki_action_batch: |
| 157 |
auth_key: abc123 |
| 158 |
org_name: YourOrg |
| 159 |
state: absent |
| 160 |
action_batch_id: 12345 |
| 161 |
delegate_to: localhost |
| 162 |
""" |
| 163 |
|
| 164 |
RETURN = r""" |
| 165 |
data: |
| 166 |
description: Information about action batch jobs. |
| 167 |
type: complex |
| 168 |
returned: always |
| 169 |
contains: |
| 170 |
id: |
| 171 |
description: Unique ID of action batch job. |
| 172 |
returned: success |
| 173 |
type: str |
| 174 |
sample: 123 |
| 175 |
organization_id: |
| 176 |
description: Unique ID of organization which owns batch job. |
| 177 |
returned: success |
| 178 |
type: str |
| 179 |
sample: 2930418 |
| 180 |
confirmed: |
| 181 |
description: Whether action batch job was confirmed for execution. |
| 182 |
returned: success |
| 183 |
type: bool |
| 184 |
synchronous: |
| 185 |
description: Whether action batch job executes synchronously or asynchronously. |
| 186 |
returned: success |
| 187 |
type: bool |
| 188 |
status: |
| 189 |
description: Information about the action batch job state. |
| 190 |
type: complex |
| 191 |
contains: |
| 192 |
completed: |
| 193 |
description: Whether job has completed. |
| 194 |
type: bool |
| 195 |
returned: success |
| 196 |
failed: |
| 197 |
description: Whether execution of action batch job failed. |
| 198 |
type: bool |
| 199 |
returned: success |
| 200 |
errors: |
| 201 |
description: List of errors, if any, created during execution. |
| 202 |
type: list |
| 203 |
returned: success |
| 204 |
created_resources: |
| 205 |
description: List of resources created during execution. |
| 206 |
type: list |
| 207 |
returned: success |
| 208 |
sample: [{"id": 100, "uri": "/networks/L_XXXXX/groupPolicies/100"}] |
| 209 |
actions: |
| 210 |
description: List of actions associated to job. |
| 211 |
type: dict |
| 212 |
""" |
| 213 |
|
| 214 |
from ansible.module_utils.basic import AnsibleModule, json |
| 215 |
from ansible_collections.cisco.meraki.plugins.module_utils.network.meraki.meraki import ( |
| 216 |
MerakiModule, |
| 217 |
meraki_argument_spec, |
| 218 |
) |
| 219 |
|
| 220 |
|
| 221 |
def _construct_payload(meraki): |
| 222 |
payload = dict() |
| 223 |
payload["confirmed"] = meraki.params["confirmed"] |
| 224 |
payload["synchronous"] = meraki.params["synchronous"] |
| 225 |
if meraki.params["actions"] is not None: # No payload is specified for an update |
| 226 |
payload["actions"] = list() |
| 227 |
for action in meraki.params["actions"]: |
| 228 |
action_detail = dict() |
| 229 |
if action["resource"] is not None: |
| 230 |
action_detail["resource"] = action["resource"] |
| 231 |
if action["operation"] is not None: |
| 232 |
action_detail["operation"] = action["operation"] |
| 233 |
if action["body"] is not None: |
| 234 |
action_detail["body"] = action["body"] |
| 235 |
payload["actions"].append(action_detail) |
| 236 |
return payload |
| 237 |
|
| 238 |
|
| 239 |
def main(): |
| 240 |
|
| 241 |
# define the available arguments/parameters that a user can pass to |
| 242 |
# the module |
| 243 |
|
| 244 |
actions_arg_spec = dict( |
| 245 |
operation=dict( |
| 246 |
type="str", |
| 247 |
choices=[ |
| 248 |
"create", |
| 249 |
"destroy", |
| 250 |
"update", |
| 251 |
"claim", |
| 252 |
"bind", |
| 253 |
"split", |
| 254 |
"unbind", |
| 255 |
"combine", |
| 256 |
"update_order", |
| 257 |
"cycle", |
| 258 |
"swap", |
| 259 |
"assignSeats", |
| 260 |
"move", |
| 261 |
"moveSeats", |
| 262 |
"renewSeats", |
| 263 |
], |
| 264 |
), |
| 265 |
resource=dict(type="str"), |
| 266 |
body=dict(type="raw"), |
| 267 |
) |
| 268 |
|
| 269 |
argument_spec = meraki_argument_spec() |
| 270 |
argument_spec.update( |
| 271 |
state=dict( |
| 272 |
type="str", choices=["present", "query", "absent"], default="present" |
| 273 |
), |
| 274 |
net_name=dict(type="str"), |
| 275 |
net_id=dict(type="str"), |
| 276 |
action_batch_id=dict(type="str", default=None), |
| 277 |
confirmed=dict(type="bool", default=False), |
| 278 |
synchronous=dict(type="bool", default=True), |
| 279 |
actions=dict( |
| 280 |
type="list", default=None, elements="dict", options=actions_arg_spec |
| 281 |
), |
| 282 |
) |
| 283 |
|
| 284 |
# the AnsibleModule object will be our abstraction working with Ansible |
| 285 |
# this includes instantiation, a couple of common attr would be the |
| 286 |
# args/params passed to the execution, as well as if the module |
| 287 |
# supports check mode |
| 288 |
module = AnsibleModule( |
| 289 |
argument_spec=argument_spec, |
| 290 |
supports_check_mode=True, |
| 291 |
) |
| 292 |
meraki = MerakiModule(module, function="action_batch") |
| 293 |
meraki.params["follow_redirects"] = "all" |
| 294 |
|
| 295 |
query_urls = {"action_batch": "/organizations/{org_id}/actionBatches"} |
| 296 |
query_one_urls = { |
| 297 |
"action_batch": "/organizations/{org_id}/actionBatches/{action_batch_id}" |
| 298 |
} |
| 299 |
create_urls = {"action_batch": "/organizations/{org_id}/actionBatches"} |
| 300 |
update_urls = { |
| 301 |
"action_batch": "/organizations/{org_id}/actionBatches/{action_batch_id}" |
| 302 |
} |
| 303 |
delete_urls = { |
| 304 |
"action_batch": "/organizations/{org_id}/actionBatches/{action_batch_id}" |
| 305 |
} |
| 306 |
|
| 307 |
meraki.url_catalog["get_all"].update(query_urls) |
| 308 |
meraki.url_catalog["get_one"].update(query_one_urls) |
| 309 |
meraki.url_catalog["create"] = create_urls |
| 310 |
meraki.url_catalog["update"] = update_urls |
| 311 |
meraki.url_catalog["delete"] = delete_urls |
| 312 |
|
| 313 |
payload = None |
| 314 |
|
| 315 |
if not meraki.params["org_name"] and not meraki.params["org_id"]: |
| 316 |
meraki.fail_json(msg="org_name or org_id is required") |
| 317 |
|
| 318 |
org_id = meraki.params["org_id"] |
| 319 |
if org_id is None: |
| 320 |
org_id = meraki.get_org_id(meraki.params["org_name"]) |
| 321 |
|
| 322 |
if meraki.params["state"] == "query": |
| 323 |
if meraki.params["action_batch_id"] is None: # Get all Action Batches |
| 324 |
path = meraki.construct_path("get_all", org_id=org_id) |
| 325 |
response = meraki.request(path, method="GET") |
| 326 |
if meraki.status == 200: |
| 327 |
meraki.result["data"] = response |
| 328 |
meraki.exit_json(**meraki.result) |
| 329 |
elif meraki.params["action_batch_id"] is not None: # Query one Action Batch job |
| 330 |
path = meraki.construct_path( |
| 331 |
"get_one", |
| 332 |
org_id=org_id, |
| 333 |
custom={"action_batch_id": meraki.params["action_batch_id"]}, |
| 334 |
) |
| 335 |
response = meraki.request(path, method="GET") |
| 336 |
if meraki.status == 200: |
| 337 |
meraki.result["data"] = response |
| 338 |
meraki.exit_json(**meraki.result) |
| 339 |
elif meraki.params["state"] == "present": |
| 340 |
if meraki.params["action_batch_id"] is None: # Create a new Action Batch job |
| 341 |
payload = _construct_payload(meraki) |
| 342 |
path = meraki.construct_path("create", org_id=org_id) |
| 343 |
response = meraki.request(path, method="POST", payload=json.dumps(payload)) |
| 344 |
if meraki.status == 201: |
| 345 |
meraki.result["data"] = response |
| 346 |
meraki.result["changed"] = True |
| 347 |
meraki.exit_json(**meraki.result) |
| 348 |
elif meraki.params["action_batch_id"] is not None: |
| 349 |
path = meraki.construct_path( |
| 350 |
"get_one", |
| 351 |
org_id=org_id, |
| 352 |
custom={"action_batch_id": meraki.params["action_batch_id"]}, |
| 353 |
) |
| 354 |
current = meraki.request(path, method="GET") |
| 355 |
payload = _construct_payload(meraki) |
| 356 |
if ( |
| 357 |
meraki.params["actions"] is not None |
| 358 |
): # Cannot update the body once a job is submitted |
| 359 |
meraki.fail_json(msg="Body cannot be updated on existing job.") |
| 360 |
if ( |
| 361 |
meraki.is_update_required(current, payload) is True |
| 362 |
): # Job needs to be modified |
| 363 |
path = meraki.construct_path( |
| 364 |
"update", |
| 365 |
org_id=org_id, |
| 366 |
custom={"action_batch_id": meraki.params["action_batch_id"]}, |
| 367 |
) |
| 368 |
response = meraki.request( |
| 369 |
path, method="PUT", payload=json.dumps(payload) |
| 370 |
) |
| 371 |
if meraki.status == 200: |
| 372 |
meraki.result["data"] = response |
| 373 |
meraki.result["changed"] = True |
| 374 |
meraki.exit_json(**meraki.result) |
| 375 |
else: # Idempotent response |
| 376 |
meraki.result["data"] = current |
| 377 |
meraki.exit_json(**meraki.result) |
| 378 |
elif meraki.params["state"] == "absent": |
| 379 |
path = meraki.construct_path( |
| 380 |
"delete", |
| 381 |
org_id=org_id, |
| 382 |
custom={"action_batch_id": meraki.params["action_batch_id"]}, |
| 383 |
) |
| 384 |
response = meraki.request(path, method="DELETE") |
| 385 |
if meraki.status == 204: |
| 386 |
meraki.result["data"] = response |
| 387 |
meraki.result["changed"] = True |
| 388 |
|
| 389 |
# in the event of a successful module execution, you will want to |
| 390 |
# simple AnsibleModule.exit_json(), passing the key/value results |
| 391 |
meraki.exit_json(**meraki.result) |
| 392 |
|
| 393 |
|
| 394 |
if __name__ == "__main__": |
| 395 |
main() |