blob: 71093744f1b5122e472812f3ae65ed4a7ea7f3ff [file] [log] [blame]
#!/usr/bin/env python3
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import sys
from teaclave import FunctionArgument
from utils import USER_ID, USER_PASSWORD, connect_authentication_service, connect_frontend_service, PlatformAdmin
class MesaPyEchoExample:
def __init__(self, user_id, user_password):
self.user_id = user_id
self.user_password = user_password
def echo(self,
payload_file="mesapy_echo_payload.py",
message="Hello, Teaclave!"):
with connect_authentication_service() as client:
print(f"[+] {self.user_id} login")
token = client.user_login(self.user_id, self.user_password)
client = connect_frontend_service()
metadata = {"id": self.user_id, "token": token}
client.metadata = metadata
print("[+] registering function")
with open(payload_file, "rb") as f:
payload = f.read()
function_id = client.register_function(
name="mesapy-echo",
description="An echo function implemented in Python",
executor_type="python",
payload=list(payload),
arguments=[FunctionArgument("message")])
print("[+] creating task")
task_id = client.create_task(function_id=function_id,
function_arguments={"message": message},
executor="mesapy")
print("[+] invoking task")
client.invoke_task(task_id)
print("[+] getting result")
result = client.get_task_result(task_id)
print("[+] done")
client.close()
return bytes(result)
def main():
example = MesaPyEchoExample(USER_ID, USER_PASSWORD)
if len(sys.argv) == 2:
message = sys.argv[1]
rt = example.echo(message=message)
elif len(sys.argv) == 3:
payload = sys.argv[1]
message = sys.argv[2]
rt = example.echo(payload, message)
else:
rt = example.echo()
print("[+] function return: ", rt)
if __name__ == '__main__':
main()