Skip to content

Commit 300ca0c

Browse files
committed
refactor to be used in Update as well
1 parent e0c26d1 commit 300ca0c

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

localstack-core/localstack/services/sns/resource_providers/aws_sns_subscription.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import localstack.services.cloudformation.provider_utils as util
99
from localstack import config
10+
from localstack.aws.connect import ServiceLevelClientFactory
1011
from localstack.services.cloudformation.resource_provider import (
1112
ConvertingInternalClientFactory,
1213
OperationStatus,
@@ -64,18 +65,7 @@ def create(
6465
6566
"""
6667
model = request.desired_state
67-
if subscription_region := model.get("Region"):
68-
# FIXME: this is hacky, maybe we should have access to the original parameters for the `aws_client_factory`
69-
# as we now need to manually use them
70-
# Not all internal CloudFormation requests will be directed to the same region and account
71-
# maybe we could need to expose a proper client factory where we can override some parameters like the
72-
# Region
73-
factory = ConvertingInternalClientFactory(use_ssl=config.DISTRIBUTED_MODE)
74-
client_params = dict(request.aws_client_factory._client_creation_params)
75-
client_params["region_name"] = subscription_region
76-
sns = factory(**client_params).sns
77-
else:
78-
sns = request.aws_client_factory.sns
68+
sns = self._get_client(request).sns
7969

8070
params = util.select_attributes(model=model, params=["TopicArn", "Protocol", "Endpoint"])
8171

@@ -141,7 +131,7 @@ def update(
141131
"""
142132
model = request.desired_state
143133
model["Id"] = request.previous_state["Id"]
144-
sns = request.aws_client_factory.sns
134+
sns = self._get_client(request).sns
145135

146136
attrs = [
147137
"DeliveryPolicy",
@@ -166,3 +156,23 @@ def update(
166156
@staticmethod
167157
def attr_val(val):
168158
return json.dumps(val) if isinstance(val, dict) else str(val)
159+
160+
@staticmethod
161+
def _get_client(
162+
request: ResourceRequest[SNSSubscriptionProperties],
163+
) -> ServiceLevelClientFactory:
164+
model = request.desired_state
165+
if subscription_region := model.get("Region"):
166+
# FIXME: this is hacky, maybe we should have access to the original parameters for the `aws_client_factory`
167+
# as we now need to manually use them
168+
# Not all internal CloudFormation requests will be directed to the same region and account
169+
# maybe we could need to expose a proper client factory where we can override some parameters like the
170+
# Region
171+
factory = ConvertingInternalClientFactory(use_ssl=config.DISTRIBUTED_MODE)
172+
client_params = dict(request.aws_client_factory._client_creation_params)
173+
client_params["region_name"] = subscription_region
174+
service_factory = factory(**client_params)
175+
else:
176+
service_factory = request.aws_client_factory
177+
178+
return service_factory

0 commit comments

Comments
 (0)