Commit c486bc51 by Karsa Zoltán István

validation ci data

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