def do_list_organization(args, config):
    """
    Lists out all the state associating with the UUIDs in the
    Transaction Family : Organization
    
    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:
        OrganizationException:
            * If failed to retrieve the list
            
    """
    b_url = config.get("DEFAULT", "url")
    client = OrganizationBatch(base_url=b_url)
    result = client.list_organization()

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

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

        print(output)
    else:
        raise OrganizationException("Could not retrieve organization listing.")
Beispiel #2
0
def do_addpart(args, config):
    id = args.id
    part_id = args.part_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 = OrganizationBatch(base_url=b_url)
            response = client.add_part(id, part_id, private_key, public_key)
            print_msg(response)
        else:
            print(output)
    else:
        print(output)
def do_amend_organization(args, config):
    """
    Amends the state associating with the UUID in the
    Transaction Family : Organization
    
    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.
    
    """
    org_id = args.org_id
    org_alias = args.alias
    org_name = args.name
    org_type = args.type
    description = args.description
    org_url = args.url
    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 = OrganizationBatch(base_url=b_url)
            response = client.amend_organization(org_id, org_alias, org_name,
                                                 org_type, description,
                                                 org_url, private_key,
                                                 public_key)

            print_msg(response, "amend")
        else:
            print(output)
    else:
        print(output)
def api_do_amend_organization(args, config):
    """
    API version of "do_amend_organization" function.
    """
    param_check = _payload_check_(args)

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

    org_id = args["organization"]["uuid"]

    org_alias = _null_cast(args["organization"], "alias")
    org_name = _null_cast(args["organization"], "name")
    org_type = _null_cast(args["organization"], "type")
    description = _null_cast(args["organization"], "description")
    org_url = _null_cast(args["organization"], "url")

    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 = OrganizationBatch(base_url=b_url)
            response = client.amend_organization(org_id, org_alias, org_name,
                                                 org_type, description,
                                                 org_url, private_key,
                                                 public_key)

            return print_msg(response, "amend")
        else:
            return output
    else:
        return output
Beispiel #5
0
def do_retrieve(args, config):
    id = args.id

    b_url = config.get('DEFAULT', 'url')
    client = OrganizationBatch(base_url=b_url)

    data = client.retrieve_organization(id)
    if data is not None:
        data = filter_output(str(data))
        output = ret_msg("success", "OK", "OrganizationRecord", data)
        print(output)
    else:
        raise OrganizationException("Organization not found: {}".format(id))
Beispiel #6
0
def do_list_organization(args, config):
    b_url = config.get('DEFAULT', 'url')

    client = OrganizationBatch(base_url=b_url)

    result = client.list_organization()

    if result is not None:
        result = refine_output_organization(str(result))
        result = refine_output(result)
        output = ret_msg("success", "OK", "ListOf:OrganizationRecord", result)

        print(output)
    else:
        raise OrganizationException("Could not retrieve organization listing.")
def api_do_addpart(args, config, del_flag=False):
    """
    API version of "do_addpart" function.
    """
    param_check = _payload_check_(args, cmd="AddPart")

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

    org_id = args["relation"]["organization_uuid"]
    pt_id = args["relation"]["part_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 = OrganizationBatch(base_url=b_url)
            response = client.add_part(org_id, pt_id, private_key, public_key,
                                       del_flag)

            return print_msg(response, "AddPart")
        else:
            return output
    else:
        return output
def do_retrieve_organization(args, config):
    """
    Retrieves the state associating with the UUID in the
    Transaction Family : Organization
    
    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:
        OrganizationException:
            * If failed to retrieve the uuid
    
    """
    all_flag = args.all
    range_flag = args.range

    org_id = args.org_id

    if range_flag != None:
        all_flag = True

    b_url = config.get("DEFAULT", "url")
    client = OrganizationBatch(base_url=b_url)
    data = client.retrieve_organization(org_id, all_flag, range_flag)

    if data is not None:

        if all_flag == False:
            output = ret_msg("success", "OK", "OrganizationRecord",
                             data.decode())
        else:
            output = ret_msg("success", "OK", "OrganizationRecord", data)

        print(output)
    else:
        raise OrganizationException(
            "Organization not found: {}".format(org_id))
def api_do_list_organization(config):
    """
    API version of "do_list_organization" function.
    """
    b_url = config.get("DEFAULT", "url")
    client = OrganizationBatch(base_url=b_url)
    organization_list = client.list_organization()

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

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

        return output
    else:
        return ret_msg(
            "failed",
            "{} : Could not retrieve {}.".format("OrganizationException",
                                                 "organization listing"),
            "OrganizationRecord", "{}")
def api_do_retrieve_organization(org_id,
                                 config,
                                 all_flag=False,
                                 range_flag=None):
    """
    API version of "do_retrieve_organization" function.
    """
    if range_flag != None:
        all_flag = True

    b_url = config.get("DEFAULT", "url")
    client = OrganizationBatch(base_url=b_url)
    data = client.retrieve_organization(org_id, all_flag, range_flag)

    if data is not None:

        if all_flag == False:
            output = ret_msg("success", "OK", "OrganizationRecord",
                             data.decode())
        else:
            if range_flag == None:
                output = ret_msg("success", "OK", "OrganizationRecord", data)
            else:
                if len(data) != 0:
                    output = ret_msg("success", "OK", "OrganizationRecord",
                                     json.dumps(data[0]))
                else:
                    output = ret_msg("success", "OK", "OrganizationRecord",
                                     "{}")

        return output
    else:
        return ret_msg(
                    "failed",
                    "OrganizationException : UUID {} does not exist." \
                    .format(org_id),
                    "OrganizationRecord", "{}"
                )