1
+ import logging
1
2
from collections import namedtuple
2
3
from six import string_types
3
4
from samtranslator .model .intrinsics import ref , fnGetAtt
24
25
from samtranslator .translator .arn_generator import ArnGenerator
25
26
from samtranslator .model .tags .resource_tagging import get_tag_list
26
27
28
+ LOG = logging .getLogger (__name__ )
29
+ LOG .setLevel (logging .INFO )
30
+
27
31
_CORS_WILDCARD = "'*'"
28
32
CorsProperties = namedtuple (
29
33
"_CorsProperties" , ["AllowMethods" , "AllowHeaders" , "AllowOrigin" , "MaxAge" , "AllowCredentials" ]
52
56
GatewayResponseProperties = ["ResponseParameters" , "ResponseTemplates" , "StatusCode" ]
53
57
54
58
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 ()
60
70
71
+
72
+ class ApiGenerator (object ):
61
73
def __init__ (
62
74
self ,
63
75
logical_id ,
@@ -69,6 +81,7 @@ def __init__(
69
81
definition_uri ,
70
82
name ,
71
83
stage_name ,
84
+ shared_api_usage_plan ,
72
85
tags = None ,
73
86
endpoint_configuration = None ,
74
87
method_settings = None ,
@@ -134,6 +147,7 @@ def __init__(
134
147
self .models = models
135
148
self .domain = domain
136
149
self .description = description
150
+ self .shared_api_usage_plan = shared_api_usage_plan
137
151
138
152
def _construct_rest_api (self ):
139
153
"""Constructs and returns the ApiGateway RestApi.
@@ -630,18 +644,19 @@ def _construct_usage_plan(self, rest_api_stage=None):
630
644
631
645
# create a usage plan for all the Apis
632
646
elif create_usage_plan == "SHARED" :
647
+ LOG .info ("Creating SHARED usage plan for all the Apis" )
633
648
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 )
636
651
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
638
653
)
639
654
api_stage = dict ()
640
655
api_stage ["ApiId" ] = ref (self .logical_id )
641
656
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
645
660
646
661
api_key = self ._construct_api_key (usage_plan_logical_id , create_usage_plan , rest_api_stage )
647
662
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_
667
682
"""
668
683
if create_usage_plan == "SHARED" :
669
684
# create an api key resource for all the apis
685
+ LOG .info ("Creating api key resource for all the Apis from SHARED usage plan" )
670
686
api_key_logical_id = "ServerlessApiKey"
671
687
api_key = ApiGatewayApiKey (logical_id = api_key_logical_id , depends_on = [usage_plan_logical_id ])
672
688
api_key .Enabled = True
673
689
stage_key = dict ()
674
690
stage_key ["RestApiId" ] = ref (self .logical_id )
675
691
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
679
695
# for create_usage_plan = "PER_API"
680
696
else :
681
697
# create an api key resource for this api
0 commit comments