[api] done -> configparser
This commit is contained in:
parent
adddba518c
commit
76f63633ca
3 changed files with 52 additions and 37 deletions
|
@ -12,7 +12,22 @@ OTP_VERIFY_ENDPOINT = verify/
|
|||
[api]
|
||||
NETWORK_PREFIX = foo
|
||||
|
||||
[woo]
|
||||
NETWORK_PREFIX = foo
|
||||
[network]
|
||||
PREFIX_LENGTH = 64
|
||||
PREFIX = 2001:db8::/48
|
||||
|
||||
[noval]
|
||||
[netbox]
|
||||
NETBOX_URL = https://replace-me.example.com
|
||||
NETBOX_TOKEN = replace me
|
||||
|
||||
[etcd]
|
||||
|
||||
FILE_PREFIX = file/
|
||||
HOST_PREFIx = host/
|
||||
IMAGE_PREFIX = image/
|
||||
IMAGE_STORE_PREFIX = imagestore/
|
||||
|
||||
NETWORK_PREFIX = network/
|
||||
REQUEST_PREFIX = request/
|
||||
USER_PREFIX = user/
|
||||
VM_PREFIX = vm/
|
||||
|
|
|
@ -10,7 +10,7 @@ from flask_restful import Resource, Api
|
|||
from ucloud.common import counters
|
||||
from ucloud.common.vm import VMStatus
|
||||
from ucloud.common.request import RequestEntry, RequestType
|
||||
from ucloud.config import (etcd_client, request_pool, vm_pool, host_pool, env_vars, image_storage_handler)
|
||||
from ucloud.config import (etcd_client, request_pool, vm_pool, host_pool, config, image_storage_handler)
|
||||
from . import schemas
|
||||
from .helper import generate_mac, mac2ipv6
|
||||
from . import logger
|
||||
|
@ -28,7 +28,7 @@ class CreateVM(Resource):
|
|||
validator = schemas.CreateVMSchema(data)
|
||||
if validator.is_valid():
|
||||
vm_uuid = uuid4().hex
|
||||
vm_key = join_path(env_vars.get("VM_PREFIX"), vm_uuid)
|
||||
vm_key = join_path(config['api']["VM_PREFIX"), vm_uuid)
|
||||
specs = {
|
||||
"cpu": validator.specs["cpu"],
|
||||
"ram": validator.specs["ram"],
|
||||
|
@ -56,7 +56,7 @@ class CreateVM(Resource):
|
|||
# Create ScheduleVM Request
|
||||
r = RequestEntry.from_scratch(
|
||||
type=RequestType.ScheduleVM, uuid=vm_uuid,
|
||||
request_prefix=env_vars.get("REQUEST_PREFIX")
|
||||
request_prefix=config['api']["REQUEST_PREFIX")
|
||||
)
|
||||
request_pool.put(r)
|
||||
|
||||
|
@ -71,7 +71,7 @@ class VmStatus(Resource):
|
|||
validator = schemas.VMStatusSchema(data)
|
||||
if validator.is_valid():
|
||||
vm = vm_pool.get(
|
||||
join_path(env_vars.get("VM_PREFIX"), data["uuid"])
|
||||
join_path(config['api']["VM_PREFIX"), data["uuid"])
|
||||
)
|
||||
vm_value = vm.value.copy()
|
||||
vm_value["ip"] = []
|
||||
|
@ -79,7 +79,7 @@ class VmStatus(Resource):
|
|||
network_name, mac, tap = network_mac_and_tap
|
||||
network = etcd_client.get(
|
||||
join_path(
|
||||
env_vars.get("NETWORK_PREFIX"),
|
||||
config['api']["NETWORK_PREFIX"),
|
||||
data["name"],
|
||||
network_name,
|
||||
),
|
||||
|
@ -100,7 +100,7 @@ class CreateImage(Resource):
|
|||
validator = schemas.CreateImageSchema(data)
|
||||
if validator.is_valid():
|
||||
file_entry = etcd_client.get(
|
||||
join_path(env_vars.get("FILE_PREFIX"), data["uuid"])
|
||||
join_path(config['api']["FILE_PREFIX"), data["uuid"])
|
||||
)
|
||||
file_entry_value = json.loads(file_entry.value)
|
||||
|
||||
|
@ -113,7 +113,7 @@ class CreateImage(Resource):
|
|||
"visibility": "public",
|
||||
}
|
||||
etcd_client.put(
|
||||
join_path(env_vars.get("IMAGE_PREFIX"), data["uuid"]),
|
||||
join_path(config['etcd']["IMAGE_PREFIX"), data["uuid"]),
|
||||
json.dumps(image_entry_json),
|
||||
)
|
||||
|
||||
|
@ -125,7 +125,7 @@ class ListPublicImages(Resource):
|
|||
@staticmethod
|
||||
def get():
|
||||
images = etcd_client.get_prefix(
|
||||
env_vars.get("IMAGE_PREFIX"), value_in_json=True
|
||||
config['etcd']["IMAGE_PREFIX"), value_in_json=True
|
||||
)
|
||||
r = {
|
||||
"images": []
|
||||
|
@ -148,7 +148,7 @@ class VMAction(Resource):
|
|||
|
||||
if validator.is_valid():
|
||||
vm_entry = vm_pool.get(
|
||||
join_path(env_vars.get("VM_PREFIX"), data["uuid"])
|
||||
join_path(config['etcd']["VM_PREFIX"), data["uuid"])
|
||||
)
|
||||
action = data["action"]
|
||||
|
||||
|
@ -172,7 +172,7 @@ class VMAction(Resource):
|
|||
type="{}VM".format(action.title()),
|
||||
uuid=data["uuid"],
|
||||
hostname=vm_entry.hostname,
|
||||
request_prefix=env_vars.get("REQUEST_PREFIX")
|
||||
request_prefix=config['etcd']["REQUEST_PREFIX"]
|
||||
)
|
||||
request_pool.put(r)
|
||||
return {"message": "VM {} Queued".format(action.title())}, 200
|
||||
|
@ -193,10 +193,10 @@ class VMMigration(Resource):
|
|||
type=RequestType.ScheduleVM,
|
||||
uuid=vm.uuid,
|
||||
destination=join_path(
|
||||
env_vars.get("HOST_PREFIX"), validator.destination.value
|
||||
config['etcd']["HOST_PREFIX"], validator.destination.value
|
||||
),
|
||||
migration=True,
|
||||
request_prefix=env_vars.get("REQUEST_PREFIX")
|
||||
request_prefix=config['etcd']["REQUEST_PREFIX"]
|
||||
)
|
||||
request_pool.put(r)
|
||||
return {"message": "VM Migration Initialization Queued"}, 200
|
||||
|
@ -212,7 +212,7 @@ class ListUserVM(Resource):
|
|||
|
||||
if validator.is_valid():
|
||||
vms = etcd_client.get_prefix(
|
||||
env_vars.get("VM_PREFIX"), value_in_json=True
|
||||
config['etcd']["VM_PREFIX"], value_in_json=True
|
||||
)
|
||||
return_vms = []
|
||||
user_vms = filter(lambda v: v.value["owner"] == data["name"], vms)
|
||||
|
@ -246,7 +246,7 @@ class ListUserFiles(Resource):
|
|||
|
||||
if validator.is_valid():
|
||||
files = etcd_client.get_prefix(
|
||||
env_vars.get("FILE_PREFIX"), value_in_json=True
|
||||
config['etcd']["FILE_PREFIX"], value_in_json=True
|
||||
)
|
||||
return_files = []
|
||||
user_files = list(
|
||||
|
@ -270,7 +270,7 @@ class CreateHost(Resource):
|
|||
data = request.json
|
||||
validator = schemas.CreateHostSchema(data)
|
||||
if validator.is_valid():
|
||||
host_key = join_path(env_vars.get("HOST_PREFIX"), uuid4().hex)
|
||||
host_key = join_path(config['etcd']["HOST_PREFIX"], uuid4().hex)
|
||||
host_entry = {
|
||||
"specs": data["specs"],
|
||||
"hostname": data["hostname"],
|
||||
|
@ -309,7 +309,7 @@ class GetSSHKeys(Resource):
|
|||
|
||||
# {user_prefix}/{realm}/{name}/key/
|
||||
etcd_key = join_path(
|
||||
env_vars.get('USER_PREFIX'),
|
||||
config['etcd']['USER_PREFIX'],
|
||||
data["realm"],
|
||||
data["name"],
|
||||
"key",
|
||||
|
@ -326,7 +326,7 @@ class GetSSHKeys(Resource):
|
|||
|
||||
# {user_prefix}/{realm}/{name}/key/{key_name}
|
||||
etcd_key = join_path(
|
||||
env_vars.get('USER_PREFIX'),
|
||||
config['etcd']['USER_PREFIX'),
|
||||
data["realm"],
|
||||
data["name"],
|
||||
"key",
|
||||
|
@ -355,7 +355,7 @@ class AddSSHKey(Resource):
|
|||
|
||||
# {user_prefix}/{realm}/{name}/key/{key_name}
|
||||
etcd_key = join_path(
|
||||
env_vars.get("USER_PREFIX"),
|
||||
config['etcd']["USER_PREFIX"],
|
||||
data["realm"],
|
||||
data["name"],
|
||||
"key",
|
||||
|
@ -385,7 +385,7 @@ class RemoveSSHKey(Resource):
|
|||
|
||||
# {user_prefix}/{realm}/{name}/key/{key_name}
|
||||
etcd_key = join_path(
|
||||
env_vars.get("USER_PREFIX"),
|
||||
config['etcd']["USER_PREFIX"],
|
||||
data["realm"],
|
||||
data["name"],
|
||||
"key",
|
||||
|
@ -421,17 +421,17 @@ class CreateNetwork(Resource):
|
|||
}
|
||||
if validator.user.value:
|
||||
nb = pynetbox.api(
|
||||
url=env_vars.get("NETBOX_URL"),
|
||||
token=env_vars.get("NETBOX_TOKEN"),
|
||||
url=config['netbox']["NETBOX_URL"],
|
||||
token=config['netbox']["NETBOX_TOKEN"],
|
||||
)
|
||||
nb_prefix = nb.ipam.prefixes.get(
|
||||
prefix=env_vars.get("PREFIX")
|
||||
prefix=config['network']["PREFIX"]
|
||||
)
|
||||
|
||||
prefix = nb_prefix.available_prefixes.create(
|
||||
data={
|
||||
"prefix_length": env_vars.get(
|
||||
"PREFIX_LENGTH", cast=int
|
||||
"prefix_length": config['network'][
|
||||
"PREFIX_LENGTH"]
|
||||
),
|
||||
"description": '{}\'s network "{}"'.format(
|
||||
data["name"], data["network_name"]
|
||||
|
@ -444,7 +444,7 @@ class CreateNetwork(Resource):
|
|||
network_entry["ipv6"] = "fd00::/64"
|
||||
|
||||
network_key = join_path(
|
||||
env_vars.get("NETWORK_PREFIX"),
|
||||
config['network']["NETWORK_PREFIX"],
|
||||
data["name"],
|
||||
data["network_name"],
|
||||
)
|
||||
|
@ -462,7 +462,7 @@ class ListUserNetwork(Resource):
|
|||
|
||||
if validator.is_valid():
|
||||
prefix = join_path(
|
||||
env_vars.get("NETWORK_PREFIX"), data["name"]
|
||||
config['network']["NETWORK_PREFIX"], data["name"]
|
||||
)
|
||||
networks = etcd_client.get_prefix(prefix, value_in_json=True)
|
||||
user_networks = []
|
||||
|
@ -498,7 +498,7 @@ api.add_resource(CreateNetwork, "/network/create")
|
|||
|
||||
|
||||
def main():
|
||||
image_stores = list(etcd_client.get_prefix(env_vars.get('IMAGE_STORE_PREFIX'), value_in_json=True))
|
||||
image_stores = list(etcd_client.get_prefix(config['etcd']['IMAGE_STORE_PREFIX'], value_in_json=True))
|
||||
if len(image_stores) == 0:
|
||||
data = {
|
||||
"is_public": True,
|
||||
|
@ -508,7 +508,7 @@ def main():
|
|||
"attributes": {"list": [], "key": [], "pool": "images"},
|
||||
}
|
||||
|
||||
etcd_client.put(join_path(env_vars.get('IMAGE_STORE_PREFIX'), uuid4().hex), json.dumps(data))
|
||||
etcd_client.put(join_path(config['etcd']['IMAGE_STORE_PREFIX'], uuid4().hex), json.dumps(data))
|
||||
|
||||
app.run(host="::", debug=True)
|
||||
|
||||
|
|
|
@ -21,7 +21,7 @@ import bitmath
|
|||
|
||||
from ucloud.common.host import HostStatus
|
||||
from ucloud.common.vm import VMStatus
|
||||
from ucloud.config import etcd_client, env_vars, vm_pool, host_pool
|
||||
from ucloud.config import etcd_client, config, vm_pool, host_pool
|
||||
from . import helper
|
||||
from .common_fields import Field, VmUUIDField
|
||||
from .helper import check_otp, resolve_vm_name
|
||||
|
@ -102,14 +102,14 @@ class CreateImageSchema(BaseSchema):
|
|||
super().__init__(data, fields)
|
||||
|
||||
def file_uuid_validation(self):
|
||||
file_entry = etcd_client.get(os.path.join(env_vars.get('FILE_PREFIX'), self.uuid.value))
|
||||
file_entry = etcd_client.get(os.path.join(config['etcd']['FILE_PREFIX'], self.uuid.value))
|
||||
if file_entry is None:
|
||||
self.add_error(
|
||||
"Image File with uuid '{}' Not Found".format(self.uuid.value)
|
||||
)
|
||||
|
||||
def image_store_name_validation(self):
|
||||
image_stores = list(etcd_client.get_prefix(env_vars.get('IMAGE_STORE_PREFIX')))
|
||||
image_stores = list(etcd_client.get_prefix(config['etcd']['IMAGE_STORE_PREFIX']))
|
||||
|
||||
image_store = next(
|
||||
filter(
|
||||
|
@ -235,7 +235,7 @@ class CreateVMSchema(OTPSchema):
|
|||
|
||||
if _network:
|
||||
for net in _network:
|
||||
network = etcd_client.get(os.path.join(env_vars.get('NETWORK_PREFIX'),
|
||||
network = etcd_client.get(os.path.join(config['etcd']['NETWORK_PREFIX'],
|
||||
self.name.value,
|
||||
net), value_in_json=True)
|
||||
if not network:
|
||||
|
@ -400,7 +400,7 @@ class VmMigrationSchema(OTPSchema):
|
|||
if vm.status != VMStatus.running:
|
||||
self.add_error("Can't migrate non-running VM")
|
||||
|
||||
if vm.hostname == os.path.join(env_vars.get('HOST_PREFIX'), self.destination.value):
|
||||
if vm.hostname == os.path.join(config['etcd']['HOST_PREFIX'], self.destination.value):
|
||||
self.add_error("Destination host couldn't be same as Source Host")
|
||||
|
||||
|
||||
|
@ -442,7 +442,7 @@ class CreateNetwork(OTPSchema):
|
|||
super().__init__(data, fields=fields)
|
||||
|
||||
def network_name_validation(self):
|
||||
network = etcd_client.get(os.path.join(env_vars.get('NETWORK_PREFIX'),
|
||||
network = etcd_client.get(os.path.join(config['etcd']['NETWORK_PREFIX'],
|
||||
self.name.value,
|
||||
self.network_name.value),
|
||||
value_in_json=True)
|
||||
|
|
Loading…
Reference in a new issue