Ejemplo n.º 1
0
def do_create_artifact(args, config):
    """
    Creates the state associating with the UUID in the
    Transaction Family : Artifact
    
    Args:
        args (ArgumentParser):
            ArgumentParser object containing required parameters
        config (ConfigParser): ConfigParser which contains the default url
        
    Returns:
        type: str
        String representing JSON object which allows the client to know that
        the call was either a success or a failure.
    
    """
    artifact_id = args.artifact_id
    artifact_alias = args.alias
    artifact_name = args.artifact_name
    artifact_type = args.artifact_type
    artifact_checksum = args.artifact_checksum
    artifact_label = args.label
    artifact_openchain = args.openchain
    private_key = args.private_key
    public_key = args.public_key

    payload = "{}"
    key = json.loads(payload)
    key["publickey"] = public_key
    key["privatekey"] = private_key
    key["allowedrole"] = [{"role": "admin"}, {"role": "member"}]
    payload = json.dumps(key)

    headers = {"content-type": "application/json"}
    response = requests.post("http://127.0.0.1:818/api/sparts/ledger/auth",
                             data=json.dumps(key),
                             headers=headers)
    output = response.content.decode("utf-8").strip()
    statusinfo = json.loads(output)

    if statusinfo.get("status") and statusinfo.get("message"):

        status = statusinfo["status"]
        message = statusinfo["message"]

        if status == "success" and message == "authorized":
            b_url = config.get("DEFAULT", "url")
            client = ArtifactBatch(base_url=b_url)
            response = client.create_artifact(private_key, public_key,
                                              artifact_id, artifact_alias,
                                              artifact_name, artifact_type,
                                              artifact_checksum,
                                              artifact_label,
                                              artifact_openchain)

            print_msg(response, "create")
        else:
            print(output)
    else:
        print(output)
Ejemplo n.º 2
0
def api_do_create_artifact(args, config):
    """
    API version of "do_create_artifact" function.
    """
    param_check = _payload_check_(args, creation=True)

    if param_check[0]:
        return ret_msg("failed", param_check[1], "EmptyRecord", "{}")

    artifact_id = args["artifact"]["uuid"]
    artifact_alias = args["artifact"]["alias"]
    artifact_name = args["artifact"]["name"]
    artifact_type = args["artifact"]["content_type"]
    artifact_checksum = args["artifact"]["checksum"]
    artifact_label = args["artifact"]["label"]
    artifact_openchain = args["artifact"]["openchain"]
    private_key = args["private_key"]
    public_key = args["public_key"]

    payload = "{}"
    key = json.loads(payload)
    key["publickey"] = public_key
    key["privatekey"] = private_key
    key["allowedrole"] = [{"role": "admin"}, {"role": "member"}]
    payload = json.dumps(key)

    headers = {"content-type": "application/json"}
    response = requests.post("http://127.0.0.1:818/api/sparts/ledger/auth",
                             data=json.dumps(key),
                             headers=headers)
    output = response.content.decode("utf-8").strip()
    statusinfo = json.loads(output)

    if statusinfo.get("status") and statusinfo.get("message"):

        status = statusinfo["status"]
        message = statusinfo["message"]

        if status == "success" and message == "authorized":
            b_url = config.get("DEFAULT", "url")
            client = ArtifactBatch(base_url=b_url)
            response = client.create_artifact(private_key, public_key,
                                              artifact_id, artifact_alias,
                                              artifact_name, artifact_type,
                                              artifact_checksum,
                                              artifact_label,
                                              artifact_openchain)

            return print_msg(response, "create")
        else:
            return output
    else:
        return output