Skip to content

Commit 944e255

Browse files
mndeveciCoshUSmoelasmar
authored andcommitted
refactor: Optimize shared API usage plan handling (aws#1973)
* fix: use instance variables for generating shared api usage plan * add extra log statements * fix: Added SAR Support Check (aws#1972) * Added SAR Support Check * Added docstring and Removed Instance Initialization for Class Method * set log level explicitly * update pyyaml version to get the security update (aws#1974) * fix: use instance variables for generating shared api usage plan * add extra log statements * set log level explicitly * black formatting * black formatting Co-authored-by: Cosh_ <[email protected]> Co-authored-by: Mohamed Elasmar <[email protected]>
1 parent 777fcfc commit 944e255

File tree

3 files changed

+35
-14
lines changed

3 files changed

+35
-14
lines changed

samtranslator/model/api/api_generator.py

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
from collections import namedtuple
23
from six import string_types
34
from samtranslator.model.intrinsics import ref, fnGetAtt
@@ -24,6 +25,9 @@
2425
from samtranslator.translator.arn_generator import ArnGenerator
2526
from samtranslator.model.tags.resource_tagging import get_tag_list
2627

28+
LOG = logging.getLogger(__name__)
29+
LOG.setLevel(logging.INFO)
30+
2731
_CORS_WILDCARD = "'*'"
2832
CorsProperties = namedtuple(
2933
"_CorsProperties", ["AllowMethods", "AllowHeaders", "AllowOrigin", "MaxAge", "AllowCredentials"]
@@ -52,12 +56,20 @@
5256
GatewayResponseProperties = ["ResponseParameters", "ResponseTemplates", "StatusCode"]
5357

5458

55-
class ApiGenerator(object):
56-
usage_plan_shared = False
57-
stage_keys_shared = list()
58-
api_stages_shared = list()
59-
depends_on_shared = list()
59+
class SharedApiUsagePlan(object):
60+
"""
61+
Collects API information from different API resources in the same template,
62+
so that these information can be used in the shared usage plan
63+
"""
64+
65+
def __init__(self):
66+
self.usage_plan_shared = False
67+
self.stage_keys_shared = list()
68+
self.api_stages_shared = list()
69+
self.depends_on_shared = list()
6070

71+
72+
class ApiGenerator(object):
6173
def __init__(
6274
self,
6375
logical_id,
@@ -69,6 +81,7 @@ def __init__(
6981
definition_uri,
7082
name,
7183
stage_name,
84+
shared_api_usage_plan,
7285
tags=None,
7386
endpoint_configuration=None,
7487
method_settings=None,
@@ -134,6 +147,7 @@ def __init__(
134147
self.models = models
135148
self.domain = domain
136149
self.description = description
150+
self.shared_api_usage_plan = shared_api_usage_plan
137151

138152
def _construct_rest_api(self):
139153
"""Constructs and returns the ApiGateway RestApi.
@@ -630,18 +644,19 @@ def _construct_usage_plan(self, rest_api_stage=None):
630644

631645
# create a usage plan for all the Apis
632646
elif create_usage_plan == "SHARED":
647+
LOG.info("Creating SHARED usage plan for all the Apis")
633648
usage_plan_logical_id = "ServerlessUsagePlan"
634-
if self.logical_id not in ApiGenerator.depends_on_shared:
635-
ApiGenerator.depends_on_shared.append(self.logical_id)
649+
if self.logical_id not in self.shared_api_usage_plan.depends_on_shared:
650+
self.shared_api_usage_plan.depends_on_shared.append(self.logical_id)
636651
usage_plan = ApiGatewayUsagePlan(
637-
logical_id=usage_plan_logical_id, depends_on=ApiGenerator.depends_on_shared
652+
logical_id=usage_plan_logical_id, depends_on=self.shared_api_usage_plan.depends_on_shared
638653
)
639654
api_stage = dict()
640655
api_stage["ApiId"] = ref(self.logical_id)
641656
api_stage["Stage"] = ref(rest_api_stage.logical_id)
642-
if api_stage not in ApiGenerator.api_stages_shared:
643-
ApiGenerator.api_stages_shared.append(api_stage)
644-
usage_plan.ApiStages = ApiGenerator.api_stages_shared
657+
if api_stage not in self.shared_api_usage_plan.api_stages_shared:
658+
self.shared_api_usage_plan.api_stages_shared.append(api_stage)
659+
usage_plan.ApiStages = self.shared_api_usage_plan.api_stages_shared
645660

646661
api_key = self._construct_api_key(usage_plan_logical_id, create_usage_plan, rest_api_stage)
647662
usage_plan_key = self._construct_usage_plan_key(usage_plan_logical_id, create_usage_plan, api_key)
@@ -667,15 +682,16 @@ def _construct_api_key(self, usage_plan_logical_id, create_usage_plan, rest_api_
667682
"""
668683
if create_usage_plan == "SHARED":
669684
# create an api key resource for all the apis
685+
LOG.info("Creating api key resource for all the Apis from SHARED usage plan")
670686
api_key_logical_id = "ServerlessApiKey"
671687
api_key = ApiGatewayApiKey(logical_id=api_key_logical_id, depends_on=[usage_plan_logical_id])
672688
api_key.Enabled = True
673689
stage_key = dict()
674690
stage_key["RestApiId"] = ref(self.logical_id)
675691
stage_key["StageName"] = ref(rest_api_stage.logical_id)
676-
if stage_key not in ApiGenerator.stage_keys_shared:
677-
ApiGenerator.stage_keys_shared.append(stage_key)
678-
api_key.StageKeys = ApiGenerator.stage_keys_shared
692+
if stage_key not in self.shared_api_usage_plan.stage_keys_shared:
693+
self.shared_api_usage_plan.stage_keys_shared.append(stage_key)
694+
api_key.StageKeys = self.shared_api_usage_plan.stage_keys_shared
679695
# for create_usage_plan = "PER_API"
680696
else:
681697
# create an api key resource for this api

samtranslator/model/sam_resources.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,7 @@ def to_cloudformation(self, **kwargs):
857857
self.Domain = intrinsics_resolver.resolve_parameter_refs(self.Domain)
858858
self.Auth = intrinsics_resolver.resolve_parameter_refs(self.Auth)
859859
redeploy_restapi_parameters = kwargs.get("redeploy_restapi_parameters")
860+
shared_api_usage_plan = kwargs.get("shared_api_usage_plan")
860861

861862
api_generator = ApiGenerator(
862863
self.logical_id,
@@ -868,6 +869,7 @@ def to_cloudformation(self, **kwargs):
868869
self.DefinitionUri,
869870
self.Name,
870871
self.StageName,
872+
shared_api_usage_plan,
871873
tags=self.Tags,
872874
endpoint_configuration=self.EndpointConfiguration,
873875
method_settings=self.MethodSettings,

samtranslator/translator/translator.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
FeatureToggleDefaultConfigProvider,
77
)
88
from samtranslator.model import ResourceTypeResolver, sam_resources
9+
from samtranslator.model.api.api_generator import SharedApiUsagePlan
910
from samtranslator.translator.verify_logical_id import verify_unique_logical_id
1011
from samtranslator.model.preferences.deployment_preference_collection import DeploymentPreferenceCollection
1112
from samtranslator.model.exceptions import (
@@ -111,6 +112,7 @@ def translate(self, sam_template, parameter_values, feature_toggle=None):
111112
)
112113
deployment_preference_collection = DeploymentPreferenceCollection()
113114
supported_resource_refs = SupportedResourceReferences()
115+
shared_api_usage_plan = SharedApiUsagePlan()
114116
document_errors = []
115117
changed_logical_ids = {}
116118
for logical_id, resource_dict in self._get_resources_to_iterate(sam_template, macro_resolver):
@@ -130,6 +132,7 @@ def translate(self, sam_template, parameter_values, feature_toggle=None):
130132
resource_dict, intrinsics_resolver
131133
)
132134
kwargs["redeploy_restapi_parameters"] = self.redeploy_restapi_parameters
135+
kwargs["shared_api_usage_plan"] = shared_api_usage_plan
133136
translated = macro.to_cloudformation(**kwargs)
134137

135138
supported_resource_refs = macro.get_resource_references(translated, supported_resource_refs)

0 commit comments

Comments
 (0)