39 lines
1.1 KiB
Python
39 lines
1.1 KiB
Python
|
import json
|
||
|
from etcd3_wrapper import Etcd3Wrapper, EtcdEntry, PsuedoEtcdEntry
|
||
|
from uuid import uuid4
|
||
|
from .helpers import SpecificEtcdEntryBase
|
||
|
from os.path import join
|
||
|
|
||
|
|
||
|
class RequestType(object):
|
||
|
CreateVM = "CreateVM"
|
||
|
ScheduleVM = "ScheduleVM"
|
||
|
StartVM = "StartVM"
|
||
|
StopVM = "StopVM"
|
||
|
InitVMMigration = "InitVMMigration"
|
||
|
TransferVM = "TransferVM"
|
||
|
DeleteVM = "DeleteVM"
|
||
|
|
||
|
|
||
|
class RequestEntry(SpecificEtcdEntryBase):
|
||
|
def __init__(self, e: EtcdEntry):
|
||
|
self.type = ""
|
||
|
super().__init__(e)
|
||
|
|
||
|
@classmethod
|
||
|
def from_scratch(cls, **kwargs):
|
||
|
e = PsuedoEtcdEntry(join("/v1/request/", uuid4().hex), value=json.dumps(kwargs).encode("utf-8"),
|
||
|
value_in_json=True)
|
||
|
return cls(e)
|
||
|
|
||
|
|
||
|
class RequestPool(object):
|
||
|
def __init__(self, etcd_client, request_prefix):
|
||
|
self.client = etcd_client
|
||
|
self.prefix = request_prefix
|
||
|
|
||
|
def put(self, obj: RequestEntry):
|
||
|
if not obj.key.startswith(self.prefix):
|
||
|
obj.key = join(self.prefix, obj.key)
|
||
|
|
||
|
self.client.put(obj.key, obj.value, value_in_json=True)
|