Various fixes (#27889)

This PR includes:
- Removal of get_exception (sadly)
- Avoid deprecating 'state' parameter with aci_rest
- Small fix for querying using aci_rest

Signed-off-by: @bcoca
This commit is contained in:
Dag Wieers 2017-08-10 01:51:13 +02:00 committed by GitHub
parent 380c43de4e
commit 6bfedc9fe5
2 changed files with 31 additions and 23 deletions

View file

@ -1,26 +1,37 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# This code is part of Ansible, but is an independent component
# This particular file snippet, and this file snippet only, is BSD licensed.
# Modules you write using this snippet, which is embedded dynamically by Ansible
# still belong to the author of the module, and may assign their own license
# to the complete work.
# Copyright 2017 Dag Wieers <dag@wieers.com> # Copyright 2017 Dag Wieers <dag@wieers.com>
# Copyright 2017 Swetha Chunduri (@schunduri) # Copyright 2017 Swetha Chunduri (@schunduri)
# All rights reserved.
# This file is part of Ansible by Red Hat # Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
# #
# Ansible is free software: you can redistribute it and/or modify # * Redistributions of source code must retain the above copyright
# it under the terms of the GNU General Public License as published by # notice, this list of conditions and the following disclaimer.
# the Free Software Foundation, either version 3 of the License, or # * Redistributions in binary form must reproduce the above copyright notice,
# (at your option) any later version. # this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# #
# Ansible is distributed in the hope that it will be useful, # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# but WITHOUT ANY WARRANTY; without even the implied warranty of # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# GNU General Public License for more details. # IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# You should have received a copy of the GNU General Public License # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import json import json
from ansible.module_utils.basic import get_exception
from ansible.module_utils.urls import fetch_url from ansible.module_utils.urls import fetch_url
from ansible.module_utils._text import to_bytes from ansible.module_utils._text import to_bytes
@ -68,8 +79,7 @@ def aci_response_json(result, rawoutput):
''' Handle APIC JSON response output ''' ''' Handle APIC JSON response output '''
try: try:
result.update(json.loads(rawoutput)) result.update(json.loads(rawoutput))
except: except Exception as e:
e = get_exception()
# Expose RAW output for troubleshooting # Expose RAW output for troubleshooting
result.update(raw=rawoutput, error_code=-1, error_text="Unable to parse output as JSON, see 'raw' output. %s" % e) result.update(raw=rawoutput, error_code=-1, error_text="Unable to parse output as JSON, see 'raw' output. %s" % e)
return return
@ -85,8 +95,7 @@ def aci_response_xml(result, rawoutput):
try: try:
xml = lxml.etree.fromstring(to_bytes(rawoutput)) xml = lxml.etree.fromstring(to_bytes(rawoutput))
xmldata = cobra.data(xml) xmldata = cobra.data(xml)
except: except Exception as e:
e = get_exception()
# Expose RAW output for troubleshooting # Expose RAW output for troubleshooting
result.update(raw=rawoutput, error_code=-1, error_text="Unable to parse output as XML, see 'raw' output. %s" % e) result.update(raw=rawoutput, error_code=-1, error_text="Unable to parse output as XML, see 'raw' output. %s" % e)
return return
@ -128,7 +137,9 @@ class ACIModule(object):
# Handle deprecated method/action parameter # Handle deprecated method/action parameter
if self.params['method']: if self.params['method']:
self.module.deprecate("Parameter 'method' or 'action' is deprecated, please use 'state' instead", '2.6') # Deprecate only if state was a valid option (not for aci_rest)
if self.module.argument_spec('state', False):
self.module.deprecate("Parameter 'method' or 'action' is deprecated, please use 'state' instead", '2.6')
method_map = dict(delete='absent', get='query', post='present') method_map = dict(delete='absent', get='query', post='present')
self.params['state'] = method_map[self.params['method']] self.params['state'] = method_map[self.params['method']]
else: else:

View file

@ -1,14 +1,11 @@
#!/usr/bin/python #!/usr/bin/python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Copyright 2017 Dag Wieers <dag@wieers.com>
# Copyright 2017 Swetha Chunduri (@schunduri)
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function from __future__ import absolute_import, division, print_function
__metaclass__ = type __metaclass__ = type
ANSIBLE_METADATA = {'metadata_version': '1.0', ANSIBLE_METADATA = {'metadata_version': '1.0',
'status': ['preview'], 'status': ['preview'],
'supported_by': 'community'} 'supported_by': 'community'}
@ -223,7 +220,7 @@ def main():
if os.path.isfile(src): if os.path.isfile(src):
file_exists = True file_exists = True
else: else:
module.fail_json(msg='Cannot find/access src:\n%s' % src) module.fail_json(msg="Cannot find/access src '%s'" % src)
# Find request type # Find request type
if path.find('.xml') != -1: if path.find('.xml') != -1:
@ -240,7 +237,7 @@ def main():
aci = ACIModule(module) aci = ACIModule(module)
if method == 'get': if method == 'get':
aci.request() aci.request(path)
module.exit_json(**aci.result) module.exit_json(**aci.result)
elif module.check_mode: elif module.check_mode:
# In check_mode we assume it works, but we don't actually perform the requested change # In check_mode we assume it works, but we don't actually perform the requested change