/// <summary> /// Create a Consul client using the provided configuration options /// </summary> /// <param name="options">the configuration options</param> /// <returns>a Consul client</returns> public static IConsulClient CreateClient(ConsulOptions options) { if (options == null) { throw new ArgumentNullException(nameof(options)); } var client = new ConsulClient(s => { s.Address = new Uri($"{options.Scheme}://{options.Host}:{options.Port}"); s.Token = options.Token; s.Datacenter = options.Datacenter; if (!string.IsNullOrEmpty(options.WaitTime)) { s.WaitTime = DateTimeConversions.ToTimeSpan(options.WaitTime); } if (!string.IsNullOrEmpty(options.Password) || !string.IsNullOrEmpty(options.Username)) { #pragma warning disable CS0618 // Type or member is obsolete s.HttpAuth = new NetworkCredential(options.Username, options.Password); #pragma warning restore CS0618 // Type or member is obsolete } }); return(client); }
/// <summary> /// At PostConfigure, confirm that settings are valid for the current environment /// </summary> /// <param name="options">ConsulOptions to evaluate</param> public static void ValidateConsulOptions(ConsulOptions options) { if ((Platform.IsContainerized || Platform.IsCloudHosted) && options.Host.Equals("localhost")) { throw new InvalidOperationException($"Consul URL {options.Scheme}://{options.Host}:{options.Port} is not valid in containerized or cloud environments. Please configure Consul:Host with a non-localhost server."); } }