Return correct http code

During toggle enabled 500 was sent in case service is not defined
Fix return code to 404
Closes-Bug: #1268976
Change-Id: I726d8b8849b2ccd44fdbf9d7b4ce65f3a62cf4fc
This commit is contained in:
Ekaterina Fedorova 2014-01-15 16:39:54 +04:00
parent f5c86a8fd0
commit 32a54a0590
2 changed files with 30 additions and 21 deletions

View File

@ -217,16 +217,17 @@ def delete_service(service_name):
@v1_api.route('/admin/services/<service_name>/toggle_enabled',
methods=['POST'])
def toggleEnabled(service_name):
def toggle_enabled(service_name):
api_utils.check_service_name(service_name)
parser = ManifestParser()
result = parser.toggle_enabled(service_name)
if result:
api_utils.reset_cache()
return jsonify(result='success')
else:
return make_response(_('Unable to toggle '
'enable parameter for specified service'), 500)
try:
parser.toggle_enabled(service_name)
except NameError:
return make_response(_("'{0}' service is not "
"defined".format(service_name)), 404)
except Exception:
return make_response(_('Error toggling service enabled'), 500)
return jsonify(result='success'),
@v1_api.route('/admin/reset_caches', methods=['POST'])

View File

@ -145,20 +145,28 @@ class ManifestParser(object):
'{0}-manifest.yaml'.format(service_name))
def toggle_enabled(self, service_name):
"""
:param service_name: string, name of the service
:return: Status code with operation result
"""
path_to_manifest = self._get_manifest_path(service_name)
if not path_to_manifest:
log.error(_("There is no manifest "
"file for '{0}' service".format(service_name)))
return False
with open(path_to_manifest) as stream:
service_manifest_data = yaml.load(stream)
service_manifest_data['enabled'] = \
not service_manifest_data.get('enabled')
with open(path_to_manifest, 'w') as manifest_file:
manifest_file.write(yaml.dump(service_manifest_data,
default_flow_style=False))
return True
if not os.path.exists(path_to_manifest):
msg = _("There is no manifest "
"file for '{0}' service".format(service_name))
log.exception(msg)
raise NameError(msg)
try:
with open(path_to_manifest) as stream:
service_manifest_data = yaml.load(stream)
enabled_value = not service_manifest_data.get('enabled')
service_manifest_data['enabled'] = enabled_value
with open(path_to_manifest, 'w') as manifest_file:
manifest_file.write(yaml.dump(service_manifest_data,
default_flow_style=False))
except IOError:
msg = _("Error during modifying '{0}'".format(path_to_manifest))
log.exception(msg)
raise Exception(msg)
def update_service(self, service_name, data):
path_to_manifest = self._get_manifest_path(service_name)