-
Notifications
You must be signed in to change notification settings - Fork 494
Expand file tree
/
Copy pathLambdaDefaultsConfigFileParser.cs
More file actions
382 lines (315 loc) · 14.3 KB
/
LambdaDefaultsConfigFileParser.cs
File metadata and controls
382 lines (315 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
using System;
using System.Collections;
using System.IO;
using System.Collections.Generic;
using System.Text.Json;
using YamlDotNet.RepresentationModel;
namespace Amazon.Lambda.TestTool.Runtime
{
/// <summary>
/// This class handles getting the configuration information from aws-lambda-tools-defaults.json file
/// and possibly a CloudFormation template. YAML CloudFormation templates aren't supported yet.
/// </summary>
public static class LambdaDefaultsConfigFileParser
{
public static LambdaConfigInfo LoadFromFile(string filePath)
{
if (!File.Exists(filePath))
{
throw new FileNotFoundException($"Lambda config file {filePath} not found");
}
var configFile = JsonSerializer.Deserialize<LambdaConfigFile>(File.ReadAllText(filePath).Trim(), new System.Text.Json.JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});
configFile.ConfigFileLocation = filePath;
return LoadFromFile(configFile);
}
public static LambdaConfigInfo LoadFromFile(LambdaConfigFile configFile)
{
var configInfo = new LambdaConfigInfo
{
AWSProfile = !string.IsNullOrEmpty(configFile.Profile) ? configFile.Profile : "default",
AWSRegion = !string.IsNullOrEmpty(configFile.Region) ? configFile.Region : null,
FunctionInfos = new List<LambdaFunctionInfo>()
};
if (string.IsNullOrEmpty(configInfo.AWSProfile))
configInfo.AWSProfile = "default";
var templateFileName = !string.IsNullOrEmpty(configFile.Template) ? configFile.Template : null;
var functionHandler = !string.IsNullOrEmpty(configFile.DetermineHandler()) ? configFile.DetermineHandler() : null;
if (!string.IsNullOrEmpty(templateFileName))
{
var directory = Directory.Exists(configFile.ConfigFileLocation) ? configFile.ConfigFileLocation : Path.GetDirectoryName(configFile.ConfigFileLocation);
var templateFullPath = Path.Combine(directory, templateFileName);
if (!File.Exists(templateFullPath))
{
throw new FileNotFoundException($"Serverless template file {templateFullPath} not found");
}
ProcessServerlessTemplate(configInfo, templateFullPath);
}
else if(!string.IsNullOrEmpty(functionHandler))
{
var info = new LambdaFunctionInfo
{
Handler = functionHandler
};
info.Name = !string.IsNullOrEmpty(configFile.FunctionName) ? configFile.FunctionName : null;
if (string.IsNullOrEmpty(info.Name))
{
info.Name = functionHandler;
}
info.Timeout = GetFunctionTimeOut(configFile);
if (configFile.EnvironmentVariables != null)
{
ParseKeyValueOption(configFile.EnvironmentVariables, info.EnvironmentVariables);
}
configInfo.FunctionInfos.Add(info);
}
configInfo.FunctionInfos.Sort((x, y ) => string.CompareOrdinal(x.Name, y.Name));
return configInfo;
}
public static void ParseKeyValueOption(string keyValueString, IDictionary<string, string> values)
{
if (string.IsNullOrWhiteSpace(keyValueString))
return;
try
{
var currentPos = 0;
while (currentPos != -1 && currentPos < keyValueString.Length)
{
string name;
GetNextToken(keyValueString, '=', ref currentPos, out name);
string value;
GetNextToken(keyValueString, ';', ref currentPos, out value);
if (string.IsNullOrEmpty(name))
throw new CommandLineParseException($"Error parsing option ({keyValueString}), format should be <key1>=<value1>;<key2>=<value2>");
values[name] = value ?? string.Empty;
}
}
catch (CommandLineParseException)
{
throw;
}
catch (Exception e)
{
throw new CommandLineParseException($"Error parsing option ({keyValueString}), format should be <key1>=<value1>;<key2>=<value2>: {e.Message}");
}
return;
}
private static void GetNextToken(string option, char endToken, ref int currentPos, out string token)
{
if (option.Length <= currentPos)
{
token = string.Empty;
return;
}
int tokenStart = currentPos;
int tokenEnd = -1;
bool inQuote = false;
if (option[currentPos] == '"')
{
inQuote = true;
tokenStart++;
currentPos++;
while (currentPos < option.Length && option[currentPos] != '"')
{
currentPos++;
}
if (option[currentPos] == '"')
tokenEnd = currentPos;
}
while (currentPos < option.Length && option[currentPos] != endToken)
{
currentPos++;
}
if (!inQuote)
{
if (currentPos < option.Length && option[currentPos] == endToken)
tokenEnd = currentPos;
}
if (tokenEnd == -1)
token = option.Substring(tokenStart);
else
token = option.Substring(tokenStart, tokenEnd - tokenStart);
currentPos++;
}
private static void ProcessServerlessTemplate(LambdaConfigInfo configInfo, string templateFilePath)
{
var content = File.ReadAllText(templateFilePath).Trim();
if(content[0] != '{')
{
ProcessYamlServerlessTemplate(configInfo, content);
}
else
{
ProcessJsonServerlessTemplate(configInfo, content);
}
}
private static void ProcessYamlServerlessTemplate(LambdaConfigInfo configInfo, string content)
{
var yaml = new YamlStream();
yaml.Load(new StringReader(content));
var root = (YamlMappingNode)yaml.Documents[0].RootNode;
if (root == null)
return;
YamlMappingNode resources = null;
if (root.Children.ContainsKey("Resources"))
{
resources = root.Children["Resources"] as YamlMappingNode;
ProcessYamlServerlessTemplateResourcesBased(configInfo, resources);
} else if (root.Children.ContainsKey("functions"))
{
resources = (YamlMappingNode) root.Children["functions"];
ProcessYamlServerlessTemplateFunctionBased(configInfo, resources);
}
;
}
private static void ProcessYamlServerlessTemplateResourcesBased(LambdaConfigInfo configInfo, YamlMappingNode resources)
{
if (resources == null)
return;
foreach (var resource in resources.Children)
{
var resourceBody = (YamlMappingNode) resource.Value;
var type = resourceBody.Children.ContainsKey("Type")
? ((YamlScalarNode) resourceBody.Children["Type"])?.Value
: null;
if (!string.Equals("AWS::Serverless::Function", type, StringComparison.Ordinal) &&
!string.Equals("AWS::Lambda::Function", type, StringComparison.Ordinal))
{
continue;
}
var properties = resourceBody.Children.ContainsKey("Properties")
? resourceBody.Children["Properties"] as YamlMappingNode
: null;
if (properties == null)
{
continue;
}
string handler = null;
if(properties.Children.ContainsKey("Handler"))
{
handler = ((YamlScalarNode)properties.Children["Handler"])?.Value;
}
if (string.IsNullOrEmpty(handler) && properties.Children.ContainsKey("ImageConfig"))
{
var imageConfigNode = properties.Children["ImageConfig"] as YamlMappingNode;
if (imageConfigNode.Children.ContainsKey("Command"))
{
var imageCommandNode = imageConfigNode.Children["Command"] as YamlSequenceNode;
// Grab the first element assuming that is the function handler.
var en = imageCommandNode.GetEnumerator();
en.MoveNext();
handler = ((YamlScalarNode)en.Current)?.Value;
}
}
if (!string.IsNullOrEmpty(handler))
{
var functionInfo = new LambdaFunctionInfo
{
Name = resource.Key.ToString(),
Handler = handler
};
configInfo.FunctionInfos.Add(functionInfo);
}
}
}
private static void ProcessYamlServerlessTemplateFunctionBased(LambdaConfigInfo configInfo, YamlMappingNode resources)
{
if (resources == null)
return;
foreach (var resource in resources.Children)
{
var resourceBody = (YamlMappingNode) resource.Value;
var handler = resourceBody.Children.ContainsKey("handler")
? ((YamlScalarNode) resourceBody.Children["handler"])?.Value
: null;
if (handler == null) continue;
if (string.IsNullOrEmpty(handler)) continue;
var functionInfo = new LambdaFunctionInfo
{
Name = resource.Key.ToString(),
Handler = handler
};
if (resourceBody.Children.TryGetValue("Environment", out var environmentProperty) && environmentProperty is YamlMappingNode)
{
foreach (var kvp in ((YamlMappingNode)environmentProperty).Children)
{
if(kvp.Key is YamlScalarNode keyNode && keyNode.Value != null &&
kvp.Value is YamlScalarNode valueNode && valueNode.Value != null)
{
functionInfo.EnvironmentVariables[keyNode.Value] = valueNode.Value;
}
}
}
configInfo.FunctionInfos.Add(functionInfo);
}
}
private static void ProcessJsonServerlessTemplate(LambdaConfigInfo configInfo, string content)
{
var rootData = JsonDocument.Parse(content);
JsonElement resourcesNode;
if (!rootData.RootElement.TryGetProperty("Resources", out resourcesNode))
return;
foreach (var resourceProperty in resourcesNode.EnumerateObject())
{
var resource = resourceProperty.Value;
JsonElement typeProperty;
if (!resource.TryGetProperty("Type", out typeProperty))
continue;
var type = typeProperty.GetString();
JsonElement propertiesProperty;
if (!resource.TryGetProperty("Properties", out propertiesProperty))
continue;
if (!string.Equals("AWS::Serverless::Function", type, StringComparison.Ordinal) &&
!string.Equals("AWS::Lambda::Function", type, StringComparison.Ordinal))
{
continue;
}
string handler = null;
if (propertiesProperty.TryGetProperty("Handler", out var handlerProperty))
{
handler = handlerProperty.GetString();
}
else if(propertiesProperty.TryGetProperty("ImageConfig", out var imageConfigProperty) &&
imageConfigProperty.TryGetProperty("Command", out var imageCommandProperty))
{
if(imageCommandProperty.GetArrayLength() > 0)
{
// Grab the first element assuming that is the function handler.
var en = imageCommandProperty.EnumerateArray();
en.MoveNext();
handler = en.Current.GetString();
}
}
if (!string.IsNullOrEmpty(handler))
{
var functionInfo = new LambdaFunctionInfo
{
Name = resourceProperty.Name,
Handler = handler
};
if(propertiesProperty.TryGetProperty("Environment", out var environmentProperty) &&
environmentProperty.TryGetProperty("Variables", out var variablesProperty))
{
foreach(var property in variablesProperty.EnumerateObject())
{
if(property.Value.ValueKind == JsonValueKind.String)
{
functionInfo.EnvironmentVariables[property.Name] = property.Value.GetString();
}
}
}
configInfo.FunctionInfos.Add(functionInfo);
}
}
}
private static TimeSpan GetFunctionTimeOut(LambdaConfigFile configFile)
{
var configValue = configFile.FunctionDebugTimeOut ?? configFile.FunctionTimeOut;
return configValue.HasValue
? TimeSpan.FromSeconds(configValue.Value)
: TimeSpan.FromMinutes(15);
}
}
}