Beispiel #1
0
def api_do_retrieve_part(pt_id, config, all_flag=False, range_flag=None):
    """
    API version of "do_retrieve_part" function.
    """
    if range_flag != None:
        all_flag = True
    
    b_url = config.get("DEFAULT", "url")
    client = PartBatch(base_url=b_url)
    data = client.retrieve_part(pt_id, all_flag, range_flag)
    
    if data is not None:
        
        if all_flag == False:
            output = ret_msg("success", "OK", "PartRecord", data.decode())
        else:
            if range_flag == None:
                output = ret_msg("success", "OK", "PartRecord", data)
            else:
                if len(data) != 0:
                    output = ret_msg(
                        "success", "OK", "PartRecord", json.dumps(data[0])
                    )
                else:
                    output = ret_msg("success", "OK", "PartRecord", "{}")
            
        return output
    else:
        return ret_msg(
                    "failed",
                    "PartException : UUID {} does not exist." \
                    .format(pt_id),
                    "PartRecord", "{}"
                )
Beispiel #2
0
def do_create(args, config):

    pt_id = args.pt_id
    pt_name = args.pt_name
    checksum = args.checksum
    version = args.version
    src_uri = args.src_uri
    licensing = args.licensing
    label = args.label
    description = args.description

    url = config.get('DEFAULT', 'url')
    key_file = config.get('DEFAULT', 'key_file')
    auth_user, auth_password = _get_auth_info(args)

    client = PartBatch(base_url=url, keyfile=key_file)

    response = client.create(pt_id,
                             pt_name,
                             checksum,
                             version,
                             src_uri,
                             licensing,
                             label,
                             description,
                             auth_user=auth_user,
                             auth_password=auth_password)
    print_msg(response)
Beispiel #3
0
def do_list_part(config):
    """
    Lists out all the state associating with the UUIDs in the
    Transaction Family : Part
    
    Args:
        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.
    
    Raises:
        PartException:
            * If failed to retrieve the list
            
    """
    b_url = config.get("DEFAULT", "url")
    client = PartBatch(base_url=b_url)
    result = client.list_part()

    if result is not None:
        result.sort(key=lambda x:x["timestamp"], reverse=True)
        result = json.dumps(result)
        
        output = ret_msg("success", "OK", "ListOf:PartRecord", result)
        
        print(output)
    else:
        raise PartException("Could not retrieve part listing.")
Beispiel #4
0
def add_Supplier(args, config):
    pt_id = args.pt_id
    supplier_id = args.supplier_id
    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':
            url = config.get('DEFAULT', 'url')
            client = PartBatch(base_url=url)
            response = client.add_supplier(pt_id, supplier_id, private_key,
                                           public_key)
            print_msg(response)
        else:
            print(output)
    else:
        print(output)
Beispiel #5
0
def do_amend_part(args, config):
    """
    Amends the state associating with the UUID in the Transaction Family : Part
    
    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.
    
    """
    pt_id = args.pt_id
    pt_name = args.pt_name
    checksum = args.checksum
    version = args.version
    alias = args.alias
    licensing = args.licensing
    label = args.label
    description = args.description
    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 = PartBatch(base_url=b_url)
            response = client.amend_part(pt_id, pt_name, checksum, version,
                                         alias, licensing, label, description,
                                         private_key, public_key)

            print_msg(response, "amend")
        else:
            print(output)
    else:
        print(output)
Beispiel #6
0
def api_do_amend_part(args, config):
    """
    API version of "do_amend_part" function.
    """
    param_check = _payload_check_(args)

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

    pt_id = args["part"]["uuid"]

    pt_name = _null_cast(args["part"], "name")
    checksum = _null_cast(args["part"], "checksum")
    version = _null_cast(args["part"], "version")
    alias = _null_cast(args["part"], "alias")
    licensing = _null_cast(args["part"], "licensing")
    label = _null_cast(args["part"], "label")
    description = _null_cast(args["part"], "description")

    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 = PartBatch(base_url=b_url)
            response = client.amend_part(pt_id, pt_name, checksum, version,
                                         alias, licensing, label, description,
                                         private_key, public_key)

            return print_msg(response, "amend")
        else:
            return output
    else:
        return output
Beispiel #7
0
def add_Supplier(args, config):
    pt_id = args.pt_id
    supplier_id = args.supplier_id

    url = config.get('DEFAULT', 'url')
    key_file = config.get('DEFAULT', 'key_file')

    client = PartBatch(base_url=url, keyfile=key_file)
    response = client.add_supplier(pt_id, supplier_id)

    print_msg(response)
Beispiel #8
0
def do_add_envelope(args, config):
    pt_id = args.pt_id
    envelope_id = args.envelope_id

    url = config.get('DEFAULT', 'url')
    key_file = config.get('DEFAULT', 'key_file')

    client = PartBatch(base_url=url, keyfile=key_file)
    response = client.add_envelope(pt_id, envelope_id)

    print_msg(response)
Beispiel #9
0
def add_Category(args, config):
    pt_id = args.pt_id
    category_id = args.category_id

    url = config.get('DEFAULT', 'url')
    key_file = config.get('DEFAULT', 'key_file')

    client = PartBatch(base_url=url, keyfile=key_file)
    response = client.add_category(pt_id, category_id)

    print_msg(response)
Beispiel #10
0
def do_create(args, config):

    #print("creating")
 
    pt_id = args.pt_id
    pt_name = args.pt_name
    checksum = args.checksum
    version = args.version
    alias = args.alias
    licensing = args.licensing
    label = args.label
    description = args.description
    private_key = args.private_key
    public_key = args.public_key

    # #
    # context = create_context('secp256k1')
    # private_key = context.new_random_private_key()
    # public_key = context.get_public_key(private_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'}
    #print("authorizing payload:", payload)

    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)
       
    #print("Output:", output)
    if statusinfo.get('status')and statusinfo.get('message'):
            
        status = statusinfo['status']
        message = statusinfo['message']
            
        if status == 'success' and message == 'authorized':
            #print("Creating Batch..")
            b_url = config.get('DEFAULT', 'url')
            client = PartBatch(base_url=b_url)
            response = client.create(
            pt_id,pt_name,checksum,version,alias,licensing,label,description,private_key,public_key
            )
            print_msg(response)
        else:
            print(output)
    else:
        print(output)
Beispiel #11
0
def do_add_organization(args, config):
    """
    Establishes relationship between Part and Organization in the state
    associating with the UUID of the Transaction Family : Part
    
    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.
    
    """
    del_flag   = args.delete
    
    pt_id           = args.pt_id
    organization_id = args.organization_id
    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 = PartBatch(base_url=b_url)
            response = client.add_organization(
                                pt_id, organization_id, private_key, public_key,
                                del_flag
                            )
                            
            print_msg(response, "AddOrganization")
        else:
            print(output)
    else:
        print(output)
Beispiel #12
0
def do_list_part(args, config):
    url = config.get('DEFAULT', 'url')

    client = PartBatch(base_url=url)
    result = client.list_part()

    if result is not None:
        result = refine_output(str(result))
        output = ret_msg("success", "OK", "ListOf:PartRecord", result)
        print(output)
    else:
        raise PartException("Could not retrieve part listing.")
Beispiel #13
0
def do_list_part(args, config):
    url = config.get('DEFAULT', 'url')
    key_file = config.get('DEFAULT', 'key_file')
    auth_user, auth_password = _get_auth_info(args)

    client = PartBatch(base_url=url, keyfile=key_file)

    result = client.list_part(auth_user=auth_user, auth_password=auth_password)

    if result is not None:
        output = refine_output(str(result))
        print(output)
    else:
        raise PartException("Could not retrieve part listing.")
Beispiel #14
0
def do_retrieve(args, config):

    pt_id = args.pt_id

    url = config.get('DEFAULT', 'url')

    client = PartBatch(base_url=url)

    result = client.retrieve_part(pt_id).decode()

    if result is not None:
        result = filter_output(str(result))
        output = ret_msg("success", "OK", "PartRecord", result)
        print(result)

    else:
        raise PartException("Part not found: {}".format(pt_id))
Beispiel #15
0
def api_do_add_artifact(args, config, del_flag=False):
    """
    API version of "do_add_artifact" function.
    """
    param_check = _payload_check_(args, cmd="AddArtifact")
    
    if param_check[0]:
        return ret_msg("failed", param_check[1], "EmptyRecord", "{}")
    
    pt_id       = args["relation"]["part_uuid"]
    artifact_id = args["relation"]["artifact_uuid"]
    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 = PartBatch(base_url=b_url)
            response = client.add_artifact(
                                pt_id, artifact_id, private_key, public_key,
                                del_flag
                            )
                            
            return print_msg(response, "AddArtifact")
        else:
            return output
    else:
        return output
Beispiel #16
0
def api_do_list_part(config):
    """
    API version of "do_list_part" function.
    """
    b_url = config.get("DEFAULT", "url")
    client = PartBatch(base_url=b_url)
    result = client.list_part()

    if result is not None:
        result.sort(key=lambda x: x["timestamp"], reverse=True)
        result = json.dumps(result)

        output = ret_msg("success", "OK", "ListOf:PartRecord", result)

        return output
    else:
        return ret_msg("failed",
                       "PartException : Could not retrieve part listing.",
                       "PartRecord", "{}")
Beispiel #17
0
def do_retrieve_part(args, config):
    """
    Retrieves the state associating with the UUID in the
    Transaction Family : Part
    
    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.
    
    Raises:
        PartException:
            * If failed to retrieve the uuid
    
    """
    all_flag = args.all
    range_flag = args.range
    
    pt_id = args.pt_id
    
    if range_flag != None:
        all_flag = True
    
    b_url = config.get("DEFAULT", "url")
    client = PartBatch(base_url=b_url)
    data = client.retrieve_part(pt_id, all_flag, range_flag)
    
    if data is not None:
        
        if all_flag == False:
            output = ret_msg("success", "OK", "PartRecord", data.decode())
        else:
            output = ret_msg("success", "OK", "PartRecord", data)
            
        print(output)
    else:
        raise PartException("Part not found: {}".format(pt_id))
Beispiel #18
0
def do_retrieve(args, config):

    pt_id = args.pt_id

    url = config.get('DEFAULT', 'url')
    key_file = config.get('DEFAULT', 'key_file')
    auth_user, auth_password = _get_auth_info(args)

    client = PartBatch(base_url=url, keyfile=key_file)

    result = client.retrieve_part(pt_id,
                                  auth_user=auth_user,
                                  auth_password=auth_password).decode()

    if result is not None:
        result = filter_output(str(result))
        print(result)

    else:
        raise PartException("Part not found: {}".format(pt_id))