Exemple #1
0
        public static void Serialize(this FunctionJsonSchema functionJson, string path)
        {
            var content = JsonConvert.SerializeObject(functionJson, Formatting.Indented);
            var dir     = Path.GetDirectoryName(path);

            Directory.CreateDirectory(dir);
            File.WriteAllText(path, content);
        }
        private bool CheckAppSettingsAndFunctionName(FunctionJsonSchema functionJson, MethodInfo method)
        {
            try
            {
                var functionName = method.GetSdkFunctionName();
                if (this._functionNamesSet.ContainsKey(functionName))
                {
                    var dupMethod = this._functionNamesSet[functionName];
                    _logger.LogError($"Function {method.DeclaringType.FullName}.{method.Name} and {dupMethod.DeclaringType.FullName}.{dupMethod.Name} have the same value for FunctionNameAttribute. Each function must have a unique name.");
                    return(false);
                }
                else
                {
                    this._functionNamesSet.Add(functionName, method);
                }

                const string settingsFileName = "local.settings.json";
                var          settingsFile     = Path.Combine(_outputPath, settingsFileName);

                if (!File.Exists(settingsFile))
                {
                    return(true); // no file to check.
                }

                var settings = JsonConvert.DeserializeObject <LocalSettingsJson>(File.ReadAllText(settingsFile));
                var values   = settings?.Values;

                if (values != null)
                {
                    // FirstOrDefault returns a KeyValuePair<string, string> which is a struct so it can't be null.
                    var azureWebJobsStorage       = values.FirstOrDefault(pair => pair.Key.Equals("AzureWebJobsStorage", StringComparison.OrdinalIgnoreCase)).Value;
                    var allWithoutStorageTriggers = functionJson
                                                    .Bindings
                                                    .Where(b => b["type"] != null)
                                                    .Select(b => b["type"].ToString())
                                                    .Where(b => b.IndexOf("Trigger", StringComparison.OrdinalIgnoreCase) != -1)
                                                    .All(t => _triggersWithoutStorage.Any(tws => tws.Equals(t, StringComparison.OrdinalIgnoreCase)));

                    if (string.IsNullOrWhiteSpace(azureWebJobsStorage) && !allWithoutStorageTriggers)
                    {
                        _logger.LogWarning($"Function [{functionName}]: Missing value for AzureWebJobsStorage in {settingsFileName}. " +
                                           $"This is required for all triggers other than {string.Join(", ", _triggersWithoutStorage)}.");
                    }

                    foreach (var binding in functionJson.Bindings)
                    {
                        foreach (var token in binding)
                        {
                            if (token.Key == "connection" || token.Key == "apiKey" || token.Key == "accountSid" || token.Key == "authToken")
                            {
                                var appSettingName = token.Value.ToString();
                                if (!values.Any(v => v.Key.Equals(appSettingName, StringComparison.OrdinalIgnoreCase)))
                                {
                                    _logger.LogWarning($"Function [{functionName}]: cannot find value named '{appSettingName}' in {settingsFileName} that matches '{token.Key}' property set on '{binding["type"]?.ToString()}'");
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                _logger.LogWarningFromException(e);
            }
            // We only return false on an error, not a warning.
            return(true);
        }