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
This commit is contained in:
Kevin Breit 2018-07-02 21:24:06 -05:00 committed by Dag Wieers
parent 3ee3fc893d
commit 40b9862d38
5 changed files with 52 additions and 29 deletions

View file

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

View file

@ -153,6 +153,7 @@ def get_admins(meraki, org_id):
), ),
method='GET' method='GET'
) )
if meraki.status == 200:
return admins return admins
@ -189,10 +190,11 @@ def find_admin(meraki, data, email):
def delete_admin(meraki, org_id, admin_id): def delete_admin(meraki, org_id, admin_id):
path = meraki.construct_path('revoke', 'admin', org_id=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, r = meraki.request(path,
method='DELETE' method='DELETE'
) )
if meraki.status == 204:
return r
def network_factory(meraki, networks, nets): 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 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) 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): def create_admin(meraki, org_id, name, email):
@ -234,6 +238,7 @@ def create_admin(meraki, org_id, name, email):
method='POST', method='POST',
payload=json.dumps(payload) payload=json.dumps(payload)
) )
if meraki.status == 201:
meraki.result['changed'] = True meraki.result['changed'] = True
return r return r
elif is_admin_existing is not None: # Update existing admin elif is_admin_existing is not None: # Update existing admin
@ -248,6 +253,7 @@ def create_admin(meraki, org_id, name, email):
method='PUT', method='PUT',
payload=json.dumps(payload) payload=json.dumps(payload)
) )
if meraki.status == 200:
meraki.result['changed'] = True meraki.result['changed'] = True
return r return r
else: else:

View file

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

View file

@ -186,17 +186,22 @@ def main():
elif meraki.params['state'] == 'present': elif meraki.params['state'] == 'present':
if meraki.params['clone']: # Cloning if meraki.params['clone']: # Cloning
payload = {'name': meraki.params['org_name']} 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'] org_name=meraki.params['clone']
), ),
payload=json.dumps(payload), payload=json.dumps(payload),
method='POST') method='POST')
if meraki.status != 201:
meraki.fail_json(msg='Organization clone failed')
meraki.result['data'] = response
meraki.result['changed'] = True meraki.result['changed'] = True
elif not meraki.params['org_id'] and meraki.params['org_name']: # Create new organization elif not meraki.params['org_id'] and meraki.params['org_name']: # Create new organization
payload = {'name': meraki.params['org_name']} payload = {'name': meraki.params['org_name']}
meraki.result['data'] = meraki.request(meraki.construct_path('create'), response = meraki.request(meraki.construct_path('create'),
method='POST', method='POST',
payload=json.dumps(payload)) payload=json.dumps(payload))
if meraki.status == 201:
meraki.result['data'] = response
meraki.result['changed'] = True meraki.result['changed'] = True
elif meraki.params['org_id'] and meraki.params['org_name']: # Update an existing organization elif meraki.params['org_id'] and meraki.params['org_name']: # Update an existing organization
payload = {'name': meraki.params['org_name'], payload = {'name': meraki.params['org_name'],
@ -208,11 +213,14 @@ def main():
meraki.params['org_id'], meraki.params['org_id'],
orgs), orgs),
payload): payload):
meraki.result['data'] = meraki.request(meraki.construct_path('update', response = meraki.request(meraki.construct_path('update',
org_id=meraki.params['org_id'] org_id=meraki.params['org_id']
), ),
method='PUT', method='PUT',
payload=json.dumps(payload)) payload=json.dumps(payload))
if meraki.status != 200:
meraki.fail_json(msg='Organization update failed')
meraki.result['data'] = response
meraki.result['changed'] = True meraki.result['changed'] = True
# in the event of a successful module execution, you will want to # in the event of a successful module execution, you will want to
# simple AnsibleModule.exit_json(), passing the key/value results # simple AnsibleModule.exit_json(), passing the key/value results

View file

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