def post(database_id): """ Saves the server and associate it to database with given database_id. Args: database_id (int): The first parameter. Returns: Information and the status of server if it is saved otherwise the error message. """ inputs = ServerInputs(request) if not inputs.validate(): return jsonify(success=False, errors=inputs.errors) server = [server for server in SERVERS if server['name'] == request.json['name']] if len(server) > 0: return make_response(jsonify({'error': 'Server name already exists'}), 404) server = [server for server in SERVERS if server['hostname'] == request.json['hostname']] if len(server) > 0: return make_response(jsonify({'error': 'Host name already exists'}), 404) if not SERVERS: server_id = 1 else: server_id = SERVERS[-1]['id'] + 1 server = { 'id': server_id, 'name': request.json['name'].strip(), 'description': request.json.get('description', "").strip(), 'hostname': request.json.get('hostname', "").strip(), 'enabled': True, 'admin-listener': request.json.get('admin-listener', "").strip(), 'zookeeper-listener': request.json.get('zookeeper-listener', "").strip(), 'replication-listener': request.json.get('replication-listener', "").strip(), 'client-listener': request.json.get('client-listener', "").strip(), 'internal-interface': request.json.get('internal-interface', "").strip(), 'external-interface': request.json.get('external-interface', "").strip(), 'public-interface': request.json.get('public-interface', "").strip(), 'internal-listener': request.json.get('internal-listener', "").strip(), 'http-listener': request.json.get('http-listener', "").strip(), 'placement-group': request.json.get('placement-group', "").strip(), } SERVERS.append(server) # Add server to the current database current_database = [database for database in DATABASES if database['id'] == database_id] if len(current_database) == 0: abort(404) if not request.json: abort(400) current_database[0]['members'].append(server_id) return jsonify({'server': server, 'status': 1, 'members': current_database[0]['members']}), 201
def put(server_id): """ Update the server with specified server_id. Args: server_id (int): The first parameter. Returns: Information of server with specified server_id after being updated otherwise the error message. """ inputs = ServerInputs(request) if not inputs.validate(): return jsonify(success=False, errors=inputs.errors) current_server = [server for server in SERVERS if server['id'] == server_id] if len(current_server) == 0: abort(404) current_server[0]['name'] = \ request.json.get('name', current_server[0]['name']) current_server[0]['hostname'] = \ request.json.get('hostname', current_server[0]['hostname']) current_server[0]['description'] = \ request.json.get('description', current_server[0]['description']) current_server[0]['enabled'] = \ request.json.get('enabled', current_server[0]['enabled']) current_server[0]['admin-listener'] = \ request.json.get('admin-listener', current_server[0]['admin-listener']) current_server[0]['internal-listener'] = \ request.json.get('internal-listener', current_server[0]['internal-listener']) current_server[0]['http-listener'] = \ request.json.get('http-listener', current_server[0]['http-listener']) current_server[0]['zookeeper-listener'] = \ request.json.get('zookeeper-listener', current_server[0]['zookeeper-listener']) current_server[0]['replication-listener'] = \ request.json.get('replication-listener', current_server[0]['replication-listener']) current_server[0]['client-listener'] = \ request.json.get('client-listener', current_server[0]['client-listener']) current_server[0]['internal-interface'] = \ request.json.get('internal-interface', current_server[0]['internal-interface']) current_server[0]['external-interface'] = \ request.json.get('external-interface', current_server[0]['external-interface']) current_server[0]['public-interface'] = \ request.json.get('public-interface', current_server[0]['public-interface']) current_server[0]['placement-group'] = \ request.json.get('placement-group', current_server[0]['placement-group']) return jsonify({'server': current_server[0], 'status': 1})