-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feat.] Function invocation methods (#510)
[Feat.] Function invocation methods Part of #438 Implements: https://docs.otc.t-systems.com/function-graph/api-ref/apis/function_invocation/index.html Reviewed-by: Artem Lifshits
- Loading branch information
1 parent
5eacede
commit 28fcc00
Showing
12 changed files
with
314 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ FunctionGraph Resources | |
:maxdepth: 1 | ||
|
||
v2/function | ||
v2/function_invocation |
13 changes: 13 additions & 0 deletions
13
doc/source/sdk/resources/function_graph/v2/function_invocation.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
otcextensions.sdk.function_graph.v2.function_invocation | ||
======================================================= | ||
|
||
.. automodule:: otcextensions.sdk.function_graph.v2.function_invocation | ||
|
||
The FunctionInvocation Class | ||
---------------------------- | ||
|
||
The ``FunctionInvocation`` class inherits from | ||
:class:`~otcextensions.sdk.sdk_resource.Resource`. | ||
|
||
.. autoclass:: otcextensions.sdk.function_graph.v2.function_invocation.FunctionInvocation | ||
:members: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#!/usr/bin/env python3 | ||
# 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. | ||
""" | ||
Function execution asynchronously | ||
""" | ||
import openstack | ||
from otcextensions import sdk | ||
|
||
openstack.enable_logging(True) | ||
conn = openstack.connect(cloud='otc') | ||
sdk.register_otc_extensions(conn) | ||
|
||
func_urn = ('urn:fss:eu-de:45c274f200d2498683982c8741fb76ac:' | ||
'function:default:access-mysql-js-1213-1737554083545:latest') | ||
inv = conn.functiongraph.executing_function_asynchronously( | ||
func_urn, attrs={'a': 'b'} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#!/usr/bin/env python3 | ||
# 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. | ||
""" | ||
Function execution synchronously | ||
""" | ||
import openstack | ||
from otcextensions import sdk | ||
|
||
openstack.enable_logging(True) | ||
conn = openstack.connect(cloud='otc') | ||
sdk.register_otc_extensions(conn) | ||
|
||
func_urn = ('urn:fss:eu-de:45c274f200d2498683982c8741fb76ac:' | ||
'function:default:access-mysql-js-1213-1737554083545:latest') | ||
inv = conn.functiongraph.executing_function_synchronously( | ||
func_urn, attrs={'a': 'b'} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
otcextensions/sdk/function_graph/v2/function_invocation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# 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. | ||
from openstack import resource | ||
from openstack import exceptions | ||
|
||
|
||
class FunctionInvocation(resource.Resource): | ||
base_path = '/fgs/functions/%(func_urn)s' | ||
# Capabilities | ||
allow_create = True | ||
|
||
_query_mapping = resource.QueryParameters('func_urn') | ||
|
||
# Properties | ||
func_urn = resource.URI('func_urn', type=str) | ||
|
||
# Attributes | ||
invoke_summary = resource.Header('X-Cff-Invoke-Summary', type=str) | ||
cff_request_id = resource.Header('X-Cff-Request-Id', type=str) | ||
request_id = resource.Body('request-id', type=str) | ||
function_log = resource.Header('X-Cff-Function-Log', type=str) | ||
billing_duration = resource.Header('X-CFF-Billing-Duration', type=str) | ||
response_version = resource.Header('X-Cff-Response-Version', type=str) | ||
err_code = resource.Header('X-Func-Err-Code', type=str) | ||
err = resource.Header('X-Is-Func-Err', type=str) | ||
|
||
def _invocation(self, session, action, attrs: dict): | ||
"""Function Execution | ||
""" | ||
url = (self.base_path % self._uri.attributes) + f'/{action}' | ||
response = session.post(url, json=str(attrs).replace("'", '"')) | ||
exceptions.raise_from_response(response) | ||
self._translate_response(response) | ||
return self |
75 changes: 75 additions & 0 deletions
75
otcextensions/tests/functional/sdk/function_graph/v2/test_function_invocation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# 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 uuid | ||
from otcextensions.sdk.function_graph.v2 import function | ||
from otcextensions.tests.functional import base | ||
|
||
from openstack import _log | ||
|
||
_logger = _log.setup_logging('openstack') | ||
|
||
|
||
class TestFunction(base.BaseFunctionalTest): | ||
ID = None | ||
uuid = uuid.uuid4().hex[:8] | ||
|
||
def setUp(self): | ||
super(TestFunction, self).setUp() | ||
self.attrs = { | ||
'func_name': 'test-function-' + self.uuid, | ||
'package': 'default', | ||
'runtime': 'Python3.9', | ||
'handler': 'index.handler', | ||
'timeout': 30, | ||
'memory_size': 128, | ||
'code_type': 'inline', | ||
'func_code': { | ||
'file': 'CmltcG9ydCBqc29uCgpkZWYgaGFuZGxlcihldmVudCwgY29udGV4d' | ||
'Ck6CiAgICB0cnk6CiAgICAgICAgIyBQYXJzZSB0aGUgaW5wdXQgZX' | ||
'ZlbnQgKGRhdGEpCiAgICAgICAgaW5wdXRfZGF0YSA9IGpzb24ubG9' | ||
'hZHMoZXZlbnQpCiAgICAgICAgCiAgICAgICAgIyBQcmludCB0aGUg' | ||
'cHJvdmlkZWQgZGF0YQogICAgICAgIHByaW50KCJSZWNlaXZlZCBkY' | ||
'XRhOiIsIGlucHV0X2RhdGEpCiAgICAgICAgCiAgICAgICAgIyBSZX' | ||
'R1cm4gYSByZXNwb25zZQogICAgICAgIHJlc3BvbnNlID0gewogICA' | ||
'gICAgICAgICAic3RhdHVzQ29kZSI6IDIwMCwKICAgICAgICAgICAg' | ||
'ImJvZHkiOiBmIkRhdGEgcmVjZWl2ZWQ6IHtpbnB1dF9kYXRhfSIKI' | ||
'CAgICAgICB9CiAgICAgICAgcmV0dXJuIGpzb24uZHVtcHMocmVzcG' | ||
'9uc2UpCgogICAgZXhjZXB0IEV4Y2VwdGlvbiBhcyBlOgogICAgICA' | ||
'gICMgSGFuZGxlIGVycm9ycwogICAgICAgIGVycm9yX3Jlc3BvbnNl' | ||
'ID0gewogICAgICAgICAgICAic3RhdHVzQ29kZSI6IDUwMCwKICAgI' | ||
'CAgICAgICAgImJvZHkiOiBmIkVycm9yOiB7c3RyKGUpfSIKICAgIC' | ||
'AgICB9CiAgICAgICAgcmV0dXJuIGpzb24uZHVtcHMoZXJyb3JfcmV' | ||
'zcG9uc2UpCg==' | ||
} | ||
} | ||
|
||
self.NAME = 'test-function-' + self.uuid | ||
self.UPDATE_NAME = 'test-function-upd-' + self.uuid | ||
|
||
self.function = self.conn.functiongraph.create_function(**self.attrs) | ||
assert isinstance(self.function, function.Function) | ||
self.assertEqual(self.NAME, self.function.func_name) | ||
self.ID = self.function.func_id | ||
self.addCleanup(self.conn.functiongraph.delete_function, self.function) | ||
|
||
def test_function_invoke_sync(self): | ||
inv = self.conn.functiongraph.executing_function_synchronously( | ||
self.function.func_urn, attrs={'a': 'b'} | ||
) | ||
self.assertEqual(inv.err_code, '0') | ||
self.assertEqual(inv.err, 'false') | ||
|
||
def test_function_invoke_async(self): | ||
inv = self.conn.functiongraph.executing_function_asynchronously( | ||
self.function.func_urn, attrs={'a': 'b'} | ||
) | ||
self.assertIsNotNone(inv.request_id) |
34 changes: 34 additions & 0 deletions
34
otcextensions/tests/unit/sdk/function_graph/v2/test_function_invocation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# 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. | ||
|
||
from openstack.tests.unit import base | ||
|
||
from otcextensions.sdk.function_graph.v2 import function_invocation | ||
|
||
|
||
EXAMPLE = { | ||
'func_urn': 'test-function', | ||
'attrs': {'a': 'b'} | ||
} | ||
|
||
|
||
class TestFunction(base.TestCase): | ||
|
||
def test_basic(self): | ||
sot = function_invocation.FunctionInvocation() | ||
path = '/fgs/functions/%(func_urn)s' | ||
self.assertEqual(path, sot.base_path) | ||
self.assertTrue(sot.allow_create) | ||
|
||
def test_make_it(self): | ||
sot = function_invocation.FunctionInvocation(**EXAMPLE) | ||
self.assertEqual(EXAMPLE['func_urn'], sot.func_urn) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
features: | ||
- | | ||
Add FGS functions invocation support. |