Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,30 @@ $fcClient->createFunction(
//Invoke function synchronously.
$fcClient->invokeFunction('service_name', 'function_name');

/*
Create function with initializer.
the current directory has a main.zip file (main.php which hava functions of my_handler and my_initializer)
set environment variables {'testKey': 'testValue'}
*/
$fcClient->createFunction(
'service_name_with_initializer',
array(
'functionName' => $functionName,
'handler' => 'index.handler',
'initializer' => 'index.initializer',
'runtime' => 'php7.2',
'memorySize' => 128,
'code' => array(
'zipFile' => base64_encode(file_get_contents(__DIR__ . '/main.zip')),
),
'description' => "test function with initializer",
'environmentVariables' => ['testKey' => 'testValue'],
)
);

//Invoke function synchronously.
$fcClient->invokeFunction('service_name_with_initializer', 'function_name');

//Create trigger, for example: oss trigger
$prefix = 'pre';
$suffix = 'suf';
Expand Down
13 changes: 7 additions & 6 deletions src/AliyunFC/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ private function doRequest($method, $path, $headers, $data = null, $query = [])
if ($query) {
$options['query'] = $query;
}

$res = $client->request($method, $url, $options);

$respStatusCode = $res->getStatusCode();
Expand Down Expand Up @@ -262,11 +261,13 @@ private function normalizeParams(&$opts) {
if (!(isset($opts['functionName']) && isset($opts['runtime']) && isset($opts['handler']) && isset($opts['code']))) {
throw new \Exception('functionName|handler|runtime|code parameters must be specified');
}
$opts['functionName'] = strval($opts['functionName']);
$opts['runtime'] = strval($opts['runtime']);
$opts['handler'] = strval($opts['handler']);
$opts['memorySize'] = isset($opts['memorySize']) ? intval($opts['memorySize']) : 256;
$opts['timeout'] = isset($opts['timeout']) ? intval($opts['timeout']) : 60;
$opts['functionName'] = strval($opts['functionName']);
$opts['runtime'] = strval($opts['runtime']);
$opts['handler'] = strval($opts['handler']);
$opts['initializer'] = isset($opts['initializer']) ? strval($opts['initializer']) : null;
$opts['memorySize'] = isset($opts['memorySize']) ? intval($opts['memorySize']) : 256;
$opts['timeout'] = isset($opts['timeout']) ? intval($opts['timeout']) : 60;
$opts['initializationTimeout'] = isset($opts['initializationTimeout']) ? intval($opts['initializationTimeout']) : 30;
}

public function createFunction($serviceName, $functionPayload, $headers = []) {
Expand Down
72 changes: 70 additions & 2 deletions test/client_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,15 @@ private function subTestListServices() {
$this->assertEquals($services[0]['serviceName'], $prefix . 'abd');
}

private function checkFunction($function, $functionName, $desc, $runtime = 'php7.2', $handler = 'index.handler', $envs = ['TestKey' => 'TestValue']) {
private function checkFunction($function, $functionName, $desc, $runtime = 'php7.2',
$handler = 'index.handler', $envs = ['TestKey' => 'TestValue'], $initializer=null) {
$etag = $function['headers']['Etag'][0];
$this->assertTrue($etag != '');
$function = $function['data'];
$this->assertEquals($function['functionName'], $functionName);
$this->assertEquals($function['runtime'], $runtime);
$this->assertEquals($function['handler'], $handler);
$this->assertEquals($function['initializer'], $initializer);
$this->assertEquals($function['description'], $desc);
$this->assertEquals($function['environmentVariables'], $envs);
$this->assertTrue(isset($function['codeChecksum']));
Expand All @@ -261,6 +263,7 @@ private function checkFunction($function, $functionName, $desc, $runtime = 'php7
$this->assertTrue(isset($function['functionId']));
$this->assertTrue(isset($function['memorySize']));
$this->assertTrue(isset($function['timeout']));
$this->assertTrue(isset($function['initializationTimeout']));

$serviceName = $this->serviceName;
$checksum = $function['codeChecksum'];
Expand All @@ -269,6 +272,7 @@ private function checkFunction($function, $functionName, $desc, $runtime = 'php7
$this->assertEquals($function['functionName'], $functionName);
$this->assertEquals($function['runtime'], $runtime);
$this->assertEquals($function['handler'], $handler);
$this->assertEquals($function['initializer'], $initializer);
$this->assertEquals($function['description'], $desc);

$code = $this->fcClient->getFunctionCode($serviceName, $functionName);
Expand All @@ -285,11 +289,75 @@ private function checkFunction($function, $functionName, $desc, $runtime = 'php7
$this->assertContains('"ErrorMessage":"the resource being modified has been changed"', $err);
}

public function testFunctionCRUDInitialize() {
$serviceName = $this->serviceName;
$serviceDesc = "测试的service, php sdk 创建";
$this->fcClient->createService($serviceName, $serviceDesc);
$functionName = "test_initialize";

$ret = $this->fcClient->createFunction(
$serviceName,
array(
'functionName' => $functionName,
'handler' => 'counter.handler',
'initializer' => 'counter.initializer',
'runtime' => 'php7.2',
'memorySize' => 128,
'code' => array(
'zipFile' => base64_encode(file_get_contents(__DIR__ . '/counter.zip')),
),
'description' => "test function",
'environmentVariables' => ['TestKey' => 'TestValue'],
)
);
$this->checkFunction($ret, $functionName, 'test function', $runtime = 'php7.2', $handler='counter.handler', $envs=['TestKey' => 'TestValue'], $initializer='counter.initializer');

$invkRet = $this->fcClient->invokeFunction($serviceName, $functionName);
$this->assertEquals($invkRet['data'], '2');

$invkRet = $this->fcClient->invokeFunction($serviceName, $functionName, '', ['x-fc-invocation-type' => 'Async']);
$this->assertEquals($invkRet['data'], '');
$this->assertTrue(isset($invkRet['headers']['X-Fc-Request-Id']));

$ret = $this->fcClient->updateFunction(
$serviceName,
$functionName,
array(
'functionName' => $functionName,
'handler' => 'counter.handler',
'initializer' => 'counter.initializer',
'runtime' => 'php7.2',
'memorySize' => 128,
'code' => array(
'zipFile' => base64_encode(file_get_contents(__DIR__ . '/counter.zip')),
),
'description' => "test update function",
'environmentVariables' => ['newTestKey' => 'newTestValue'],
)
);
$this->checkFunction($ret, $functionName, 'test update function', $runtime = 'php7.2', $handler='counter.handler', $envs=['newTestKey' => 'newTestValue'], $initializer='counter.initializer');
$etag = $ret['headers']['Etag'][0];
# now success with valid etag.
$this->fcClient->deleteFunction($serviceName, $functionName, $headers = ['if-match' => $etag]);

$err = '';
try {
$this->fcClient->getFunction($serviceName, $functionName);
} catch (Exception $e) {
$err = $e->getMessage();
}
$this->assertTrue($err != '');

$this->subTestListFunctions($serviceName);

$this->clearAllTestRes();
}

public function testFunctionCRUDInvoke() {
$serviceName = $this->serviceName;
$serviceDesc = "测试的service, php sdk 创建";
$this->fcClient->createService($serviceName, $serviceDesc);
$functionName = "test";
$functionName = "test_invoke";

$ret = $this->fcClient->createFunction(
$serviceName,
Expand Down
Binary file added test/counter.zip
Binary file not shown.