cfn-model 0.4.17 → 0.4.18
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/cfn-model/transforms/serverless.rb +53 -24
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a30f11c718dd0eff8aca481d309eb43ec956fb7589f4bac4509568029c49d96a
|
4
|
+
data.tar.gz: b49b0f87f0314422b9596e83377ac80ab53f8587426549b8dca118c9bc1f56e3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5be4b84d0149684785f3696941d2a288f7f7080057bb2d31945e9561ca73625134aed3a9a405930651d3bdcdc29529f79bc2ab7b2a31b80eeb81b7cc9ae18306
|
7
|
+
data.tar.gz: f863433dfba321ace4cfd1b685d2176b8ff56ec09826f9b6863f44079b7ac96ecf80206f394ac2f0967082686c9ca7e95bfc0343e10294cc38b4780005bd12d1
|
@@ -7,10 +7,13 @@ class CfnModel
|
|
7
7
|
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/transform-aws-serverless.html
|
8
8
|
class Serverless
|
9
9
|
def perform_transform(cfn_hash)
|
10
|
+
with_line_numbers = false
|
10
11
|
resources = cfn_hash['Resources'].clone
|
11
12
|
resources.each do |resource_name, resource|
|
12
|
-
next unless resource['Type']
|
13
|
-
|
13
|
+
next unless matching_resource_type?(resource['Type'], 'AWS::Serverless::Function')
|
14
|
+
|
15
|
+
with_line_numbers = true if resource['Type'].is_a? Hash
|
16
|
+
replace_serverless_function cfn_hash, resource_name, with_line_numbers
|
14
17
|
end
|
15
18
|
end
|
16
19
|
|
@@ -39,6 +42,23 @@ class CfnModel
|
|
39
42
|
end
|
40
43
|
end
|
41
44
|
|
45
|
+
def matching_resource_type?(resource_type, type_to_match)
|
46
|
+
matching_string_resource_type?(resource_type, type_to_match) ||
|
47
|
+
matching_line_number_enriched_resource_type?(resource_type, type_to_match)
|
48
|
+
end
|
49
|
+
|
50
|
+
def matching_string_resource_type?(resource_type, type_to_match)
|
51
|
+
resource_type.is_a?(String) && resource_type.eql?(type_to_match)
|
52
|
+
end
|
53
|
+
|
54
|
+
def matching_line_number_enriched_resource_type?(resource_type, type_to_match)
|
55
|
+
resource_type.is_a?(Hash) && resource_type['value'].eql?(type_to_match)
|
56
|
+
end
|
57
|
+
|
58
|
+
def format_resource_type(type, line_no, numbers)
|
59
|
+
numbers ? { 'value' => type, 'line' => line_no } : type
|
60
|
+
end
|
61
|
+
|
42
62
|
def resolve_globals_function_property(cfn_hash, property_name)
|
43
63
|
cfn_hash['Globals'] && cfn_hash['Globals']['Function'] && cfn_hash['Globals']['Function'][property_name]
|
44
64
|
end
|
@@ -61,12 +81,13 @@ class CfnModel
|
|
61
81
|
lambda_fn_params
|
62
82
|
end
|
63
83
|
|
64
|
-
def serverless_function_properties(cfn_hash, serverless_function)
|
84
|
+
def serverless_function_properties(cfn_hash, serverless_function, with_line_numbers)
|
65
85
|
code_uri = serverless_function_property(serverless_function, cfn_hash, 'CodeUri')
|
66
86
|
|
67
87
|
lambda_fn_params = {
|
68
88
|
handler: serverless_function_property(serverless_function, cfn_hash, 'Handler'),
|
69
|
-
runtime: serverless_function_property(serverless_function, cfn_hash, 'Runtime')
|
89
|
+
runtime: serverless_function_property(serverless_function, cfn_hash, 'Runtime'),
|
90
|
+
with_line_numbers: with_line_numbers
|
70
91
|
}
|
71
92
|
|
72
93
|
lambda_fn_params = transform_code_uri(
|
@@ -77,15 +98,15 @@ class CfnModel
|
|
77
98
|
lambda_fn_params
|
78
99
|
end
|
79
100
|
|
80
|
-
def replace_serverless_function(cfn_hash, resource_name)
|
101
|
+
def replace_serverless_function(cfn_hash, resource_name, with_line_numbers)
|
81
102
|
serverless_function = cfn_hash['Resources'][resource_name]
|
82
103
|
|
83
|
-
lambda_fn_params = serverless_function_properties(cfn_hash, serverless_function)
|
104
|
+
lambda_fn_params = serverless_function_properties(cfn_hash, serverless_function, with_line_numbers)
|
84
105
|
|
85
106
|
cfn_hash['Resources'][resource_name] = lambda_function lambda_fn_params
|
86
|
-
cfn_hash['Resources']['FunctionNameRole'] = function_name_role
|
107
|
+
cfn_hash['Resources']['FunctionNameRole'] = function_name_role with_line_numbers
|
87
108
|
|
88
|
-
transform_function_events(cfn_hash, serverless_function, resource_name) if \
|
109
|
+
transform_function_events(cfn_hash, serverless_function, resource_name, with_line_numbers) if \
|
89
110
|
serverless_function['Properties']['Events']
|
90
111
|
end
|
91
112
|
|
@@ -104,9 +125,9 @@ class CfnModel
|
|
104
125
|
|
105
126
|
# Return the hash structure of the 'FunctionNameRole'
|
106
127
|
# AWS::IAM::Role resource as created by Serverless transform
|
107
|
-
def function_name_role
|
128
|
+
def function_name_role(with_line_numbers)
|
108
129
|
{
|
109
|
-
'Type' => 'AWS::IAM::Role',
|
130
|
+
'Type' => format_resource_type('AWS::IAM::Role', -1, with_line_numbers),
|
110
131
|
'Properties' => {
|
111
132
|
'ManagedPolicyArns' => [
|
112
133
|
'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole'
|
@@ -131,9 +152,10 @@ class CfnModel
|
|
131
152
|
def lambda_function(handler:,
|
132
153
|
code_bucket: nil,
|
133
154
|
code_key: nil,
|
134
|
-
runtime
|
155
|
+
runtime:,
|
156
|
+
with_line_numbers: false)
|
135
157
|
fn_resource = {
|
136
|
-
'Type' => 'AWS::Lambda::Function',
|
158
|
+
'Type' => format_resource_type('AWS::Lambda::Function', -1, with_line_numbers),
|
137
159
|
'Properties' => {
|
138
160
|
'Handler' => handler,
|
139
161
|
'Role' => { 'Fn::GetAtt' => %w[FunctionNameRole Arn] },
|
@@ -145,15 +167,16 @@ class CfnModel
|
|
145
167
|
|
146
168
|
# Return the Event structure of a AWS::Lambda::Function as created
|
147
169
|
# by Serverless transform
|
148
|
-
def transform_function_events(cfn_hash, serverless_function, function_name)
|
170
|
+
def transform_function_events(cfn_hash, serverless_function, function_name, with_line_numbers)
|
149
171
|
serverless_function['Properties']['Events'].each do |_, event|
|
150
|
-
serverlessrestapi_resources(cfn_hash, event, function_name) if
|
172
|
+
serverlessrestapi_resources(cfn_hash, event, function_name, with_line_numbers) if \
|
173
|
+
matching_resource_type?(event['Type'], 'Api')
|
151
174
|
end
|
152
175
|
end
|
153
176
|
|
154
|
-
def serverlessrestapi_resources(cfn_hash, event, func_name)
|
177
|
+
def serverlessrestapi_resources(cfn_hash, event, func_name, with_line_numbers)
|
155
178
|
# ServerlessRestApi
|
156
|
-
cfn_hash['Resources']['ServerlessRestApi'] ||= serverlessrestapi_base
|
179
|
+
cfn_hash['Resources']['ServerlessRestApi'] ||= serverlessrestapi_base with_line_numbers
|
157
180
|
add_serverlessrestapi_event(
|
158
181
|
cfn_hash['Resources']['ServerlessRestApi']['Properties']['Body']['paths'],
|
159
182
|
event,
|
@@ -161,15 +184,15 @@ class CfnModel
|
|
161
184
|
)
|
162
185
|
|
163
186
|
# ServerlessRestApiDeployment
|
164
|
-
cfn_hash['Resources']['ServerlessRestApiDeployment'] = serverlessrestapi_deployment
|
187
|
+
cfn_hash['Resources']['ServerlessRestApiDeployment'] = serverlessrestapi_deployment with_line_numbers
|
165
188
|
|
166
189
|
# ServerlessRestApiProdStage
|
167
|
-
cfn_hash['Resources']['ServerlessRestApiProdStage'] = serverlessrestapi_stage
|
190
|
+
cfn_hash['Resources']['ServerlessRestApiProdStage'] = serverlessrestapi_stage with_line_numbers
|
168
191
|
end
|
169
192
|
|
170
|
-
def serverlessrestapi_base
|
193
|
+
def serverlessrestapi_base(with_line_nos)
|
171
194
|
{
|
172
|
-
'Type' => 'AWS::ApiGateway::RestApi',
|
195
|
+
'Type' => format_resource_type('AWS::ApiGateway::RestApi', -1, with_line_nos),
|
173
196
|
'Properties' => {
|
174
197
|
'Body' => {
|
175
198
|
'info' => {
|
@@ -196,20 +219,26 @@ class CfnModel
|
|
196
219
|
}
|
197
220
|
end
|
198
221
|
|
199
|
-
def serverlessrestapi_deployment
|
222
|
+
def serverlessrestapi_deployment(with_line_nos)
|
200
223
|
{
|
201
|
-
'Type' => 'AWS::ApiGateway::Deployment',
|
224
|
+
'Type' => format_resource_type('AWS::ApiGateway::Deployment', -1, with_line_nos),
|
202
225
|
'Properties' => {
|
203
226
|
'Description' => 'Generated by cfn-model',
|
204
227
|
'RestApiId' => { 'Ref' => 'ServerlessRestApi' },
|
228
|
+
'StageDescription' => {
|
229
|
+
'AccessLogSetting' => {
|
230
|
+
'DestinationArn' => 'arn:aws:logs:region:account:group/ApiLogs',
|
231
|
+
'Format' => '$context.requestId'
|
232
|
+
}
|
233
|
+
},
|
205
234
|
'StageName' => 'Stage'
|
206
235
|
}
|
207
236
|
}
|
208
237
|
end
|
209
238
|
|
210
|
-
def serverlessrestapi_stage
|
239
|
+
def serverlessrestapi_stage(with_line_nos)
|
211
240
|
{
|
212
|
-
'Type' => 'AWS::ApiGateway::Stage',
|
241
|
+
'Type' => format_resource_type('AWS::ApiGateway::Stage', -1, with_line_nos),
|
213
242
|
'Properties' => {
|
214
243
|
'DeploymentId' => { 'Ref' => 'ServerlessRestApiDeployment' },
|
215
244
|
'RestApiId' => { 'Ref' => 'ServerlessRestApi' },
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cfn-model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.18
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eric Kascic
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-02-
|
11
|
+
date: 2020-02-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rubocop
|