/// <summary>
        /// Checks which namespace the service is running in to determine the
        /// correct connection string to use
        /// </summary>
        /// <returns>The connection string for that namespaces state</returns>
        private string DetermineCorrectConnectionString(EnvironmentVariablePayload payload)
        {
            // Check the NAMESPACE env
            string currentNamespace = Environment.GetEnvironmentVariable("NAMESPACE");

            // Check the Namespace env
            if (String.IsNullOrEmpty(currentNamespace))
            {
                currentNamespace = Environment.GetEnvironmentVariable("Namespace");
            }

            // Check the namespace env
            if (String.IsNullOrEmpty(currentNamespace))
            {
                currentNamespace = Environment.GetEnvironmentVariable("namespace");
            }

            if (String.IsNullOrEmpty(currentNamespace))
            {
                throw new Exception("Could not find current namespace. Please inject namespace into running deployment");
            }

            if (currentNamespace.ToLower().Contains("prod"))
            {
                return(payload.Production);
            }
            else if (currentNamespace.ToLower().Contains("staging"))
            {
                return(payload.Staging);
            }
            else
            {
                return(payload.Development);
            }
        }
        public EnvironmentVariablePayload GetEnvironmentVariableFromKeyVault(string option)
        {
            EnvironmentVariablePayload payload = new EnvironmentVariablePayload();

            switch (option)
            {
            case "Database":
            {
                payload.Development = File.ReadAllText("/kvmnt/database-dev");
                payload.Staging     = File.ReadAllText("/kvmnt/database-staging");
                payload.Production  = File.ReadAllText("/kvmnt/database-prod");
                break;
            }

            case "BlobStorage":
            {
                payload.Development = File.ReadAllText("/kvmnt/blobstorage-dev");
                payload.Staging     = File.ReadAllText("/kvmnt/blobstorage-staging");
                payload.Production  = File.ReadAllText("/kvmnt/blobstorage-prod");
                break;
            }

            default:
            {
                throw new Exception("Unknown error has occured");
            }
            }

            return(payload);
        }
        /// <summary>
        /// Calls the Secrets Service to grab the specified connection strings
        /// </summary>
        /// <returns></returns>
        public async Task <string> GetConnectionStringForClient()
        {
            string connectionString;

            HttpClient client = this.factory.CreateClient();
            // Make the API call to the Secrets Service
            HttpRequestMessage request = new HttpRequestMessage();

            request.RequestUri = this.secretServiceUri;
            request.Method     = HttpMethod.Get;
            HttpResponseMessage response = await client.SendAsync(request);

            string content = await response.Content.ReadAsStringAsync();

            EnvironmentVariablePayload payload = JsonConvert.DeserializeObject <EnvironmentVariablePayload>(content);

            // Determine which connection string to use
            connectionString = this.DetermineCorrectConnectionString(payload);

            return(connectionString);
        }
        public async void ShouldProvideUsersWithTheCorrectEnvironmentVariableWhenRunningInANamespace()
        {
            List <String> namespaceVariations = new List <String>();

            namespaceVariations.Add("NAMESPACE");
            namespaceVariations.Add("Namespace");
            namespaceVariations.Add("namespace");

            List <String> environments = new List <String>();

            environments.Add("developement");
            environments.Add("staging");
            environments.Add("production");

            IHttpClientFactory httpClientFactoryMock = Substitute.For <IHttpClientFactory>();

            EnvironmentVariablePayload payload = new EnvironmentVariablePayload()
            {
                Development = "Development",
                Staging     = "Staging",
                Production  = "Production",
            };

            HttpResponseMessage fakeResponseMessage = new HttpResponseMessage();

            fakeResponseMessage.StatusCode = HttpStatusCode.OK;
            fakeResponseMessage.Content    = new StringContent(JsonConvert.SerializeObject(payload), Encoding.UTF8, "application/json");

            FakeHttpMessageHandler fakeHttpMessageHandler = new FakeHttpMessageHandler(fakeResponseMessage);

            HttpClient fakeHttpClient = new HttpClient(fakeHttpMessageHandler);

            httpClientFactoryMock.CreateClient().Returns(fakeHttpClient);

            // No need to test the Storage Factory since they are built the same
            DatabaseClientFactory databaseClientFactory = new DatabaseClientFactory(httpClientFactoryMock);

            // Check if we get back the right variable with development env

            string dbConnectionString;

            foreach (String namespaces in namespaceVariations)
            {
                foreach (String environment in environments)
                {
                    Environment.SetEnvironmentVariable(namespaces, environment);
                    dbConnectionString = await databaseClientFactory.GetConnectionStringForClient();

                    switch (environment)
                    {
                    case ("development"):
                        Assert.Equal(dbConnectionString, payload.Development);
                        break;

                    case ("staging"):
                        Assert.Equal(dbConnectionString, payload.Staging);
                        break;

                    case ("production"):
                        Assert.Equal(dbConnectionString, payload.Production);
                        break;
                    }

                    Environment.SetEnvironmentVariable(namespaces, null);
                }
            }
        }