Allow modification of system templates

Closes #82
This commit is contained in:
2020-02-18 16:18:27 -05:00
parent b322841edf
commit ca68321be3
5 changed files with 302 additions and 4 deletions

View File

@ -4024,12 +4024,79 @@ class API_Provisioner_Template_System_Element(Resource):
node_autostart
)
@RequestParser([
{ 'name': 'vcpus' },
{ 'name': 'vram' },
{ 'name': 'serial' },
{ 'name': 'vnc' },
{ 'name': 'vnc_bind' },
{ 'name': 'node_limit' },
{ 'name': 'node_selector' },
{ 'name': 'node_autostart' }
])
@Authenticator
def put(self, template):
def put(self, template, reqargs):
"""
TODO
Modify an existing system template {template}
---
tags:
- provisioner / template
parameters:
- in: query
name: vcpus
type: integer
description: vCPU count for VM
- in: query
name: vram
type: integer
description: vRAM size in MB for VM
- in: query
name: serial
type: boolean
description: Whether to enable serial console for VM
- in: query
name: vnc
type: boolean
description: Whether to enable VNC console for VM
- in: query
name: vnc_bind
type: string
description: VNC bind address when VNC console is enabled
- in: query
name: node_limit
type: string
description: CSV list of node(s) to limit VM assignment to
- in: query
name: node_selector
type: string
description: Selector to use for VM node assignment on migration/move
- in: query
name: node_autostart
type: boolean
description: Whether to start VM with node ready state (one-time)
responses:
200:
description: OK
schema:
type: object
id: Message
400:
description: Bad request
schema:
type: object
id: Message
"""
pass
return api_provisioner.modify_template_system(
template,
reqargs.get('vcpus', None),
reqargs.get('vram', None),
reqargs.get('serial', None),
reqargs.get('vnc', None),
reqargs.get('vnc_bind'),
reqargs.get('node_limit', None),
reqargs.get('node_selector', None),
reqargs.get('node_autostart', None)
)
@Authenticator
def delete(self, template):

View File

@ -30,6 +30,8 @@ import time
import shlex
import subprocess
from distutils.util import strtobool
import daemon_lib.common as pvc_common
import daemon_lib.node as pvc_node
import daemon_lib.vm as pvc_vm
@ -341,6 +343,84 @@ def create_template_storage_element(name, disk_id, pool, source_volume=None, dis
close_database(conn, cur)
return retmsg, retcode
#
# Template Modify functions
#
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) }
retcode = 400
return retmsg, retcode
fields = []
if vcpu_count is not None:
try:
vcpu_count = int(vcpu_count)
except:
retmsg = { 'message': 'The vcpus value must be an integer' }
retcode = 400
return retmsg, retcode
fields.append({'field': 'vcpu_count', 'data': vcpu_count})
if vram_mb is not None:
try:
vram_mb = int(vram_mb)
except:
retmsg = { 'message': 'The vram value must be an integer' }
retcode = 400
return retmsg, retcode
fields.append({'field': 'vram_mb', 'data': vram_mb})
if serial is not None:
try:
serial = bool(strtobool(serial))
except:
retmsg = { 'message': 'The serial value must be a boolean' }
retcode = 400
return retmsg, retcode
fields.append({'field': 'serial', 'data': serial})
if vnc is not None:
try:
vnc = bool(strtobool(vnc))
except:
retmsg = { 'message': 'The vnc value must be a boolean' }
retcode = 400
return retmsg, retcode
fields.append({'field': 'vnc', 'data': vnc})
if vnc_bind is not None:
fields.append({'field': 'vnc_bind', 'data': vnc_bind})
if node_limit is not None:
fields.append({'field': 'node_limit', 'data': node_limit})
if node_selector is not None:
fields.append({'field': 'node_selector', 'data': node_selector})
if node_autostart is not None:
try:
node_autostart = bool(strtobool(node_autostart))
except:
retmsg = { 'message': 'The node_autostart value must be a boolean' }
retcode = 400
fields.append({'field': 'node_autostart', 'data': node_autostart})
conn, cur = open_database(config)
try:
for field in fields:
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) }
retcode = 200
except Exception as e:
retmsg = { 'message': 'Failed to modify entry "{}": {}'.format(name, e) }
retcode = 400
close_database(conn, cur)
return retmsg, retcode
#
# Template Delete functions
#