Commit c486bc51 by Karsa Zoltán István

validation ci data

parent 5c8fa224
......@@ -60,7 +60,7 @@ from vm.models import (
InstanceTemplate, Lease, InterfaceTemplate, Node, Trait, Instance
)
from .models import Profile, GroupProfile, Message
from .validators import domain_validator
from .validators import domain_validator, meta_data_validator, user_data_validator
LANGUAGES_WITH_CODE = ((l[0], ugettext_lazy(l[1], " (", l[0], ")"))
for l in LANGUAGES)
......@@ -569,6 +569,9 @@ class TemplateForm(forms.ModelForm):
self.fields['raw_data'].validators.append(domain_validator)
self.fields['ci_user_data'].validators.append(user_data_validator)
self.fields['ci_meta_data'].validators.append(meta_data_validator)
def clean_owner(self):
if self.instance.pk is not None:
return User.objects.get(pk=self.instance.owner.pk)
......@@ -1535,9 +1538,11 @@ class RawDataForm(forms.ModelForm):
class CIDataForm(forms.ModelForm):
ci_meta_data = forms.CharField(disabled=True, help_text='meta-data',
validators=[meta_data_validator],
widget=forms.Textarea(attrs={'rows': 5}),
required=False)
ci_user_data = forms.CharField(disabled=True, help_text='user-data',
validators=[user_data_validator],
widget=forms.Textarea(attrs={'rows': 5}),
required=False)
......@@ -1551,12 +1556,6 @@ class CIDataForm(forms.ModelForm):
helper.form_show_labels = False
return helper
def clean(self):
cleaned_data = super(forms.ModelForm, self).clean()
Instance.validate_meta_data(cleaned_data['ci_meta_data'])
Instance.validate_user_data(cleaned_data['ci_user_data'])
return cleaned_data
class GroupPermissionForm(forms.ModelForm):
permissions = forms.ModelMultipleChoiceField(
......
......@@ -20,6 +20,9 @@ from django.utils.translation import ugettext_lazy as _
from lxml import etree as ET
import logging
import yaml
from vm.models import Instance
rng_file = "/usr/share/libvirt/schemas/domain.rng"
......@@ -50,6 +53,20 @@ def domain_validator(value):
raise ValidationError(e.message)
def meta_data_validator(value):
try:
Instance.validate_meta_data(value)
except yaml.YAMLError as exc:
raise ValidationError(exc.problem_mark)
def user_data_validator(value):
try:
Instance.validate_user_data(value)
except yaml.YAMLError as exc:
raise ValidationError(exc.problem_mark)
def connect_command_template_validator(value):
"""Validate value as a connect command template.
......
......@@ -376,10 +376,7 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel, OperatedMixin,
def validate_user_data(cls, user_data):
data = user_data.replace('{{user}}', 'user')
data = data.replace('{{password}}', 'passwd')
try:
yaml.dump(yaml.load(data, Loader=yaml.Loader))
except yaml.YAMLError as exc:
raise forms.ValidationError(exc.problem_mark)
yaml.dump(yaml.load(data, Loader=yaml.Loader))
return True
@property
......@@ -390,10 +387,7 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel, OperatedMixin,
@classmethod
def validate_meta_data(cls, meta_data):
data = meta_data.replace('{{hostname}}', 'hostname')
try:
yaml.dump(yaml.load(data, Loader=yaml.Loader))
except yaml.YAMLError as exc:
raise forms.ValidationError(exc.problem_mark)
yaml.dump(yaml.load(data, Loader=yaml.Loader))
return True
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment