def createConnection(req): try: t = req.cookies.get(COOKIE_NAME) if not t: raise HttpError() return SwiftConnect.SwiftConnect(t) except: raise HttpError("no token received from client", 401)
def doPlot(plotType): nrDataSource = json.loads(request.args.get("nrDataSource")) print(nrDataSource) url = appConfig.nodered_url + nrDataSource['url'] r = requests.get(url) if r.status_code == 404: raise HttpError("the Node-RED data source is not reachable: {}".format(url), 420) try: data = json.JSONDecoder(object_pairs_hook=collections.OrderedDict).decode(r.content.decode()) dataKeys = getListOfKeys(data[0]) df = pandas.DataFrame(data, columns=dataKeys) df[dataKeys[0]] = df[dataKeys[0]].map(lambda x: str(x)[:20]) print(df) if('2bar' == plotType): c = doPlot2(data=df, nrDataSource=nrDataSource) elif('bar' == plotType): c = doPlot1(data=df, nrDataSource=nrDataSource) print(nrDataSource, plotType) return Response(json.dumps(c), mimetype="application/json") except: raise HttpError("the Node-RED data source produced malformatted data", 500)
def create_objectclass(): swift = createConnection(request) internal_data = InternalStorageManager(swift) try: class_definition = request.json.get("objectClass") class_name = class_definition.get("name") class_schema = class_definition.get("schema") except AttributeError: raise HttpError("malformed request", 400) if not class_name or not class_schema: raise HttpError("class name or class schema definition missing", 400) class_names = internal_data.get_keys("object classes") if class_name in class_names: raise HttpError("class already exists", 422) try: Draft4Validator(CLASS_SCHEMA, format_checker=FormatChecker()).validate(class_definition) except ValidationError as e: raise HttpError("invalid class definition: {}".format(e), 400) internal_data.store_data("object classes", class_name, json.dumps(class_definition)) return "", 201
def change_container(container_name): swift = createConnection(request) # TODO: check schema validity since somebody else could store a rouge class definition in the object store (via direct interfacing with the object store) try: container_definition = request.json.get("container") container_name = container_definition.get("name") except AttributeError: raise HttpError("malformed request", 400) if not container_name: raise HttpError("container name is missing", 400) containers = swift.get_container_list()[1] if container_name not in [container.get("name") for container in containers]: raise HttpError("container does not exist", 404) container_metadata = {} try: class_name = container_definition.get("objectClass") internal_data = InternalStorageManager(swift) class_definition = internal_data.get_data("object classes", class_name) if class_name: if class_definition is None: raise HttpError("class does not exist", 404) container_metadata = {"x-container-meta-object-class": class_name} except AttributeError: pass # ignore empty or missing class definition swift.create_container(container_name, container_metadata) return "", 201
def create_object(container_name): swift = createConnection(request) # returns werkzeug.datastructures.FileStorage i.e. file-like # Underlying stream is either BytesIO for small files or _TemporaryFileWrapper for large files file = request.files["objectName"] object_name = secure_filename(file.filename) # check whether an object with the same name already exists in the same container all_objects = swift.get_object_list(container_name)[1] if object_name in [obj.get("name") for obj in all_objects]: raise HttpError( "object with this name already exists in this container", 422) headers = {} retentime = request.form["RetentionPeriod"] if retentime: try: converted_retentime = datetime.strptime(retentime, "%Y-%m-%d") reten_timestamp = int(time.mktime(converted_retentime.timetuple())) headers["X-Object-Meta-RetentionTime"] = reten_timestamp except Exception as e: log.debug( "invalid date format for form parameter RetentionPeriod: {}, for request: {}. Expected format: yyyy-mm-dd" .format(retentime)) raise HttpError( "invalid date format for form parameter RetentionPeriod: {}. Expected format: yyyy-mm-dd" .format(retentime), 400) class_metadata_json = request.form["metadata"] if class_metadata_json: class_metadata = json.loads(class_metadata_json) class_name = swift.get_container_metadata(container_name).get( "x-container-meta-object-class") if class_name: internal_data = InternalStorageManager(swift) class_definition = json.loads( internal_data.get_data("object classes", class_name)) Draft4Validator( class_definition, format_checker=FormatChecker()).validate(class_metadata) for field in class_metadata.keys(): val = class_metadata[field] if val is not None: field_header = xform_header_names(field) xformed_class_name = xform_header_names(class_name) headers["X-Object-Meta-" + xformed_class_name + "-Class-" + field_header] = class_metadata[field] swift.streaming_object_upload(object_name, container_name, file, headers) return "", 201
def doLogin(): try: user = request.json.get("username") log.debug("authenticating user: {}".format(user)) password = request.json.get("password") token = SwiftConnect.doAuthGetToken(user, password) r = Response() r.set_cookie(COOKIE_NAME, value=token) return r except ClientException as e: log.exception("Login error") raise HttpError(e.msg, 401) except Exception: log.exception("Login error") raise HttpError("Internal Server Error", 500)
def get_objects_in_container(container_name): swift = createConnection(request) optional_params = {} limit = request.args.get("limit") if limit is not None: if limit.isdigit() and int(limit) > 0: optional_params["limit"] = int(limit) else: log.debug("invalid query parameter limit: {}, for request: {}".format(limit, request.url)) raise HttpError("specified query parameter limit: {}, must be a positive integer".format(limit), 400) marker = request.args.get("marker") if marker is not None: optional_params["marker"] = marker prefix = request.args.get("prefix") if prefix is not None: optional_params["prefix"] = prefix cts = swift.get_object_list(container_name, **optional_params) resp = {} resp["metadata"] = {"objectClass": cts[0].get("x-container-meta-object-class"), "objectCount": cts[0].get("x-container-object-count")} resp["objects"] = cts[1] return Response(json.dumps(resp, sort_keys=True), mimetype="application/json")
def index(path=""): if path[:5] != "swift": # return render_template('index.html') return send_file("angular/index.html") else: # this function is only called, when no other route matches raise HttpError("the requested endpoint does not exist", 404)
def get_objectclass(class_name): swift = createConnection(request) internal_data = InternalStorageManager(swift) class_def = internal_data.get_data("object classes", class_name) if not class_def: raise HttpError("class does not exist", 404) return Response(class_def, mimetype="application/json")
def delete_objectclass(class_name): swift = createConnection(request) internal_data = InternalStorageManager(swift) class_def = internal_data.get_data("object classes", class_name) if not class_def: raise HttpError("class does not exist", 404) internal_data.remove_data("object classes", class_name) return "", 204
def getDataFromNodeRed(nrDataSource): url = appConfig.nodered_url + nrDataSource['url'] log.debug("getting data from node red at: {}".format(url)) r = requests.get(url) if r.status_code == 404: raise HttpError( "the Node-RED data source is not reachable: {}".format(url), 420) try: data = json.JSONDecoder( object_pairs_hook=collections.OrderedDict).decode( r.content.decode()) dataKeys = getListOfKeys(data[0]) df = pandas.DataFrame(data, columns=dataKeys) df[dataKeys[0]] = df[dataKeys[0]].map(lambda x: str(x)[:20]) return df except: log.exception("JSON parse error:") raise HttpError("the Node-RED result is no valid JSON", 500)
def delete_object(container_name, object_name): swift = createConnection(request) metadata = swift.get_object_metadata(container_name, object_name) retentimestamp = metadata.get("x-object-meta-retentiontime") if retentimestamp and not isRetentionPeriodExpired(retentimestamp): error_msg = "Deletion failed due to retention enforcement, file cannot be deleted till {}!".format(time.strftime("%a, %d. %B %Y", time.localtime(int(retentimestamp)))) log.debug(error_msg) raise HttpError(error_msg, 412) swift.delete_object(container_name, object_name) return "", 204
def doPlot(): nrDataSource = json.loads( urlParse.unquote(request.args.get("nrDataSource"))) plotType = request.args.get("plotType") log.debug("producing plot: {} for: {}".format(plotType, nrDataSource)) df = getDataFromNodeRed(nrDataSource=nrDataSource) try: print(df) if ('2bar' == plotType): c = doPlot2(data=df, nrDataSource=nrDataSource) elif ('bar' == plotType): c = doPlot1(data=df, nrDataSource=nrDataSource) elif ('bar_log' == plotType): c = doPlot1log(data=df, nrDataSource=nrDataSource) elif ('line' == plotType): c = doPlot11(data=df, nrDataSource=nrDataSource) return Response(json.dumps(c), mimetype="application/json") except: log.exception("plotting error:") raise HttpError( "the Node-RED result could not be plotted. Maybe wrong data format for the plot type? Check result table", 500)
def change_object_metadata(container_name, object_name): raise HttpError("funcion not implemented yet")
def change_container_metadata(container_name, metadata): print(metadata) raise HttpError("funcion not implemented yet")