Use metadata.err for error msg and return logs only when debug is true

This commit is contained in:
Hiroaki Nakamura 2016-06-30 21:42:05 +09:00
parent 308e0984f7
commit cc8b54d3cc

View file

@ -278,9 +278,8 @@ lxd_container:
returned: when state is started or restarted returned: when state is started or restarted
sample: "stopped" sample: "stopped"
logs: logs:
descriptions: The logs of requests and responses. This key exists only when you set the descriptions: The logs of requests and responses.
debug paramter to true or this module failed. returned: when the debug parameter is true and any requests were sent.
returned: when requests are sent
actions: actions:
description: List of actions performed for the container. description: List of actions performed for the container.
returned: success returned: success
@ -432,12 +431,12 @@ class LxdContainerManagement(object):
if resp_type == 'error': if resp_type == 'error':
if ok_error_codes is not None and resp_json['error_code'] in ok_error_codes: if ok_error_codes is not None and resp_json['error_code'] in ok_error_codes:
return resp_json return resp_json
self.module.fail_json( fail_params = {
msg='error response', 'msg': self._get_err_from_resp_json(resp_json),
request={'method': method, 'url': url, 'json': body_json, 'timeout': self.timeout}, }
response={'json': resp_json}, if self.debug:
logs=self.logs fail_params['logs'] = self.logs
) self.module.fail_json(**fail_params)
return resp_json return resp_json
except socket.error as e: except socket.error as e:
if self.url is None: if self.url is None:
@ -452,18 +451,27 @@ class LxdContainerManagement(object):
error=e error=e
) )
@staticmethod
def _get_err_from_resp_json(resp_json):
metadata = resp_json.get('metadata', None)
if metadata is not None:
err = metadata.get('err', None)
if err is None:
err = resp_json.get('error', None)
return err
def _operate_and_wait(self, method, path, body_json=None): def _operate_and_wait(self, method, path, body_json=None):
resp_json = self._send_request(method, path, body_json=body_json) resp_json = self._send_request(method, path, body_json=body_json)
if resp_json['type'] == 'async': if resp_json['type'] == 'async':
url = '{0}/wait'.format(resp_json['operation']) url = '{0}/wait'.format(resp_json['operation'])
resp_json = self._send_request('GET', url) resp_json = self._send_request('GET', url)
if resp_json['metadata']['status'] != 'Success': if resp_json['metadata']['status'] != 'Success':
self.module.fail_json( fail_params = {
msg='error response for waiting opearation', 'msg': self._get_err_from_resp_json(resp_json),
request={'method': method, 'url': url}, }
response={'json': resp_json}, if self.debug:
logs=self.logs fail_params['logs'] = self.logs
) self.module.fail_json(**fail_params)
return resp_json return resp_json
def _get_container_json(self): def _get_container_json(self):
@ -541,12 +549,14 @@ class LxdContainerManagement(object):
return return
state_changed = len(self.actions) > 0 state_changed = len(self.actions) > 0
self.module.fail_json( fail_params = {
failed=True, 'msg': 'timeout for getting IPv4 addresses',
msg='timeout for getting IPv4 addresses', 'changed': state_changed,
changed=state_changed, 'actions': self.actions
actions=self.actions, }
logs=self.logs) if self.debug:
fail_params['logs'] = self.logs
self.module.fail_json(**fail_params)
def _started(self): def _started(self):
if self.old_state == 'absent': if self.old_state == 'absent':
@ -669,7 +679,6 @@ class LxdContainerManagement(object):
self._create_profile() self._create_profile()
else: else:
self.module.fail_json( self.module.fail_json(
failed=True,
msg='new_name must not be set when the profile does not exist and the specified state is present', msg='new_name must not be set when the profile does not exist and the specified state is present',
changed=False) changed=False)
else: else:
@ -683,7 +692,6 @@ class LxdContainerManagement(object):
self._delete_profile() self._delete_profile()
else: else:
self.module.fail_json( self.module.fail_json(
failed=True,
msg='new_name must not be set when the profile exists and the specified state is absent', msg='new_name must not be set when the profile exists and the specified state is absent',
changed=False) changed=False)