use post only

This commit is contained in:
deflax 2016-04-10 06:37:32 +03:00
parent 9052ef4669
commit 9dbeea07f8
2 changed files with 40 additions and 37 deletions

View file

@ -329,7 +329,7 @@ def query_vm(req_vmid):
else: else:
continue continue
if target == 0: if target == 0:
logger.error('grid> vmid {} cannot be found!' + str(req_vmid)) logger.error('grid> vmid {} cannot be found!'.format(req_vmid))
return int(-1), "None" return int(-1), "None"
path = utils.get_path(grid_data, target) path = utils.get_path(grid_data, target)

View file

@ -28,8 +28,9 @@ def welcome():
def selector(fn, req, vmid=0): def selector(fn, req, vmid=0):
""" TRY to exec commands """ """ try to exec commands """
json = req.context['doc'] json = req.context['doc']
print(json)
apipass = json['apikey'] apipass = json['apikey']
if apipass != config.get('general', 'apipass'): if apipass != config.get('general', 'apipass'):
status = falcon.HTTP_404 status = falcon.HTTP_404
@ -45,31 +46,40 @@ def selector(fn, req, vmid=0):
body = clientsdb.validate(clientemail, passwd) body = clientsdb.validate(clientemail, passwd)
elif fn == 'create': elif fn == 'create':
body = urllib.parse.urlencode(plugin.vmcreate(req.params)) body = plugin.vmcreate(json)
elif fn == 'status': elif fn == 'status':
body = urllib.parse.urlencode(plugin.vmstatus(vmid)) body = plugin.vmstatus(vmid)
elif fn == 'delete': elif fn == 'delete':
body = urllib.parse.urlencode(plugin.vmdelete(vmid)) body = plugin.vmdelete(vmid)
elif fn == 'suspend': elif fn == 'suspend':
body = urllib.parse.urlencode(plugin.vmsuspend(vmid)) body = plugin.vmsuspend(vmid)
elif fn == 'resume': elif fn == 'resume':
body = urllib.parse.urlencode(plugin.vmresume(vmid)) body = plugin.vmresume(vmid)
elif fn == 'start': elif fn == 'start':
body = urllib.parse.urlencode(plugin.vmstart(vmid)) body = plugin.vmstart(vmid)
elif fn == 'shutdown': elif fn == 'shutdown':
body = urllib.parse.urlencode(plugin.vmshutdown(vmid)) body = plugin.vmshutdown(vmid)
elif fn == 'stop': elif fn == 'stop':
body = urllib.parse.urlencode(plugin.vmstop(vmid)) body = plugin.vmstop(vmid)
elif fn == 'vnc': elif fn == 'vnc':
body = urllib.parse.urlencode(plugin.vmvnc(vmid)) body = plugin.vmvnc(vmid)
except: except:
logger.critical('grid> {} malfunction!'.format(fn)) logger.critical('grid> {} malfunction!'.format(fn))
body = '793 Zombie Apocalypse' body = '503 Service Unavailable'
status = falcon.HTTP_404 status = falcon.HTTP_503
raise
else: else:
#logger.info('{}> 200 OK'.format(fn)) logger.info('grid> {} ok'.format(fn))
status = falcon.HTTP_202 status = falcon.HTTP_202
return status, body return status, body
@ -108,7 +118,7 @@ class JSONTranslator(object):
req.context['doc'] = json.loads(body.decode('utf-8')) req.context['doc'] = json.loads(body.decode('utf-8'))
except (ValueError, UnicodeDecodeError): except (ValueError, UnicodeDecodeError):
raise falcon.HTTPError(falcon.HTTP_753, raise falcon.HTTPError(falcon.HTTP_400,
'Malformed JSON', 'Malformed JSON',
'Could not decode the request body. The ' 'Could not decode the request body. The '
'JSON was incorrect or not encoded as ' 'JSON was incorrect or not encoded as '
@ -141,14 +151,7 @@ class ValidateResource(object):
resp.status, response = selector('validate', req) resp.status, response = selector('validate', req)
req.context['result'] = response req.context['result'] = response
class ClusterResource(object): class CreateResource(object):
#def on_get(self, req, resp):
# """ TEST . List cluster nodes . TEST """
# logger.info('grid> cache status')
# response = grid.sync(False)
# response = 'poke'
# req.context['result'] = response
@falcon.before(max_body(64 * 1024)) @falcon.before(max_body(64 * 1024))
def on_post(self, req, resp): def on_post(self, req, resp):
"""Create a cluster node, returns array of: status, vmid, pass, ipv4, """ """Create a cluster node, returns array of: status, vmid, pass, ipv4, """
@ -172,7 +175,7 @@ class DeleteResource(object):
resp.status, response = selector('delete', req, vmid) resp.status, response = selector('delete', req, vmid)
req.context['result'] = response req.context['result'] = response
class ArchivateResource(object): class SuspendResource(object):
@falcon.before(max_body(64 * 1024)) @falcon.before(max_body(64 * 1024))
def on_post(self, req, resp, vmid): def on_post(self, req, resp, vmid):
""" Temporary suspend the instance """ """ Temporary suspend the instance """
@ -180,7 +183,7 @@ class ArchivateResource(object):
resp.status, response = selector('suspend', req, vmid) resp.status, response = selector('suspend', req, vmid)
req.context['result'] = response req.context['result'] = response
class UnArchiveResource(object): class ResumeResource(object):
@falcon.before(max_body(64 * 1024)) @falcon.before(max_body(64 * 1024))
def on_post(self, req, resp, vmid): def on_post(self, req, resp, vmid):
""" Unuspend the instance """ """ Unuspend the instance """
@ -232,32 +235,32 @@ wsgi_app = api = application = falcon.API(middleware=[
res_validate = ValidateResource() res_validate = ValidateResource()
api.add_route('/validate', res_validate) api.add_route('/validate', res_validate)
res_cluster = ClusterResource() res_create = CreateResource()
api.add_route('/instance', res_cluster) api.add_route('/create', res_create)
res_status = StatusResource() res_status = StatusResource()
api.add_route('/instance/{vmid}', res_status) api.add_route('/status/{vmid}', res_status)
res_delete = DeleteResource() res_delete = DeleteResource()
api.add_route('/instance/delete/{vmid}', res_delete) api.add_route('/delete/{vmid}', res_delete)
res_archivate = ArchivateResource() res_suspend = SuspendResource()
api.add_route('/instance/archivate/{vmid}', res_archivate) api.add_route('/suspend/{vmid}', res_suspend)
res_unarchive = UnArchiveResource() res_resume = ResumeResource()
api.add_route('/instance/unarchive/{vmid}', res_unarchive) api.add_route('/resume/{vmid}', res_resume)
res_start = StartResource() res_start = StartResource()
api.add_route('/instance/start/{vmid}', res_start) api.add_route('/start/{vmid}', res_start)
res_shutdown = ShutdownResource() res_shutdown = ShutdownResource()
api.add_route('/instance/shutdown/{vmid}', res_shutdown) api.add_route('/shutdown/{vmid}', res_shutdown)
res_stop = StopResource() res_stop = StopResource()
api.add_route('/instance/stop/{vmid}', res_stop) api.add_route('/stop/{vmid}', res_stop)
res_vnc = VNCResource() res_vnc = VNCResource()
api.add_route('/instance/vnc/{vmid}', res_vnc) api.add_route('/vnc/{vmid}', res_vnc)
#display motd #display motd
welcome() welcome()