Meraki modules - Added support for checking HTTP response codes (#42144)

* Added support for checking HTTP response codes
- All request calls now check for response code before responding
- If the response code isn't what it should be, it fails or returns nothing
- Breaking this into multiple PRs to make backporting easier
- Using status property in Meraki module utility which has the code

* Change logic of HTTP checks so success is default
pull/42085/head
Kevin Breit 6 years ago committed by Dag Wieers
parent 3ee3fc893d
commit 40b9862d38

@ -150,7 +150,10 @@ class MerakiModule(object):
def get_orgs(self):
"""Downloads all organizations for a user."""
return self.request('/organizations', method='GET')
response = self.request('/organizations', method='GET')
if self.status != 200:
self.fail_json(msg='Organization lookup failed')
return response
def is_org_valid(self, data, org_name=None, org_id=None):
"""Checks whether a specific org exists and is duplicated.
@ -195,6 +198,8 @@ class MerakiModule(object):
org_id = self.get_org_id(org_name)
path = self.construct_path('get_all', org_id=org_id, function='network')
r = self.request(path, method='GET')
if self.status != 200:
self.fail_json(msg='Network lookup failed')
return r
def get_net(self, org_name, net_name, data=None):

@ -153,6 +153,7 @@ def get_admins(meraki, org_id):
),
method='GET'
)
if meraki.status == 200:
return admins
@ -189,10 +190,11 @@ def find_admin(meraki, data, email):
def delete_admin(meraki, org_id, admin_id):
path = meraki.construct_path('revoke', 'admin', org_id=org_id) + admin_id
# meraki.fail_json(msg=path)
r = meraki.request(path,
method='DELETE'
)
if meraki.status == 204:
return r
def network_factory(meraki, networks, nets):
@ -209,7 +211,9 @@ def network_factory(meraki, networks, nets):
def get_nets_temp(meraki, org_id): # Function won't be needed when get_nets is added to util
path = meraki.construct_path('get_all', function='network', org_id=org_id)
return meraki.request(path, method='GET')
response = meraki.request(path, method='GET')
if meraki.status == 200:
return response
def create_admin(meraki, org_id, name, email):
@ -234,6 +238,7 @@ def create_admin(meraki, org_id, name, email):
method='POST',
payload=json.dumps(payload)
)
if meraki.status == 201:
meraki.result['changed'] = True
return r
elif is_admin_existing is not None: # Update existing admin
@ -248,6 +253,7 @@ def create_admin(meraki, org_id, name, email):
method='PUT',
payload=json.dumps(payload)
)
if meraki.status == 200:
meraki.result['changed'] = True
return r
else:

@ -218,6 +218,7 @@ def main():
method='POST',
payload=json.dumps(payload)
)
if meraki.status == 201:
meraki.result['data'] = r
meraki.result['changed'] = True
else:
@ -239,6 +240,7 @@ def main():
r = meraki.request(path,
method='PUT',
payload=json.dumps(payload))
if meraki.status == 200:
meraki.result['data'] = r
meraki.result['changed'] = True
elif meraki.params['state'] == 'absent':

@ -186,17 +186,22 @@ def main():
elif meraki.params['state'] == 'present':
if meraki.params['clone']: # Cloning
payload = {'name': meraki.params['org_name']}
meraki.result['data'] = meraki.request(meraki.construct_path('clone',
response = meraki.request(meraki.construct_path('clone',
org_name=meraki.params['clone']
),
payload=json.dumps(payload),
method='POST')
if meraki.status != 201:
meraki.fail_json(msg='Organization clone failed')
meraki.result['data'] = response
meraki.result['changed'] = True
elif not meraki.params['org_id'] and meraki.params['org_name']: # Create new organization
payload = {'name': meraki.params['org_name']}
meraki.result['data'] = meraki.request(meraki.construct_path('create'),
response = meraki.request(meraki.construct_path('create'),
method='POST',
payload=json.dumps(payload))
if meraki.status == 201:
meraki.result['data'] = response
meraki.result['changed'] = True
elif meraki.params['org_id'] and meraki.params['org_name']: # Update an existing organization
payload = {'name': meraki.params['org_name'],
@ -208,11 +213,14 @@ def main():
meraki.params['org_id'],
orgs),
payload):
meraki.result['data'] = meraki.request(meraki.construct_path('update',
response = meraki.request(meraki.construct_path('update',
org_id=meraki.params['org_id']
),
method='PUT',
payload=json.dumps(payload))
if meraki.status != 200:
meraki.fail_json(msg='Organization update failed')
meraki.result['data'] = response
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

@ -125,6 +125,7 @@ def get_snmp(meraki, org_id):
r = meraki.request(path,
method='GET',
)
if meraki.status == 200:
return r
@ -169,6 +170,7 @@ def set_snmp(meraki, org_id):
r = meraki.request(path,
method='PUT',
payload=json.dumps(payload))
if meraki.status == 200:
meraki.result['changed'] = True
return r
return -1

Loading…
Cancel
Save