def put(self, cluster_id, resource): """ Update the resource information of the specified cluster in database :param cluster_id: the unique identifier of the cluster :param resource: resource name(security, networking, partition) """ resources = { self.SECURITY: { 'validator': 'is_valid_security_config', 'column': 'security_config' }, self.NETWORKING: { 'validator': 'is_valid_networking_config', 'column': 'networking_config' }, self.PARTITION: { 'validator': 'is_valid_partition_config', 'column': 'partition_config' }, } request_data = json.loads(request.data) with database.session() as session: cluster = session.query(ModelCluster).filter_by(id=cluster_id)\ .first() if not cluster: error_msg = 'You are trying to update a non-existing cluster!' return errors.handle_not_exist( errors.ObjectDoesNotExist(error_msg)) if resource not in request_data: error_msg = "Invalid resource name '%s'" % resource return errors.handle_invalid_usage( errors.UserInvalidUsage(error_msg)) value = request_data[resource] if resource not in resources.keys(): error_msg = "Invalid resource name '%s'" % resource return errors.handle_invalid_usage( errors.UserInvalidUsage(error_msg)) validate_func = resources[resource]['validator'] module = globals()['util'] is_valid, msg = getattr(module, validate_func)(value) if is_valid: column = resources[resource]['column'] session.query(ModelCluster).filter_by(id=cluster_id)\ .update({column: json.dumps(value)}) else: return errors.handle_mssing_input( errors.InputMissingError(msg)) return util.make_json_response(200, {"status": "OK"})
def put(self, host_id): """ Update configuration of the specified cluster host :param host_id: the unique identifier of the host """ with database.session() as session: host = session.query(ModelClusterHost).filter_by(id=host_id)\ .first() if not host: error_msg = "The host id=%s does not exist!" % host_id return errors.handle_not_exist( errors.ObjectDoesNotExist(error_msg)) logging.debug("cluster config put request.data %s", request.data) request_data = json.loads(request.data) if not request_data: error_msg = "request data is %s" % request_data return errors.handle_mssing_input( errors.InputMissingError(error_msg)) if not host.mutable: error_msg = "The host 'id=%s' is not mutable!" % host_id return errors.handle_invalid_usage( errors.UserInvalidUsage(error_msg)) #Valid if keywords in request_data are all correct if 'hostname' in request_data: hostname = request_data['hostname'] cluster_id = host.cluster_id test_host = session.query(ModelClusterHost)\ .filter_by(cluster_id=cluster_id, hostname=hostname).first() if test_host: error_msg = ("Hostname '%s' has been used for other host " "in the cluster, cluster ID is %s!" % (hostname, cluster_id)) return errors.handle_invalid_usage( errors.UserInvalidUsage(error_msg)) session.query(ModelClusterHost).filter_by(id=host_id)\ .update({"hostname": request_data['hostname']}) del request_data['hostname'] try: util.valid_host_config(request_data) except errors.UserInvalidUsage as exc: return errors.handle_invalid_usage(exc) host.config = request_data return util.make_json_response( 200, {"status": "OK"})
def put(self, cluster_id, resource): """ Update the resource information of the specified cluster in database :param cluster_id: the unique identifier of the cluster :param resource: resource name(security, networking, partition) """ resources = { self.SECURITY: {'validator': 'is_valid_security_config', 'column': 'security_config'}, self.NETWORKING: {'validator': 'is_valid_networking_config', 'column': 'networking_config'}, self.PARTITION: {'validator': 'is_valid_partition_config', 'column': 'partition_config'}, } request_data = json.loads(request.data) with database.session() as session: cluster = session.query(ModelCluster).filter_by(id=cluster_id)\ .first() if not cluster: error_msg = 'You are trying to update a non-existing cluster!' return errors.handle_not_exist( errors.ObjectDoesNotExist(error_msg) ) if resource not in request_data: error_msg = "Invalid resource name '%s'" % resource return errors.handle_invalid_usage( errors.UserInvalidUsage(error_msg)) value = request_data[resource] if resource not in resources.keys(): error_msg = "Invalid resource name '%s'" % resource return errors.handle_invalid_usage( errors.UserInvalidUsage(error_msg)) validate_func = resources[resource]['validator'] module = globals()['util'] is_valid, msg = getattr(module, validate_func)(value) if is_valid: column = resources[resource]['column'] session.query(ModelCluster).filter_by(id=cluster_id)\ .update({column: json.dumps(value)}) else: return errors.handle_mssing_input( errors.InputMissingError(msg)) return util.make_json_response( 200, {"status": "OK"})
def put(self, host_id): """ Update configuration of the specified cluster host :param host_id: the unique identifier of the host """ with database.session() as session: host = session.query(ModelClusterHost).filter_by(id=host_id)\ .first() if not host: error_msg = "The host id=%s does not exist!" % host_id return errors.handle_not_exist( errors.ObjectDoesNotExist(error_msg)) logging.debug("cluster config put request.data %s", request.data) request_data = json.loads(request.data) if not request_data: error_msg = "request data is %s" % request_data return errors.handle_mssing_input( errors.InputMissingError(error_msg)) if not host.mutable: error_msg = "The host 'id=%s' is not mutable!" % host_id return errors.handle_invalid_usage( errors.UserInvalidUsage(error_msg)) #Valid if keywords in request_data are all correct if 'hostname' in request_data: session.query(ModelClusterHost).filter_by(id=host_id)\ .update({"hostname": request_data['hostname']}) del request_data['hostname'] try: util.valid_host_config(request_data) except errors.UserInvalidUsage as exc: return errors.handle_invalid_usage(exc) host.config = request_data return util.make_json_response(200, {"status": "OK"})