Beispiel #1
0
def deletesnippet(productid, serviceid, apiid, versionid, resourceid, methodid,
                  snippetid):
    methodpath = "servicedesigns/" + serviceid + "/api/" + versionid + "/resources/" + resourceid + "/methods/" + methodid + ".json"
    status, statusmessage, methodjson = datafile.load_s3_file(methodpath)
    del methodjson["snippets"][snippetid]
    snippetorder = methodjson["snippets"]["snippetorder"]
    count = 0
    for item in snippetorder:
        if item["snippetid"] == snippetid:
            break
        else:
            count = count + 1
    snippetorder.pop(count)
    methodjson["snippets"].update({"snippetorder": snippetorder})
    methodjson["methodpath"] = methodjson["methodpath"].replace("{", "<")
    methodjson["methodpath"] = methodjson["methodpath"].replace("}", ">")
    # if methodjson["methodpath"]!="":
    # 	apicallpath="/"+versionname+"/"+resource["resourcename"]+methodjson["methodpath"]
    # else:
    # 	apicallpath="/"+versionname+"/"+resource["resourcename"]
    # methodpath="servicedesigns/"+serviceid+"/api/"+versionid+"/resources/"+resourceid+"/methods/"+methodid+".json"
    # methodjson["type_code"]=methodjson["type_code"].replace("METHODID",methodid)
    # methodjson["type_code"]=methodjson["type_code"].replace("METHODPATH",apicallpath)
    codestring, requirement = consolidate_code(methodjson)
    codepath = "servicedesigns/" + serviceid + "/api/" + versionid + "/resources/" + resourceid + "/methods/" + methodid + "/code/" + methodid + ".py"
    codestatus, codestatusmessage, code = datafile.write_txt_file(
        codepath, codestring)
    if codestatus == "error":
        message = {"statusCode": 400, "errorMessage": code}
        return (message)
    reqstring = ""
    for i in requirement:
        reqstring = reqstring + i + "\n"
    # act["requirements"]=reqstring
    requirementpath = "servicedesigns/" + serviceid + "/api/" + versionid + "/resources/" + resourceid + "/methods/" + methodid + "/code/requirement.txt"
    requirementstatus, requirementstatusmessage, req = datafile.write_txt_file(
        requirementpath, reqstring)
    if requirementstatus == "error":
        message = {"statusCode": 400, "errorMessage": req}
        return (message)
    methstatus, methstatusmessage, methres = datafile.write_s3_file(
        methodpath, methodjson)
    if methstatus == "error":
        message = {"statusCode": 400, "errorMessage": code}
        return (message)
    if methstatus == "success" and codestatus == "success" and requirementstatus == "success":
        message = {"statusCode": 200, "body": methodjson}
    return (message)
Beispiel #2
0
def updatemethod(productid, serviceid, apiid, versionid, resourceid, methodid):
    json_dict = request.get_json()
    methodpath = "servicedesigns/" + serviceid + "/api/" + versionid + "/resources/" + resourceid + "/methods/" + methodid + ".json"
    status, statusmessage, method = datafile.load_s3_file(methodpath)
    req_string = ""
    if status == "success":
        snippetid = request.json["snippetid"]
        if "inputschema" in json_dict:
            method['inputschema'] = request.json['inputschema']
        if "outputschema" in json_dict:
            method['outputschema'] = request.json['outputschema']
        if "requirements" in json_dict:
            method["snippets"][snippetid]['requirements'] = request.json[
                'requirements']
        if "import_code" in json_dict:
            method["snippets"][snippetid]["import_code"] = request.json[
                "import_code"]
        if "function_code" in json_dict:
            method["snippets"][snippetid]["function_code"] = request.json[
                "function_code"]
        if "function_call" in json_dict:
            method["snippets"][snippetid]["function_call"] = request.json[
                "function_call"]
        if "data_code" in json_dict:
            method["snippets"][snippetid]["data_code"] = request.json[
                "data_code"]
        codestring, requirements = consolidate_code(method)
        # codestring=method["snippets"][snippetid]["import_code"]+"\n"+method["snippets"][snippetid]["data_code"]+"\n"+method["snippets"][snippetid]["function_code"]+"\n"+method["snippets"][snippetid]["function_call"] + "\n" +method["type_code"]
        codepath = "servicedesigns/" + serviceid + "/api/" + versionid + "/resources/" + resourceid + "/methods/" + methodid + "/code/" + methodid + ".py"
        codestatus, codestatusmessage, code = datafile.write_txt_file(
            codepath, codestring)
        for requirement in requirements:
            req_string = req_string + requirement + '\n'
            requirementpath = "servicedesigns/" + serviceid + "/api/" + versionid + "/resources/" + resourceid + "/methods/" + methodid + "/code/requirement.txt"
            reqwritestatus, reqwritestatusmessage, req = datafile.write_txt_file(
                requirementpath, req_string)
        writestatus, statusmessage, response = datafile.write_s3_file(
            methodpath, method)
        if writestatus == "success" and codestatus == "success":
            message = {
                "statusCode": 200,
                'body': method["snippets"][snippetid]
            }
        else:
            message = {"statusCode": 400, "errorMessage": response}
    else:
        message = {"errorMessage": statusmessage, "Error": method}
    return (message)
def generatemethod(productid, serviceid, apiid, versionid, resourceid):
    resourcepath = "servicedesigns/" + serviceid + "/api/" + versionid + "/resources/" + resourceid + "/config.json"
    status, statusmessage, resource = datafile.load_s3_file(resourcepath)
    actions = request.json["actions"]
    versionname = request.json["versionname"]
    methodmessage = getmethod(productid, serviceid, apiid, versionid,
                              resourceid)
    if methodmessage["statusCode"] == 200:
        if methodmessage["body"] != []:
            for method in methodmessage["body"]:
                for action in actions:
                    action["methodpath"] = action["methodpath"].replace(
                        "{", "<")
                    action["methodpath"] = action["methodpath"].replace(
                        "}", ">")
                    if action["methodpath"] == method["methodpath"] and action[
                            "methodtype"] == method["methodtype"]:
                        actions.remove(action)
    else:
        return (methodmessage)
    autopoppulate = {
        "resourceid": resourceid,
        "serviceid": serviceid,
        "versionid": versionid,
        "actions": actions
    }
    actionlist = []
    for act in autopoppulate["actions"]:
        methodid = uniqid("meth")
        act["methodid"] = methodid
        act["methodpath"] = act["methodpath"].replace("{", "<")
        act["methodpath"] = act["methodpath"].replace("}", ">")
        if act["methodpath"] != "":
            apicallpath = "/" + versionname + "/" + resource[
                "resourcename"] + act["methodpath"]
        else:
            apicallpath = "/" + versionname + "/" + resource["resourcename"]
        method = {"Ref": methodid}
        resource["methods"].append(method)
        resstatus, resstatusmessage, res = datafile.write_s3_file(
            resourcepath, resource)
        if resstatus == "error":
            message = {"statusCode": 400, "errorMessage": res}
            return (message)
        methodpath = "servicedesigns/" + serviceid + "/api/" + versionid + "/resources/" + resourceid + "/methods/" + methodid + ".json"
        full_code = consolidate_code(act, requirements)
        # act["import_code"]=act["import_code"].replace("METHODID",methodid)
        # act["import_code"]=act["import_code"].replace("METHODPATH",apicallpath)
        # act["function_code"]=act["function_code"].replace("METHODID",methodid)
        # act["function_code"]=act["function_code"].replace("METHODPATH",apicallpath)
        # act["main_code"]=act["main_code"].replace("METHODID",methodid)
        # act["main_code"]=act["main_code"].replace("METHODPATH",apicallpath)
        # act["type_code"]=act["type_code"].replace("METHODID",methodid)
        # act["type_code"]=act["type_code"].replace("METHODPATH",apicallpath)
        # codestring=act["import_code"]+"\n"+act["function_code"]+"\n"+act["main_code"]+"\n"+act["type_code"]
        codepath = "servicedesigns/" + serviceid + "/api/" + versionid + "/resources/" + resourceid + "/methods/" + methodid + "/code/" + methodid + ".py"
        codestatus, codestatusmessage, code = datafile.write_txt_file(
            codepath, codestring)
        if codestatus == "error":
            message = {"statusCode": 400, "errorMessage": code}
            return (message)
        requirement = act["requirements"]
        reqstring = ""
        for i in requirement:
            reqstring = reqstring + i + "\n"
        # act["requirements"]=reqstring
        requirementpath = "servicedesigns/" + serviceid + "/api/" + versionid + "/resources/" + resourceid + "/methods/" + methodid + "/code/requirement.txt"
        requirementstatus, requirementstatusmessage, req = datafile.write_txt_file(
            requirementpath, reqstring)
        if requirementstatus == "error":
            message = {"statusCode": 400, "errorMessage": req}
            return (message)
        methstatus, methstatusmessage, methres = datafile.write_s3_file(
            methodpath, act)
        if methstatus == "error":
            message = {"statusCode": 400, "errorMessage": code}
            return (message)
        if methstatus == "success" and codestatus == "success" and requirementstatus == "success":
            actionlist.append(act)
    message = {"statusCode": 200, "body": actionlist}
    return (message)
def createmethod(productid, serviceid, apiid, versionid, resourceid):
    iop = {"methodtype": "", "path": ""}
    methodtype = request.json["methodtype"]
    json_dict = request.get_json()
    resourcepath = "servicedesigns/" + serviceid + "/api/" + versionid + "/resources/" + resourceid + "/config.json"
    status, statusmessage, resource = datafile.load_s3_file(resourcepath)
    if status == "success":
        if resource["methods"] == []:
            methodid = uniqid('meth')
            method = {"Ref": methodid}
            resource["methods"].append(method)
        elif resource["methods"] != []:
            methodids = []
            for method in resource["methods"]:
                methodids.append(method["Ref"])
            a = True
            while a is True:
                methodid = uniqid('meth')
                if methodid in methodids:
                    a = True
                else:
                    a = False
            method = {"Ref": methodid}
            resource["methods"].append(method)
        resstatus, resstatusmessage, res = datafile.write_s3_file(
            resourcepath, resource)
        methodjson = {
            "methodtype": methodtype,
            "methodid": methodid,
            "codetype": "python",
            "language": "python3.6",
            "servicetype": "dynamodb_eks_flask",
            "requirements": "",
            "import_code": "",
            "function_code": "",
            "main_code": "",
            "type_code": ""
        }
        if "path" in json_dict:
            methodjson["methodpath"] = request.json["path"]
        if "description" in json_dict:
            methodjson["description"] = request.json["description"]
        methodpath = "servicedesigns/" + serviceid + "/api/" + versionid + "/resources/" + resourceid + "/methods/" + methodid + ".json"
        codestring = methodjson["import_code"] + "\n" + methodjson[
            "function_code"] + "\n" + methodjson[
                "main_code"] + "\n" + methodjson["type_code"]
        codepath = "servicedesigns/" + serviceid + "/api/" + versionid + "/resources/" + resourceid + "/methods/" + methodid + "/code/" + methodid + ".py"
        codestatus, codestatusmessage, code = datafile.write_txt_file(
            codepath, codestring)
        requirement = methodjson["requirements"]
        requirementpath = "servicedesigns/" + serviceid + "/api/" + versionid + "/resources/" + resourceid + "/methods/" + methodid + "/code/requirement.txt"
        requirementstatus, requirementstatusmessage, req = datafile.write_txt_file(
            requirementpath, requirement)
        methstatus, methstatusmessage, methres = datafile.write_s3_file(
            methodpath, methodjson)
        if methstatus == "success" and codestatus == "success" and requirementstatus == "success":
            message = {"statusCode": 200, "body": methodjson}
        else:
            message = {"errorMessage": methstatus, "Error": methres}
    else:
        message = {"errorMessage": statusmessage, "Error": resource}
    return (message)
Beispiel #5
0
def addsnippet(productid, serviceid, apiid, versionid, resourceid, methodid):
    resourcepath = "servicedesigns/" + serviceid + "/api/" + versionid + "/resources/" + resourceid + "/config.json"
    status, statusmessage, resource = datafile.load_s3_file(resourcepath)
    if "snippetname" not in request.json:
        snippet = request.json["snippet"]
        versionname = request.json["versionname"]
        snippetid = uniqid("sn_")
        updated_method = {
            "import_code": snippet["import_code"],
            "function_code": snippet["function_code"],
            "function_call": snippet["function_call"],
            "requirements": snippet["requirements"],
            "version": snippet["version"],
            "name": snippet["name"]
        }
        methodpath = "servicedesigns/" + serviceid + "/api/" + versionid + "/resources/" + resourceid + "/methods/" + methodid + ".json"
        status, statusmessage, methodjson = datafile.load_s3_file(methodpath)
        table = request.json["table"]
        if "TABLENAME" in updated_method["function_code"]:
            updated_method["function_code"] = updated_method[
                "function_code"].replace("TABLENAME", table["tablename"])
        if "TABLENAME" in updated_method["function_call"]:
            updated_method["function_call"] = updated_method[
                "function_call"].replace("TABLENAME", table["tablename"])
        if "PRIMARYKEY" in updated_method["function_code"]:
            updated_method["function_code"] = updated_method[
                "function_code"].replace("PRIMARYKEY",
                                         table['indexes']['primary']['key'])
        if "PRIMARYKEY" in updated_method["function_call"]:
            updated_method["function_call"] = updated_method[
                "function_call"].replace("PRIMARYKEY",
                                         table['indexes']['primary']['key'])
        updated_method["function_code"] = updated_method[
            "function_code"].replace("SNIPPETID", snippetid)
        updated_method["function_call"] = updated_method[
            "function_call"].replace("SNIPPETID", snippetid)
        updated_method.update({"snippetid": snippetid})
        methodpath = "servicedesigns/" + serviceid + "/api/" + versionid + "/resources/" + resourceid + "/methods/" + methodid + ".json"
        status, statusmessage, methodjson = datafile.load_s3_file(methodpath)
        if status == "success":
            snippetorder = methodjson["snippets"]["snippetorder"]
            snippetorder.append({
                "name": snippet["name"],
                "snippetid": snippetid
            })
            methodjson["snippets"].update({
                snippetid: updated_method,
                "snippetorder": snippetorder
            })
            methodjson["methodpath"] = methodjson["methodpath"].replace(
                "{", "<")
            methodjson["methodpath"] = methodjson["methodpath"].replace(
                "}", ">")
            if methodjson["methodpath"] != "":
                apicallpath = "/" + versionname + "/" + resource[
                    "resourcename"] + methodjson["methodpath"]
            else:
                apicallpath = "/" + versionname + "/" + resource["resourcename"]
            methodpath = "servicedesigns/" + serviceid + "/api/" + versionid + "/resources/" + resourceid + "/methods/" + methodid + ".json"
            methodjson["type_code"] = methodjson["type_code"].replace(
                "METHODID", methodid)
            methodjson["type_code"] = methodjson["type_code"].replace(
                "METHODPATH", apicallpath)
            codestring, requirement = consolidate_code(methodjson)
            codepath = "servicedesigns/" + serviceid + "/api/" + versionid + "/resources/" + resourceid + "/methods/" + methodid + "/code/" + methodid + ".py"
            codestatus, codestatusmessage, code = datafile.write_txt_file(
                codepath, codestring)
            if codestatus == "error":
                message = {"statusCode": 400, "errorMessage": code}
                return (message)
            reqstring = ""
            for i in requirement:
                reqstring = reqstring + i + "\n"
            # act["requirements"]=reqstring
            requirementpath = "servicedesigns/" + serviceid + "/api/" + versionid + "/resources/" + resourceid + "/methods/" + methodid + "/code/requirement.txt"
            requirementstatus, requirementstatusmessage, req = datafile.write_txt_file(
                requirementpath, reqstring)
            if requirementstatus == "error":
                message = {"statusCode": 400, "errorMessage": req}
                return (message)
            methstatus, methstatusmessage, methres = datafile.write_s3_file(
                methodpath, methodjson)
            if methstatus == "error":
                message = {"statusCode": 400, "errorMessage": code}
                return (message)
            if methstatus == "success" and codestatus == "success" and requirementstatus == "success":
                message = {
                    "statusCode": 200,
                    "body": methodjson,
                    "snippetid": snippetid
                }
        return (message)
    else:
        versionname = request.json["versionname"]
        snippetname = request.json["snippetname"]
        functionname = request.json["functionname"]
        import_code = "import boto3,json\n"
        function_code = "def " + functionname + "():\r\n  # Write your code here\r\n  return()\r\n"
        data_code = ""
        methodpath = "servicedesigns/" + serviceid + "/api/" + versionid + "/resources/" + resourceid + "/methods/" + methodid + ".json"
        status, statusmessage, methodjson = datafile.load_s3_file(methodpath)
        dbtype = methodjson["dbtype"]
        if dbtype == "dynamodb":
            data_code = "dynamodb=boto3.resource('dynamodb',region_name='us-east-1')\n"
        function_call = ""
        snippetid = uniqid("sn_")
        custom_snippet = {
            "import_code": import_code,
            "data_code": data_code,
            "function_code": function_code,
            "function_call": function_call,
            "name": snippetname + functionname,
            "snippetid": snippetid,
            "requirements": []
        }
        snippetorder = methodjson["snippets"]["snippetorder"]
        snippetorder.append({
            "name": custom_snippet["name"],
            "snippetid": snippetid
        })
        methodjson["snippets"].update({
            snippetid: custom_snippet,
            "snippetorder": snippetorder
        })
        methodjson["methodpath"] = methodjson["methodpath"].replace("{", "<")
        methodjson["methodpath"] = methodjson["methodpath"].replace("}", ">")
        if methodjson["methodpath"] != "":
            apicallpath = "/" + versionname + "/" + resource[
                "resourcename"] + methodjson["methodpath"]
        else:
            apicallpath = "/" + versionname + "/" + resource["resourcename"]
        methodpath = "servicedesigns/" + serviceid + "/api/" + versionid + "/resources/" + resourceid + "/methods/" + methodid + ".json"
        methodjson["type_code"] = methodjson["type_code"].replace(
            "METHODID", methodid)
        methodjson["type_code"] = methodjson["type_code"].replace(
            "METHODPATH", apicallpath)
        codestring, requirement = consolidate_code(methodjson)
        codepath = "servicedesigns/" + serviceid + "/api/" + versionid + "/resources/" + resourceid + "/methods/" + methodid + "/code/" + methodid + ".py"
        codestatus, codestatusmessage, code = datafile.write_txt_file(
            codepath, codestring)
        if codestatus == "error":
            message = {"statusCode": 400, "errorMessage": code}
            return (message)
        reqstring = ""
        for i in requirement:
            reqstring = reqstring + i + "\n"
        # act["requirements"]=reqstring
        requirementpath = "servicedesigns/" + serviceid + "/api/" + versionid + "/resources/" + resourceid + "/methods/" + methodid + "/code/requirement.txt"
        requirementstatus, requirementstatusmessage, req = datafile.write_txt_file(
            requirementpath, reqstring)
        if requirementstatus == "error":
            message = {"statusCode": 400, "errorMessage": req}
            return (message)
        methstatus, methstatusmessage, methres = datafile.write_s3_file(
            methodpath, methodjson)
        if methstatus == "error":
            message = {"statusCode": 400, "errorMessage": code}
            return (message)
        if methstatus == "success" and codestatus == "success" and requirementstatus == "success":
            message = {
                "statusCode": 200,
                "body": methodjson,
                "snippetid": snippetid
            }
        return (message)