64 lines
1.7 KiB
Python
64 lines
1.7 KiB
Python
from etcd3_wrapper import Etcd3Wrapper
|
|
from specs_parser import SpecsParser
|
|
|
|
specs_parser = SpecsParser(exceptional_devices=["cpu"])
|
|
|
|
|
|
class Field(object):
|
|
def __init__(self, _name, _type, _value=None):
|
|
self.name = _name
|
|
self.value = _value
|
|
self.type = _type
|
|
self.__errors = []
|
|
|
|
def validation(self):
|
|
return True
|
|
|
|
def is_valid(self):
|
|
if self.value == KeyError:
|
|
self.add_error(f"'{self.name}' field is a required field")
|
|
else:
|
|
if not isinstance(self.value, self.type):
|
|
self.add_error(f"Incorrect Type for '{self.name}' field")
|
|
else:
|
|
self.validation()
|
|
|
|
if self.__errors:
|
|
return False
|
|
return True
|
|
|
|
def get_errors(self):
|
|
return self.__errors
|
|
|
|
def add_error(self, error):
|
|
self.__errors.append(error)
|
|
|
|
|
|
class VmUUIDField(Field):
|
|
def __init__(self, data):
|
|
self.uuid = data.get("uuid", KeyError)
|
|
|
|
super().__init__("uuid", str, self.uuid)
|
|
|
|
self.validation = self.vm_uuid_validation
|
|
|
|
def vm_uuid_validation(self):
|
|
client = Etcd3Wrapper()
|
|
|
|
r = client.get(f"/v1/vm/{self.uuid}")
|
|
if not r:
|
|
self.add_error(f"VM with uuid {self.uuid} does not exists")
|
|
|
|
|
|
class SpecsField(Field):
|
|
def __init__(self, data):
|
|
self.specs = data.get("specs", KeyError)
|
|
|
|
super().__init__("specs", dict, self.specs)
|
|
|
|
self.validation = self.specs_validation
|
|
|
|
def specs_validation(self):
|
|
if not specs_parser.transform_specs(self.specs):
|
|
self.add_error("Invalid unit - "
|
|
f"Please use following units {specs_parser.get_allowed_units()}")
|