/// <summary> /// Creates the Client based on the config and reads the health check settings from S3 /// </summary> /// <param name="config">The client configuration options</param> /// <returns>A new AreWeUpClient with the specified configuration</returns> public static async Task <AreWeUpClient> CreateClient(AreWeUpClientConfig config) { return(new AreWeUpClient(config) { HealthChecks = await ReadConfig(config) }); }
/// <summary> /// Initializes the client with the specified config /// </summary> /// <param name="config">The client configuration options</param> public AreWeUpClient(AreWeUpClientConfig config) { this.Configuration = config; this.InitializeHttpClients(); ReadConfig(this.Configuration).Wait(); }
/// <summary> /// Default constructor that Lambda will invoke. This sets up the AreWeUpClient. /// </summary> public LambdaEntryPoint() { string Bucket = Environment.GetEnvironmentVariable("ConfigBucket"); string Key = Environment.GetEnvironmentVariable("ConfigKey"); AreWeUpClientConfig ClientConfig = new AreWeUpClientConfig(Bucket, Key) { SNSTopicArn = Environment.GetEnvironmentVariable("SNSTopic"), DefaultCustomerId = Environment.GetEnvironmentVariable("CustomerId") }; if (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("Subject"))) { ClientConfig.DefaultSubject = Environment.GetEnvironmentVariable("Subject"); } else { ClientConfig.DefaultSubject = String.Empty; } // If this can't be parsed, it doesn't exist and the AreWeUpClientConfig // will use its own default value for the http timeout if (Int32.TryParse(Environment.GetEnvironmentVariable("HttpTimeout"), out int HttpTimeout)) { // Make sure the user didn't specify 0 to mean // the app should use its own default value if (HttpTimeout > 0) { ClientConfig.HttpRequestTimeout = HttpTimeout; } } //If we can't parse the variable, or it's not present, defaults to false if (Boolean.TryParse(Environment.GetEnvironmentVariable("IgnoreSslCertificateErrors"), out bool TempBool)) { ClientConfig.DefaultIgnoreSslErrors = TempBool; } if (Boolean.TryParse(Environment.GetEnvironmentVariable("SendToCW"), out TempBool)) { ClientConfig.DefaultSendToCloudWatch = TempBool; } if (Boolean.TryParse(Environment.GetEnvironmentVariable("ForceRefresh"), out TempBool)) { ClientConfig.ForceRefresh = TempBool; } if (Int32.TryParse(Environment.GetEnvironmentVariable("DefaultTimeout"), out int Timeout)) { // Make sure the user didn't specify 0 to mean // the app should use its own default value if (Timeout > 0) { ClientConfig.DefaultTimeout = Timeout; } } //Doing this here means we can load the S3 config once on initialization and only //reread it if the environment variable for force refresh is set this.SetupClient(ClientConfig).Wait(); }
/// <summary> /// Sets up the AreWeUpClient and also loads the health check config into the client /// </summary> /// <param name="config">The config to use with the client</param> /// <returns></returns> private async Task SetupClient(AreWeUpClientConfig config) { this._Client = await AreWeUpClient.CreateClient(config); }
/// <summary> /// Reads the config file from S3 /// </summary> /// <returns>The health check request configuration</returns> private static async Task <HealthCheckConfiguration> ReadConfig(AreWeUpClientConfig defaultConfig) { try { //Stream the content of the S3 object using (Stream Result = await _XFerUtil.OpenStreamAsync(new TransferUtilityOpenStreamRequest() { BucketName = defaultConfig.S3Bucket, Key = defaultConfig.S3Key })) { //Read the stream using (StreamReader Reader = new StreamReader(Result)) { JObject JO = JObject.Parse(await Reader.ReadToEndAsync()); JObject NewConfig = new JObject(); // Iterates the keys like Http, Https, Tcp, Udp, Icmp foreach (KeyValuePair <string, JToken> Item in JO) { JArray ItemArray = new JArray(); // Iterates each config in that category foreach (JObject Config in ((JArray)JO[Item.Key]).Children <JObject>()) { // Add in the default values if they weren't defined. JToken CId = Config.GetValue("CustomerId", StringComparison.OrdinalIgnoreCase); if (CId == null) { if (!String.IsNullOrEmpty(defaultConfig.DefaultCustomerId)) { Config.Add("CustomerId", defaultConfig.DefaultCustomerId); } } JToken CW = Config.GetValue("SendToCloudWatch", StringComparison.OrdinalIgnoreCase); if (CW == null) { Config.Add("SendToCloudWatch", defaultConfig.DefaultSendToCloudWatch); } if (Item.Key == "Https") { JToken SSL = Config.GetValue("IgnoreSslErrors", StringComparison.OrdinalIgnoreCase); if (SSL == null) { Config.Add("IgnoreSslErrors", defaultConfig.DefaultIgnoreSslErrors); } } if (Item.Key.Equals("Tcp", StringComparison.OrdinalIgnoreCase) || Item.Key.Equals("Udp", StringComparison.OrdinalIgnoreCase) || Item.Key.Equals("Icmp", StringComparison.OrdinalIgnoreCase)) { JToken Timeout = Config.GetValue("Timeout", StringComparison.OrdinalIgnoreCase); if (Timeout == null) { Config.Add("Timeout", defaultConfig.DefaultTimeout); } } else { // Otherwise this is a web request, which will all use the // same request timeout value Config.Add("Timeout", defaultConfig.HttpRequestTimeout); } JToken SNS = Config.GetValue("SNSTopicARN", StringComparison.OrdinalIgnoreCase); if (SNS == null) { Config.Add("SNSTopicArn", defaultConfig.SNSTopicArn); } JToken Subject = Config.GetValue("Subject", StringComparison.OrdinalIgnoreCase); if (Subject == null) { if (!String.IsNullOrEmpty(defaultConfig.DefaultSubject)) { Config.Add("Subject", defaultConfig.DefaultSubject); } } ItemArray.Add(Config); } NewConfig.Add(Item.Key, ItemArray); } JsonSerializer Serializer = new JsonSerializer(); // Be able to convert the string method value to an HttpMethod class object Serializer.Converters.Add(new HttpMethodConverter()); Serializer.Converters.Add(new KeyValueConverter()); // Be able to set properties that have a private setter Serializer.ContractResolver = new PrivateSetterResolver(); return(NewConfig.ToObject <HealthCheckConfiguration>(Serializer)); } } } catch (Exception e) { Console.WriteLine($"Error processing the S3 object { defaultConfig.S3Bucket}\\{ defaultConfig.S3Key}:\r\n{SerializeException(e)}"); return(null); } }
/// <summary> /// Initializes the client with the specified config and the ILambdaContext that will be used for logging /// </summary> /// <param name="config">The client configuration options</param> /// <param name="context">The ILambdaContext that will be used for logging</param> public AreWeUpClient(AreWeUpClientConfig config, ILambdaContext context) : this(config) { this._LambdaContext = context; }