2019-10-25 06:42:40 +00:00
|
|
|
import os
|
|
|
|
|
2020-01-08 19:40:05 +00:00
|
|
|
from uncloud.common.shared import shared
|
2019-12-22 07:26:48 +00:00
|
|
|
|
2019-10-25 06:42:40 +00:00
|
|
|
|
2019-11-11 18:42:57 +00:00
|
|
|
class Optional:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2019-10-25 06:42:40 +00:00
|
|
|
class Field:
|
|
|
|
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:
|
2019-12-30 09:35:07 +00:00
|
|
|
self.add_error(
|
|
|
|
"'{}' field is a required field".format(self.name)
|
|
|
|
)
|
2019-10-25 06:42:40 +00:00
|
|
|
else:
|
2019-11-11 18:42:57 +00:00
|
|
|
if isinstance(self.value, Optional):
|
|
|
|
pass
|
|
|
|
elif not isinstance(self.value, self.type):
|
2019-12-30 09:35:07 +00:00
|
|
|
self.add_error(
|
|
|
|
"Incorrect Type for '{}' field".format(self.name)
|
|
|
|
)
|
2019-10-25 06:42:40 +00:00
|
|
|
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):
|
2019-12-30 09:35:07 +00:00
|
|
|
r = shared.etcd_client.get(
|
2020-01-13 04:57:41 +00:00
|
|
|
os.path.join(shared.settings["etcd"]["vm_prefix"], self.uuid)
|
2019-12-30 09:35:07 +00:00
|
|
|
)
|
2019-10-25 06:42:40 +00:00
|
|
|
if not r:
|
2020-01-13 04:57:41 +00:00
|
|
|
self.add_error("VM with uuid {} does not exists".format(self.uuid))
|