2nd commit

This commit is contained in:
ahmadbilalkhalid 2020-01-10 00:33:35 +05:00
parent e5dd5e45c6
commit f1bb1ee3ca
2 changed files with 64 additions and 63 deletions

View File

@ -33,16 +33,16 @@ arg_parser = argparse.ArgumentParser('api', add_help=False)
arg_parser.add_argument('--port', '-p')
@app.errorhandler(Exception)
def handle_exception(e):
app.logger.error(e)
# pass through HTTP errors
if isinstance(e, HTTPException):
return e
# now you're handling non-HTTP exceptions only
return {'message': 'Server Error'}, 500
# @app.errorhandler(Exception)
# def handle_exception(e):
# app.logger.error(e)
#
# # pass through HTTP errors
# if isinstance(e, HTTPException):
# return e
#
# # now you're handling non-HTTP exceptions only
# return {'message': 'Server Error'}, 500
class CreateVM(Resource):
@ -63,22 +63,22 @@ class CreateVM(Resource):
'os-ssd': validator.specs['os-ssd'],
'hdd': validator.specs['hdd'],
}
macs = [generate_mac() for _ in range(len(validator.network.value))]
macs = [generate_mac() for _ in range(len(validator.network))]
tap_ids = [
counters.increment_etcd_counter(settings['etcd']['tap_counter'])
for _ in range(len(validator.network.value))
for _ in range(len(validator.network))
]
vm_entry = {
'name': validator.vm_name.value,
'owner': validator.name.value,
'owner_realm': validator.realm.value,
'name': validator.vm_name,
'owner': validator.name,
'owner_realm': validator.realm,
'specs': specs,
'hostname': '',
'status': VMStatus.stopped,
'image_uuid': validator.image_uuid,
'log': [],
'vnc_socket': '',
'network': list(zip(validator.network.value, macs, tap_ids)),
'network': list(zip(validator.network, macs, tap_ids)),
'metadata': {'ssh-keys': []},
'in_migration': False,
}
@ -94,7 +94,7 @@ class CreateVM(Resource):
return make_return_message('VM Creation Queued')
class VmStatus(Resource):
class GetVMStatus(Resource):
@staticmethod
def post():
data = request.json
@ -104,13 +104,13 @@ class VmStatus(Resource):
except (ValidationException, Exception) as err:
return make_return_message(err, 400)
else:
vm = shared.vm_pool.get(join_path(settings['etcd']['vm_prefix'], data['uuid']))
vm = shared.vm_pool.get(join_path(settings['etcd']['vm_prefix'], validator.uuid))
vm_value = vm.value.copy()
vm_value['ip'] = []
for network_mac_and_tap in vm.network:
network_name, mac, tap = network_mac_and_tap
network = shared.etcd_client.get(
join_path(settings['etcd']['network_prefix'], data['name'], network_name),
join_path(settings['etcd']['network_prefix'], validator.name, network_name),
value_in_json=True,
)
ipv6_addr = (network.value.get('ipv6').split('::')[0] + '::')
@ -131,7 +131,7 @@ class CreateImage(Resource):
else:
try:
file_entry = shared.etcd_client.get(
join_path(settings['etcd']['file_prefix'], data['uuid']), value_in_json=True
join_path(settings['etcd']['file_prefix'], validator.uuid), value_in_json=True
)
except KeyError:
# TODO: Add some message
@ -146,7 +146,7 @@ class CreateImage(Resource):
'visibility': 'public',
}
shared.etcd_client.put(
join_path(settings['etcd']['image_prefix'], data['uuid']),
join_path(settings['etcd']['image_prefix'], validator.uuid),
json.dumps(image_entry_json),
)
@ -174,8 +174,8 @@ class VMAction(Resource):
except ValidationException as err:
return make_return_message(err, 400)
else:
vm_entry = shared.vm_pool.get(join_path(settings['etcd']['vm_prefix'], data['uuid']))
action = validator.action.value
vm_entry = shared.vm_pool.get(join_path(settings['etcd']['vm_prefix'], validator.uuid))
action = validator.action
if action == 'start':
action = 'schedule'
@ -195,7 +195,7 @@ class VMAction(Resource):
r = RequestEntry.from_scratch(
type='{}VM'.format(action.title()),
uuid=data['uuid'],
uuid=validator.uuid,
hostname=vm_entry.hostname,
request_prefix=settings['etcd']['request_prefix'],
)
@ -213,13 +213,13 @@ class VMMigration(Resource):
except ValidationException as err:
return make_return_message(err), 400
else:
vm = shared.vm_pool.get(validator.uuid.value)
vm = shared.vm_pool.get(validator.uuid)
r = RequestEntry.from_scratch(
type=RequestType.InitVMMigration,
uuid=vm.uuid,
hostname=join_path(
settings['etcd']['host_prefix'],
validator.destination.value,
validator.destination,
),
request_prefix=settings['etcd']['request_prefix'],
)
@ -240,7 +240,7 @@ class ListUserVM(Resource):
else:
vms = shared.etcd_client.get_prefix(settings['etcd']['vm_prefix'], value_in_json=True)
return_vms = []
user_vms = filter(lambda v: v.value['owner'] == validator.name.value, vms)
user_vms = filter(lambda v: v.value['owner'] == validator.name, vms)
for vm in user_vms:
return_vms.append(
{
@ -267,7 +267,7 @@ class ListUserFiles(Resource):
else:
files = shared.etcd_client.get_prefix(settings['etcd']['file_prefix'], value_in_json=True)
return_files = []
user_files = [f for f in files if f.value['owner'] == data['name']]
user_files = [f for f in files if f.value['owner'] == validator.name]
for file in user_files:
file_uuid = file.key.split('/')[-1]
file = file.value
@ -292,8 +292,8 @@ class CreateHost(Resource):
else:
host_key = join_path(settings['etcd']['host_prefix'], uuid4().hex)
host_entry = {
'specs': validator.specs.value,
'hostname': validator.hostname.value,
'specs': validator.specs,
'hostname': validator.hostname,
'status': HostStatus.dead,
'last_heartbeat': '',
}
@ -325,9 +325,9 @@ class GetSSHKeys(Resource):
except ValidationException as err:
return make_return_message(err, 400)
else:
etcd_key = join_path(settings['etcd']['user_prefix'], validator.realm.value,
validator.name.value, 'key')
if not validator.key_name.value:
etcd_key = join_path(settings['etcd']['user_prefix'], validator.realm,
validator.name, 'key')
if not validator.key_name:
etcd_entry = shared.etcd_client.get_prefix(etcd_key, value_in_json=True)
keys = {
key.key.split('/')[-1]: key.value
@ -335,7 +335,7 @@ class GetSSHKeys(Resource):
}
return {'keys': keys}
else:
etcd_key = join_path(validator.key_name.value)
etcd_key = join_path(validator.key_name)
try:
etcd_entry = shared.etcd_client.get(etcd_key, value_in_json=True)
except KeyError:
@ -358,17 +358,17 @@ class AddSSHKey(Resource):
else:
# {user_prefix}/{realm}/{name}/key/{key_name}
etcd_key = join_path(
settings['etcd']['user_prefix'], validator.realm.value,
validator.name.value, 'key', validator.key_name.value
settings['etcd']['user_prefix'], validator.realm,
validator.name, 'key', validator.key_name
)
try:
shared.etcd_client.get(etcd_key, value_in_json=True)
except KeyError:
# Key Not Found. It implies user' haven't added any key yet.
shared.etcd_client.put(etcd_key, validator.key.value, value_in_json=True)
shared.etcd_client.put(etcd_key, validator.key, value_in_json=True)
return make_return_message('Key added successfully')
else:
return make_return_message('Key "{}" already exists'.format(validator.key_name.value))
return make_return_message('Key "{}" already exists'.format(validator.key_name))
class RemoveSSHKey(Resource):
@ -382,12 +382,12 @@ class RemoveSSHKey(Resource):
return make_return_message(err, 400)
else:
# {user_prefix}/{realm}/{name}/key/{key_name}
etcd_key = join_path(settings['etcd']['user_prefix'], validator.realm.value,
validator.name.value, 'key', validator.key_name.value)
etcd_key = join_path(settings['etcd']['user_prefix'], validator.realm,
validator.name, 'key', validator.key_name)
try:
etcd_entry = shared.etcd_client.get(etcd_key, value_in_json=True)
except KeyError:
return make_return_message('No Key "{}" exists.'.format(validator.key_name.value))
return make_return_message('No Key "{}" exists.'.format(validator.key_name))
if etcd_entry:
shared.etcd_client.client.delete(etcd_key)
return {'message': 'Key successfully removed.'}
@ -405,9 +405,9 @@ class CreateNetwork(Resource):
else:
network_entry = {
'id': counters.increment_etcd_counter(settings['etcd']['vxlan_counter']),
'type': validator.type.value,
'type': validator.type,
}
if validator.user.value:
if validator.user:
try:
nb = pynetbox.api(url=settings['netbox']['url'], token=settings['netbox']['token'])
nb_prefix = nb.ipam.prefixes.get(prefix=settings['network']['prefix'])
@ -415,8 +415,8 @@ class CreateNetwork(Resource):
data={
'prefix_length': int(settings['network']['prefix_length']),
'description': '{}\'s network "{}"'.format(
validator.name.value,
validator.network_name.value
validator.name,
validator.network_name
),
'is_pool': True,
}
@ -429,8 +429,8 @@ class CreateNetwork(Resource):
else:
network_entry['ipv6'] = 'fd00::/64'
network_key = join_path(settings['etcd']['network_prefix'], validator.name.value,
validator.network_name.value)
network_key = join_path(settings['etcd']['network_prefix'], validator.name,
validator.network_name)
shared.etcd_client.put(network_key, network_entry, value_in_json=True)
return make_return_message('Network successfully added.')
@ -445,7 +445,7 @@ class ListUserNetwork(Resource):
except ValidationException as err:
return make_return_message(err, 400)
else:
prefix = join_path(settings['etcd']['network_prefix'], data['name'])
prefix = join_path(settings['etcd']['network_prefix'], validator.name)
networks = shared.etcd_client.get_prefix(prefix, value_in_json=True)
user_networks = []
for net in networks:
@ -455,7 +455,7 @@ class ListUserNetwork(Resource):
api.add_resource(CreateVM, '/vm/create')
api.add_resource(VmStatus, '/vm/status')
api.add_resource(GetVMStatus, '/vm/status')
api.add_resource(VMAction, '/vm/action')
api.add_resource(VMMigration, '/vm/migrate')

View File

@ -30,9 +30,9 @@ class Field:
def is_valid(self):
if not isinstance(self.value, self.type):
raise ValidationException("Incorrect Type for '{}' field".format(self.name))
else:
for validator in self.validators:
validator()
for validator in self.validators:
validator()
def __repr__(self):
return self.name
@ -52,8 +52,7 @@ class VmUUIDField(Field):
class BaseSchema:
def __init__(self, data):
print(data)
def __init__(self):
self.fields = [getattr(self, field) for field in dir(self) if isinstance(getattr(self, field), Field)]
def validation(self):
@ -69,6 +68,9 @@ class BaseSchema:
self.validation()
for field in self.fields:
setattr(self, field.name, field.value)
def get(dictionary: dict, key: str, return_default=False, default=None):
if dictionary is None:
@ -88,7 +90,7 @@ class OTPSchema(BaseSchema):
self.name = Field('name', str, get(data, 'name'))
self.realm = Field('realm', str, get(data, 'realm'))
self.token = Field('token', str, get(data, 'token'))
super().__init__(data=data)
super().__init__()
def validation(self):
if check_otp(self.name.value, self.realm.value, self.token.value) != 200:
@ -101,8 +103,7 @@ class CreateImageSchema(BaseSchema):
self.name = Field('name', str, get(data, 'name'))
self.image_store = Field('image_store', str, get(data, 'image_store'),
validators=[self.image_store_name_validation])
super().__init__(data)
super().__init__()
def file_uuid_validation(self):
try:
@ -123,7 +124,7 @@ class CreateHostSchema(OTPSchema):
self.specs = Field('specs', dict, get(data, 'specs'), validators=[self.specs_validation])
self.hostname = Field('hostname', str, get(data, 'hostname'))
super().__init__(data=data)
super().__init__(data)
def specs_validation(self):
allowed_base = 10
@ -191,7 +192,7 @@ class CreateVMSchema(OTPSchema):
def image_validation(self):
try:
image_uuid = helper.resolve_image_name(self.image.value)
except Exception as e:
except Exception:
raise ValidationException('No image of name \'{}\' found'.format(self.image.value))
else:
self.image_uuid = image_uuid
@ -214,7 +215,7 @@ class CreateVMSchema(OTPSchema):
raise ValidationException('Network with name {} does not exists'.format(net))
def specs_validation(self):
ALLOWED_BASE = 10
allowed_base = 10
try:
_cpu = get(self.specs.value, 'cpu')
@ -228,10 +229,10 @@ class CreateVMSchema(OTPSchema):
parsed_ram = bitmath.parse_string_unsafe(_ram)
parsed_os_ssd = bitmath.parse_string_unsafe(_os_ssd)
if parsed_ram.base != ALLOWED_BASE:
if parsed_ram.base != allowed_base:
raise ValidationException('Your specified RAM is not in correct units')
if parsed_os_ssd.base != ALLOWED_BASE:
if parsed_os_ssd.base != allowed_base:
raise ValidationException('Your specified OS-SSD is not in correct units')
if int(_cpu) < 1:
@ -246,7 +247,7 @@ class CreateVMSchema(OTPSchema):
parsed_hdd = []
for hdd in _hdd:
_parsed_hdd = bitmath.parse_string_unsafe(hdd)
if _parsed_hdd.base != ALLOWED_BASE:
if _parsed_hdd.base != allowed_base:
raise ValidationException('Your specified HDD is not in correct units')
else:
parsed_hdd.append(str(_parsed_hdd))