2018-04-18 16:46:53 -04:00
|
|
|
from flask_restful import Resource
|
|
|
|
from flask import request
|
|
|
|
from flask_jwt_extended import jwt_required
|
|
|
|
from marshmallow import ValidationError
|
|
|
|
from master.schema.instanceschema import InstanceSchema
|
|
|
|
from master import ctx
|
2018-09-29 15:52:22 -04:00
|
|
|
import json
|
2018-04-18 16:46:53 -04:00
|
|
|
|
|
|
|
class Instance(Resource):
|
|
|
|
def get(self, id=None):
|
|
|
|
if id is None:
|
|
|
|
schema = InstanceSchema(many=True)
|
2018-04-19 01:48:14 -04:00
|
|
|
instances = schema.dump(ctx.get_instances())
|
2018-04-18 16:46:53 -04:00
|
|
|
return instances
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
instance = ctx.get_instance(id)
|
|
|
|
return InstanceSchema().dump(instance)
|
|
|
|
except KeyError:
|
|
|
|
return {'message' : 'instance not found'}, 404
|
|
|
|
|
|
|
|
@jwt_required
|
|
|
|
def put(self, id):
|
|
|
|
try:
|
2018-09-29 15:52:22 -04:00
|
|
|
for server in request.json['servers']:
|
2018-11-25 21:00:36 -05:00
|
|
|
if 'ip' not in server or server['ip'] == 'localhost':
|
|
|
|
server['ip'] = request.remote_addr
|
2018-04-18 16:46:53 -04:00
|
|
|
instance = InstanceSchema().load(request.json)
|
|
|
|
except ValidationError as err:
|
|
|
|
return {'message' : err.messages }, 400
|
|
|
|
ctx.update_instance(instance)
|
2018-04-19 01:48:14 -04:00
|
|
|
return { 'message' : 'instance updated successfully' }, 200
|
2018-04-18 16:46:53 -04:00
|
|
|
|
|
|
|
@jwt_required
|
|
|
|
def post(self):
|
|
|
|
try:
|
2018-09-29 15:52:22 -04:00
|
|
|
for server in request.json['servers']:
|
2018-11-25 21:00:36 -05:00
|
|
|
if 'ip' not in server or server['ip'] == 'localhost':
|
|
|
|
server['ip'] = request.remote_addr
|
2018-04-18 16:46:53 -04:00
|
|
|
instance = InstanceSchema().load(request.json)
|
|
|
|
except ValidationError as err:
|
2018-04-19 01:48:14 -04:00
|
|
|
return {'message' : err.messages }, 400
|
2018-04-18 16:46:53 -04:00
|
|
|
ctx.add_instance(instance)
|
2018-04-19 01:48:14 -04:00
|
|
|
return { 'message' : 'instance added successfully' }, 200
|