Refactor E-Series AMG module to use module_utils (#20871)

* Refactor E-Series AMG module to use module_utils

Refactor the NetApp E-Series module to utlize the common module_utils
and doc_fragments.

* Resolve a PEP8 issue with a missing newline

* Resolve compatibility issue with json import
This commit is contained in:
Michael Price 2017-02-14 14:53:18 -06:00 committed by jctanner
commit 56dd5a702d
3 changed files with 96 additions and 64 deletions

View file

@ -1,5 +1,6 @@
#
# (c) 2016, Sumit Kumar <sumit4@netapp.com>
# (c) 2016, Michael Price <michael.price@netapp.com>
#
# This file is part of Ansible
#
@ -16,6 +17,16 @@
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
try:
import json
except ImportError:
import simplejson as json
from ansible.module_utils.six.moves.urllib.error import HTTPError
from ansible.module_utils.pycompat24 import get_exception
from ansible.module_utils.urls import open_url
HAS_NETAPP_LIB = False
try:
from netapp_lib.api.zapi import zapi
@ -88,3 +99,55 @@ def setup_ontap_zapi(module, vserver=None):
return server
else:
module.fail_json(msg="the python NetApp-Lib module is required")
def eseries_host_argument_spec():
"""Retrieve a base argument specifiation common to all NetApp E-Series modules"""
argument_spec = basic_auth_argument_spec()
argument_spec.update(dict(
api_username=dict(type='str', required=True),
api_password=dict(type='str', required=True, no_log=True),
api_url=dict(type='str', required=True),
ssid=dict(type='str', required=True),
validate_certs=dict(required=False, default=True),
))
return argument_spec
def request(url, data=None, headers=None, method='GET', use_proxy=True,
force=False, last_mod_time=None, timeout=10, validate_certs=True,
url_username=None, url_password=None, http_agent=None, force_basic_auth=True, ignore_errors=False):
"""Issue an HTTP request to a url, retrieving an optional JSON response."""
if headers is None:
headers = {
"Content-Type": "application/json",
"Accept": "application/json",
}
try:
r = open_url(url=url, data=data, headers=headers, method=method, use_proxy=use_proxy,
force=force, last_mod_time=last_mod_time, timeout=timeout, validate_certs=validate_certs,
url_username=url_username, url_password=url_password, http_agent=http_agent,
force_basic_auth=force_basic_auth)
except HTTPError:
err = get_exception()
r = err.fp
try:
raw_data = r.read()
if raw_data:
data = json.loads(raw_data)
else:
raw_data = None
except:
if ignore_errors:
pass
else:
raise Exception(raw_data)
resp_code = r.getcode()
if resp_code >= 400 and not ignore_errors:
raise Exception(resp_code, data)
else:
return resp_code, data