From 4b8de61a8e17c41c11d5a9dd268e3e6a497293a8 Mon Sep 17 00:00:00 2001 From: felicijus Date: Tue, 1 Oct 2024 14:01:40 +0200 Subject: [PATCH 1/5] feat: grpc_address --- pyzeebe/channel/insecure_channel.py | 9 ++++----- pyzeebe/channel/secure_channel.py | 9 +++++---- pyzeebe/channel/utils.py | 7 +++---- tests/unit/channel/insecure_channel_test.py | 18 +----------------- tests/unit/channel/secure_channel_test.py | 18 +----------------- tests/unit/channel/utils_test.py | 10 +++------- 6 files changed, 17 insertions(+), 54 deletions(-) diff --git a/pyzeebe/channel/insecure_channel.py b/pyzeebe/channel/insecure_channel.py index d428fd62..55fb97ff 100644 --- a/pyzeebe/channel/insecure_channel.py +++ b/pyzeebe/channel/insecure_channel.py @@ -8,19 +8,18 @@ def create_insecure_channel( - hostname: Optional[str] = None, port: Optional[int] = None, channel_options: Optional[ChannelArgumentType] = None + grpc_address: Optional[str] = None, channel_options: Optional[ChannelArgumentType] = None ) -> grpc.aio.Channel: """ Create an insecure channel Args: - hostname (Optional[str], optional): Zeebe gateway hostname - port (Optional[int], optional): Zeebe gateway port + grpc_address (Optional[str], optional): Zeebe gateway hostname channel_options (Optional[Dict], optional): GRPC channel options. See https://grpc.github.io/grpc/python/glossary.html#term-channel_arguments Returns: grpc.aio.Channel: A GRPC Channel connected to the Zeebe gateway. """ - address = create_address(hostname, port) - return grpc.aio.insecure_channel(address, options=get_channel_options(channel_options)) + grpc_address = create_address(grpc_address=grpc_address) + return grpc.aio.insecure_channel(target=grpc_address, options=get_channel_options(channel_options)) diff --git a/pyzeebe/channel/secure_channel.py b/pyzeebe/channel/secure_channel.py index 17342cce..dacd5604 100644 --- a/pyzeebe/channel/secure_channel.py +++ b/pyzeebe/channel/secure_channel.py @@ -8,8 +8,7 @@ def create_secure_channel( - hostname: Optional[str] = None, - port: Optional[int] = None, + grpc_address: Optional[str] = None, channel_options: Optional[ChannelArgumentType] = None, channel_credentials: Optional[grpc.ChannelCredentials] = None, ) -> grpc.aio.Channel: @@ -27,6 +26,8 @@ def create_secure_channel( Returns: grpc.aio.Channel: A GRPC Channel connected to the Zeebe gateway. """ - address = create_address(hostname, port) + grpc_address = create_address(grpc_address=grpc_address) credentials = channel_credentials or grpc.ssl_channel_credentials() - return grpc.aio.secure_channel(address, credentials, options=get_channel_options(channel_options)) + return grpc.aio.secure_channel( + target=grpc_address, credentials=credentials, options=get_channel_options(channel_options) + ) diff --git a/pyzeebe/channel/utils.py b/pyzeebe/channel/utils.py index 8b768c36..b7df275e 100644 --- a/pyzeebe/channel/utils.py +++ b/pyzeebe/channel/utils.py @@ -7,9 +7,8 @@ def create_address( - hostname: Optional[str] = None, - port: Optional[int] = None, + grpc_address: Optional[str] = None, ) -> str: - if hostname or port: - return f"{hostname or DEFAULT_HOSTNAME}:{port or DEFAULT_PORT}" + if grpc_address: + return grpc_address return os.getenv("ZEEBE_ADDRESS", DEFAULT_ADDRESS) diff --git a/tests/unit/channel/insecure_channel_test.py b/tests/unit/channel/insecure_channel_test.py index 6878fea7..9b35622f 100644 --- a/tests/unit/channel/insecure_channel_test.py +++ b/tests/unit/channel/insecure_channel_test.py @@ -30,20 +30,4 @@ def test_uses_default_address(self, insecure_channel_mock: Mock): create_insecure_channel() insecure_channel_call = insecure_channel_mock.mock_calls[0] - assert insecure_channel_call.args[0] == create_address() - - def test_overrides_default_port_if_provided(self, insecure_channel_mock: Mock): - port = 123 - - create_insecure_channel(port=port) - - insecure_channel_call = insecure_channel_mock.mock_calls[0] - assert insecure_channel_call.args[0] == f"{DEFAULT_HOSTNAME}:{port}" - - def test_overrides_default_hostname_if_provided(self, insecure_channel_mock: Mock): - hostname = str(uuid4()) - - create_insecure_channel(hostname=hostname) - - insecure_channel_call = insecure_channel_mock.mock_calls[0] - assert insecure_channel_call.args[0] == f"{hostname}:{DEFAULT_PORT}" + assert insecure_channel_call.kwargs["target"] == create_address() diff --git a/tests/unit/channel/secure_channel_test.py b/tests/unit/channel/secure_channel_test.py index 50ac7487..245161cd 100644 --- a/tests/unit/channel/secure_channel_test.py +++ b/tests/unit/channel/secure_channel_test.py @@ -36,20 +36,4 @@ def test_uses_default_address(self, secure_channel_mock: Mock): create_secure_channel() secure_channel_call = secure_channel_mock.mock_calls[0] - assert secure_channel_call.args[0] == create_address() - - def test_overrides_default_port_if_provided(self, secure_channel_mock: Mock): - port = 123 - - create_secure_channel(port=port) - - secure_channel_call = secure_channel_mock.mock_calls[0] - assert secure_channel_call.args[0] == f"{DEFAULT_HOSTNAME}:{port}" - - def test_overrides_default_hostname_if_provided(self, secure_channel_mock: Mock): - hostname = str(uuid4()) - - create_secure_channel(hostname=hostname) - - secure_channel_call = secure_channel_mock.mock_calls[0] - assert secure_channel_call.args[0] == f"{hostname}:{DEFAULT_PORT}" + assert secure_channel_call.kwargs["target"] == create_address() diff --git a/tests/unit/channel/utils_test.py b/tests/unit/channel/utils_test.py index 7d262ff8..590996cb 100644 --- a/tests/unit/channel/utils_test.py +++ b/tests/unit/channel/utils_test.py @@ -10,15 +10,11 @@ def test_returns_default_address(self): assert address == DEFAULT_ADDRESS - def test_default_port_is_26500(self): - address = create_address(hostname=str(uuid4())) - - assert address.split(":")[1] == "26500" - - def test_default_hostname_is_localhost(self): - address = create_address(port=12) + def test_default_hostname_port(self): + address = create_address() assert address.split(":")[0] == "localhost" + assert address.split(":")[1] == "26500" def test_returns_env_var_if_provided(self): zeebe_address = str(uuid4()) From 2803f5635ced0297ed7d69d16de7d739610179a5 Mon Sep 17 00:00:00 2001 From: felicijus Date: Tue, 1 Oct 2024 16:05:29 +0200 Subject: [PATCH 2/5] docs: add meaningful docstring --- pyzeebe/channel/insecure_channel.py | 3 ++- pyzeebe/channel/secure_channel.py | 4 ++-- tests/unit/channel/utils_test.py | 12 ++++++------ 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/pyzeebe/channel/insecure_channel.py b/pyzeebe/channel/insecure_channel.py index 55fb97ff..72b54831 100644 --- a/pyzeebe/channel/insecure_channel.py +++ b/pyzeebe/channel/insecure_channel.py @@ -14,7 +14,8 @@ def create_insecure_channel( Create an insecure channel Args: - grpc_address (Optional[str], optional): Zeebe gateway hostname + grpc_address (Optional[str], optional): Zeebe Gateway Address + Default: None, alias the ZEEBE_ADDRESS environment variable or "localhost:26500" channel_options (Optional[Dict], optional): GRPC channel options. See https://grpc.github.io/grpc/python/glossary.html#term-channel_arguments diff --git a/pyzeebe/channel/secure_channel.py b/pyzeebe/channel/secure_channel.py index dacd5604..4a4a2124 100644 --- a/pyzeebe/channel/secure_channel.py +++ b/pyzeebe/channel/secure_channel.py @@ -16,8 +16,8 @@ def create_secure_channel( Create a secure channel Args: - hostname (Optional[str], optional): Zeebe gateway hostname - port (Optional[int], optional): Zeebe gateway port + grpc_address (Optional[str], optional): Zeebe Gateway Address + Default: None, alias the ZEEBE_ADDRESS environment variable or "localhost:26500" channel_options (Optional[Dict], optional): GRPC channel options. See https://grpc.github.io/grpc/python/glossary.html#term-channel_arguments channel_credentials (Optional[grpc.ChannelCredentials]): Channel credentials to use. diff --git a/tests/unit/channel/utils_test.py b/tests/unit/channel/utils_test.py index 590996cb..cb73e3a4 100644 --- a/tests/unit/channel/utils_test.py +++ b/tests/unit/channel/utils_test.py @@ -5,16 +5,16 @@ class TestCreateAddress: - def test_returns_default_address(self): - address = create_address() - assert address == DEFAULT_ADDRESS + def test_returns_passed_address(self): + address = str(uuid4()) - def test_default_hostname_port(self): + assert address == create_address(address) + + def test_returns_default_address(self): address = create_address() - assert address.split(":")[0] == "localhost" - assert address.split(":")[1] == "26500" + assert address == DEFAULT_ADDRESS def test_returns_env_var_if_provided(self): zeebe_address = str(uuid4()) From 18eb96f66c168aa351c1331475a1a16da35718ef Mon Sep 17 00:00:00 2001 From: felicijus Date: Tue, 1 Oct 2024 16:15:48 +0200 Subject: [PATCH 3/5] docs: change hostname, port to grpc_address --- docs/channels.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/channels.rst b/docs/channels.rst index 3701cf2a..eb0ba4c1 100644 --- a/docs/channels.rst +++ b/docs/channels.rst @@ -22,7 +22,7 @@ Example: from pyzeebe import create_insecure_channel - channel = create_insecure_channel(hostname="zeebe", port=443) + channel = create_insecure_channel(grpc_address="localhost:26500") Secure @@ -41,7 +41,7 @@ Example: grpc.ssl_channel_credentials(root_certificates="", private_key="") - channel = create_secure_channel(channel_credentials=credentials) + channel = create_secure_channel(grpc_address="host:port", channel_credentials=credentials) Example with oauth2 (like Camunda Identity): @@ -57,7 +57,7 @@ Example with oauth2 (like Camunda Identity): call_credentials = grpc.metadata_call_credentials(AuthMetadataPlugin(credentials=credentials)) ssl_credentials = grpc.ssl_channel_credentials(root_certificates="", private_key="") channel_credentials = grpc.composite_channel_credentials(ssl_credentials, call_credentials) - channel = create_secure_channel(channel_credentials=channel_credentials) + channel = create_secure_channel(grpc_address="host:port", channel_credentials=channel_credentials) Camunda Cloud From 8293481907da76193078591cb7ff18e41b34017d Mon Sep 17 00:00:00 2001 From: felicijus Date: Tue, 1 Oct 2024 16:25:23 +0200 Subject: [PATCH 4/5] docs: update examples grpc_address --- README.md | 4 ++-- examples/client.py | 2 +- examples/worker.py | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index abd7ab84..5e58cb54 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ import asyncio from pyzeebe import ZeebeWorker, Job, JobController, create_insecure_channel -channel = create_insecure_channel(hostname="localhost", port=26500) # Create grpc channel +channel = create_insecure_channel(grpc_address="localhost:26500") # Create grpc channel worker = ZeebeWorker(channel) # Create a zeebe worker @@ -82,7 +82,7 @@ await zeebe_worker.stop() # Stops worker after all running jobs have been comple from pyzeebe import ZeebeClient, create_insecure_channel # Create a zeebe client -channel = create_insecure_channel(hostname="localhost", port=26500) +channel = create_insecure_channel(grpc_address="localhost:26500") zeebe_client = ZeebeClient(channel) # Run a Zeebe process instance diff --git a/examples/client.py b/examples/client.py index 28d7b98d..5c9ec4c0 100644 --- a/examples/client.py +++ b/examples/client.py @@ -6,7 +6,7 @@ ) # Create a zeebe client without credentials -grpc_channel = create_insecure_channel(hostname="localhost", port=26500) +grpc_channel = create_insecure_channel(grpc_address="localhost:26500") zeebe_client = ZeebeClient(grpc_channel) # Create a zeebe client with TLS diff --git a/examples/worker.py b/examples/worker.py index 975b97c0..feb744a6 100644 --- a/examples/worker.py +++ b/examples/worker.py @@ -24,16 +24,16 @@ async def example_logging_task_decorator(job: Job) -> Job: grpc_channel = create_insecure_channel() worker = ZeebeWorker(grpc_channel) -# With custom hostname/port -grpc_channel = create_insecure_channel(hostname="zeebe-gateway.mydomain", port=443) +# With custom grpc_address +grpc_channel = create_insecure_channel(grpc_address="zeebe-gateway.mydomain:443") worker = ZeebeWorker(grpc_channel) # Will use environment variable ZEEBE_ADDRESS or localhost:26500 and use TLS grpc_channel = create_secure_channel() worker = ZeebeWorker(grpc_channel) -# With custom hostname/port -grpc_channel = create_secure_channel(hostname="zeebe-gateway.mydomain", port=443) +# With custom grpc_address +grpc_channel = create_secure_channel(grpc_address="zeebe-gateway.mydomain:443") worker = ZeebeWorker(grpc_channel) # Connect to zeebe cluster in camunda cloud From 45792f7a810f07b550f693d8c3a97a3a43a9b258 Mon Sep 17 00:00:00 2001 From: felicijus Date: Tue, 1 Oct 2024 16:29:46 +0200 Subject: [PATCH 5/5] fix: remove unused imports --- pyzeebe/channel/utils.py | 6 ++---- tests/unit/channel/insecure_channel_test.py | 3 +-- tests/unit/channel/secure_channel_test.py | 3 +-- tests/unit/channel/utils_test.py | 4 ++-- 4 files changed, 6 insertions(+), 10 deletions(-) diff --git a/pyzeebe/channel/utils.py b/pyzeebe/channel/utils.py index b7df275e..fcea921d 100644 --- a/pyzeebe/channel/utils.py +++ b/pyzeebe/channel/utils.py @@ -1,9 +1,7 @@ import os from typing import Optional -DEFAULT_HOSTNAME = "localhost" -DEFAULT_PORT = 26500 -DEFAULT_ADDRESS = f"{DEFAULT_HOSTNAME}:{DEFAULT_PORT}" +DEFAULT_ZEEBE_ADDRESS = "localhost:26500" def create_address( @@ -11,4 +9,4 @@ def create_address( ) -> str: if grpc_address: return grpc_address - return os.getenv("ZEEBE_ADDRESS", DEFAULT_ADDRESS) + return os.getenv("ZEEBE_ADDRESS", DEFAULT_ZEEBE_ADDRESS) diff --git a/tests/unit/channel/insecure_channel_test.py b/tests/unit/channel/insecure_channel_test.py index 9b35622f..e76fe6ae 100644 --- a/tests/unit/channel/insecure_channel_test.py +++ b/tests/unit/channel/insecure_channel_test.py @@ -1,12 +1,11 @@ from unittest.mock import Mock, patch -from uuid import uuid4 import grpc import pytest from pyzeebe import create_insecure_channel from pyzeebe.channel.channel_options import get_channel_options -from pyzeebe.channel.utils import DEFAULT_HOSTNAME, DEFAULT_PORT, create_address +from pyzeebe.channel.utils import create_address class TestCreateInsecureChannel: diff --git a/tests/unit/channel/secure_channel_test.py b/tests/unit/channel/secure_channel_test.py index 245161cd..181f5f80 100644 --- a/tests/unit/channel/secure_channel_test.py +++ b/tests/unit/channel/secure_channel_test.py @@ -1,12 +1,11 @@ from unittest.mock import Mock, patch -from uuid import uuid4 import grpc import pytest from pyzeebe import create_secure_channel from pyzeebe.channel.channel_options import get_channel_options -from pyzeebe.channel.utils import DEFAULT_HOSTNAME, DEFAULT_PORT, create_address +from pyzeebe.channel.utils import create_address class TestCreateSecureChannel: diff --git a/tests/unit/channel/utils_test.py b/tests/unit/channel/utils_test.py index cb73e3a4..f4d0a9ef 100644 --- a/tests/unit/channel/utils_test.py +++ b/tests/unit/channel/utils_test.py @@ -1,7 +1,7 @@ import os from uuid import uuid4 -from pyzeebe.channel.utils import DEFAULT_ADDRESS, create_address +from pyzeebe.channel.utils import DEFAULT_ZEEBE_ADDRESS, create_address class TestCreateAddress: @@ -14,7 +14,7 @@ def test_returns_passed_address(self): def test_returns_default_address(self): address = create_address() - assert address == DEFAULT_ADDRESS + assert address == DEFAULT_ZEEBE_ADDRESS def test_returns_env_var_if_provided(self): zeebe_address = str(uuid4())