def __init__(self, test_name=None, timeout=None, workflow_filename=None, new_user=False, delete_user=False): self._settings = self._get_settings() if new_user: random_str = str(random.randint(0, 10000)) + "_" + str(time.time()) random_user = hashlib.sha256(random_str.encode()).hexdigest() random_user = "******" + random_user[0:31] + "@knix.io" print("User: "******"User: "******"mfn_user"]) self._client = MfnClient() if workflow_filename is None: self._workflow_filename = self._settings[ "workflow_description_file"] else: self._workflow_filename = workflow_filename ind = self._workflow_filename.rfind("/") if ind != -1: self._workflow_folder = self._workflow_filename[:ind + 1] else: self._workflow_folder = "./" print("Workflow folder: " + self._workflow_folder) self._workflow_description = self._get_json_file( self._workflow_filename) if "name" in self._workflow_description: self._workflow_name = self._workflow_description["name"] else: self._workflow_name = self._workflow_filename[ 0:self._workflow_filename.rfind(".")] if test_name is not None: self._test_name = test_name else: self._test_name = self._workflow_filename if timeout is not None: self._settings["timeout"] = timeout self._log_clear_timestamp = int(time.time() * 1000.0 * 1000.0) # will be the deployed workflow object in self._client self._workflow = None self._deployment_error = "" self._workflow_resources = [] self.upload_workflow() self.deploy_workflow()
def get_client(self): if not (self.mfn_url and self.mfn_user and self.mfn_password): print("Please login") sys.exit(2) else: return MfnClient(self.mfn_url, self.mfn_user, self.mfn_password, self.mfn_name, self.proxies)
# 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 time import logging from mfn_sdk import MfnClient c = MfnClient() logging.basicConfig(level=logging.DEBUG) fn = c.add_function("echo") fn.source = { 'code': """ def handle(event, context): context.log("Echoing event: "+str(event)) return event """ } workflow = c.add_workflow("echo_wf") workflow.json = """{
def setUp(self): self._settings = self._get_settings() self._client = MfnClient()
""" zip: a script that exemplifies the upload of a function's custom ZIP file """ import requests import os import base64 import sys import datetime from zipfile import ZipFile from mfn_sdk import MfnClient import logging logging.basicConfig(level=logging.DEBUG) c = MfnClient('https://knix.io', '*****@*****.**', 'test123', proxies={}) """ This example uploads a given ZIP file to a function """ # Create a new function g = c.add_function('custom') # Create a zip file from the directory contents zip_name = "custom_function.zip" if os.path.exists(zip_name): os.remove(zip_name) for root, dirs, files in os.walk('.'): with ZipFile(zip_name, 'w') as zf: for fn in files: zf.write(fn)
# Copyright 2020 The KNIX Authors # # Licensed 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 base64 from mfn_sdk import MfnClient c = MfnClient('https://knix.io/mfn', '*****@*****.**', 'test123', proxies={''}) print("Retrieving all objects") for key in list(c.keys()): print(key, c.get(key))
# 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. """ transfer: a script that transfers all functions, workflows and objects from an existing account at one microfunctions platform to another """ from mfn_sdk import MfnClient import logging logging.basicConfig(level=logging.DEBUG) # The account to read from (tries to find default settings) c1 = MfnClient() # The account to write to c2 = MfnClient.load_json(filename="settings_target.json") print("Copying all contents of") print("User", c1.user, "at microfunctions", c1.url) print(" TO") print("User", c2.user, "at microfunctions", c2.url) for fn1 in c1.functions: print("Syncing function", fn1.name) fn2 = c2.add_function(fn1.name, fn1.runtime) s = fn1.source if 'zip' in s: print("Function ", fn1.name, "has type zip with", str(len(s['zip'])),