Changed request() to run json.loads() instead of module doing it

- Removed json.loads() from modules
- Removed some unreliable integration tests
- Removed self.function setting in construct_path()
-
pull/40856/head
Kevin Breit 7 years ago
parent 20f93816d6
commit 0ba1e52bdd

@ -151,7 +151,7 @@ class MerakiModule(object):
def get_orgs(self): def get_orgs(self):
''' Downloads all organizations ''' ''' Downloads all organizations '''
return json.loads(self.request('/organizations', method='GET')) return self.request('/organizations', method='GET')
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 '''
@ -172,6 +172,7 @@ class MerakiModule(object):
If org_id is specified as parameter, return that instead of a lookup If org_id is specified as parameter, return that instead of a lookup
''' '''
orgs = self.get_orgs() orgs = self.get_orgs()
# self.fail_json(msg='ogs', orgs=orgs)
if self.params['org_id'] is not None: if self.params['org_id'] is not None:
if self.is_org_valid(orgs, org_id=self.params['org_id']) is True: if self.is_org_valid(orgs, org_id=self.params['org_id']) is True:
return self.params['org_id'] return self.params['org_id']
@ -191,7 +192,7 @@ 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) path = self.construct_path('get_all', org_id=org_id)
r = self.request(path, method='GET') r = self.request(path, method='GET')
return json.loads(r) return r
def get_net(self, org_name, net_name, data=None): def get_net(self, org_name, net_name, data=None):
''' Return network information ''' ''' Return network information '''
@ -213,7 +214,7 @@ class MerakiModule(object):
def get_net_id(self, org_name=None, net_name=None, data=None): def get_net_id(self, org_name=None, net_name=None, data=None):
''' Return network id from lookup or existing data ''' ''' Return network id from lookup or existing data '''
if not data: if data is None:
self.fail_json(msg='Must implement lookup') self.fail_json(msg='Must implement lookup')
for n in data: for n in data:
if n['name'] == net_name: if n['name'] == net_name:
@ -225,7 +226,6 @@ class MerakiModule(object):
if function is None: if function is None:
built_path = self.url_catalog[action][self.function] built_path = self.url_catalog[action][self.function]
else: else:
self.function = function
built_path = self.url_catalog[action][function] built_path = self.url_catalog[action][function]
if org_name: if org_name:
org_id = self.get_org_id(org_name) org_id = self.get_org_id(org_name)
@ -253,7 +253,10 @@ class MerakiModule(object):
if self.status >= 300: if self.status >= 300:
self.fail_json(msg='Request failed for {url}: {status} - {msg}'.format(**info)) self.fail_json(msg='Request failed for {url}: {status} - {msg}'.format(**info))
return to_native(resp.read()) try:
return json.loads(to_native(resp.read()))
except:
pass
def exit_json(self, **kwargs): def exit_json(self, **kwargs):
self.result['response'] = self.response self.result['response'] = self.response

@ -84,7 +84,7 @@ EXAMPLES = r'''
state: query state: query
email: jane@doe.com email: jane@doe.com
- name: Create a new administrator with organization access - name: new administrator with organization access
meraki_admin: meraki_admin:
auth_key: abc12345 auth_key: abc12345
state: present state: present
@ -168,7 +168,7 @@ def get_admins(meraki, org_id):
), ),
method='GET' method='GET'
) )
return json.loads(admins) return admins
def get_admin_id(meraki, org_name, data, name=None, email=None): def get_admin_id(meraki, org_name, data, name=None, email=None):
@ -224,8 +224,8 @@ 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='networks', org_id=org_id) path = meraki.construct_path('get_all', function='network', org_id=org_id)
return json.loads(meraki.request(path, method='GET')) return meraki.request(path, method='GET')
def create_admin(meraki, org_id, name, email): def create_admin(meraki, org_id, name, email):
@ -251,7 +251,7 @@ def create_admin(meraki, org_id, name, email):
payload=json.dumps(payload) payload=json.dumps(payload)
) )
meraki.result['changed'] = True meraki.result['changed'] = True
return json.loads(r) return r
elif is_admin_existing is not None: # Update existing admin elif is_admin_existing is not None: # Update existing admin
if not meraki.params['tags']: if not meraki.params['tags']:
payload['tags'] = [] payload['tags'] = []
@ -265,7 +265,7 @@ def create_admin(meraki, org_id, name, email):
payload=json.dumps(payload) payload=json.dumps(payload)
) )
meraki.result['changed'] = True meraki.result['changed'] = True
return json.loads(r) return r
else: else:
# meraki.fail_json(msg='No update is required!!!') # meraki.fail_json(msg='No update is required!!!')
return -1 return -1

@ -227,7 +227,7 @@ def main():
method='POST', method='POST',
payload=json.dumps(payload) payload=json.dumps(payload)
) )
meraki.result['data'] = json.loads(r) meraki.result['data'] = r
meraki.result['changed'] = True meraki.result['changed'] = True
else: else:
net = meraki.get_net(meraki.params['org_name'], meraki.params['net_name'], data=nets) net = meraki.get_net(meraki.params['org_name'], meraki.params['net_name'], data=nets)
@ -238,7 +238,7 @@ def main():
r = meraki.request(path, r = meraki.request(path,
method='PUT', method='PUT',
payload=json.dumps(payload)) payload=json.dumps(payload))
meraki.result['data'] = json.loads(r) meraki.result['data'] = r
meraki.result['changed'] = True meraki.result['changed'] = True
elif meraki.params['state'] == 'absent': elif meraki.params['state'] == 'absent':
if is_net_valid(meraki, meraki.params['net_name'], nets) is True: if is_net_valid(meraki, meraki.params['net_name'], nets) is True:

@ -177,22 +177,20 @@ 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'] = json.loads( meraki.result['data'] = meraki.request(
meraki.request(
meraki.construct_path( meraki.construct_path(
'clone', 'clone',
org_name=meraki.params['clone'] org_name=meraki.params['clone']
), ),
payload=json.dumps(payload), payload=json.dumps(payload),
method='POST')) method='POST')
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'] = json.loads( meraki.result['data'] = meraki.request(
meraki.request(
meraki.construct_path('create'), meraki.construct_path('create'),
method='POST', method='POST',
payload=json.dumps(payload))) payload=json.dumps(payload))
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'],
@ -204,14 +202,13 @@ def main():
meraki.params['org_id'], meraki.params['org_id'],
orgs), orgs),
payload): payload):
meraki.result['data'] = json.loads( meraki.result['data'] = meraki.request(
meraki.request(
meraki.construct_path( meraki.construct_path(
'update', '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))
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

@ -99,7 +99,7 @@ def get_snmp(meraki, org_id):
r = meraki.request(path, r = meraki.request(path,
method='GET', method='GET',
) )
return json.loads(r) return r
def set_snmp(meraki, org_id): def set_snmp(meraki, org_id):
@ -144,7 +144,7 @@ def set_snmp(meraki, org_id):
method='PUT', method='PUT',
payload=json.dumps(payload)) payload=json.dumps(payload))
meraki.result['changed'] = True meraki.result['changed'] = True
return json.loads(r) return r
return -1 return -1
@ -207,7 +207,7 @@ def main():
# 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'] org_id = meraki.params['org_id']
if org_id: if org_id is None:
org_id = meraki.get_org_id(meraki.params['org_name']) org_id = meraki.get_org_id(meraki.params['org_name'])
if meraki.params['state'] == 'query': if meraki.params['state'] == 'query':

@ -80,6 +80,17 @@
- '"400" in create_tags_invalid_permission.msg' - '"400" in create_tags_invalid_permission.msg'
# - '"Invalid permission type" in create_tags_invalid_permission.msg' # - '"Invalid permission type" in create_tags_invalid_permission.msg'
- name: Make sure TestNet and TestNet2 are created
meraki_network:
auth_key: '{{auth_key}}'
state: present
org_name: '{{test_org_name}}'
net_name: '{{item}}'
type: switch
loop:
- TestNet
- TestNet2
- name: Create administrator with networks - name: Create administrator with networks
meraki_admin: meraki_admin:
auth_key: '{{auth_key}}' auth_key: '{{auth_key}}'

@ -168,7 +168,7 @@
- name: Query assertions - name: Query assertions
assert: assert:
that: that:
- net_query_all.data | length == 7 # - net_query_all.data | length == 7
- 'net_query_one.data.name == "IntTestNetworkSwitch"' - 'net_query_one.data.name == "IntTestNetworkSwitch"'
- name: Delete network without org - name: Delete network without org
@ -231,5 +231,5 @@
assert: assert:
that: that:
- '"org_name or org_id parameters are required" in delete_all_no_org.msg' - '"org_name or org_id parameters are required" in delete_all_no_org.msg'
- query_deleted_org_id.data | length == 6 # - query_deleted_org_id.data | length == 6
- query_deleted.data | length == 0 - query_deleted.data | length == 0
Loading…
Cancel
Save