Meraki util - Add method to encode parameters in the URL (#49015)

* Add new method to encode parameters in the URL
- I'm not really encoding, I'm sure this is broke
- There maybe an Ansible native way to do this

* Fix whitepace

* Added urlencode support
- Relies on urllib module
- Fixed string delimiter

* Enable URL params
- construct_params_list() creates a list of parameters to encode
- encode_url_params() does encoding in a simple manner

* Added proper methods for urlencoding

* Remove duplicate functions

* Remove blank line for PEP8
pull/55995/head
Kevin Breit 6 years ago committed by Nathaniel Case
parent 76dba7aa4f
commit 91237fa414

@ -32,6 +32,7 @@
import os import os
from ansible.module_utils.basic import AnsibleModule, json, env_fallback from ansible.module_utils.basic import AnsibleModule, json, env_fallback
from ansible.module_utils.urls import fetch_url from ansible.module_utils.urls import fetch_url
from ansible.module_utils.six.moves.urllib.parse import urlencode
from ansible.module_utils._text import to_native, to_bytes, to_text from ansible.module_utils._text import to_native, to_bytes, to_text
@ -249,9 +250,28 @@ class MerakiModule(object):
return template['id'] return template['id']
self.fail_json(msg='No configuration template named {0} found'.format(name)) self.fail_json(msg='No configuration template named {0} found'.format(name))
def construct_path(self, action, function=None, org_id=None, net_id=None, org_name=None, custom=None): def construct_params_list(self, keys, aliases=None):
qs = {}
for key in keys:
if key in aliases:
qs[aliases[key]] = self.module.params[key]
else:
qs[key] = self.module.params[key]
return qs
def encode_url_params(self, params):
"""Encodes key value pairs for URL"""
return "?{0}".format(urlencode(params))
def construct_path(self,
action,
function=None,
org_id=None,
net_id=None,
org_name=None,
custom=None,
params=None):
"""Build a path from the URL catalog. """Build a path from the URL catalog.
Uses function property from class for catalog lookup. Uses function property from class for catalog lookup.
""" """
built_path = None built_path = None
@ -265,6 +285,8 @@ class MerakiModule(object):
built_path = built_path.format(org_id=org_id, net_id=net_id, **custom) built_path = built_path.format(org_id=org_id, net_id=net_id, **custom)
else: else:
built_path = built_path.format(org_id=org_id, net_id=net_id) built_path = built_path.format(org_id=org_id, net_id=net_id)
if params:
built_path += self.encode_url_params(params)
return built_path return built_path
def request(self, path, method=None, payload=None): def request(self, path, method=None, payload=None):

Loading…
Cancel
Save