1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Allow module to claim devices into an organization (#42448)

- Before this, it allowed claiming devices into a network only
- Make integration tests a block
- Note, API doesn't allow unclaiming in an organization, only net
- Added an integration test for claiming into an org
	- Requires unclaiming manually
- There is a bug in the API which isn't showing claimed devices
This commit is contained in:
Kevin Breit 2018-07-09 04:08:37 -05:00 committed by Dag Wieers
parent 5960b215bb
commit 779f3c0c1a
2 changed files with 232 additions and 215 deletions

View file

@ -211,6 +211,14 @@ def is_device_valid(meraki, serial, data):
return False return False
def get_org_devices(meraki, org_id):
path = meraki.construct_path('get_all_org', org_id=org_id)
response = meraki.request(path, method='GET')
if meraki.status != 200:
meraki.fail_json(msg='Failed to query all devices belonging to the organization')
return response
def temp_get_nets(meraki, org_name, net_name): def temp_get_nets(meraki, org_name, net_name):
org_id = meraki.get_org_id(org_name) org_id = meraki.get_org_id(org_name)
path = meraki.construct_path('get_all', function='network', org_id=org_id) path = meraki.construct_path('get_all', function='network', org_id=org_id)
@ -263,24 +271,19 @@ def main():
meraki.params['follow_redirects'] = 'all' meraki.params['follow_redirects'] = 'all'
query_urls = {'device': '/networks/{net_id}/devices', query_urls = {'device': '/networks/{net_id}/devices'}
} query_org_urls = {'device': '/organizations/{org_id}/deviceStatuses'}
query_device_urls = {'device': '/networks/{net_id}/devices/'}
query_device_urls = {'device': '/networks/{net_id}/devices/', claim_device_urls = {'device': '/networks/{net_id}/devices/claim'}
} bind_org_urls = {'device': '/organizations/{org_id}/claim'}
update_device_urls = {'device': '/networks/{net_id}/devices/'}
claim_device_urls = {'device': '/networks/{net_id}/devices/claim', delete_device_urls = {'device': '/networks/{net_id}/devices/'}
}
update_device_urls = {'device': '/networks/{net_id}/devices/',
}
delete_device_urls = {'device': '/networks/{net_id}/devices/',
}
meraki.url_catalog['get_all'].update(query_urls) meraki.url_catalog['get_all'].update(query_urls)
meraki.url_catalog['get_all_org'] = query_org_urls
meraki.url_catalog['get_device'] = query_device_urls meraki.url_catalog['get_device'] = query_device_urls
meraki.url_catalog['create'] = claim_device_urls meraki.url_catalog['create'] = claim_device_urls
meraki.url_catalog['bind_org'] = bind_org_urls
meraki.url_catalog['update'] = update_device_urls meraki.url_catalog['update'] = update_device_urls
meraki.url_catalog['delete'] = delete_device_urls meraki.url_catalog['delete'] = delete_device_urls
@ -297,15 +300,19 @@ def main():
# manipulate or modify the state as needed (this is going to be the # manipulate or modify the state as needed (this is going to be the
# part where your module will do what it needs to do) # part where your module will do what it needs to do)
org_id = meraki.params['org_id']
if org_id is None:
org_id = meraki.get_org_id(meraki.params['org_name'])
net_id = meraki.params['net_id']
if net_id is None:
if meraki.params['net_name']:
nets = temp_get_nets(meraki, meraki.params['org_name'], meraki.params['net_name']) nets = temp_get_nets(meraki, meraki.params['org_name'], meraki.params['net_name'])
net_id = meraki.get_net_id(net_name=meraki.params['net_name'], data=nets)
if meraki.params['state'] == 'query': if meraki.params['state'] == 'query':
if meraki.params['net_name'] or meraki.params['net_id']: if meraki.params['net_name'] or meraki.params['net_id']:
device = [] device = []
if meraki.params['net_name']:
net_id = meraki.get_net_id(net_name=meraki.params['net_name'], data=nets)
elif meraki.params['net_id']:
net_id = meraki.params['net_id']
if meraki.params['serial']: if meraki.params['serial']:
path = meraki.construct_path('get_device', net_id=net_id) + meraki.params['serial'] path = meraki.construct_path('get_device', net_id=net_id) + meraki.params['serial']
request = meraki.request(path, method='GET') request = meraki.request(path, method='GET')
@ -341,24 +348,16 @@ def main():
request = meraki.request(path, method='GET') request = meraki.request(path, method='GET')
meraki.result['data'] = request meraki.result['data'] = request
else: else:
devices = [] path = meraki.construct_path('get_all_org', org_id=org_id)
for net in nets: # Gather all devices in all networks devices = meraki.request(path, method='GET')
path = meraki.construct_path('get_all', net_id=net['id'])
request = meraki.request(path, method='GET')
devices.append(request)
if meraki.params['serial']: if meraki.params['serial']:
for network in devices: for device in devices:
for dev in network: if device['serial'] == meraki.params['serial']:
if dev['serial'] == meraki.params['serial']: meraki.result['data'] = device
meraki.result['data'] = [dev]
else: else:
meraki.result['data'] = devices meraki.result['data'] = devices
elif meraki.params['state'] == 'present': elif meraki.params['state'] == 'present':
device = [] device = []
if meraki.params['net_name']:
net_id = meraki.get_net_id(net_name=meraki.params['net_name'], data=nets)
elif meraki.params['net_id']:
net_id = meraki.params['net_id']
if meraki.params['hostname']: if meraki.params['hostname']:
query_path = meraki.construct_path('get_all', net_id=net_id) query_path = meraki.construct_path('get_all', net_id=net_id)
device_list = meraki.request(query_path, method='GET') device_list = meraki.request(query_path, method='GET')
@ -379,10 +378,21 @@ def main():
updated_device.append(meraki.request(path, method='PUT', payload=json.dumps(payload))) updated_device.append(meraki.request(path, method='PUT', payload=json.dumps(payload)))
meraki.result['data'] = updated_device meraki.result['data'] = updated_device
meraki.result['changed'] = True meraki.result['changed'] = True
else:
if net_id is None:
device_list = get_org_devices(meraki, org_id)
if is_device_valid(meraki, meraki.params['serial'], device_list) is False:
payload = {'serial': meraki.params['serial']}
path = meraki.construct_path('bind_org', org_id=org_id)
created_device = []
created_device.append(meraki.request(path, method='POST', payload=json.dumps(payload)))
meraki.result['data'] = created_device
meraki.result['changed'] = True
else: else:
query_path = meraki.construct_path('get_all', net_id=net_id) query_path = meraki.construct_path('get_all', net_id=net_id)
device_list = meraki.request(query_path, method='GET') device_list = meraki.request(query_path, method='GET')
if is_device_valid(meraki, meraki.params['serial'], device_list) is False: if is_device_valid(meraki, meraki.params['serial'], device_list) is False:
if net_id:
payload = {'serial': meraki.params['serial']} payload = {'serial': meraki.params['serial']}
path = meraki.construct_path('create', net_id=net_id) path = meraki.construct_path('create', net_id=net_id)
created_device = [] created_device = []
@ -391,10 +401,6 @@ def main():
meraki.result['changed'] = True meraki.result['changed'] = True
elif meraki.params['state'] == 'absent': elif meraki.params['state'] == 'absent':
device = [] device = []
if meraki.params['net_name']:
net_id = meraki.get_net_id(net_name=meraki.params['net_name'], data=nets)
elif meraki.params['net_id']:
net_id = meraki.params['net_id']
query_path = meraki.construct_path('get_all', net_id=net_id) query_path = meraki.construct_path('get_all', net_id=net_id)
device_list = meraki.request(query_path, method='GET') device_list = meraki.request(query_path, method='GET')
if is_device_valid(meraki, meraki.params['serial'], device_list) is True: if is_device_valid(meraki, meraki.params['serial'], device_list) is True:

View file

@ -1,5 +1,30 @@
--- ---
- name: Claim a device - block:
- name: Claim a device into an organization
meraki_device:
auth_key: '{{auth_key}}'
org_name: '{{test_org_name}}'
serial: '{{serial}}'
state: present
delegate_to: localhost
register: claim_device_org
- assert:
that:
- claim_device_org.changed == true
- name: Query status of all devices in an organization
meraki_device:
auth_key: '{{auth_key}}'
org_name: '{{test_org_name}}'
state: query
delegate_to: localhost
register: query_device_org
- debug:
msg: '{{query_device_org}}'
- name: Claim a device into a network
meraki_device: meraki_device:
auth_key: '{{auth_key}}' auth_key: '{{auth_key}}'
org_name: '{{test_org_name}}' org_name: '{{test_org_name}}'
@ -9,29 +34,14 @@
delegate_to: localhost delegate_to: localhost
register: claim_device register: claim_device
- debug: - debug:
msg: '{{claim_device}}' msg: '{{claim_device}}'
- assert: - assert:
that: that:
- claim_device.changed == true - claim_device.changed == true
- name: Query all devices - name: Query all devices in one network by network ID
meraki_device:
auth_key: '{{auth_key}}'
org_name: '{{test_org_name}}'
state: query
delegate_to: localhost
register: query_all
- debug:
msg: '{{query_all}}'
- assert:
that:
- query_all.changed == False
- name: Query all devices in one network by network ID
meraki_device: meraki_device:
auth_key: '{{auth_key}}' auth_key: '{{auth_key}}'
org_name: '{{test_org_name}}' org_name: '{{test_org_name}}'
@ -40,10 +50,10 @@
delegate_to: localhost delegate_to: localhost
register: query_one_net_id register: query_one_net_id
- debug: - debug:
msg: '{{query_one_net_id}}' msg: '{{query_one_net_id}}'
- name: Query all devices in one network - name: Query all devices in one network
meraki_device: meraki_device:
auth_key: '{{auth_key}}' auth_key: '{{auth_key}}'
org_name: '{{test_org_name}}' org_name: '{{test_org_name}}'
@ -52,10 +62,10 @@
delegate_to: localhost delegate_to: localhost
register: query_one_net register: query_one_net
- debug: - debug:
msg: '{{query_one_net}}' msg: '{{query_one_net}}'
- name: Query device by serial - name: Query device by serial
meraki_device: meraki_device:
auth_key: '{{auth_key}}' auth_key: '{{auth_key}}'
org_name: '{{test_org_name}}' org_name: '{{test_org_name}}'
@ -64,10 +74,10 @@
delegate_to: localhost delegate_to: localhost
register: query_serial_no_net register: query_serial_no_net
- debug: - debug:
msg: '{{query_serial_no_net}}' msg: '{{query_serial_no_net}}'
- name: Query device by serial - name: Query device by serial
meraki_device: meraki_device:
auth_key: '{{auth_key}}' auth_key: '{{auth_key}}'
org_name: '{{test_org_name}}' org_name: '{{test_org_name}}'
@ -77,14 +87,14 @@
delegate_to: localhost delegate_to: localhost
register: query_serial register: query_serial
- debug: - debug:
msg: '{{query_serial}}' msg: '{{query_serial}}'
- assert: - assert:
that: that:
- query_serial.changed == False - query_serial.changed == False
- name: Query uplink information for a device - name: Query uplink information for a device
meraki_device: meraki_device:
auth_key: '{{auth_key}}' auth_key: '{{auth_key}}'
org_name: '{{test_org_name}}' org_name: '{{test_org_name}}'
@ -94,10 +104,10 @@
delegate_to: localhost delegate_to: localhost
register: query_serial_uplink register: query_serial_uplink
- debug: - debug:
msg: '{{query_serial_uplink}}' msg: '{{query_serial_uplink}}'
- name: Query LLDP/CDP information about a device - name: Query LLDP/CDP information about a device
meraki_device: meraki_device:
auth_key: '{{auth_key}}' auth_key: '{{auth_key}}'
org_name: '{{test_org_name}}' org_name: '{{test_org_name}}'
@ -108,10 +118,10 @@
delegate_to: localhost delegate_to: localhost
register: query_serial_lldp_cdp register: query_serial_lldp_cdp
- debug: - debug:
msg: '{{query_serial_lldp_cdp}}' msg: '{{query_serial_lldp_cdp}}'
- name: Query a device by hostname - name: Query a device by hostname
meraki_device: meraki_device:
auth_key: '{{auth_key}}' auth_key: '{{auth_key}}'
org_name: '{{test_org_name}}' org_name: '{{test_org_name}}'
@ -121,10 +131,10 @@
delegate_to: localhost delegate_to: localhost
register: query_hostname register: query_hostname
- debug: - debug:
msg: '{{query_hostname}}' msg: '{{query_hostname}}'
- name: Query a device by model - name: Query a device by model
meraki_device: meraki_device:
auth_key: '{{auth_key}}' auth_key: '{{auth_key}}'
org_name: '{{test_org_name}}' org_name: '{{test_org_name}}'
@ -134,10 +144,10 @@
delegate_to: localhost delegate_to: localhost
register: query_model register: query_model
- debug: - debug:
msg: '{{query_model}}' msg: '{{query_model}}'
- name: Update a device - name: Update a device
meraki_device: meraki_device:
auth_key: '{{auth_key}}' auth_key: '{{auth_key}}'
org_name: '{{test_org_name}}' org_name: '{{test_org_name}}'
@ -153,15 +163,15 @@
delegate_to: localhost delegate_to: localhost
register: update_device register: update_device
- debug: - debug:
msg: '{{update_device}}' msg: '{{update_device}}'
# - assert: # - assert:
# that: # that:
# - update_device.changed == true # - update_device.changed == true
# - '"1060 W. Addison St., Chicago, IL" in update_device.data.0.address' # - '"1060 W. Addison St., Chicago, IL" in update_device.data.0.address'
- name: Update a device with idempotency - name: Update a device with idempotency
meraki_device: meraki_device:
auth_key: '{{auth_key}}' auth_key: '{{auth_key}}'
org_name: '{{test_org_name}}' org_name: '{{test_org_name}}'
@ -177,14 +187,15 @@
delegate_to: localhost delegate_to: localhost
register: update_device_idempotent register: update_device_idempotent
- debug: - debug:
msg: '{{update_device_idempotent}}' msg: '{{update_device_idempotent}}'
- assert: - assert:
that: that:
- update_device_idempotent.changed == False - update_device_idempotent.changed == False
- name: Remove a device always:
- name: Remove a device
meraki_device: meraki_device:
auth_key: '{{auth_key}}' auth_key: '{{auth_key}}'
org_name: '{{test_org_name}}' org_name: '{{test_org_name}}'
@ -194,9 +205,9 @@
delegate_to: localhost delegate_to: localhost
register: delete_device register: delete_device
- debug: - debug:
msg: '{{delete_device}}' msg: '{{delete_device}}'
- assert: - assert:
that: that:
- delete_device.changed == true - delete_device.changed == true