Add profile modification endpoint

This commit is contained in:
Joshua Boniface 2020-01-16 22:35:55 -05:00
parent 5af7dc422d
commit 99b220b051
3 changed files with 221 additions and 0 deletions

View File

@ -782,6 +782,96 @@ def create_profile(name, system_template, network_template, storage_template, us
close_database(conn, cur)
return retmsg, retcode
def modify_profile(name, system_template, network_template, storage_template, userdata, script, arguments=None):
if list_profile(name, is_fuzzy=False)[-1] != 200:
retmsg = { 'message': 'The profile "{}" does not exist'.format(name) }
retcode = 400
return retmsg, retcode
fields = []
if system_template is not None:
system_templates, code = list_template_system(None)
system_template_id = None
for template in system_templates:
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) }
retcode = 400
return retmsg, retcode
fields.append({'field': 'system_template', 'data': system_template_id})
if network_template is not None:
network_templates, code = list_template_network(None)
network_template_id = None
for template in network_templates:
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) }
retcode = 400
return retmsg, retcode
fields.append({'field': 'network_template', 'data': network_template_id})
if storage_template is not None:
storage_templates, code = list_template_storage(None)
storage_template_id = None
for template in storage_templates:
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) }
retcode = 400
return retmsg, retcode
fields.append({'field': 'storage_template', 'data': storage_template_id})
if userdata is not None:
userdatas, code = list_userdata(None)
userdata_id = None
for template in userdatas:
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) }
retcode = 400
return retmsg, retcode
fields.append({'field': 'userdata', 'data': userdata_id})
if script is not None:
scripts, code = list_script(None)
script_id = None
for scr in scripts:
if scr['name'] == script:
script_id = scr['id']
if not script_id:
retmsg = { 'message': 'The script "{}" for profile "{}" does not exist'.format(script, name) }
retcode = 400
return retmsg, retcode
fields.append({'field': 'script', 'data': script_id})
if arguments is not None:
if isinstance(arguments, list):
arguments_formatted = '|'.join(arguments)
else:
arguments_formatted = ''
fields.append({'field': 'arguments', 'data': arguments_formatted})
conn, cur = open_database(config)
try:
for field in fields:
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) }
retcode = 200
except Exception as e:
retmsg = { 'message': 'Failed to modify entry "{}": {}'.format(name, e) }
retcode = 400
close_database(conn, cur)
return retmsg, retcode
def delete_profile(name):
if list_profile(name, is_fuzzy=False)[-1] != 200:
retmsg = { 'message': 'The profile "{}" does not exist'.format(name) }

View File

@ -5340,6 +5340,73 @@ class API_Provisioner_Profile_Element(Resource):
reqargs.get('arg', [])
)
@RequestParser([
{ 'name': 'system_template' },
{ 'name': 'network_template' },
{ 'name': 'storage_template' },
{ 'name': 'userdata' },
{ 'name': 'script' },
{ 'name': 'arg', 'action': 'append' }
])
@Authenticator
def put(self, profile, reqargs):
"""
Modify profile {profile}
---
tags:
- provisioner
parameters:
- in: query
name: script
type: string
required: false
description: Script name
- in: query
name: system_template
type: string
required: false
description: System template name
- in: query
name: network_template
type: string
required: false
description: Network template name
- in: query
name: storage_template
type: string
required: false
description: Storage template name
- in: query
name: userdata
type: string
required: false
description: Userdata template name
- in: query
name: arg
type: string
description: Script install() function keywork argument in "arg=data" format; may be specified multiple times to add multiple arguments
responses:
200:
description: OK
schema:
type: object
id: Message
400:
description: Bad request
schema:
type: object
id: Message
"""
return api_provisioner.modify_profile(
profile,
reqargs.get('system_template', None),
reqargs.get('network_template', None),
reqargs.get('storage_template', None),
reqargs.get('userdata', None),
reqargs.get('script', None),
reqargs.get('arg', [])
)
@Authenticator
def delete(self, profile):
"""

View File

@ -2396,6 +2396,70 @@
"tags": [
"provisioner"
]
},
"put": {
"description": "",
"parameters": [
{
"description": "Script name",
"in": "query",
"name": "script",
"required": false,
"type": "string"
},
{
"description": "System template name",
"in": "query",
"name": "system_template",
"required": false,
"type": "string"
},
{
"description": "Network template name",
"in": "query",
"name": "network_template",
"required": false,
"type": "string"
},
{
"description": "Storage template name",
"in": "query",
"name": "storage_template",
"required": false,
"type": "string"
},
{
"description": "Userdata template name",
"in": "query",
"name": "userdata",
"required": false,
"type": "string"
},
{
"description": "Script install() function keywork argument in \"arg=data\" format; may be specified multiple times to add multiple arguments",
"in": "query",
"name": "arg",
"type": "string"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/Message"
}
},
"400": {
"description": "Bad request",
"schema": {
"$ref": "#/definitions/Message"
}
}
},
"summary": "Modify profile {profile}",
"tags": [
"provisioner"
]
}
},
"/api/v1/provisioner/script": {