From b25216b68aa3dd93c73c9fc3a76309a78cfa54b0 Mon Sep 17 00:00:00 2001 From: Benjamin Simon Date: Wed, 30 Apr 2025 00:50:56 +0200 Subject: [PATCH 1/3] add classmethod to recreate services --- .../services/events/api_destination.py | 17 ++++++++++++++ .../localstack/services/events/connection.py | 22 ++++++++++++++++--- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/localstack-core/localstack/services/events/api_destination.py b/localstack-core/localstack/services/events/api_destination.py index a7fe116eaed21..6c51485cffc6d 100644 --- a/localstack-core/localstack/services/events/api_destination.py +++ b/localstack-core/localstack/services/events/api_destination.py @@ -64,6 +64,23 @@ def __init__( description, ) + @classmethod + def from_api_destination_and_connection( + cls, api_destination: ApiDestination, connection: Connection + ): + api_destination_service = cls( + name=api_destination.name, + region=api_destination.region, + account_id=api_destination.account_id, + connection_arn=api_destination.connection_arn, + connection=connection, + invocation_endpoint=api_destination.invocation_endpoint, + http_method=api_destination.http_method, + invocation_rate_limit_per_second=api_destination.invocation_rate_limit_per_second, + ) + api_destination_service.api_destination = api_destination + return api_destination_service + @property def arn(self) -> Arn: return self.api_destination.arn diff --git a/localstack-core/localstack/services/events/connection.py b/localstack-core/localstack/services/events/connection.py index bb855c9203e0c..59a9b9cb10b6e 100644 --- a/localstack-core/localstack/services/events/connection.py +++ b/localstack-core/localstack/services/events/connection.py @@ -32,12 +32,16 @@ def __init__( auth_parameters: CreateConnectionAuthRequestParameters, description: ConnectionDescription | None = None, invocation_connectivity_parameters: ConnectivityResourceParameters | None = None, + create_secret: bool = True, ): self._validate_input(name, authorization_type) state = self._get_initial_state(authorization_type) - secret_arn = self.create_connection_secret( - region, account_id, name, authorization_type, auth_parameters - ) + + secret_arn = None + if create_secret: + secret_arn = self.create_connection_secret( + region, account_id, name, authorization_type, auth_parameters + ) public_auth_parameters = self._get_public_parameters(authorization_type, auth_parameters) self.connection = Connection( @@ -52,6 +56,18 @@ def __init__( invocation_connectivity_parameters, ) + @classmethod + def from_connection(cls, connection: Connection): + connection_service = cls( + connection.name, + connection.region, + connection.account_id, + connection.authorization_type, + connection.auth_parameters, + ) + connection_service.connection = connection + return connection_service + @property def arn(self) -> Arn: return self.connection.arn From b1598e9e07524aa791a80f211a37e5344a3eb7fb Mon Sep 17 00:00:00 2001 From: Benjamin Simon Date: Wed, 30 Apr 2025 01:00:13 +0200 Subject: [PATCH 2/3] do not create secret when using classmethod --- localstack-core/localstack/services/events/connection.py | 1 + 1 file changed, 1 insertion(+) diff --git a/localstack-core/localstack/services/events/connection.py b/localstack-core/localstack/services/events/connection.py index 59a9b9cb10b6e..2b6b2e9976cf8 100644 --- a/localstack-core/localstack/services/events/connection.py +++ b/localstack-core/localstack/services/events/connection.py @@ -64,6 +64,7 @@ def from_connection(cls, connection: Connection): connection.account_id, connection.authorization_type, connection.auth_parameters, + create_secret=False, ) connection_service.connection = connection return connection_service From e74736b64e82d4b66744a5109e30c19372ab9566 Mon Sep 17 00:00:00 2001 From: Benjamin Simon Date: Tue, 6 May 2025 13:40:08 +0200 Subject: [PATCH 3/3] address PR comments --- localstack-core/localstack/services/events/api_destination.py | 2 +- localstack-core/localstack/services/events/connection.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/localstack-core/localstack/services/events/api_destination.py b/localstack-core/localstack/services/events/api_destination.py index 6c51485cffc6d..0bb9f097ffb4b 100644 --- a/localstack-core/localstack/services/events/api_destination.py +++ b/localstack-core/localstack/services/events/api_destination.py @@ -65,7 +65,7 @@ def __init__( ) @classmethod - def from_api_destination_and_connection( + def restore_from_api_destination_and_connection( cls, api_destination: ApiDestination, connection: Connection ): api_destination_service = cls( diff --git a/localstack-core/localstack/services/events/connection.py b/localstack-core/localstack/services/events/connection.py index 2b6b2e9976cf8..c2b72a2025328 100644 --- a/localstack-core/localstack/services/events/connection.py +++ b/localstack-core/localstack/services/events/connection.py @@ -57,7 +57,7 @@ def __init__( ) @classmethod - def from_connection(cls, connection: Connection): + def restore_from_connection(cls, connection: Connection): connection_service = cls( connection.name, connection.region,