Fix issues in naming and few other things
This commit is contained in:
parent
f919719b1e
commit
71279a968f
21 changed files with 274 additions and 281 deletions
|
|
@ -10,7 +10,10 @@ 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, config, 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 +31,7 @@ class CreateVM(Resource):
|
|||
validator = schemas.CreateVMSchema(data)
|
||||
if validator.is_valid():
|
||||
vm_uuid = uuid4().hex
|
||||
vm_key = join_path(config['etcd']["VM_PREFIX"], vm_uuid)
|
||||
vm_key = join_path(config['etcd']['vm_prefix'], vm_uuid)
|
||||
specs = {
|
||||
"cpu": validator.specs["cpu"],
|
||||
"ram": validator.specs["ram"],
|
||||
|
|
@ -56,7 +59,7 @@ class CreateVM(Resource):
|
|||
# Create ScheduleVM Request
|
||||
r = RequestEntry.from_scratch(
|
||||
type=RequestType.ScheduleVM, uuid=vm_uuid,
|
||||
request_prefix=config['etcd']["REQUEST_PREFIX"]
|
||||
request_prefix=config['etcd']['request_prefix']
|
||||
)
|
||||
request_pool.put(r)
|
||||
|
||||
|
|
@ -71,7 +74,7 @@ class VmStatus(Resource):
|
|||
validator = schemas.VMStatusSchema(data)
|
||||
if validator.is_valid():
|
||||
vm = vm_pool.get(
|
||||
join_path(config['etcd']["VM_PREFIX"], data["uuid"])
|
||||
join_path(config['etcd']['vm_prefix'], data["uuid"])
|
||||
)
|
||||
vm_value = vm.value.copy()
|
||||
vm_value["ip"] = []
|
||||
|
|
@ -79,7 +82,7 @@ class VmStatus(Resource):
|
|||
network_name, mac, tap = network_mac_and_tap
|
||||
network = etcd_client.get(
|
||||
join_path(
|
||||
config['etcd']["NETWORK_PREFIX"],
|
||||
config['etcd']['network_prefix'],
|
||||
data["name"],
|
||||
network_name,
|
||||
),
|
||||
|
|
@ -100,7 +103,7 @@ class CreateImage(Resource):
|
|||
validator = schemas.CreateImageSchema(data)
|
||||
if validator.is_valid():
|
||||
file_entry = etcd_client.get(
|
||||
join_path(config['etcd']["FILE_PREFIX"], data["uuid"])
|
||||
join_path(config['etcd']['file_prefix'], data["uuid"])
|
||||
)
|
||||
file_entry_value = json.loads(file_entry.value)
|
||||
|
||||
|
|
@ -113,7 +116,7 @@ class CreateImage(Resource):
|
|||
"visibility": "public",
|
||||
}
|
||||
etcd_client.put(
|
||||
join_path(config['etcd']["IMAGE_PREFIX"], data["uuid"]),
|
||||
join_path(config['etcd']['image_prefix'], data["uuid"]),
|
||||
json.dumps(image_entry_json),
|
||||
)
|
||||
|
||||
|
|
@ -125,7 +128,7 @@ class ListPublicImages(Resource):
|
|||
@staticmethod
|
||||
def get():
|
||||
images = etcd_client.get_prefix(
|
||||
config['etcd']["IMAGE_PREFIX"], value_in_json=True
|
||||
config['etcd']['image_prefix'], value_in_json=True
|
||||
)
|
||||
r = {
|
||||
"images": []
|
||||
|
|
@ -148,7 +151,7 @@ class VMAction(Resource):
|
|||
|
||||
if validator.is_valid():
|
||||
vm_entry = vm_pool.get(
|
||||
join_path(config['etcd']["VM_PREFIX"], data["uuid"])
|
||||
join_path(config['etcd']['vm_prefix'], data["uuid"])
|
||||
)
|
||||
action = data["action"]
|
||||
|
||||
|
|
@ -172,7 +175,7 @@ class VMAction(Resource):
|
|||
type="{}VM".format(action.title()),
|
||||
uuid=data["uuid"],
|
||||
hostname=vm_entry.hostname,
|
||||
request_prefix=config['etcd']["REQUEST_PREFIX"]
|
||||
request_prefix=config['etcd']['request_prefix']
|
||||
)
|
||||
request_pool.put(r)
|
||||
return {"message": "VM {} Queued".format(action.title())}, 200
|
||||
|
|
@ -193,10 +196,10 @@ class VMMigration(Resource):
|
|||
type=RequestType.ScheduleVM,
|
||||
uuid=vm.uuid,
|
||||
destination=join_path(
|
||||
config['etcd']["HOST_PREFIX"], validator.destination.value
|
||||
config['etcd']['host_prefix'], validator.destination.value
|
||||
),
|
||||
migration=True,
|
||||
request_prefix=config['etcd']["REQUEST_PREFIX"]
|
||||
request_prefix=config['etcd']['request_prefix']
|
||||
)
|
||||
request_pool.put(r)
|
||||
return {"message": "VM Migration Initialization Queued"}, 200
|
||||
|
|
@ -212,7 +215,7 @@ class ListUserVM(Resource):
|
|||
|
||||
if validator.is_valid():
|
||||
vms = etcd_client.get_prefix(
|
||||
config['etcd']["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 +249,7 @@ class ListUserFiles(Resource):
|
|||
|
||||
if validator.is_valid():
|
||||
files = etcd_client.get_prefix(
|
||||
config['etcd']["FILE_PREFIX"], value_in_json=True
|
||||
config['etcd']['file_prefix'], value_in_json=True
|
||||
)
|
||||
return_files = []
|
||||
user_files = list(
|
||||
|
|
@ -270,7 +273,7 @@ class CreateHost(Resource):
|
|||
data = request.json
|
||||
validator = schemas.CreateHostSchema(data)
|
||||
if validator.is_valid():
|
||||
host_key = join_path(config['etcd']["HOST_PREFIX"], uuid4().hex)
|
||||
host_key = join_path(config['etcd']['host_prefix'], uuid4().hex)
|
||||
host_entry = {
|
||||
"specs": data["specs"],
|
||||
"hostname": data["hostname"],
|
||||
|
|
@ -309,7 +312,7 @@ class GetSSHKeys(Resource):
|
|||
|
||||
# {user_prefix}/{realm}/{name}/key/
|
||||
etcd_key = join_path(
|
||||
config['etcd']['USER_PREFIX'],
|
||||
config['etcd']['user_prefix'],
|
||||
data["realm"],
|
||||
data["name"],
|
||||
"key",
|
||||
|
|
@ -326,7 +329,7 @@ class GetSSHKeys(Resource):
|
|||
|
||||
# {user_prefix}/{realm}/{name}/key/{key_name}
|
||||
etcd_key = join_path(
|
||||
config['etcd']['USER_PREFIX'],
|
||||
config['etcd']['user_prefix'],
|
||||
data["realm"],
|
||||
data["name"],
|
||||
"key",
|
||||
|
|
@ -355,7 +358,7 @@ class AddSSHKey(Resource):
|
|||
|
||||
# {user_prefix}/{realm}/{name}/key/{key_name}
|
||||
etcd_key = join_path(
|
||||
config['etcd']["USER_PREFIX"],
|
||||
config['etcd']['user_prefix'],
|
||||
data["realm"],
|
||||
data["name"],
|
||||
"key",
|
||||
|
|
@ -385,7 +388,7 @@ class RemoveSSHKey(Resource):
|
|||
|
||||
# {user_prefix}/{realm}/{name}/key/{key_name}
|
||||
etcd_key = join_path(
|
||||
config['etcd']["USER_PREFIX"],
|
||||
config['etcd']['user_prefix'],
|
||||
data["realm"],
|
||||
data["name"],
|
||||
"key",
|
||||
|
|
@ -420,31 +423,35 @@ class CreateNetwork(Resource):
|
|||
"type": data["type"],
|
||||
}
|
||||
if validator.user.value:
|
||||
nb = pynetbox.api(
|
||||
url=config['netbox']["NETBOX_URL"],
|
||||
token=config['netbox']["NETBOX_TOKEN"],
|
||||
)
|
||||
nb_prefix = nb.ipam.prefixes.get(
|
||||
prefix=config['network']["PREFIX"]
|
||||
)
|
||||
|
||||
prefix = nb_prefix.available_prefixes.create(
|
||||
data={
|
||||
"prefix_length": config['network']["PREFIX_LENGTH"],
|
||||
"description": '{}\'s network "{}"'.format(
|
||||
data["name"], data["network_name"]
|
||||
),
|
||||
"is_pool": True,
|
||||
}
|
||||
)
|
||||
network_entry["ipv6"] = prefix["prefix"]
|
||||
try:
|
||||
nb = pynetbox.api(
|
||||
url=config['netbox']['url'],
|
||||
token=config['netbox']['token'],
|
||||
)
|
||||
nb_prefix = nb.ipam.prefixes.get(
|
||||
prefix=config['network']['prefix']
|
||||
)
|
||||
prefix = nb_prefix.available_prefixes.create(
|
||||
data={
|
||||
"prefix_length": int(config['network']['prefix_length']),
|
||||
"description": '{}\'s network "{}"'.format(
|
||||
data["name"], data["network_name"]
|
||||
),
|
||||
"is_pool": True,
|
||||
}
|
||||
)
|
||||
except Exception:
|
||||
logger.exception("Exception occur while contacting netbox")
|
||||
return {"message": "Error occured while creating network."}
|
||||
else:
|
||||
network_entry["ipv6"] = prefix["prefix"]
|
||||
else:
|
||||
network_entry["ipv6"] = "fd00::/64"
|
||||
|
||||
network_key = join_path(
|
||||
config['network']["NETWORK_PREFIX"],
|
||||
data["name"],
|
||||
data["network_name"],
|
||||
config['etcd']['network_prefix'],
|
||||
data['name'],
|
||||
data['network_name'],
|
||||
)
|
||||
etcd_client.put(network_key, network_entry, value_in_json=True)
|
||||
return {"message": "Network successfully added."}
|
||||
|
|
@ -460,7 +467,7 @@ class ListUserNetwork(Resource):
|
|||
|
||||
if validator.is_valid():
|
||||
prefix = join_path(
|
||||
config['network']["NETWORK_PREFIX"], data["name"]
|
||||
config['etcd']['network_prefix'], data["name"]
|
||||
)
|
||||
networks = etcd_client.get_prefix(prefix, value_in_json=True)
|
||||
user_networks = []
|
||||
|
|
@ -496,7 +503,7 @@ api.add_resource(CreateNetwork, "/network/create")
|
|||
|
||||
|
||||
def main():
|
||||
image_stores = list(etcd_client.get_prefix(config['etcd']['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,
|
||||
|
|
@ -506,7 +513,7 @@ def main():
|
|||
"attributes": {"list": [], "key": [], "pool": "images"},
|
||||
}
|
||||
|
||||
etcd_client.put(join_path(config['etcd']['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)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue