Normalize all return messages

Ensure all API return messages are formated the same: no "error", a
final period except when displaying Exception text, and a regular spaced
out format.
This commit is contained in:
2020-02-20 22:38:31 -05:00
parent 31a117e21c
commit 56a9e48163
4 changed files with 167 additions and 167 deletions

View File

@ -141,7 +141,7 @@ def list_template_system(limit, is_fuzzy=True):
if data:
return data, 200
else:
return {'message': 'No system templates found'}, 404
return { 'message': 'No system templates found.' }, 404
def list_template_network(limit, is_fuzzy=True):
"""
@ -151,7 +151,7 @@ def list_template_network(limit, is_fuzzy=True):
if data:
return data, 200
else:
return {'message': 'No network templates found'}, 404
return { 'message': 'No network templates found.' }, 404
def list_template_network_vnis(name):
"""
@ -162,7 +162,7 @@ def list_template_network_vnis(name):
if networks:
return networks, 200
else:
return {'message': 'No network template networks found'}, 404
return { 'message': 'No network template networks found.' }, 404
def list_template_storage(limit, is_fuzzy=True):
"""
@ -172,7 +172,7 @@ def list_template_storage(limit, is_fuzzy=True):
if data:
return data, 200
else:
return {'message': 'No storage templates found'}, 404
return { 'message': 'No storage templates found.' }, 404
def list_template_storage_disks(name):
"""
@ -183,7 +183,7 @@ def list_template_storage_disks(name):
if disks:
return disks, 200
else:
return {'message': 'No storage template disks found'}, 404
return { 'message': 'No storage template disks found.' }, 404
def template_list(limit):
system_templates, code = list_template_system(limit)
@ -203,7 +203,7 @@ def template_list(limit):
#
def create_template_system(name, vcpu_count, vram_mb, serial=False, vnc=False, vnc_bind=None, node_limit=None, node_selector=None, node_autostart=False, ova=None):
if list_template_system(name, is_fuzzy=False)[-1] != 404:
retmsg = { 'message': 'The system template "{}" already exists'.format(name) }
retmsg = { 'message': 'The system template "{}" already exists.'.format(name) }
retcode = 400
return retmsg, retcode
@ -213,7 +213,7 @@ def create_template_system(name, vcpu_count, vram_mb, serial=False, vnc=False, v
conn, cur = open_database(config)
try:
cur.execute(query, args)
retmsg = { 'message': 'Added new system template "{}"'.format(name) }
retmsg = { 'message': 'Added new system template "{}".'.format(name) }
retcode = 200
except Exception as e:
retmsg = { 'message': 'Failed to create system template "{}": {}'.format(name, e) }
@ -223,7 +223,7 @@ def create_template_system(name, vcpu_count, vram_mb, serial=False, vnc=False, v
def create_template_network(name, mac_template=None):
if list_template_network(name, is_fuzzy=False)[-1] != 404:
retmsg = { 'message': 'The network template "{}" already exists'.format(name) }
retmsg = { 'message': 'The network template "{}" already exists.'.format(name) }
retcode = 400
return retmsg, retcode
@ -232,7 +232,7 @@ def create_template_network(name, mac_template=None):
query = "INSERT INTO network_template (name, mac_template) VALUES (%s, %s);"
args = (name, mac_template)
cur.execute(query, args)
retmsg = { 'message': 'Added new network template "{}"'.format(name) }
retmsg = { 'message': 'Added new network template "{}".'.format(name) }
retcode = 200
except Exception as e:
retmsg = { 'message': 'Failed to create network template "{}": {}'.format(name, e) }
@ -242,7 +242,7 @@ def create_template_network(name, mac_template=None):
def create_template_network_element(name, vni):
if list_template_network(name, is_fuzzy=False)[-1] != 200:
retmsg = { 'message': 'The network template "{}" does not exist'.format(name) }
retmsg = { 'message': 'The network template "{}" does not exist.'.format(name) }
retcode = 400
return retmsg, retcode
@ -254,7 +254,7 @@ def create_template_network_element(name, vni):
if int(network['vni']) == vni:
found_vni = True
if found_vni:
retmsg = { 'message': 'The VNI "{}" in network template "{}" already exists'.format(vni, name) }
retmsg = { 'message': 'The VNI "{}" in network template "{}" already exists.'.format(vni, name) }
retcode = 400
return retmsg, retcode
@ -267,17 +267,17 @@ def create_template_network_element(name, vni):
query = "INSERT INTO network (network_template, vni) VALUES (%s, %s);"
args = (template_id, vni)
cur.execute(query, args)
retmsg = { 'message': 'Added new network "{}" to network template "{}"'.format(vni, name) }
retmsg = { 'message': 'Added new network "{}" to network template "{}".'.format(vni, name) }
retcode = 200
except Exception as e:
retmsg = { 'message': 'Failed to create entry "{}"'.format(vni, e) }
retmsg = { 'message': 'Failed to create entry "{}": {}'.format(vni, e) }
retcode = 400
close_database(conn, cur)
return retmsg, retcode
def create_template_storage(name):
if list_template_storage(name, is_fuzzy=False)[-1] != 404:
retmsg = { 'message': 'The storage template "{}" already exists'.format(name) }
retmsg = { 'message': 'The storage template "{}" already exists.'.format(name) }
retcode = 400
return retmsg, retcode
@ -286,7 +286,7 @@ def create_template_storage(name):
query = "INSERT INTO storage_template (name) VALUES (%s);"
args = (name,)
cur.execute(query, args)
retmsg = { 'message': 'Added new storage template "{}"'.format(name) }
retmsg = { 'message': 'Added new storage template "{}".'.format(name) }
retcode = 200
except Exception as e:
retmsg = { 'message': 'Failed to create entry "{}": {}'.format(name, e) }
@ -296,7 +296,7 @@ def create_template_storage(name):
def create_template_storage_element(name, disk_id, pool, source_volume=None, disk_size_gb=None, filesystem=None, filesystem_args=[], mountpoint=None):
if list_template_storage(name, is_fuzzy=False)[-1] != 200:
retmsg = { 'message': 'The storage template "{}" does not exist'.format(name) }
retmsg = { 'message': 'The storage template "{}" does not exist.'.format(name) }
retcode = 400
return retmsg, retcode
@ -308,7 +308,7 @@ def create_template_storage_element(name, disk_id, pool, source_volume=None, dis
if disk['disk_id'] == disk_id:
found_disk = True
if found_disk:
retmsg = { 'message': 'The disk "{}" in storage template "{}" already exists'.format(disk_id, name) }
retmsg = { 'message': 'The disk "{}" in storage template "{}" already exists.'.format(disk_id, name) }
retcode = 400
return retmsg, retcode
@ -335,10 +335,10 @@ def create_template_storage_element(name, disk_id, pool, source_volume=None, dis
fsargs = ''
args = (template_id, pool, disk_id, source_volume, disk_size_gb, mountpoint, filesystem, fsargs)
cur.execute(query, args)
retmsg = { 'message': 'Added new disk "{}" to storage template "{}"'.format(disk_id, name) }
retmsg = { 'message': 'Added new disk "{}" to storage template "{}".'.format(disk_id, name) }
retcode = 200
except Exception as e:
retmsg = { 'message': 'Failed to create entry "{}"'.format(disk_id, e) }
retmsg = { 'message': 'Failed to create entry "{}": {}'.format(disk_id, e) }
retcode = 400
close_database(conn, cur)
return retmsg, retcode
@ -348,7 +348,7 @@ def create_template_storage_element(name, disk_id, pool, source_volume=None, dis
#
def modify_template_system(name, vcpu_count=None, vram_mb=None, serial=None, vnc=None, vnc_bind=None, node_limit=None, node_selector=None, node_autostart=None):
if list_profile(name, is_fuzzy=False)[-1] != 200:
retmsg = { 'message': 'The system template "{}" does not exist'.format(name) }
retmsg = { 'message': 'The system template "{}" does not exist.'.format(name) }
retcode = 400
return retmsg, retcode
@ -358,7 +358,7 @@ def modify_template_system(name, vcpu_count=None, vram_mb=None, serial=None, vnc
try:
vcpu_count = int(vcpu_count)
except:
retmsg = { 'message': 'The vcpus value must be an integer' }
retmsg = { 'message': 'The vcpus value must be an integer.' }
retcode = 400
return retmsg, retcode
fields.append({'field': 'vcpu_count', 'data': vcpu_count})
@ -367,7 +367,7 @@ def modify_template_system(name, vcpu_count=None, vram_mb=None, serial=None, vnc
try:
vram_mb = int(vram_mb)
except:
retmsg = { 'message': 'The vram value must be an integer' }
retmsg = { 'message': 'The vram value must be an integer.' }
retcode = 400
return retmsg, retcode
fields.append({'field': 'vram_mb', 'data': vram_mb})
@ -376,7 +376,7 @@ def modify_template_system(name, vcpu_count=None, vram_mb=None, serial=None, vnc
try:
serial = bool(strtobool(serial))
except:
retmsg = { 'message': 'The serial value must be a boolean' }
retmsg = { 'message': 'The serial value must be a boolean.' }
retcode = 400
return retmsg, retcode
fields.append({'field': 'serial', 'data': serial})
@ -385,7 +385,7 @@ def modify_template_system(name, vcpu_count=None, vram_mb=None, serial=None, vnc
try:
vnc = bool(strtobool(vnc))
except:
retmsg = { 'message': 'The vnc value must be a boolean' }
retmsg = { 'message': 'The vnc value must be a boolean.' }
retcode = 400
return retmsg, retcode
fields.append({'field': 'vnc', 'data': vnc})
@ -403,7 +403,7 @@ def modify_template_system(name, vcpu_count=None, vram_mb=None, serial=None, vnc
try:
node_autostart = bool(strtobool(node_autostart))
except:
retmsg = { 'message': 'The node_autostart value must be a boolean' }
retmsg = { 'message': 'The node_autostart value must be a boolean.' }
retcode = 400
fields.append({'field': 'node_autostart', 'data': node_autostart})
@ -413,7 +413,7 @@ def modify_template_system(name, vcpu_count=None, vram_mb=None, serial=None, vnc
query = "UPDATE system_template SET {} = %s WHERE name = %s;".format(field.get('field'))
args = (field.get('data'), name)
cur.execute(query, args)
retmsg = { "message": 'Modified system template "{}"'.format(name) }
retmsg = { "message": 'Modified system template "{}".'.format(name) }
retcode = 200
except Exception as e:
retmsg = { 'message': 'Failed to modify entry "{}": {}'.format(name, e) }
@ -426,7 +426,7 @@ def modify_template_system(name, vcpu_count=None, vram_mb=None, serial=None, vnc
#
def delete_template_system(name):
if list_template_system(name, is_fuzzy=False)[-1] != 200:
retmsg = { 'message': 'The system template "{}" does not exist'.format(name) }
retmsg = { 'message': 'The system template "{}" does not exist.'.format(name) }
retcode = 400
return retmsg, retcode
@ -435,7 +435,7 @@ def delete_template_system(name):
query = "DELETE FROM system_template WHERE name = %s;"
args = (name,)
cur.execute(query, args)
retmsg = { "message": 'Removed system template "{}"'.format(name) }
retmsg = { "message": 'Removed system template "{}".'.format(name) }
retcode = 200
except Exception as e:
retmsg = { 'message': 'Failed to delete entry "{}": {}'.format(name, e) }
@ -445,7 +445,7 @@ def delete_template_system(name):
def delete_template_network(name):
if list_template_network(name, is_fuzzy=False)[-1] != 200:
retmsg = { 'message': 'The network template "{}" does not exist'.format(name) }
retmsg = { 'message': 'The network template "{}" does not exist.'.format(name) }
retcode = 400
return retmsg, retcode
@ -461,7 +461,7 @@ def delete_template_network(name):
query = "DELETE FROM network_template WHERE name = %s;"
args = (name,)
cur.execute(query, args)
retmsg = { "message": 'Removed network template "{}"'.format(name) }
retmsg = { "message": 'Removed network template "{}".'.format(name) }
retcode = 200
except Exception as e:
retmsg = { 'message': 'Failed to delete entry "{}": {}'.format(name, e) }
@ -471,7 +471,7 @@ def delete_template_network(name):
def delete_template_network_element(name, vni):
if list_template_network(name, is_fuzzy=False)[-1] != 200:
retmsg = { 'message': 'The network template "{}" does not exist'.format(name) }
retmsg = { 'message': 'The network template "{}" does not exist.'.format(name) }
retcode = 400
return retmsg, retcode
@ -481,7 +481,7 @@ def delete_template_network_element(name, vni):
if network['vni'] == int(vni):
found_vni = True
if not found_vni:
retmsg = { 'message': 'The VNI "{}" in network template "{}" does not exist'.format(vni, name) }
retmsg = { 'message': 'The VNI "{}" in network template "{}" does not exist.'.format(vni, name) }
retcode = 400
return retmsg, retcode
@ -494,7 +494,7 @@ def delete_template_network_element(name, vni):
query = "DELETE FROM network WHERE network_template = %s and vni = %s;"
args = (template_id, vni)
cur.execute(query, args)
retmsg = { "message": 'Removed network "{}" from network template "{}"'.format(vni, name) }
retmsg = { "message": 'Removed network "{}" from network template "{}".'.format(vni, name) }
retcode = 200
except Exception as e:
retmsg = { 'message': 'Failed to delete entry "{}": {}'.format(name, e) }
@ -504,7 +504,7 @@ def delete_template_network_element(name, vni):
def delete_template_storage(name):
if list_template_storage(name, is_fuzzy=False)[-1] != 200:
retmsg = { 'message': 'The storage template "{}" does not exist'.format(name) }
retmsg = { 'message': 'The storage template "{}" does not exist.'.format(name) }
retcode = 400
return retmsg, retcode
@ -520,7 +520,7 @@ def delete_template_storage(name):
query = "DELETE FROM storage_template WHERE name = %s;"
args = (name,)
cur.execute(query, args)
retmsg = { "message": 'Removed storage template "{}"'.format(name) }
retmsg = { "message": 'Removed storage template "{}".'.format(name) }
retcode = 200
except Exception as e:
retmsg = { 'message': 'Failed to delete entry "{}": {}'.format(name, e) }
@ -530,7 +530,7 @@ def delete_template_storage(name):
def delete_template_storage_element(name, disk_id):
if list_template_storage(name, is_fuzzy=False)[-1] != 200:
retmsg = { 'message': 'The storage template "{}" does not exist'.format(name) }
retmsg = { 'message': 'The storage template "{}" does not exist.'.format(name) }
retcode = 400
return retmsg, retcode
@ -540,7 +540,7 @@ def delete_template_storage_element(name, disk_id):
if disk['disk_id'] == disk_id:
found_disk = True
if not found_disk:
retmsg = { 'message': 'The disk "{}" in storage template "{}" does not exist'.format(disk_id, name) }
retmsg = { 'message': 'The disk "{}" in storage template "{}" does not exist.'.format(disk_id, name) }
retcode = 400
return retmsg, retcode
@ -553,7 +553,7 @@ def delete_template_storage_element(name, disk_id):
query = "DELETE FROM storage WHERE storage_template = %s and disk_id = %s;"
args = (template_id, disk_id)
cur.execute(query, args)
retmsg = { "message": 'Removed disk "{}" from storage template "{}"'.format(disk_id, name) }
retmsg = { "message": 'Removed disk "{}" from storage template "{}".'.format(disk_id, name) }
retcode = 200
except Exception as e:
retmsg = { 'message': 'Failed to delete entry "{}": {}'.format(name, e) }
@ -590,11 +590,11 @@ def list_userdata(limit, is_fuzzy=True):
if data:
return data, 200
else:
return {'message': 'No userdata documents found'}, 404
return { 'message': 'No userdata documents found.' }, 404
def create_userdata(name, userdata):
if list_userdata(name, is_fuzzy=False)[-1] != 404:
retmsg = { 'message': 'The userdata document "{}" already exists'.format(name) }
retmsg = { 'message': 'The userdata document "{}" already exists.'.format(name) }
retcode = 400
return retmsg, retcode
@ -603,7 +603,7 @@ def create_userdata(name, userdata):
query = "INSERT INTO userdata (name, userdata) VALUES (%s, %s);"
args = (name, userdata)
cur.execute(query, args)
retmsg = { "message": 'Created userdata document "{}"'.format(name) }
retmsg = { "message": 'Created userdata document "{}".'.format(name) }
retcode = 200
except Exception as e:
retmsg = { 'message': 'Failed to create entry "{}": {}'.format(name, e) }
@ -613,7 +613,7 @@ def create_userdata(name, userdata):
def update_userdata(name, userdata):
if list_userdata(name, is_fuzzy=False)[-1] != 200:
retmsg = { 'message': 'The userdata "{}" does not exist'.format(name) }
retmsg = { 'message': 'The userdata "{}" does not exist.'.format(name) }
retcode = 400
return retmsg, retcode
@ -625,7 +625,7 @@ def update_userdata(name, userdata):
query = "UPDATE userdata SET userdata = %s WHERE id = %s;"
args = (userdata, tid)
cur.execute(query, args)
retmsg = { "message": 'Updated userdata document "{}"'.format(name) }
retmsg = { "message": 'Updated userdata document "{}".'.format(name) }
retcode = 200
except Exception as e:
retmsg = { 'message': 'Failed to update entry "{}": {}'.format(name, e) }
@ -635,7 +635,7 @@ def update_userdata(name, userdata):
def delete_userdata(name):
if list_userdata(name, is_fuzzy=False)[-1] != 200:
retmsg = { 'message': 'The userdata "{}" does not exist'.format(name) }
retmsg = { 'message': 'The userdata "{}" does not exist.'.format(name) }
retcode = 400
return retmsg, retcode
@ -644,10 +644,10 @@ def delete_userdata(name):
query = "DELETE FROM userdata WHERE name = %s;"
args = (name,)
cur.execute(query, args)
retmsg = { "message": 'Removed userdata document "{}"'.format(name) }
retmsg = { "message": 'Removed userdata document "{}".'.format(name) }
retcode = 200
except Exception as e:
retmsg = { 'message': 'Failed to delete entry "{}"'.format(name), 'error': str(e) }
retmsg = { 'message': 'Failed to delete entry "{}": {}'.format(name, e) }
retcode = 400
close_database(conn, cur)
return retmsg, retcode
@ -681,11 +681,11 @@ def list_script(limit, is_fuzzy=True):
if data:
return data, 200
else:
return {'message': 'No scripts found'}, 404
return { 'message': 'No scripts found.' }, 404
def create_script(name, script):
if list_script(name, is_fuzzy=False)[-1] != 404:
retmsg = { 'message': 'The script "{}" already exists'.format(name) }
retmsg = { 'message': 'The script "{}" already exists.'.format(name) }
retcode = 400
return retmsg, retcode
@ -694,7 +694,7 @@ def create_script(name, script):
query = "INSERT INTO script (name, script) VALUES (%s, %s);"
args = (name, script)
cur.execute(query, args)
retmsg = { "message": 'Created provisioning script "{}"'.format(name) }
retmsg = { "message": 'Created provisioning script "{}".'.format(name) }
retcode = 200
except Exception as e:
retmsg = { 'message': 'Failed to create entry "{}": {}'.format(name, e) }
@ -704,7 +704,7 @@ def create_script(name, script):
def update_script(name, script):
if list_script(name, is_fuzzy=False)[-1] != 200:
retmsg = { 'message': 'The script "{}" does not exist'.format(name) }
retmsg = { 'message': 'The script "{}" does not exist.'.format(name) }
retcode = 400
return retmsg, retcode
@ -716,7 +716,7 @@ def update_script(name, script):
query = "UPDATE script SET script = %s WHERE id = %s;"
args = (script, tid)
cur.execute(query, args)
retmsg = { "message": 'Updated provisioning script "{}"'.format(name) }
retmsg = { "message": 'Updated provisioning script "{}".'.format(name) }
retcode = 200
except Exception as e:
retmsg = { 'message': 'Failed to update entry "{}": {}'.format(name, e) }
@ -726,7 +726,7 @@ def update_script(name, script):
def delete_script(name):
if list_script(name, is_fuzzy=False)[-1] != 200:
retmsg = { 'message': 'The script "{}" does not exist'.format(name) }
retmsg = { 'message': 'The script "{}" does not exist.'.format(name) }
retcode = 400
return retmsg, retcode
@ -735,10 +735,10 @@ def delete_script(name):
query = "DELETE FROM script WHERE name = %s;"
args = (name,)
cur.execute(query, args)
retmsg = { "message": 'Removed provisioning script "{}"'.format(name) }
retmsg = { "message": 'Removed provisioning script "{}".'.format(name) }
retcode = 200
except Exception as e:
retmsg = { 'message': 'Failed to delete entry "{}"'.format(name), 'error': str(e) }
retmsg = { 'message': 'Failed to delete entry "{}": {}'.format(name, e) }
retcode = 400
close_database(conn, cur)
return retmsg, retcode
@ -792,16 +792,16 @@ def list_profile(limit, is_fuzzy=True):
if data:
return data, 200
else:
return {'message': 'No profiles found'}, 404
return { 'message': 'No profiles found.' }, 404
def create_profile(name, profile_type, system_template, network_template, storage_template, userdata=None, script=None, ova=None, arguments=None):
if list_profile(name, is_fuzzy=False)[-1] != 404:
retmsg = { 'message': 'The profile "{}" already exists'.format(name) }
retmsg = { 'message': 'The profile "{}" already exists.'.format(name) }
retcode = 400
return retmsg, retcode
if profile_type not in ['provisioner', 'ova']:
retmsg = { 'message': 'A valid profile type (provisioner, ova) must be specified' }
retmsg = { 'message': 'A valid profile type (provisioner, ova) must be specified.' }
retcode = 400
return retmsg, retcode
@ -811,7 +811,7 @@ def create_profile(name, profile_type, system_template, network_template, storag
if template['name'] == system_template:
system_template_id = template['id']
if not system_template_id:
retmsg = { 'message': 'The system template "{}" for profile "{}" does not exist'.format(system_template, name) }
retmsg = { 'message': 'The system template "{}" for profile "{}" does not exist.'.format(system_template, name) }
retcode = 400
return retmsg, retcode
@ -821,7 +821,7 @@ def create_profile(name, profile_type, system_template, network_template, storag
if template['name'] == network_template:
network_template_id = template['id']
if not network_template_id and profile_type != 'ova':
retmsg = { 'message': 'The network template "{}" for profile "{}" does not exist'.format(network_template, name) }
retmsg = { 'message': 'The network template "{}" for profile "{}" does not exist.'.format(network_template, name) }
retcode = 400
return retmsg, retcode
@ -831,7 +831,7 @@ def create_profile(name, profile_type, system_template, network_template, storag
if template['name'] == storage_template:
storage_template_id = template['id']
if not storage_template_id and profile_type != 'ova':
retmsg = { 'message': 'The storage template "{}" for profile "{}" does not exist'.format(storage_template, name) }
retmsg = { 'message': 'The storage template "{}" for profile "{}" does not exist.'.format(storage_template, name) }
retcode = 400
return retmsg, retcode
@ -863,7 +863,7 @@ def create_profile(name, profile_type, system_template, network_template, storag
query = "INSERT INTO profile (name, profile_type, system_template, network_template, storage_template, userdata, script, ova, arguments) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s);"
args = (name, profile_type, system_template_id, network_template_id, storage_template_id, userdata_id, script_id, ova_id, arguments_formatted)
cur.execute(query, args)
retmsg = { "message": 'Created VM profile "{}"'.format(name) }
retmsg = { "message": 'Created VM profile "{}".'.format(name) }
retcode = 200
except Exception as e:
retmsg = { 'message': 'Failed to create entry "{}": {}'.format(name, e) }
@ -873,7 +873,7 @@ def create_profile(name, profile_type, system_template, network_template, storag
def modify_profile(name, profile_type, system_template, network_template, storage_template, userdata, script, ova, arguments=None):
if list_profile(name, is_fuzzy=False)[-1] != 200:
retmsg = { 'message': 'The profile "{}" does not exist'.format(name) }
retmsg = { 'message': 'The profile "{}" does not exist.'.format(name) }
retcode = 400
return retmsg, retcode
@ -881,7 +881,7 @@ def modify_profile(name, profile_type, system_template, network_template, storag
if profile_type is not None:
if profile_type not in ['provisioner', 'ova']:
retmsg = { 'message': 'A valid profile type (provisioner, ova) must be specified' }
retmsg = { 'message': 'A valid profile type (provisioner, ova) must be specified.' }
retcode = 400
return retmsg, retcode
fields.append({'field': 'type', 'data': profile_type})
@ -893,7 +893,7 @@ def modify_profile(name, profile_type, system_template, network_template, storag
if template['name'] == system_template:
system_template_id = template['id']
if not system_template_id:
retmsg = { 'message': 'The system template "{}" for profile "{}" does not exist'.format(system_template, name) }
retmsg = { 'message': 'The system template "{}" for profile "{}" does not exist.'.format(system_template, name) }
retcode = 400
return retmsg, retcode
fields.append({'field': 'system_template', 'data': system_template_id})
@ -905,7 +905,7 @@ def modify_profile(name, profile_type, system_template, network_template, storag
if template['name'] == network_template:
network_template_id = template['id']
if not network_template_id:
retmsg = { 'message': 'The network template "{}" for profile "{}" does not exist'.format(network_template, name) }
retmsg = { 'message': 'The network template "{}" for profile "{}" does not exist.'.format(network_template, name) }
retcode = 400
return retmsg, retcode
fields.append({'field': 'network_template', 'data': network_template_id})
@ -917,7 +917,7 @@ def modify_profile(name, profile_type, system_template, network_template, storag
if template['name'] == storage_template:
storage_template_id = template['id']
if not storage_template_id:
retmsg = { 'message': 'The storage template "{}" for profile "{}" does not exist'.format(storage_template, name) }
retmsg = { 'message': 'The storage template "{}" for profile "{}" does not exist.'.format(storage_template, name) }
retcode = 400
return retmsg, retcode
fields.append({'field': 'storage_template', 'data': storage_template_id})
@ -929,7 +929,7 @@ def modify_profile(name, profile_type, system_template, network_template, storag
if template['name'] == userdata:
userdata_id = template['id']
if not userdata_id:
retmsg = { 'message': 'The userdata template "{}" for profile "{}" does not exist'.format(userdata, name) }
retmsg = { 'message': 'The userdata template "{}" for profile "{}" does not exist.'.format(userdata, name) }
retcode = 400
return retmsg, retcode
fields.append({'field': 'userdata', 'data': userdata_id})
@ -941,7 +941,7 @@ def modify_profile(name, profile_type, system_template, network_template, storag
if scr['name'] == script:
script_id = scr['id']
if not script_id:
retmsg = { 'message': 'The script "{}" for profile "{}" does not exist'.format(script, name) }
retmsg = { 'message': 'The script "{}" for profile "{}" does not exist.'.format(script, name) }
retcode = 400
return retmsg, retcode
fields.append({'field': 'script', 'data': script_id})
@ -953,7 +953,7 @@ def modify_profile(name, profile_type, system_template, network_template, storag
if ov['name'] == ova:
ova_id = ov['id']
if not ova_id:
retmsg = { 'message': 'The OVA "{}" for profile "{}" does not exist'.format(ova, name) }
retmsg = { 'message': 'The OVA "{}" for profile "{}" does not exist.'.format(ova, name) }
retcode = 400
return retmsg, retcode
fields.append({'field': 'ova', 'data': ova_id})
@ -971,7 +971,7 @@ def modify_profile(name, profile_type, system_template, network_template, storag
query = "UPDATE profile SET {}=%s WHERE name=%s;".format(field.get('field'))
args = (field.get('data'), name)
cur.execute(query, args)
retmsg = { "message": 'Modified VM profile "{}"'.format(name) }
retmsg = { "message": 'Modified VM profile "{}".'.format(name) }
retcode = 200
except Exception as e:
retmsg = { 'message': 'Failed to modify entry "{}": {}'.format(name, e) }
@ -981,7 +981,7 @@ def modify_profile(name, profile_type, system_template, network_template, storag
def delete_profile(name):
if list_profile(name, is_fuzzy=False)[-1] != 200:
retmsg = { 'message': 'The profile "{}" does not exist'.format(name) }
retmsg = { 'message': 'The profile "{}" does not exist.'.format(name) }
retcode = 400
return retmsg, retcode
@ -990,7 +990,7 @@ def delete_profile(name):
query = "DELETE FROM profile WHERE name = %s;"
args = (name,)
cur.execute(query, args)
retmsg = { "message": 'Removed VM profile "{}"'.format(name) }
retmsg = { "message": 'Removed VM profile "{}".'.format(name) }
retcode = 200
except Exception as e:
retmsg = { 'message': 'Failed to delete entry "{}": {}'.format(name, e) }
@ -1127,7 +1127,7 @@ def create_vm(self, vm_name, vm_profile, define_vm=True, start_vm=True):
# Verify that a VM with this name does not already exist
if pvc_vm.searchClusterByName(zk_conn, vm_name):
raise ClusterError("A VM with the name '{}' already exists in the cluster".format(vm_name))
raise ClusterError("A VM with the name '{}' already exists in the cluster.".format(vm_name))
# Verify that at least one host has enough free RAM to run the VM
_discard, nodes = pvc_node.get_list(zk_conn, None)
@ -1146,7 +1146,7 @@ def create_vm(self, vm_name, vm_profile, define_vm=True, start_vm=True):
target_node = node['name']
# Raise if no node was found
if not target_node:
raise ClusterError("No ready cluster node contains at least {}+512 MB of free RAM".format(vm_data['system_details']['vram_mb']))
raise ClusterError("No ready cluster node contains at least {}+512 MB of free RAM.".format(vm_data['system_details']['vram_mb']))
print('Selecting target node "{}" with "{}" MB free RAM'.format(target_node, last_free))
@ -1155,7 +1155,7 @@ def create_vm(self, vm_name, vm_profile, define_vm=True, start_vm=True):
for network in vm_data['networks']:
vni = str(network['vni'])
if not vni in cluster_networks:
raise ClusterError('The network VNI "{}" is not present on the cluster'.format(vni))
raise ClusterError('The network VNI "{}" is not present on the cluster.'.format(vni))
print("All configured networks for VM are valid")
@ -1167,12 +1167,12 @@ def create_vm(self, vm_name, vm_profile, define_vm=True, start_vm=True):
volume_data, status = pvc_ceph.getVolumeInformation(zk_conn, volume['pool'], volume['source_volume'])
pools[volume['pool']] = volume_data['disk_size_gb']
if not status:
raise ClusterError('The source volume {}/{} could not be found'.format(volume['pool'], volume['source_volume']))
raise ClusterError('The source volume {}/{} could not be found.'.format(volume['pool'], volume['source_volume']))
else:
volume_data, status = pvc_ceph.getVolumeInformation(zk_conn, volume['pool'], volume['source_volume'])
pools[volume['pool']] += volume_data['disk_size_gb']
if not status:
raise ClusterError('The source volume {}/{} could not be found'.format(volume['pool'], volume['source_volume']))
raise ClusterError('The source volume {}/{} could not be found.'.format(volume['pool'], volume['source_volume']))
else:
if not volume['pool'] in pools:
pools[volume['pool']] = volume['disk_size_gb']
@ -1182,12 +1182,12 @@ def create_vm(self, vm_name, vm_profile, define_vm=True, start_vm=True):
for pool in pools:
pool_information = pvc_ceph.getPoolInformation(zk_conn, pool)
if not pool_information:
raise ClusterError('Pool "{}" is not present on the cluster'.format(pool))
raise ClusterError('Pool "{}" is not present on the cluster.'.format(pool))
pool_free_space_gb = int(pool_information['stats']['free_bytes'] / 1024 / 1024 / 1024)
pool_vm_usage_gb = int(pools[pool])
if pool_vm_usage_gb >= pool_free_space_gb:
raise ClusterError('Pool "{}" has only {} GB free and VM requires {} GB'.format(pool, pool_free_space_gb, pool_vm_usage_gb))
raise ClusterError('Pool "{}" has only {} GB free and VM requires {} GB.'.format(pool, pool_free_space_gb, pool_vm_usage_gb))
print("There is enough space on cluster to store VM volumes")
@ -1236,7 +1236,7 @@ def create_vm(self, vm_name, vm_profile, define_vm=True, start_vm=True):
# Verify that the install() function is valid
if not "install" in dir(installer_script):
raise ProvisioningError("Specified script does not contain an install() function")
raise ProvisioningError("Specified script does not contain an install() function.")
print("Provisioning script imported successfully")
@ -1388,12 +1388,12 @@ def create_vm(self, vm_name, vm_profile, define_vm=True, start_vm=True):
success, message = pvc_ceph.clone_volume(zk_conn, volume['pool'], "{}_{}".format(vm_name, volume['disk_id']), volume['source_volume'])
print(message)
if not success:
raise ClusterError('Failed to clone volume "{}" to "{}"'.format(volume['source_volume'], volume['disk_id']))
raise ClusterError('Failed to clone volume "{}" to "{}".'.format(volume['source_volume'], volume['disk_id']))
else:
success, message = pvc_ceph.add_volume(zk_conn, volume['pool'], "{}_{}".format(vm_name, volume['disk_id']), "{}G".format(volume['disk_size_gb']))
print(message)
if not success:
raise ClusterError('Failed to create volume "{}"'.format(volume['disk_id']))
raise ClusterError('Failed to create volume "{}".'.format(volume['disk_id']))
# Phase 7 - disk mapping
# * Map each volume to the local host in order