Skip to content

Commit

Permalink
[CDF-22068] Fixes in Functions (#1847)
Browse files Browse the repository at this point in the history
  • Loading branch information
doctrino authored Jul 15, 2024
1 parent 854bd9c commit d6730a2
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 125 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ Changes are grouped as follows
- `Fixed` for any bug fixes.
- `Security` in case of vulnerabilities.

## [7.54.1] - 2024-07-13

### Fixed
- Calling `client.functions.retrieve` or `client.functions.delete` with more than 10 ids no longer
raises a `CogniteAPIError`.
- Iterating over functions using `client.functions` or `client.functions(...)` no longer raises a `CogniteAPIError`.
### Added
- Added missing filter parameter `metadata` to `client.functions.list`.
### Changed
- When creating a new function without specifying `description` or `owner`, the default values are now
correctly set to `None` instead of `""`.

## [7.54.0] - 2024-07-12
### Added
- In the `client.data_modeling.instances` the methods `.search`, `.retrieve`,`.list`, `.query`, and `.sync` now
Expand Down
232 changes: 147 additions & 85 deletions cognite/client/_api/functions.py

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion cognite/client/_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from __future__ import annotations

__version__ = "7.54.0"
__version__ = "7.54.1"
__api_subversion__ = "20230101"
76 changes: 40 additions & 36 deletions cognite/client/data_classes/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
from cognite.client import CogniteClient

RunTime: TypeAlias = Literal["py38", "py39", "py310", "py311"]
FunctionStatus: TypeAlias = Literal["Queued", "Deploying", "Ready", "Failed"]
HANDLER_FILE_NAME = "handler.py"


class FunctionCore(WriteableCogniteResource["FunctionWrite"], ABC):
Expand All @@ -35,13 +37,13 @@ class FunctionCore(WriteableCogniteResource["FunctionWrite"], ABC):
description (str | None): Description of the function.
owner (str | None): Owner of the function.
file_id (int | None): File id of the code represented by this object.
function_path (str | None): Relative path from the root folder to the file containing the `handle` function. Defaults to `handler.py`. Must be on posix path format.
secrets (dict | None): Secrets attached to the function ((key, value) pairs).
env_vars (dict | None): User specified environment variables on the function ((key, value) pairs).
cpu (float | None): Number of CPU cores per function. Defaults to 0.25. Allowed values are in the range [0.1, 0.6].
memory (float | None): Memory per function measured in GB. Defaults to 1. Allowed values are in the range [0.1, 2.5].
runtime (str | None): Runtime of the function. Allowed values are ["py38", "py39","py310"]. The runtime "py38" resolves to the latest version of the Python 3.8 series. Will default to "py38" if not specified.
metadata (dict | None): Metadata associated with a function as a set of key:value pairs.
function_path (str): Relative path from the root folder to the file containing the `handle` function. Defaults to `handler.py`. Must be on posix path format.
secrets (dict[str, str] | None): Secrets attached to the function ((key, value) pairs).
env_vars (dict[str, str] | None): User specified environment variables on the function ((key, value) pairs).
cpu (float | None): Number of CPU cores per function. Allowed range and default value are given by the `limits endpoint. <https://developer.cognite.com/api#tag/Functions/operation/functionsLimits>`_, and None translates to the API default. On Azure, only the default value is used.
memory (float | None): Memory per function measured in GB. Allowed range and default value are given by the `limits endpoint. <https://developer.cognite.com/api#tag/Functions/operation/functionsLimits>`_, and None translates to the API default. On Azure, only the default value is used.
runtime (str | None): Runtime of the function. Allowed values are ["py38", "py39","py310", "py311"]. The runtime "py38" resolves to the latest version of the Python 3.8 series.
metadata (dict[str, str] | None): Metadata associated with a function as a set of key:value pairs.
"""

def __init__(
Expand All @@ -51,13 +53,13 @@ def __init__(
description: str | None = None,
owner: str | None = None,
file_id: int | None = None,
function_path: str | None = None,
secrets: dict | None = None,
env_vars: dict | None = None,
function_path: str = HANDLER_FILE_NAME,
secrets: dict[str, str] | None = None,
env_vars: dict[str, str] | None = None,
cpu: float | None = None,
memory: float | None = None,
runtime: str | None = None,
metadata: dict | None = None,
metadata: dict[str, str] | None = None,
) -> None:
# name/file_id are required when using the class to read,
# but don't make sense passing in when creating a new object. So in order to make the typing
Expand Down Expand Up @@ -90,15 +92,15 @@ class Function(FunctionCore):
owner (str | None): Owner of the function.
status (str | None): Status of the function.
file_id (int | None): File id of the code represented by this object.
function_path (str | None): Relative path from the root folder to the file containing the `handle` function. Defaults to `handler.py`. Must be on posix path format.
function_path (str): Relative path from the root folder to the file containing the `handle` function. Defaults to `handler.py`. Must be on posix path format.
created_time (int | None): Created time in UNIX.
secrets (dict | None): Secrets attached to the function ((key, value) pairs).
env_vars (dict | None): User specified environment variables on the function ((key, value) pairs).
cpu (float | None): Number of CPU cores per function. Defaults to 0.25. Allowed values are in the range [0.1, 0.6].
memory (float | None): Memory per function measured in GB. Defaults to 1. Allowed values are in the range [0.1, 2.5].
runtime (str | None): Runtime of the function. Allowed values are ["py38", "py39","py310"]. The runtime "py38" resolves to the latest version of the Python 3.8 series. Will default to "py38" if not specified.
secrets (dict[str, str] | None): Secrets attached to the function ((key, value) pairs).
env_vars (dict[str, str] | None): User specified environment variables on the function ((key, value) pairs).
cpu (float | None): Number of CPU cores per function. Allowed range and default value are given by the `limits endpoint. <https://developer.cognite.com/api#tag/Functions/operation/functionsLimits>`_, and None translates to the API default. On Azure, only the default value is used.
memory (float | None): Memory per function measured in GB. Allowed range and default value are given by the `limits endpoint. <https://developer.cognite.com/api#tag/Functions/operation/functionsLimits>`_, and None translates to the API default. On Azure, only the default value is used.
runtime (str | None): Runtime of the function. Allowed values are ["py38", "py39","py310", "py311"]. The runtime "py38" resolves to the latest version of the Python 3.8 series.
runtime_version (str | None): The complete specification of the function runtime with major, minor and patch version numbers.
metadata (dict | None): Metadata associated with a function as a set of key:value pairs.
metadata (dict[str, str] | None): Metadata associated with a function as a set of key:value pairs.
error (dict | None): Dictionary with keys "message" and "trace", which is populated if deployment fails.
cognite_client (CogniteClient | None): An optional CogniteClient to associate with this data class.
"""
Expand All @@ -112,15 +114,15 @@ def __init__(
owner: str | None = None,
status: str | None = None,
file_id: int | None = None,
function_path: str | None = None,
function_path: str = HANDLER_FILE_NAME,
created_time: int | None = None,
secrets: dict | None = None,
env_vars: dict | None = None,
secrets: dict[str, str] | None = None,
env_vars: dict[str, str] | None = None,
cpu: float | None = None,
memory: float | None = None,
runtime: str | None = None,
runtime_version: str | None = None,
metadata: dict | None = None,
metadata: dict[str, str] | None = None,
error: dict | None = None,
cognite_client: CogniteClient | None = None,
) -> None:
Expand Down Expand Up @@ -255,13 +257,13 @@ class FunctionWrite(FunctionCore):
external_id (str | None): External id of the function.
description (str | None): Description of the function.
owner (str | None): Owner of the function.
function_path (str | None): Relative path from the root folder to the file containing the `handle` function. Defaults to `handler.py`. Must be on posix path format.
secrets (dict | None): Secrets attached to the function ((key, value) pairs).
env_vars (dict | None): User specified environment variables on the function ((key, value) pairs).
cpu (float | None): Number of CPU cores per function. Defaults to 0.25. Allowed values are in the range [0.1, 0.6].
memory (float | None): Memory per function measured in GB. Defaults to 1. Allowed values are in the range [0.1, 2.5].
runtime (RunTime | None): Runtime of the function. Allowed values are ["py38", "py39","py310"]. The runtime "py38" resolves to the latest version of the Python 3.8 series. Will default to "py38" if not specified.
metadata (dict | None): Metadata associated with a function as a set of key:value pairs.
function_path (str): Relative path from the root folder to the file containing the `handle` function. Defaults to `handler.py`. Must be on posix path format.
secrets (dict[str, str] | None): Secrets attached to the function ((key, value) pairs).
env_vars (dict[str, str] | None): User specified environment variables on the function ((key, value) pairs).
cpu (float | None): Number of CPU cores per function. Allowed range and default value are given by the `limits endpoint. <https://developer.cognite.com/api#tag/Functions/operation/functionsLimits>`_, and None translates to the API default. On Azure, only the default value is used.
memory (float | None): Memory per function measured in GB. Allowed range and default value are given by the `limits endpoint. <https://developer.cognite.com/api#tag/Functions/operation/functionsLimits>`_, and None translates to the API default. On Azure, only the default value is used.
runtime (RunTime | None): Runtime of the function. Allowed values are ["py38", "py39","py310", "py311"]. The runtime "py38" resolves to the latest version of the Python 3.8 series.
metadata (dict[str, str] | None): Metadata associated with a function as a set of key:value pairs.
index_url (str | None): Specify a different python package index, allowing for packages published in private repositories. Supports basic HTTP authentication as described in pip basic authentication. See the documentation for additional information related to the security risks of using this option.
extra_index_urls (list[str] | None): Extra package index URLs to use when building the function, allowing for packages published in private repositories. Supports basic HTTP authentication as described in pip basic authentication. See the documentation for additional information related to the security risks of using this option.
"""
Expand All @@ -273,13 +275,13 @@ def __init__(
external_id: str | None = None,
description: str | None = None,
owner: str | None = None,
function_path: str | None = None,
secrets: dict | None = None,
env_vars: dict | None = None,
function_path: str = HANDLER_FILE_NAME,
secrets: dict[str, str] | None = None,
env_vars: dict[str, str] | None = None,
cpu: float | None = None,
memory: float | None = None,
runtime: RunTime | None = None,
metadata: dict | None = None,
metadata: dict[str, str] | None = None,
index_url: str | None = None,
extra_index_urls: list[str] | None = None,
) -> None:
Expand Down Expand Up @@ -308,7 +310,7 @@ def _load(cls, resource: dict[str, Any], cognite_client: CogniteClient | None =
description=resource.get("description"),
owner=resource.get("owner"),
file_id=resource["fileId"],
function_path=resource.get("functionPath"),
function_path=resource.get("functionPath", HANDLER_FILE_NAME),
secrets=resource.get("secrets"),
env_vars=resource.get("envVars"),
cpu=resource.get("cpu"),
Expand All @@ -330,16 +332,18 @@ def __init__(
name: str | None = None,
owner: str | None = None,
file_id: int | None = None,
status: str | None = None,
status: FunctionStatus | None = None,
external_id_prefix: str | None = None,
created_time: dict[str, int] | TimestampRange | None = None,
created_time: dict[Literal["min", "max"], int] | TimestampRange | None = None,
metadata: dict[str, str] | None = None,
) -> None:
self.name = name
self.owner = owner
self.file_id = file_id
self.status = status
self.external_id_prefix = external_id_prefix
self.created_time = created_time
self.metadata = metadata


class FunctionCallsFilter(CogniteFilter):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]
name = "cognite-sdk"

version = "7.54.0"
version = "7.54.1"
description = "Cognite Python SDK"
readme = "README.md"
documentation = "https://cognite-sdk-python.readthedocs-hosted.com"
Expand Down
16 changes: 16 additions & 0 deletions tests/tests_integration/test_api/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest

from cognite.client import CogniteClient
from cognite.client.data_classes import Function, FunctionList
from cognite.client.exceptions import CogniteNotFoundError


Expand Down Expand Up @@ -35,3 +36,18 @@ def test_create_schedule_with_bad_external_id(self, cognite_client: CogniteClien
cron_expression="* * * * *",
name="test_schedule",
)

def test_iterate_functions(self, cognite_client: CogniteClient) -> None:
for function in cognite_client.functions:
assert isinstance(function, Function)
break
else:
assert False, "Expected at least one function"

def test_iterate_chunked_functions(self, cognite_client: CogniteClient) -> None:
for function in cognite_client.functions(chunk_size=2):
assert isinstance(function, FunctionList)
assert len(function) <= 2
break
else:
assert False, "Expected at least one function"
4 changes: 2 additions & 2 deletions tests/tests_unit/test_api/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ def test_zip_and_upload_folder(self, function_folder, function_name, exception,

@patch("cognite.client._api.functions.MAX_RETRIES", 1)
def test_create_function_with_file_not_uploaded(self, mock_file_not_uploaded, cognite_client):
with pytest.raises(IOError):
with pytest.raises(RuntimeError):
cognite_client.functions.create(name="myfunction", file_id=123)

def test_create_with_path(self, mock_functions_create_response, cognite_client):
Expand Down Expand Up @@ -489,7 +489,7 @@ def test_create_with_path_and_file_id_raises(self, mock_functions_create_respons
)

def test_create_with_cpu_and_memory(self, mock_functions_create_response, cognite_client):
res = cognite_client.functions.create(name="myfunction", file_id=1234, cpu=0.2, memory=1)
res = cognite_client.functions.create(name="myfunction", file_id=1234, cpu=0.2, memory=1.0)

assert isinstance(res, Function)
assert mock_functions_create_response.calls[1].response.json()["items"][0] == res.dump(camel_case=True)
Expand Down

0 comments on commit d6730a2

Please sign in to comment.