This commit is contained in:
ahmadbilalkhalid 2019-09-03 20:44:57 +05:00
parent 8ca850a6fe
commit a722e21bc4
2 changed files with 50 additions and 1 deletions

24
main.py
View file

@ -17,7 +17,7 @@ from ucloud_common.request import RequestEntry, RequestPool, RequestType
from schemas import (CreateVMSchema, VMStatusSchema, from schemas import (CreateVMSchema, VMStatusSchema,
CreateImageSchema, VmActionSchema, CreateImageSchema, VmActionSchema,
OTPSchema, CreateHostSchema, OTPSchema, CreateHostSchema,
VmMigrationSchema) VmMigrationSchema, CreateNetworkSchema)
app = Flask(__name__) app = Flask(__name__)
api = Api(app) api = Api(app)
@ -142,6 +142,26 @@ class VMAction(Resource):
return validator.get_errors(), 400 return validator.get_errors(), 400
class CreateNetwork(Resource):
@staticmethod
def post():
data = request.json
validator = CreateNetwork(data)
if validator.is_valid():
network_entry_json = {
"name": data["network_name"],
"type": data["network_type"],
"first_mac_address": data["first_mac_address"],
"network_size": data["network_size"],
"network_address": data["network_address"],
"dns": data["dns"]
}
client.put(f"/v1/network/{uuid4()}", json.dumps(network_entry_json))
else:
return validator.get_errors(), 400
class VMMigration(Resource): class VMMigration(Resource):
@staticmethod @staticmethod
def post(): def post():
@ -247,6 +267,8 @@ api.add_resource(VmStatus, "/vm/status")
api.add_resource(VMAction, "/vm/action") api.add_resource(VMAction, "/vm/action")
api.add_resource(VMMigration, "/vm/migrate") api.add_resource(VMMigration, "/vm/migrate")
api.add_resource(CreateNetwork, "/network/create")
api.add_resource(CreateImage, "/image/create") api.add_resource(CreateImage, "/image/create")
api.add_resource(ListPublicImages, "/image/list-public") api.add_resource(ListPublicImages, "/image/list-public")

View file

@ -86,6 +86,33 @@ class CreateVMSchema(OTPSchema):
self.add_error("Image UUID not valid") self.add_error("Image UUID not valid")
class CreateNetworkSchema(OTPSchema):
def __init__(self, data):
self.network_name = Field("network_name", str, data.get("network_name", KeyError))
self.network_type = Field("network_type", str, data.get("network_type", KeyError))
self.first_mac_address = Field("first_mac_address", str, data.get("first_mac_address", KeyError))
self.network_size = Field("network_size", str, data.get("network_size", KeyError))
self.network_address = Field("network_address", str, data.get("network_address", KeyError))
self.dns = Field("dns", str, data.get("dns", KeyError))
self.network_type.validation = self.network_type_validation
self.network_size.validation = self.network_size.validation
fields = [self.network_name, self.network_type, self.first_mac_address,
self.network_size, self.network_address, self.dns]
if fields:
fields += fields
super().__init__(data=data, fields=fields)
def network_type_validation(self):
if self.network_type.value not in ["bridged", "vxlan"]:
self.add_error(f"{self.network_type.value} not supported.")
def network_size_validation(self):
if self.network_size.value <= 0:
self.add_error(f"Network Size must be a positive number")
class VMStatusSchema(BaseSchema): class VMStatusSchema(BaseSchema):
def __init__(self, data): def __init__(self, data):
self.uuid = VmUUIDField(data) self.uuid = VmUUIDField(data)