Skip to content

Commit

Permalink
Merge a8a6d3c into 610af52
Browse files Browse the repository at this point in the history
  • Loading branch information
Arlenmbx committed Oct 21, 2018
2 parents 610af52 + a8a6d3c commit a850022
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 10 deletions.
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ Getting started
# set environment variables {'testKey': 'testValue'}
client.create_function('service_name', 'function_name', 'python3', 'main.my_handler', codeZipFile = 'main.zip', environmentVariables = {'testKey': 'testValue'})
# Create function with initailizer
# main.my_initializer is the entry point of initializer interface
client.create_function('service_name', 'function_name', 'python3', 'main.my_handler', "main.my_initializer", codeZipFile = 'main.zip', environmentVariables = {'testKey': 'testValue'})
# Invoke function synchronously.
client.invoke_function('service_name', 'function_name')
Expand Down
28 changes: 28 additions & 0 deletions fc2/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ def _check_function_param_valid(self, codeZipFile, codeDir, codeOSSBucket, codeO

def create_function(
self, serviceName, functionName, runtime, handler,
initializer=None, initializationTimeout=30,
codeZipFile=None, codeDir=None, codeOSSBucket=None, codeOSSObject=None,
description=None, memorySize=256, timeout=60, headers={}, environmentVariables=None):
"""
Expand All @@ -375,13 +376,15 @@ def create_function(
:param functionName: (required, string) the name of the function.
:param runtime: (required, string) the runtime type. For example, nodejs4.4, python2.7 and etc.
:param handler: (required, string) the entry point of the function.
:param initializer: (required, string) the entry point of the initializer.
:param codeZipFile: (optional, string) the file path of the zipped code.
:param codeDir: (optional, string) the directory of the code.
:param codeOSSBucket: (optional, string) the oss bucket where the code located in.
:param codeOSSObject: (optional, string) the zipped code stored as a OSS object.
:param description: (optional, string) the readable description of the function.
:param memorySize: (optional, integer) the memory size of the function, in MB.
:param timeout: (optional, integer) the max execution time of the function, in second.
:param initializationTimeout: (optional, integer) the max execution time of the initializer, in second.
:param environmentVariables: (optional, dict) the environment variables of the function, both key and value are string type.
:param headers, optional
1, 'x-fc-trace-id': string (a uuid to do the request tracing)
Expand All @@ -397,15 +400,19 @@ def create_function(
'functionId': 'string',
'functionName': 'string',
'handler': 'string',
'initializer': 'string',
'lastModifiedTime': 'string',
'memorySize': 512, // in MB
'runtime': 'string',
'timeout': 60, // in second
'initializationTimeout': 30 // in second
}
"""
serviceName, functionName, runtime, handler, memorySize, timeout = \
str(serviceName), str(functionName), str(runtime), str(handler), int(memorySize), int(timeout)

initializer = str(initializer) if initializer else initializer
initializationTimeout = int(initializationTimeout) if initializationTimeout else initializationTimeout
codeZipFile = str(codeZipFile) if codeZipFile else codeZipFile
codeDir = str(codeDir) if codeDir else codeDir
codeOSSBucket = str(codeOSSBucket) if codeOSSBucket else codeOSSBucket
Expand Down Expand Up @@ -437,9 +444,15 @@ def create_function(
if memorySize:
payload['memorySize'] = memorySize

if initializer:
payload['initializer'] = initializer

if timeout:
payload['timeout'] = timeout

if initializationTimeout:
payload['initializationTimeout'] = initializationTimeout

if environmentVariables != None:
payload['environmentVariables'] = environmentVariables

Expand All @@ -449,6 +462,7 @@ def create_function(

def update_function(
self, serviceName, functionName,
initializer=None, initializationTimeout=None,
codeZipFile=None, codeDir=None, codeOSSBucket=None, codeOSSObject=None,
description=None, handler=None, memorySize=None, runtime=None, timeout=None,
headers={}, environmentVariables=None):
Expand All @@ -458,13 +472,15 @@ def update_function(
:param functionName: (required, string) the name of the function.
:param runtime: (required, string) the runtime type. For example, nodejs4.4, python2.7 and etc.
:param handler: (required, string) the entry point of the function.
:param initializer: (required, string) the entry point of the initializer.
:param codeZipFile: (optional, string) the file path of the zipped code.
:param codeDir: (optional, string) the directory of the code.
:param codeOSSBucket: (optional, string) the oss bucket where the code located in.
:param codeOSSObject: (optional, string) the zipped code stored as a OSS object.
:param description: (optional, string) the readable description of the function.
:param memorySize: (optional, integer) the memory size of the function, in MB.
:param timeout: (optional, integer) the max execution time of the function, in second.
:param initializationTimeout: (optional, integer) the max execution time of the initializer, in second.
:param etag: (optional, string) delete the service only when matched the given etag.
:param environmentVariables: (optional, dict) the environment variables of the function, both key and value are string type.
:param headers, optional
Expand All @@ -482,17 +498,21 @@ def update_function(
'functionId': 'string',
'functionName': 'string',
'handler': 'string',
'initializer': 'string',
'lastModifiedTime': 'string',
'memorySize': 512, // in MB
'runtime': 'string',
'timeout': 60, // in second
'initializationTimeout': 30, // in second
}
"""
serviceName, functionName = str(serviceName), str(functionName)
handler = str(handler) if handler else handler
initializer = str(initializer) if initializer else initializer
runtime = str(runtime) if runtime else runtime
memorySize = int(memorySize) if memorySize else memorySize
timeout = int(timeout) if timeout else timeout
initializationTimeout = int(initializationTimeout) if initializationTimeout else initializationTimeout
codeZipFile = str(codeZipFile) if codeZipFile else codeZipFile
codeDir = str(codeDir) if codeDir else codeDir
codeOSSBucket = str(codeOSSBucket) if codeOSSBucket else codeOSSBucket
Expand All @@ -509,6 +529,9 @@ def update_function(
if handler:
payload['handler'] = handler

if initializer:
payload['initializer'] = initializer

if codeZipFile:
# codeZipFile has highest priority.
file = open(codeZipFile, 'rb')
Expand All @@ -532,6 +555,9 @@ def update_function(
if timeout:
payload['timeout'] = timeout

if initializationTimeout:
payload['initializationTimeout'] = initializationTimeout

if environmentVariables != None:
payload['environmentVariables'] = environmentVariables

Expand Down Expand Up @@ -623,10 +649,12 @@ def list_functions(self, serviceName, limit=None, nextToken=None, prefix=None, s
'functionId': 'string',
'functionName': 'string',
'handler': 'string',
'initializer': 'string',
'lastModifiedTime': 'string',
'memorySize': 512, // in MB
'runtime': 'string',
'timeout': 60, // in second
'initializationTimeout': 30, // in second
},
...
],
Expand Down
Binary file added test/counter/counter.zip
Binary file not shown.
17 changes: 17 additions & 0 deletions test/counter/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-

import logging

counter = 0
def my_initializer(context):
logger = logging.getLogger()
global counter
counter += 1
logger.info(counter)

def my_handler(event, context):
logger = logging.getLogger()
global counter
counter += 1
logger.info(counter)
return counter
66 changes: 56 additions & 10 deletions test/function_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,27 @@ def tearDownClass(cls):

def test_create(self):
functionName= 'test_create_' + ''.join(random.choice(string.ascii_lowercase) for _ in range(8))
desc = u'这是测试function'
desc1 = u'这是测试function'
logging.info('Create function: {0}'.format(functionName))
function = self.client.create_function(
function1 = self.client.create_function(
self.serviceName, functionName,
handler='main.my_handler', runtime='python2.7', codeDir='test/hello_world', description=desc, environmentVariables={'testKey': 'testValue'})
self.check_function(function, functionName, desc, 'python2.7')
function = function.data
self.assertEqual(function['environmentVariables']['testKey'], 'testValue')


def check_function(self, function, functionName, desc, runtime = 'python2.7'):
handler='main.my_handler', runtime='python2.7', codeDir='test/hello_world', description=desc1, environmentVariables={'testKey': 'testValue'})
self.check_function(function1, functionName, desc1, 'python2.7')
function1 = function1.data
self.assertEqual(function1['environmentVariables']['testKey'], 'testValue')

# test create function with initializer
functionName2= 'test_create_' + ''.join(random.choice(string.ascii_lowercase) for _ in range(8))
desc2 = u'test for initializer'
function2 = self.client.create_function(
self.serviceName, functionName2,
handler='main.my_handler', runtime='python2.7', codeDir='test/counter', initializer='main.my_initializer',
description=desc2, environmentVariables={'testKey': 'testValue'})
self.check_function(function2, functionName2, desc2, 'python2.7', 'main.my_initializer')
function2 = function2.data
self.assertEqual(function2['environmentVariables']['testKey'], 'testValue')

def check_function(self, function, functionName, desc, runtime = 'python2.7', initializer = None, initializationTimeout = None):
etag = function.headers['etag']
self.assertNotEqual(etag, '')
function = function.data
Expand All @@ -70,14 +80,20 @@ def check_function(self, function, functionName, desc, runtime = 'python2.7'):
self.assertTrue('functionId' in function)
self.assertTrue('memorySize' in function)
self.assertTrue('timeout' in function)

if desc == u'test for initializer':
self.assertEqual(function['initializer'], initializer)
self.assertEqual(function['initializationTimeout'], 30)

checksum = function['codeChecksum']
function = self.client.get_function(self.serviceName, functionName, headers ={'x-fc-trace-id':str(uuid.uuid4())})
function = function.data
self.assertEqual(function['functionName'], functionName)
self.assertEqual(function['runtime'], runtime)
self.assertEqual(function['handler'], 'main.my_handler')
self.assertEqual(function['description'], desc)
if desc == u'test for initializer':
self.assertEqual(function['initializer'], initializer)
self.assertEqual(function['initializationTimeout'], 30)

code = self.client.get_function_code(self.serviceName, functionName)
code = code.data
Expand Down Expand Up @@ -135,6 +151,21 @@ def test_update(self):
self.client.update_function(self.serviceName, functionName, codeZipFile='test/hello_world/hello_world.zip', runtime = 10)
self.client.delete_function(self.serviceName, functionName)

# test update function with initializer
functionName = 'test_update_with_initializer_' + ''.join(random.choice(string.ascii_lowercase) for _ in range(8))
logging.info('Create function: {0}'.format(functionName))
self.client.create_function(
self.serviceName, functionName,
handler='main.my_handler', initializer='main.my_initializer' ,runtime='python2.7', codeDir='test/counter', environmentVariables={'testKey0':'testValue0', 'testKey1':'testValue1'})
desc = 'function description'
func = self.client.update_function(self.serviceName, functionName, codeDir='test/counter', initializationTimeout=60, description=desc, environmentVariables={'newTestKey':'newTestValue'})
etag = func.headers['etag']
self.assertNotEqual(etag, '')
func = func.data
self.assertEqual(func['description'], desc)
self.assertEqual(func['initializationTimeout'], 60)
self.assertEqual(func['environmentVariables'], {'newTestKey':'newTestValue'})
self.client.delete_function(self.serviceName, functionName)

def _clear_list_function(self):
prefix = 'test_list_'
Expand Down Expand Up @@ -234,6 +265,21 @@ def test_list(self):
self.assertTrue(functions[0]['functionName'], prefix + 'abd')
self._clear_list_function()

def test_initialize(self):
functionName = 'test_invoke_counter_' + ''.join(random.choice(string.ascii_lowercase) for _ in range(8))
logging.info('create function: {0}'.format(functionName))
self.client.create_function(
self.serviceName, functionName,
handler='main.my_handler', runtime='python2.7',
initializer='main.my_initializer',
codeZipFile='test/counter/counter.zip')

r = self.client.invoke_function(self.serviceName, functionName)
self.assertEqual(r.data.decode('utf-8'), '2')
r = self.client.invoke_function(self.serviceName, functionName)
self.assertEqual(r.data.decode('utf-8'), '3')
self.client.delete_function(self.serviceName, functionName)

def test_invoke(self):
helloWorld= 'test_invoke_hello_world_' + ''.join(random.choice(string.ascii_lowercase) for _ in range(8))
logging.info('create function: {0}'.format(helloWorld))
Expand Down

0 comments on commit a850022

Please sign in to comment.