11
11
AuthProperties = namedtuple ("_AuthProperties" , ["Authorizers" , "DefaultAuthorizer" ])
12
12
AuthProperties .__new__ .__defaults__ = (None , None )
13
13
DefaultStageName = "$default"
14
+ HttpApiTagName = "httpapi:createdBy"
14
15
15
16
16
17
class HttpApiGenerator (object ):
@@ -70,6 +71,7 @@ def _construct_http_api(self):
70
71
)
71
72
72
73
self ._add_auth ()
74
+ self ._add_tags ()
73
75
74
76
if self .definition_uri :
75
77
http_api .BodyS3Location = self ._construct_body_s3_dict ()
@@ -83,9 +85,6 @@ def _construct_http_api(self):
83
85
"add a 'HttpApi' event to an 'AWS::Serverless::Function'" ,
84
86
)
85
87
86
- if self .tags is not None :
87
- http_api .Tags = get_tag_list (self .tags )
88
-
89
88
return http_api
90
89
91
90
def _add_auth (self ):
@@ -97,7 +96,7 @@ def _add_auth(self):
97
96
98
97
if self .auth and not self .definition_body :
99
98
raise InvalidResourceException (
100
- self .logical_id , "Auth works only with inline Swagger specified in " " 'DefinitionBody' property"
99
+ self .logical_id , "Auth works only with inline OpenApi specified in the 'DefinitionBody' property. "
101
100
)
102
101
103
102
# Make sure keys in the dict are recognized
@@ -107,7 +106,7 @@ def _add_auth(self):
107
106
if not OpenApiEditor .is_valid (self .definition_body ):
108
107
raise InvalidResourceException (
109
108
self .logical_id ,
110
- "Unable to add Auth configuration because " " 'DefinitionBody' does not contain a valid Swagger " ,
109
+ "Unable to add Auth configuration because 'DefinitionBody' does not contain a valid OpenApi definition. " ,
111
110
)
112
111
open_api_editor = OpenApiEditor (self .definition_body )
113
112
auth_properties = AuthProperties (** self .auth )
@@ -120,6 +119,36 @@ def _add_auth(self):
120
119
)
121
120
self .definition_body = open_api_editor .openapi
122
121
122
+ def _add_tags (self ):
123
+ """
124
+ Adds tags to the Http Api, including a default SAM tag.
125
+ """
126
+ if self .tags and not self .definition_body :
127
+ raise InvalidResourceException (
128
+ self .logical_id , "Tags works only with inline OpenApi specified in the 'DefinitionBody' property."
129
+ )
130
+
131
+ if not self .definition_body :
132
+ return
133
+
134
+ if self .tags and not OpenApiEditor .is_valid (self .definition_body ):
135
+ raise InvalidResourceException (
136
+ self .logical_id ,
137
+ "Unable to add `Tags` because 'DefinitionBody' does not contain a valid OpenApi definition." ,
138
+ )
139
+ elif not OpenApiEditor .is_valid (self .definition_body ):
140
+ return
141
+
142
+ if not self .tags :
143
+ self .tags = {}
144
+ self .tags [HttpApiTagName ] = "SAM"
145
+
146
+ open_api_editor = OpenApiEditor (self .definition_body )
147
+
148
+ # authorizers is guaranteed to return a value or raise an exception
149
+ open_api_editor .add_tags (self .tags )
150
+ self .definition_body = open_api_editor .openapi
151
+
123
152
def _set_default_authorizer (self , open_api_editor , authorizers , default_authorizer , api_authorizers ):
124
153
"""
125
154
Sets the default authorizer if one is given in the template
@@ -134,7 +163,9 @@ def _set_default_authorizer(self, open_api_editor, authorizers, default_authoriz
134
163
if not authorizers .get (default_authorizer ):
135
164
raise InvalidResourceException (
136
165
self .logical_id ,
137
- "Unable to set DefaultAuthorizer because '" + default_authorizer + "' was not defined in 'Authorizers'" ,
166
+ "Unable to set DefaultAuthorizer because '"
167
+ + default_authorizer
168
+ + "' was not defined in 'Authorizers'." ,
138
169
)
139
170
140
171
for path in open_api_editor .iter_on_path ():
@@ -151,7 +182,7 @@ def _get_authorizers(self, authorizers_config, default_authorizer=None):
151
182
authorizers = {}
152
183
153
184
if not isinstance (authorizers_config , dict ):
154
- raise InvalidResourceException (self .logical_id , "Authorizers must be a dictionary" )
185
+ raise InvalidResourceException (self .logical_id , "Authorizers must be a dictionary. " )
155
186
156
187
for authorizer_name , authorizer in authorizers_config .items ():
157
188
if not isinstance (authorizer , dict ):
@@ -179,7 +210,7 @@ def _construct_body_s3_dict(self):
179
210
if not self .definition_uri .get ("Bucket" , None ) or not self .definition_uri .get ("Key" , None ):
180
211
# DefinitionUri is a dictionary but does not contain Bucket or Key property
181
212
raise InvalidResourceException (
182
- self .logical_id , "'DefinitionUri' requires Bucket and Key properties to be specified"
213
+ self .logical_id , "'DefinitionUri' requires Bucket and Key properties to be specified. "
183
214
)
184
215
s3_pointer = self .definition_uri
185
216
@@ -226,9 +257,6 @@ def _construct_stage(self):
226
257
stage .AccessLogSettings = self .access_log_settings
227
258
stage .AutoDeploy = True
228
259
229
- if self .tags is not None :
230
- stage .Tags = get_tag_list (self .tags )
231
-
232
260
return stage
233
261
234
262
def to_cloudformation (self ):
0 commit comments