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:
continue
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"
path = utils.get_path(grid_data, target)

View file

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