ucloud_common/ucloud_common/request.py

49 lines
1.3 KiB
Python
Raw Permalink Normal View History

import json
2019-09-11 13:01:15 +00:00
from decouple import config
2019-09-14 15:26:44 +00:00
from etcd3_wrapper.etcd3_wrapper import PsuedoEtcdEntry
from uuid import uuid4
2019-09-14 15:26:44 +00:00
from .helpers import SpecificEtcdEntryBase
from os.path import join
2019-09-11 13:01:15 +00:00
class RequestType:
CreateVM = "CreateVM"
ScheduleVM = "ScheduleVM"
StartVM = "StartVM"
StopVM = "StopVM"
InitVMMigration = "InitVMMigration"
TransferVM = "TransferVM"
DeleteVM = "DeleteVM"
class RequestEntry(SpecificEtcdEntryBase):
2019-09-14 15:26:44 +00:00
def __init__(self, e):
self.type = None # type: str
self.migration = None # type: bool
self.destination = None # type: str
self.uuid = None # type: str
self.hostname = None # type: str
super().__init__(e)
@classmethod
def from_scratch(cls, **kwargs):
2019-09-11 13:01:15 +00:00
e = PsuedoEtcdEntry(join(config("REQUEST_PREFIX"), uuid4().hex), value=json.dumps(kwargs).encode("utf-8"),
value_in_json=True)
return cls(e)
2019-09-11 13:01:15 +00:00
class RequestPool:
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)
2019-09-14 15:26:44 +00:00
self.client.put(obj.key, obj.value, value_in_json=True)