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)
def do_list_artifact(args, config): """ Lists out all the state associating with the UUIDs in the Transaction Family : Artifact 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: ArtifactException: * If failed to retrieve the list """ b_url = config.get("DEFAULT", "url") client = ArtifactBatch(base_url=b_url) result = client.list_artifact() if result is not None: result.sort(key=lambda x: x["timestamp"], reverse=True) result = json.dumps(result) output = ret_msg("success", "OK", "ListOf:ArtifactRecord", result) print(output) else: raise ArtifactException("Could not retrieve artifact listing.")
def do_add_sub_artifact(args, config): artifact_id = args.artifact_id sub_artifact_id = args.sub_artifact_id path = args.path 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 = ArtifactBatch(base_url=url) response = client.add_artifact(private_key,public_key,artifact_id,sub_artifact_id,path) print_msg(response) else: print(output) else: print(output)
def do_add_uri_to_artifact(args, config): """ Establishes relationship between Artifact and URI in the state associating with the UUID of 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. """ deleteURI = args.delete artifact_id = args.artifact_id version = args.version checksum = args.checksum content_type = args.content_type size = args.size uri_type = args.uri_type location = args.location 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.add_uri(private_key, public_key, artifact_id, version, checksum, content_type, size, uri_type, location, deleteURI) print_msg(response, "AddURI") else: print(output) else: print(output)
def api_do_amend_artifact(args, config): """ API version of "do_amend_artifact" function. """ param_check = _payload_check_(args) if param_check[0]: return ret_msg("failed", param_check[1], "EmptyRecord", "{}") artifact_id = args["artifact"]["uuid"] artifact_alias = _null_cast(args["artifact"], "alias") artifact_name = _null_cast(args["artifact"], "name") artifact_type = _null_cast(args["artifact"], "content_type") artifact_checksum = _null_cast(args["artifact"], "checksum") artifact_label = _null_cast(args["artifact"], "label") artifact_openchain = _null_cast(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.amend_artifact(private_key, public_key, artifact_id, artifact_alias, artifact_name, artifact_type, artifact_checksum, artifact_label, artifact_openchain) return print_msg(response, "amend") else: return output else: return output
def api_do_add_uri_to_artifact(args, config, del_flag=False): """ API version of "do_add_uri_to_artifact" function. """ param_check = _payload_check_(args, cmd="AddURI") if param_check[0]: return ret_msg("failed", param_check[1], "EmptyRecord", "{}") artifact_id = args["uuid"] version = args["uri"]["version"] checksum = args["uri"]["checksum"] content_type = args["uri"]["content_type"] size = args["uri"]["size"] uri_type = args["uri"]["uri_type"] location = args["uri"]["location"] 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.add_uri(private_key, public_key, artifact_id, version, checksum, content_type, size, uri_type, location, del_flag) return print_msg(response, "AddURI") else: return output else: return output
def do_list_artifact(args, config): url = config.get('DEFAULT', 'url') client = ArtifactBatch(base_url=url) result = client.list_artifact() if (result is not None): result = refine_output_artifact(str(result)) result = amend_artifact_fields(result) output = ret_msg("success","OK","ListOf:ArtifactRecord",result) print(output) else: raise ArtifactException("Could not retrieve artifact listing")
def do_retrieve_artifact(args, config): artifact_id = args.artifact_id url = config.get('DEFAULT', 'url') client = ArtifactBatch(base_url=url) result = client.retrieve_artifact(artifact_id).decode() if result is not None: output = '' result = filter_output(result) output = ret_msg("success","OK","ArtifactRecord",result) print(output) else: raise ArtifactException("Artifact not found {}".format(artifact_id))
def do_retrieve_artifact(args, config): """ Retrieves 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. Raises: ArtifactException: * If failed to retrieve the uuid """ all_flag = args.all range_flag = args.range artifact_id = args.artifact_id if range_flag != None: all_flag = True b_url = config.get("DEFAULT", "url") client = ArtifactBatch(base_url=b_url) data = client.retrieve_artifact(artifact_id, all_flag, range_flag) if data is not None: if all_flag == False: output = ret_msg("success", "OK", "ArtifactRecord", data.decode()) else: output = ret_msg("success", "OK", "ArtifactRecord", data) print(output) else: raise ArtifactException("Artifact not found {}".format(artifact_id))
def api_do_list_artifact(config): """ API version of "do_list_artifact" function. """ b_url = config.get("DEFAULT", "url") client = ArtifactBatch(base_url=b_url) artifact_list = client.list_artifact() if artifact_list is not None: artifact_list.sort(key=lambda x: x["timestamp"], reverse=True) result = json.dumps(artifact_list) output = ret_msg("success", "OK", "ListOf:ArtifactRecord", result) return output else: return ret_msg( "failed", "ArtifactException : Could not retrieve artifact listing.", "ArtifactRecord", "{}")
def do_create(args, config): artifact_id = args.artifact_id alias = args.alias artifact_name = args.artifact_name artifact_type = args.artifact_type artifact_checksum = args.artifact_checksum label = args.label openchain = args.openchain private_key = args.private_key public_key = args.public_key timestampUTC = datetime.utcnow() timestamp = str(timestampUTC) 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 = ArtifactBatch(base_url=url) response = client.create(private_key,public_key,artifact_id,alias,artifact_name,artifact_type,artifact_checksum,label,openchain,timestamp) print_msg(response) else: print(output) else: print(output)
def api_do_retrieve_artifact(artifact_id, config, all_flag=False, range_flag=None): """ API version of "do_retrieve_artifact" function. """ if range_flag != None: all_flag = True b_url = config.get("DEFAULT", "url") client = ArtifactBatch(base_url=b_url) data = client.retrieve_artifact(artifact_id, all_flag, range_flag) if data is not None: if all_flag == False: output = ret_msg("success", "OK", "ArtifactRecord", data.decode()) else: if range_flag == None: output = ret_msg("success", "OK", "ArtifactRecord", data) else: if len(data) != 0: output = ret_msg("success", "OK", "ArtifactRecord", json.dumps(data[0])) else: output = ret_msg("success", "OK", "ArtifactRecord", "{}") return output else: return ret_msg( "failed", "ArtifactException : UUID {} does not exist." \ .format(artifact_id), "ArtifactRecord", "{}" )