Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add client_credentials param to workflow execution trigger method #1512

Merged
merged 3 commits into from
Nov 16, 2023
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ Changes are grouped as follows
- `Fixed` for any bug fixes.
- `Security` in case of vulnerabilities.

## [7.2.0] - 2023-11-16
### Added
- The `trigger` method of the Workflow Execution API, now accepts a `client_credentials` to allow specifying specific
credentials to run with. Previously, the current credentials set on the CogniteClient object doing the call would be used.

## [7.1.0] - 2023-11-16
### Added
- The list method for asset mappings in the 3D API now supports `intersects_bounding_box`, allowing users to only
Expand Down
28 changes: 17 additions & 11 deletions cognite/client/_api/workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

if TYPE_CHECKING:
from cognite.client import ClientConfig, CogniteClient
from cognite.client.data_classes import ClientCredentials


class BetaWorkflowAPIClient(APIClient, ABC):
Expand Down Expand Up @@ -147,6 +148,7 @@ def trigger(
version: str,
input: dict | None = None,
metadata: dict | None = None,
client_credentials: ClientCredentials | None = None,
) -> WorkflowExecution:
"""`Trigger a workflow execution. <https://pr-2282.specs.preview.cogniteapp.com/20230101.json.html#tag/Workflow-Execution/operation/TriggerRunOfSpecificVersionOfWorkflow>`_

Expand All @@ -155,6 +157,7 @@ def trigger(
version (str): Version of the workflow.
input (dict | None): The input to the workflow execution. This will be available for tasks that have specified it as an input with the string "${workflow.input}" See tip below for more information.
metadata (dict | None): Application specific metadata. Keys have a maximum length of 32 characters, values a maximum of 255, and there can be a maximum of 10 key-value pairs.
client_credentials (ClientCredentials | None): Specific credentials that should be used to trigger the workflow execution. When passed will take precedence over the current credentials.

Tip:
The workflow input can be available in the workflow tasks. For example, if you have a Task with
Expand All @@ -172,31 +175,34 @@ def trigger(

Examples:

Trigger workflow execution for workflow my workflow version 1:
Trigger a workflow execution for the workflow "foo", version 1:

>>> from cognite.client import CogniteClient
>>> c = CogniteClient()
>>> res = c.workflows.executions.trigger("my workflow", "1")
>>> res = c.workflows.executions.trigger("foo", "1")

Trigger workflow execution for workflow my workflow version 1 with input data '{"a": 1, "b": 2}:
Trigger a workflow execution with input data:

>>> from cognite.client import CogniteClient
>>> c = CogniteClient()
>>> res = c.workflows.executions.trigger("my workflow", "1", input={"a": 1, "b": 2})
>>> res = c.workflows.executions.trigger("foo", "1", input={"a": 1, "b": 2})

Trigger a workflow execution using a specific set of client credentials (i.e. not your current credentials):

>>> import os
>>> from cognite.client.data_classes import ClientCredentials
>>> credentials = ClientCredentials("my-client-id", os.environ["MY_CLIENT_SECRET"])
>>> res = c.workflows.executions.trigger("foo", "1", client_credentials=credentials)
"""
self._warning.warn()
nonce = create_session_and_return_nonce(self._cognite_client, api_name="Workflow API")
nonce = create_session_and_return_nonce(
self._cognite_client, api_name="Workflow API", client_credentials=client_credentials
)
body = {"authentication": {"nonce": nonce}}
if input is not None:
body["input"] = input
if metadata is not None:
body["metadata"] = metadata

response = self._post(
url_path=f"/workflows/{workflow_external_id}/versions/{version}/run",
json=body,
)
response = self._post(url_path=f"/workflows/{workflow_external_id}/versions/{version}/run", json=body)
return WorkflowExecution.load(response.json())

def list(
Expand Down
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.1.0"
__version__ = "7.2.0"
__api_subversion__ = "V20220125"
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.1.0"
version = "7.2.0"
description = "Cognite Python SDK"
readme = "README.md"
documentation = "https://cognite-sdk-python.readthedocs-hosted.com"
Expand Down
2 changes: 2 additions & 0 deletions tests/tests_unit/test_docstring_examples.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import doctest
from collections import defaultdict
from unittest import TextTestRunner
from unittest.mock import patch

Expand Down Expand Up @@ -39,6 +40,7 @@ def run_docstring_tests(module):


@patch("cognite.client.CogniteClient", CogniteClientMock)
@patch("os.environ", defaultdict(lambda: "value")) # ensure env.var. lookups does not fail in doctests
class TestDocstringExamples:
def test_time_series(self):
run_docstring_tests(time_series)
Expand Down