Coverage for plugins/modules/meraki_ssid.py: 79%

# Coverage for plugins/modules/meraki_ssid.py : 79%

## 142 statements 127 run 15 missing 0 excluded 24 partial

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 | #!/usr/bin/python # -*- coding: utf-8 -*- # Copyright: (c) 2018, Kevin Breit (@kbreit) <kevin.breit@kevinbreit.net> # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) from __future__ import absolute_import, division, print_function __metaclass__ = type ANSIBLE_METADATA = { 'metadata_version': '1.1', 'status': ['preview'], 'supported_by': 'community' } DOCUMENTATION = r''' --- module: meraki_ssid short_description: Manage wireless SSIDs in the Meraki cloud version_added: "2.7" description: - Allows for management of SSIDs in a Meraki wireless environment. notes: - Deleting an SSID does not delete RADIUS servers. options: state: description: - Specifies whether SNMP information should be queried or modified. type: str choices: [ absent, query, present ] default: present number: description: - SSID number within network. type: int aliases: [ssid_number] name: description: - Name of SSID. type: str net_name: description: - Name of network. type: str net_id: description: - ID of network. type: str enabled: description: - Enable or disable SSID network. type: bool auth_mode: description: - Set authentication mode of network. type: str choices: [open, psk, open-with-radius, 8021x-meraki, 8021x-radius] encryption_mode: description: - Set encryption mode of network. type: str choices: [wpa, eap, wpa-eap] psk: description: - Password for wireless network. - Requires auth_mode to be set to psk. type: str wpa_encryption_mode: description: - Encryption mode within WPA2 specification. type: str choices: [WPA1 and WPA2, WPA2 only] splash_page: description: - Set to enable splash page and specify type of splash. type: str choices: ['None', 'Click-through splash page', 'Billing', 'Password-protected with Meraki RADIUS', 'Password-protected with custom RADIUS', 'Password-protected with Active Directory', 'Password-protected with LDAP', 'SMS authentication', 'Systems Manager Sentry', 'Facebook Wi-Fi', 'Google OAuth', 'Sponsored guest'] radius_servers: description: - List of RADIUS servers. type: list suboptions: host: description: - IP address or hostname of RADIUS server. type: str port: description: - Port number RADIUS server is listening to. type: int secret: description: - RADIUS password. - Setting password is not idempotent. type: str radius_coa_enabled: description: - Enable or disable RADIUS CoA (Change of Authorization) on SSID. type: bool radius_failover_policy: description: - Set client access policy in case RADIUS servers aren't available. type: str choices: [Deny access, Allow access] radius_load_balancing_policy: description: - Set load balancing policy when multiple RADIUS servers are specified. type: str choices: [Strict priority order, Round robin] radius_accounting_enabled: description: - Enable or disable RADIUS accounting. type: bool radius_accounting_servers: description: - List of RADIUS servers for RADIUS accounting. type: list suboptions: host: description: - IP address or hostname of RADIUS server. type: str port: description: - Port number RADIUS server is listening to. type: int secret: description: - RADIUS password. - Setting password is not idempotent. type: str ip_assignment_mode: description: - Method of which SSID uses to assign IP addresses. type: str choices: ['NAT mode', 'Bridge mode', 'Layer 3 roaming', 'Layer 3 roaming with a concentrator', 'VPN'] use_vlan_tagging: description: - Set whether to use VLAN tagging. - Requires C(default_vlan_id) to be set. type: bool default_vlan_id: description: - Default VLAN ID. - Requires C(ip_assignment_mode) to be C(Bridge mode) or C(Layer 3 roaming). type: int vlan_id: description: - ID number of VLAN on SSID. - Requires C(ip_assignment_mode) to be C(ayer 3 roaming with a concentrator) or C(VPN). type: int ap_tags_vlan_ids: description: - List of VLAN tags. - Requires C(ip_assignment_mode) to be C(Bridge mode) or C(Layer 3 roaming). - Requires C(use_vlan_tagging) to be C(True). type: list suboptions: tags: description: - List of AP tags. type: list vlan_id: description: - Numerical identifier that is assigned to the VLAN. type: int walled_garden_enabled: description: - Enable or disable walled garden functionality. type: bool walled_garden_ranges: description: - List of walled garden ranges. type: list min_bitrate: description: - Minimum bitrate (Mbps) allowed on SSID. type: float choices: [1, 2, 5.5, 6, 9, 11, 12, 18, 24, 36, 48, 54] band_selection: description: - Set band selection mode. type: str choices: ['Dual band operation', '5 GHz band only', 'Dual band operation with Band Steering'] per_client_bandwidth_limit_up: description: - Maximum bandwidth in Mbps devices on SSID can upload. type: int per_client_bandwidth_limit_down: description: - Maximum bandwidth in Mbps devices on SSID can download. type: int concentrator_network_id: description: - The concentrator to use for 'Layer 3 roaming with a concentrator' or 'VPN'. type: str author: - Kevin Breit (@kbreit) extends_documentation_fragment: meraki ''' EXAMPLES = r''' - name: Enable and name SSID meraki_ssid: auth_key: abc123 state: present org_name: YourOrg net_name: WiFi name: GuestSSID enabled: true delegate_to: localhost - name: Set PSK with invalid encryption mode meraki_ssid: auth_key: abc123 state: present org_name: YourOrg net_name: WiFi name: GuestSSID auth_mode: psk psk: abc1234 encryption_mode: eap ignore_errors: yes delegate_to: localhost - name: Configure RADIUS servers meraki_ssid: auth_key: abc123 state: present org_name: YourOrg net_name: WiFi name: GuestSSID auth_mode: open-with-radius radius_servers: - host: 192.0.1.200 port: 1234 secret: abc98765 delegate_to: localhost - name: Enable click-through splash page meraki_ssid: auth_key: abc123 state: present org_name: YourOrg net_name: WiFi name: GuestSSID splash_page: Click-through splash page delegate_to: localhost ''' RETURN = r''' data: description: List of wireless SSIDs. returned: success type: complex contains: number: description: Zero-based index number for SSIDs. returned: success type: int sample: 0 name: description: - Name of wireless SSID. - This value is what is broadcasted. returned: success type: str sample: CorpWireless enabled: description: Enabled state of wireless network. returned: success type: bool sample: true splash_page: description: Splash page to show when user authenticates. returned: success type: str sample: Click-through splash page ssid_admin_accessible: description: Whether SSID is administratively accessible. returned: success type: bool sample: true auth_mode: description: Authentication method. returned: success type: str sample: psk psk: description: Secret wireless password. returned: success type: str sample: SecretWiFiPass encryption_mode: description: Wireless traffic encryption method. returned: success type: str sample: wpa wpa_encryption_mode: description: Enabled WPA versions. returned: success type: str sample: WPA2 only ip_assignment_mode: description: Wireless client IP assignment method. returned: success type: str sample: NAT mode min_bitrate: description: Minimum bitrate a wireless client can connect at. returned: success type: int sample: 11 band_selection: description: Wireless RF frequency wireless network will be broadcast on. returned: success type: str sample: 5 GHz band only per_client_bandwidth_limit_up: description: Maximum upload bandwidth a client can use. returned: success type: int sample: 1000 per_client_bandwidth_limit_down: description: Maximum download bandwidth a client can use. returned: success type: int sample: 0 ''' from ansible.module_utils.basic import AnsibleModule, json from ansible_collections.cisco.meraki.plugins.module_utils.network.meraki.meraki import MerakiModule, meraki_argument_spec def get_available_number(data): 351 ↛ 354line 351 didn't jump to line 354, because the loop on line 351 didn't complete for item in data: if 'Unconfigured SSID' in item['name']: return item['number'] return False def get_ssid_number(name, data): for ssid in data: if name == ssid['name']: return ssid['number'] return False def get_ssids(meraki, net_id): path = meraki.construct_path('get_all', net_id=net_id) return meraki.request(path, method='GET') def construct_payload(meraki): param_map = {'name': 'name', 'enabled': 'enabled', 'authMode': 'auth_mode', 'encryptionMode': 'encryption_mode', 'psk': 'psk', 'wpaEncryptionMode': 'wpa_encryption_mode', 'splashPage': 'splash_page', 'radiusServers': 'radius_servers', 'radiusCoaEnabled': 'radius_coa_enabled', 'radiusFailoverPolicy': 'radius_failover_policy', 'radiusLoadBalancingPolicy': 'radius_load_balancing_policy', 'radiusAccountingEnabled': 'radius_accounting_enabled', 'radiusAccountingServers': 'radius_accounting_servers', 'ipAssignmentMode': 'ip_assignment_mode', 'useVlanTagging': 'use_vlan_tagging', 'concentratorNetworkId': 'concentrator_network_id', 'vlanId': 'vlan_id', 'defaultVlanId': 'default_vlan_id', 'apTagsAndVlanIds': 'ap_tags_vlan_ids', 'walledGardenEnabled': 'walled_garden_enabled', 'walledGardenRanges': 'walled_garden_ranges', 'minBitrate': 'min_bitrate', 'bandSelection': 'band_selection', 'perClientBandwidthLimitUp': 'per_client_bandwidth_limit_up', 'perClientBandwidthLimitDown': 'per_client_bandwidth_limit_down', } payload = dict() for k, v in param_map.items(): if meraki.params[v] is not None: payload[k] = meraki.params[v] if meraki.params['ap_tags_vlan_ids'] is not None: for i in payload['apTagsAndVlanIds']: try: i['vlanId'] = i['vlan_id'] del i['vlan_id'] except KeyError: pass if payload.get('walledGardenRanges', False): payload['walledGardenRanges'] = list_to_str(payload['walledGardenRanges']) return payload def list_to_str(data): space_separated_list = "" for item in data: space_separated_list = space_separated_list + " " + item return space_separated_list.strip() def per_line_to_str(data): return data.replace('\n', ' ') def main(): default_payload = {'name': 'Unconfigured SSID', 'auth_mode': 'open', 'splashPage': 'None', 'perClientBandwidthLimitUp': 0, 'perClientBandwidthLimitDown': 0, 'ipAssignmentMode': 'NAT mode', 'enabled': False, 'bandSelection': 'Dual band operation', 'minBitrate': 11, } # define the available arguments/parameters that a user can pass to # the module radius_arg_spec = dict(host=dict(type='str', required=True), port=dict(type='int'), secret=dict(type='str', no_log=True), ) vlan_arg_spec = dict(tags=dict(type='list'), vlan_id=dict(type='int'), ) argument_spec = meraki_argument_spec() argument_spec.update(state=dict(type='str', choices=['absent', 'present', 'query'], default='present'), number=dict(type='int', aliases=['ssid_number']), name=dict(type='str'), org_name=dict(type='str', aliases=['organization']), org_id=dict(type='str'), net_name=dict(type='str'), net_id=dict(type='str'), enabled=dict(type='bool'), auth_mode=dict(type='str', choices=['open', 'psk', 'open-with-radius', '8021x-meraki', '8021x-radius']), encryption_mode=dict(type='str', choices=['wpa', 'eap', 'wpa-eap']), psk=dict(type='str', no_log=True), wpa_encryption_mode=dict(type='str', choices=['WPA1 and WPA2', 'WPA2 only']), splash_page=dict(type='str', choices=['None', 'Click-through splash page', 'Billing', 'Password-protected with Meraki RADIUS', 'Password-protected with custom RADIUS', 'Password-protected with Active Directory', 'Password-protected with LDAP', 'SMS authentication', 'Systems Manager Sentry', 'Facebook Wi-Fi', 'Google OAuth', 'Sponsored guest']), radius_servers=dict(type='list', default=None, element='dict', options=radius_arg_spec), radius_coa_enabled=dict(type='bool'), radius_failover_policy=dict(type='str', choices=['Deny access', 'Allow access']), radius_load_balancing_policy=dict(type='str', choices=['Strict priority order', 'Round robin']), radius_accounting_enabled=dict(type='bool'), radius_accounting_servers=dict(type='list', element='dict', options=radius_arg_spec), ip_assignment_mode=dict(type='str', choices=['NAT mode', 'Bridge mode', 'Layer 3 roaming', 'Layer 3 roaming with a concentrator', 'VPN']), use_vlan_tagging=dict(type='bool'), concentrator_network_id=dict(type='str'), vlan_id=dict(type='int'), default_vlan_id=dict(type='int'), ap_tags_vlan_ids=dict(type='list', default=None, element='dict', options=vlan_arg_spec), walled_garden_enabled=dict(type='bool'), walled_garden_ranges=dict(type='list'), min_bitrate=dict(type='float', choices=[1, 2, 5.5, 6, 9, 11, 12, 18, 24, 36, 48, 54]), band_selection=dict(type='str', choices=['Dual band operation', '5 GHz band only', 'Dual band operation with Band Steering']), per_client_bandwidth_limit_up=dict(type='int'), per_client_bandwidth_limit_down=dict(type='int'), ) # the AnsibleModule object will be our abstraction working with Ansible # this includes instantiation, a couple of common attr would be the # args/params passed to the execution, as well as if the module # supports check mode module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True, ) meraki = MerakiModule(module, function='ssid') meraki.params['follow_redirects'] = 'all' query_urls = {'ssid': '/networks/{net_id}/ssids'} query_url = {'ssid': '/networks/{net_id}/ssids/{number}'} update_url = {'ssid': '/networks/{net_id}/ssids/'} meraki.url_catalog['get_all'].update(query_urls) meraki.url_catalog['get_one'].update(query_url) meraki.url_catalog['update'] = update_url payload = None # execute checks for argument completeness 523 ↛ 524line 523 didn't jump to line 524, because the condition on line 523 was never true if meraki.params['psk']: if meraki.params['auth_mode'] != 'psk': meraki.fail_json(msg='PSK is only allowed when auth_mode is set to psk') if meraki.params['encryption_mode'] != 'wpa': meraki.fail_json(msg='PSK requires encryption_mode be set to wpa') if meraki.params['radius_servers']: 529 ↛ 530line 529 didn't jump to line 530, because the condition on line 529 was never true if meraki.params['auth_mode'] not in ('open-with-radius', '8021x-radius'): meraki.fail_json(msg='radius_servers requires auth_mode to be open-with-radius or 8021x-radius') 531 ↛ 532line 531 didn't jump to line 532, because the condition on line 531 was never true if meraki.params['radius_accounting_enabled'] is True: if meraki.params['auth_mode'] not in ('open-with-radius', '8021x-radius'): meraki.fails_json(msg='radius_accounting_enabled is only allowed when auth_mode is open-with-radius or 8021x-radius') 534 ↛ 535line 534 didn't jump to line 535, because the condition on line 534 was never true if meraki.params['radius_accounting_servers'] is True: if meraki.params['auth_mode'] not in ('open-with-radius', '8021x-radius') or meraki.params['radius_accounting_enabled'] is False: meraki.fail_json(msg='radius_accounting_servers is only allowed when auth_mode is open_with_radius or 8021x-radius and \ radius_accounting_enabled is true') 538 ↛ 539line 538 didn't jump to line 539, because the condition on line 538 was never true if meraki.params['use_vlan_tagging'] is True: 539 ↛ 540, 539 ↛ 5442 missed branches: 1) line 539 didn't jump to line 540, because the condition on line 539 was never true, 2) line 539 didn't jump to line 544, because the condition on line 539 was never false if meraki.params['default_vlan_id'] is None: meraki.fail_json(msg="default_vlan_id is required when use_vlan_tagging is True") # manipulate or modify the state as needed (this is going to be the # part where your module will do what it needs to do) org_id = meraki.params['org_id'] net_id = meraki.params['net_id'] 546 ↛ 548line 546 didn't jump to line 548, because the condition on line 546 was never false if org_id is None: org_id = meraki.get_org_id(meraki.params['org_name']) 548 ↛ 549line 548 didn't jump to line 549, because the condition on line 548 was never true if net_id is None: nets = meraki.get_nets(org_id=org_id) net_id = meraki.get_net_id(org_id, meraki.params['net_name'], data=nets) if meraki.params['state'] == 'query': if meraki.params['name']: ssid_id = get_ssid_number(meraki.params['name'], get_ssids(meraki, net_id)) path = meraki.construct_path('get_one', net_id=net_id, custom={'number': ssid_id}) meraki.result['data'] = meraki.request(path, method='GET') 557 ↛ 561line 557 didn't jump to line 561, because the condition on line 557 was never false elif meraki.params['number'] is not None: path = meraki.construct_path('get_one', net_id=net_id, custom={'number': meraki.params['number']}) meraki.result['data'] = meraki.request(path, method='GET') else: meraki.result['data'] = get_ssids(meraki, net_id) elif meraki.params['state'] == 'present': payload = construct_payload(meraki) ssids = get_ssids(meraki, net_id) number = meraki.params['number'] 566 ↛ 567, 566 ↛ 5682 missed branches: 1) line 566 didn't jump to line 567, because the condition on line 566 was never true, 2) line 566 didn't jump to line 568, because the condition on line 566 was never false if number is None: number = get_ssid_number(meraki.params['name'], ssids) original = ssids[number] 569 ↛ 571line 569 didn't jump to line 571, because the condition on line 569 was never false if original.get('walledGardenRanges', False): # Update original so it's a string original['walledGardenRanges'] = per_line_to_str(original['walledGardenRanges']) 571 ↛ 589line 571 didn't jump to line 589, because the condition on line 571 was never false if meraki.is_update_required(original, payload, optional_ignore=['secret']): ssid_id = meraki.params['number'] 573 ↛ 579line 573 didn't jump to line 579, because the condition on line 573 was never false if ssid_id is None: # Name should be used to lookup number ssid_id = get_ssid_number(meraki.params['name'], ssids) 575 ↛ 579line 575 didn't jump to line 579, because the condition on line 575 was never false if ssid_id is False: ssid_id = get_available_number(ssids) 577 ↛ 579line 577 didn't jump to line 579, because the condition on line 577 was never false if ssid_id is False: meraki.fail_json(msg='No unconfigured SSIDs are available. Specify a number.') 579 ↛ 584line 579 didn't jump to line 584, because the condition on line 579 was never false if meraki.check_mode is True: original.update(payload) meraki.result['data'] = original meraki.result['changed'] = True meraki.exit_json(**meraki.result) path = meraki.construct_path('update', net_id=net_id) + str(ssid_id) result = meraki.request(path, 'PUT', payload=json.dumps(payload)) meraki.result['data'] = result meraki.result['changed'] = True else: meraki.result['data'] = original 590 ↛ 612line 590 didn't jump to line 612, because the condition on line 590 was never false elif meraki.params['state'] == 'absent': ssids = get_ssids(meraki, net_id) ssid_id = meraki.params['number'] 593 ↛ 594line 593 didn't jump to line 594, because the condition on line 593 was never true if ssid_id is None: # Name should be used to lookup number ssid_id = get_ssid_number(meraki.params['name'], ssids) 595 ↛ 599line 595 didn't jump to line 599, because the condition on line 595 was never false if ssid_id is False: ssid_id = get_available_number(ssids) 597 ↛ 599line 597 didn't jump to line 599, because the condition on line 597 was never false if ssid_id is False: meraki.fail_json(msg='No SSID found by specified name and no number was referenced.') if meraki.check_mode is True: meraki.result['data'] = {} meraki.result['changed'] = True meraki.exit_json(**meraki.result) path = meraki.construct_path('update', net_id=net_id) + str(ssid_id) payload = default_payload payload['name'] = payload['name'] + ' ' + str(ssid_id + 1) result = meraki.request(path, 'PUT', payload=json.dumps(payload)) meraki.result['data'] = result meraki.result['changed'] = True # in the event of a successful module execution, you will want to # simple AnsibleModule.exit_json(), passing the key/value results meraki.exit_json(**meraki.result) 615 ↛ exitline 615 didn't exit the module, because the condition on line 615 was never falseif __name__ == '__main__': main()