Commit f1bb1ee3 authored by Ahmed Bilal's avatar Ahmed Bilal 💬

2nd commit

parent e5dd5e45
This diff is collapsed.
......@@ -30,9 +30,9 @@ class Field:
def is_valid(self):
if not isinstance(self.value, self.type):
raise ValidationException("Incorrect Type for '{}' field".format(self.name))
else:
for validator in self.validators:
validator()
for validator in self.validators:
validator()
def __repr__(self):
return self.name
......@@ -52,8 +52,7 @@ class VmUUIDField(Field):
class BaseSchema:
def __init__(self, data):
print(data)
def __init__(self):
self.fields = [getattr(self, field) for field in dir(self) if isinstance(getattr(self, field), Field)]
def validation(self):
......@@ -69,6 +68,9 @@ class BaseSchema:
self.validation()
for field in self.fields:
setattr(self, field.name, field.value)
def get(dictionary: dict, key: str, return_default=False, default=None):
if dictionary is None:
......@@ -88,7 +90,7 @@ class OTPSchema(BaseSchema):
self.name = Field('name', str, get(data, 'name'))
self.realm = Field('realm', str, get(data, 'realm'))
self.token = Field('token', str, get(data, 'token'))
super().__init__(data=data)
super().__init__()
def validation(self):
if check_otp(self.name.value, self.realm.value, self.token.value) != 200:
......@@ -101,8 +103,7 @@ class CreateImageSchema(BaseSchema):
self.name = Field('name', str, get(data, 'name'))
self.image_store = Field('image_store', str, get(data, 'image_store'),
validators=[self.image_store_name_validation])
super().__init__(data)
super().__init__()
def file_uuid_validation(self):
try:
......@@ -123,7 +124,7 @@ class CreateHostSchema(OTPSchema):
self.specs = Field('specs', dict, get(data, 'specs'), validators=[self.specs_validation])
self.hostname = Field('hostname', str, get(data, 'hostname'))
super().__init__(data=data)
super().__init__(data)
def specs_validation(self):
allowed_base = 10
......@@ -191,7 +192,7 @@ class CreateVMSchema(OTPSchema):
def image_validation(self):
try:
image_uuid = helper.resolve_image_name(self.image.value)
except Exception as e:
except Exception:
raise ValidationException('No image of name \'{}\' found'.format(self.image.value))
else:
self.image_uuid = image_uuid
......@@ -214,7 +215,7 @@ class CreateVMSchema(OTPSchema):
raise ValidationException('Network with name {} does not exists'.format(net))
def specs_validation(self):
ALLOWED_BASE = 10
allowed_base = 10
try:
_cpu = get(self.specs.value, 'cpu')
......@@ -228,10 +229,10 @@ class CreateVMSchema(OTPSchema):
parsed_ram = bitmath.parse_string_unsafe(_ram)
parsed_os_ssd = bitmath.parse_string_unsafe(_os_ssd)
if parsed_ram.base != ALLOWED_BASE:
if parsed_ram.base != allowed_base:
raise ValidationException('Your specified RAM is not in correct units')
if parsed_os_ssd.base != ALLOWED_BASE:
if parsed_os_ssd.base != allowed_base:
raise ValidationException('Your specified OS-SSD is not in correct units')
if int(_cpu) < 1:
......@@ -246,7 +247,7 @@ class CreateVMSchema(OTPSchema):
parsed_hdd = []
for hdd in _hdd:
_parsed_hdd = bitmath.parse_string_unsafe(hdd)
if _parsed_hdd.base != ALLOWED_BASE:
if _parsed_hdd.base != allowed_base:
raise ValidationException('Your specified HDD is not in correct units')
else:
parsed_hdd.append(str(_parsed_hdd))
......
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 to comment