2019-09-22 18:51:33 +00:00
|
|
|
"""
|
|
|
|
This module contain classes thats validates and intercept/modify
|
|
|
|
data coming from ucloud-cli (user)
|
|
|
|
|
|
|
|
It was primarily developed as an alternative to argument parser
|
|
|
|
of Flask_Restful which is going to be deprecated. I also tried
|
|
|
|
marshmallow for that purpose but it was an overkill (because it
|
|
|
|
do validation + serialization + deserialization) and little
|
|
|
|
inflexible for our purpose.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# TODO: Fix error message when user's mentioned VM (referred by name)
|
|
|
|
# does not exists.
|
2019-09-26 12:54:05 +00:00
|
|
|
#
|
2019-09-22 18:51:33 +00:00
|
|
|
# Currently, it says uuid is a required field.
|
|
|
|
|
2019-08-01 10:04:40 +00:00
|
|
|
import json
|
2019-09-12 15:55:25 +00:00
|
|
|
import os
|
2019-08-01 10:04:40 +00:00
|
|
|
|
2019-08-11 17:01:27 +00:00
|
|
|
from ucloud_common.host import HostPool, HostStatus
|
|
|
|
from ucloud_common.vm import VmPool, VMStatus
|
2019-09-12 15:55:25 +00:00
|
|
|
|
|
|
|
from common_fields import Field, VmUUIDField, SpecsField
|
2019-09-22 18:51:33 +00:00
|
|
|
from helper import check_otp, resolve_vm_name
|
2019-09-03 16:01:40 +00:00
|
|
|
from config import etcd_client as client
|
2019-09-22 18:51:33 +00:00
|
|
|
from config import HOST_PREFIX, VM_PREFIX, IMAGE_PREFIX, FILE_PREFIX, IMAGE_STORE_PREFIX
|
2019-08-01 10:04:40 +00:00
|
|
|
|
2019-09-12 15:55:25 +00:00
|
|
|
HOST_POOL = HostPool(client, HOST_PREFIX)
|
|
|
|
VM_POOL = VmPool(client, VM_PREFIX)
|
2019-08-01 10:04:40 +00:00
|
|
|
|
|
|
|
|
2019-09-03 16:01:40 +00:00
|
|
|
class BaseSchema:
|
2019-08-01 10:04:40 +00:00
|
|
|
def __init__(self, data, fields=None):
|
2019-09-12 15:55:25 +00:00
|
|
|
_ = data # suppress linter warning
|
2019-08-01 10:04:40 +00:00
|
|
|
self.__errors = []
|
|
|
|
if fields is None:
|
|
|
|
self.fields = []
|
|
|
|
else:
|
|
|
|
self.fields = fields
|
|
|
|
|
|
|
|
def validation(self):
|
|
|
|
# custom validation is optional
|
|
|
|
return True
|
|
|
|
|
|
|
|
def is_valid(self):
|
|
|
|
for field in self.fields:
|
|
|
|
field.is_valid()
|
|
|
|
self.add_field_errors(field)
|
|
|
|
|
|
|
|
for parent in self.__class__.__bases__:
|
|
|
|
try:
|
|
|
|
parent.validation(self)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
if not self.__errors:
|
|
|
|
self.validation()
|
|
|
|
|
|
|
|
if self.__errors:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def get_errors(self):
|
2019-08-11 17:01:27 +00:00
|
|
|
return {"message": self.__errors}
|
2019-08-01 10:04:40 +00:00
|
|
|
|
|
|
|
def add_field_errors(self, field: Field):
|
|
|
|
self.__errors += field.get_errors()
|
|
|
|
|
|
|
|
def add_error(self, error):
|
|
|
|
self.__errors.append(error)
|
|
|
|
|
|
|
|
|
|
|
|
class OTPSchema(BaseSchema):
|
|
|
|
def __init__(self, data: dict, fields=None):
|
|
|
|
self.name = Field("name", str, data.get("name", KeyError))
|
|
|
|
self.realm = Field("realm", str, data.get("realm", KeyError))
|
|
|
|
self.token = Field("token", str, data.get("token", KeyError))
|
|
|
|
|
|
|
|
_fields = [self.name, self.realm, self.token]
|
|
|
|
if fields:
|
|
|
|
_fields += fields
|
|
|
|
super().__init__(data=data, fields=_fields)
|
|
|
|
|
|
|
|
def validation(self):
|
2019-09-12 15:55:25 +00:00
|
|
|
if check_otp(self.name.value, self.realm.value, self.token.value) != 200:
|
2019-08-01 10:04:40 +00:00
|
|
|
self.add_error("Wrong Credentials")
|
|
|
|
|
|
|
|
|
2019-09-22 18:51:33 +00:00
|
|
|
########################## Image Operations ###############################################
|
2019-08-01 10:04:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CreateImageSchema(BaseSchema):
|
|
|
|
def __init__(self, data):
|
|
|
|
# Fields
|
2019-09-12 15:55:25 +00:00
|
|
|
self.uuid = Field("uuid", str, data.get("uuid", KeyError))
|
2019-08-01 10:04:40 +00:00
|
|
|
self.name = Field("name", str, data.get("name", KeyError))
|
|
|
|
self.image_store = Field("image_store", str, data.get("image_store", KeyError))
|
2019-09-07 10:38:58 +00:00
|
|
|
|
2019-08-01 10:04:40 +00:00
|
|
|
# Validations
|
|
|
|
self.uuid.validation = self.file_uuid_validation
|
2019-09-07 10:38:58 +00:00
|
|
|
self.image_store.validation = self.image_store_name_validation
|
2019-08-01 10:04:40 +00:00
|
|
|
|
|
|
|
# All Fields
|
|
|
|
fields = [self.uuid, self.name, self.image_store]
|
|
|
|
super().__init__(data, fields)
|
|
|
|
|
|
|
|
def file_uuid_validation(self):
|
2019-09-12 15:55:25 +00:00
|
|
|
file_entry = client.get(os.path.join(FILE_PREFIX, self.uuid.value))
|
2019-08-01 10:04:40 +00:00
|
|
|
if file_entry is None:
|
2019-09-22 18:51:33 +00:00
|
|
|
self.add_error(
|
|
|
|
"Image File with uuid '{}' Not Found".format(self.uuid.value)
|
|
|
|
)
|
2019-08-01 10:04:40 +00:00
|
|
|
|
|
|
|
def image_store_name_validation(self):
|
2019-09-12 15:55:25 +00:00
|
|
|
image_stores = list(client.get_prefix(IMAGE_STORE_PREFIX))
|
2019-08-01 10:04:40 +00:00
|
|
|
|
2019-09-22 18:51:33 +00:00
|
|
|
image_store = next(
|
|
|
|
filter(
|
|
|
|
lambda s: json.loads(s.value)["name"] == self.image_store.value,
|
|
|
|
image_stores,
|
|
|
|
),
|
|
|
|
None,
|
|
|
|
)
|
2019-08-01 10:04:40 +00:00
|
|
|
if not image_store:
|
2019-09-12 15:55:25 +00:00
|
|
|
self.add_error("Store '{}' does not exists".format(self.image_store.value))
|
2019-08-01 10:04:40 +00:00
|
|
|
|
|
|
|
|
2019-09-22 18:51:33 +00:00
|
|
|
########################## Host Operations ###############################################
|
|
|
|
|
|
|
|
|
|
|
|
class CreateHostSchema(OTPSchema):
|
|
|
|
def __init__(self, data):
|
|
|
|
self.specs = SpecsField(data)
|
|
|
|
self.hostname = Field("hostname", str, data.get("hostname", KeyError))
|
|
|
|
|
|
|
|
fields = [self.specs, self.hostname]
|
|
|
|
|
|
|
|
super().__init__(data=data, fields=fields)
|
|
|
|
|
|
|
|
def validation(self):
|
|
|
|
if self.realm.value != "ungleich-admin":
|
|
|
|
self.add_error("Invalid Credentials/Insufficient Permission")
|
|
|
|
|
|
|
|
|
|
|
|
########################## VM Operations ###############################################
|
|
|
|
|
|
|
|
|
|
|
|
class CreateVMSchema(OTPSchema):
|
|
|
|
def __init__(self, data):
|
|
|
|
self.specs = SpecsField(data)
|
|
|
|
|
|
|
|
self.vm_name = Field("vm_name", str, data.get("vm_name", KeyError))
|
|
|
|
self.image_uuid = Field("image_uuid", str, data.get("image_uuid", KeyError))
|
|
|
|
|
|
|
|
self.image_uuid.validation = self.image_uuid_validation
|
|
|
|
self.vm_name.validation = self.vm_name_validation
|
|
|
|
|
|
|
|
fields = [self.vm_name, self.specs, self.image_uuid]
|
|
|
|
super().__init__(data=data, fields=fields)
|
|
|
|
|
|
|
|
def image_uuid_validation(self):
|
|
|
|
images = client.get_prefix(IMAGE_PREFIX)
|
|
|
|
|
|
|
|
if self.image_uuid.value not in [i.key.split("/")[-1] for i in images]:
|
|
|
|
self.add_error("Image UUID not valid")
|
|
|
|
|
|
|
|
def vm_name_validation(self):
|
|
|
|
if resolve_vm_name(name=self.vm_name.value, owner=self.name.value):
|
|
|
|
self.add_error(
|
|
|
|
'VM with same name "{}" already exists'.format(self.vm_name.value)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class VMStatusSchema(OTPSchema):
|
|
|
|
def __init__(self, data):
|
|
|
|
data["uuid"] = (
|
|
|
|
resolve_vm_name(
|
|
|
|
name=data.get("vm_name", None),
|
|
|
|
owner=(data.get("in_support_of", None) or data.get("name", None)),
|
|
|
|
)
|
|
|
|
or KeyError
|
|
|
|
)
|
|
|
|
self.uuid = VmUUIDField(data)
|
|
|
|
|
|
|
|
fields = [self.uuid]
|
|
|
|
|
|
|
|
super().__init__(data, fields)
|
|
|
|
|
|
|
|
def validation(self):
|
|
|
|
vm = VM_POOL.get(self.uuid.value)
|
|
|
|
if not (
|
|
|
|
vm.value["owner"] == self.name.value or self.realm.value == "ungleich-admin"
|
|
|
|
):
|
|
|
|
self.add_error("Invalid User")
|
|
|
|
|
|
|
|
|
2019-08-01 10:04:40 +00:00
|
|
|
class VmActionSchema(OTPSchema):
|
|
|
|
def __init__(self, data):
|
2019-09-22 18:51:33 +00:00
|
|
|
data["uuid"] = (
|
|
|
|
resolve_vm_name(
|
|
|
|
name=data.get("vm_name", None),
|
|
|
|
owner=(data.get("in_support_of", None) or data.get("name", None)),
|
|
|
|
)
|
|
|
|
or KeyError
|
|
|
|
)
|
2019-08-01 10:04:40 +00:00
|
|
|
self.uuid = VmUUIDField(data)
|
|
|
|
self.action = Field("action", str, data.get("action", KeyError))
|
|
|
|
|
|
|
|
self.action.validation = self.action_validation
|
|
|
|
|
2019-08-11 17:01:27 +00:00
|
|
|
_fields = [self.uuid, self.action]
|
2019-09-22 18:51:33 +00:00
|
|
|
|
2019-08-11 17:01:27 +00:00
|
|
|
super().__init__(data=data, fields=_fields)
|
2019-08-01 10:04:40 +00:00
|
|
|
|
|
|
|
def action_validation(self):
|
2019-08-11 17:01:27 +00:00
|
|
|
allowed_actions = ["start", "stop", "delete"]
|
2019-08-01 10:04:40 +00:00
|
|
|
if self.action.value not in allowed_actions:
|
2019-09-22 18:51:33 +00:00
|
|
|
self.add_error(
|
|
|
|
"Invalid Action. Allowed Actions are {}".format(allowed_actions)
|
|
|
|
)
|
2019-08-01 10:04:40 +00:00
|
|
|
|
|
|
|
def validation(self):
|
2019-09-12 15:55:25 +00:00
|
|
|
vm = VM_POOL.get(self.uuid.value)
|
2019-09-22 18:51:33 +00:00
|
|
|
if not (
|
|
|
|
vm.value["owner"] == self.name.value or self.realm.value == "ungleich-admin"
|
|
|
|
):
|
2019-08-01 10:04:40 +00:00
|
|
|
self.add_error("Invalid User")
|
|
|
|
|
2019-09-22 18:51:33 +00:00
|
|
|
if (
|
|
|
|
self.action.value == "start"
|
|
|
|
and vm.status == VMStatus.running
|
|
|
|
and vm.hostname != ""
|
|
|
|
):
|
2019-08-11 17:01:27 +00:00
|
|
|
self.add_error("VM Already Running")
|
|
|
|
|
2019-09-22 18:51:33 +00:00
|
|
|
if self.action.value == "stop":
|
|
|
|
if vm.status == VMStatus.stopped:
|
|
|
|
self.add_error("VM Already Stopped")
|
|
|
|
elif vm.status != VMStatus.running:
|
|
|
|
self.add_error("Cannot stop non-running VM")
|
2019-08-11 17:01:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
class VmMigrationSchema(OTPSchema):
|
|
|
|
def __init__(self, data):
|
2019-09-22 18:51:33 +00:00
|
|
|
data["uuid"] = (
|
|
|
|
resolve_vm_name(
|
|
|
|
name=data.get("vm_name", None),
|
|
|
|
owner=(data.get("in_support_of", None) or data.get("name", None)),
|
|
|
|
)
|
|
|
|
or KeyError
|
|
|
|
)
|
|
|
|
|
2019-08-11 17:01:27 +00:00
|
|
|
self.uuid = VmUUIDField(data)
|
|
|
|
self.destination = Field("destination", str, data.get("destination", KeyError))
|
|
|
|
|
|
|
|
self.destination.validation = self.destination_validation
|
|
|
|
|
|
|
|
fields = [self.destination]
|
|
|
|
super().__init__(data=data, fields=fields)
|
|
|
|
|
|
|
|
def destination_validation(self):
|
|
|
|
host_key = self.destination.value
|
2019-09-12 15:55:25 +00:00
|
|
|
host = HOST_POOL.get(host_key)
|
2019-08-11 17:01:27 +00:00
|
|
|
if not host:
|
2019-09-12 15:55:25 +00:00
|
|
|
self.add_error("No Such Host ({}) exists".format(self.destination.value))
|
2019-08-11 17:01:27 +00:00
|
|
|
elif host.status != HostStatus.alive:
|
|
|
|
self.add_error("Destination Host is dead")
|
|
|
|
|
|
|
|
def validation(self):
|
2019-09-12 15:55:25 +00:00
|
|
|
vm = VM_POOL.get(self.uuid.value)
|
2019-09-22 18:51:33 +00:00
|
|
|
if not (
|
|
|
|
vm.value["owner"] == self.name.value or self.realm.value == "ungleich-admin"
|
|
|
|
):
|
2019-08-11 17:01:27 +00:00
|
|
|
self.add_error("Invalid User")
|
|
|
|
|
|
|
|
if vm.status != VMStatus.running:
|
|
|
|
self.add_error("Can't migrate non-running VM")
|
|
|
|
|
2019-09-12 15:55:25 +00:00
|
|
|
if vm.hostname == os.path.join(HOST_PREFIX, self.destination.value):
|
2019-08-11 17:01:27 +00:00
|
|
|
self.add_error("Destination host couldn't be same as Source Host")
|
2019-09-26 12:54:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AddSSHSchema(OTPSchema):
|
|
|
|
def __init__(self, data):
|
|
|
|
self.key_name = Field("key_name", str, data.get("key_name", KeyError))
|
|
|
|
self.key = Field("key", str, data.get("key_name", KeyError))
|
|
|
|
|
|
|
|
fields = [self.key_name, self.key]
|
|
|
|
super().__init__(data=data, fields=fields)
|
|
|
|
|
|
|
|
class RemoveSSHSchema(OTPSchema):
|
|
|
|
def __init__(self, data):
|
|
|
|
self.key_name = Field("key_name", str, data.get("key_name", KeyError))
|
|
|
|
|
|
|
|
fields = [self.key_name]
|
|
|
|
super().__init__(data=data, fields=fields)
|