public async Task GetWithAcceptDateTime()
        {
            var connectionString = Environment.GetEnvironmentVariable("AZ_CONFIG_CONNECTION");

            Assert.NotNull(connectionString, "Set AZ_CONFIG_CONNECTION environment variable to the connection string");
            var service = new ConfigurationClient(connectionString);

            try
            {
                await service.SetAsync(s_testSetting);

                // Test
                ConfigurationSetting responseSetting = await service.GetAsync(s_testSetting.Key, s_testSetting.Label, DateTimeOffset.MaxValue);

                Assert.AreEqual(s_testSetting, responseSetting);
            }
            finally
            {
                await service.DeleteAsync(s_testSetting.Key, s_testSetting.Label);
            }
        }
        public async Task GetBatchSettingKeyLabel()
        {
            ConfigurationClient  service     = TestEnvironment.GetClient();
            ConfigurationSetting testSetting = CreateSetting();

            try
            {
                await service.SetAsync(testSetting);

                var          selector = new SettingSelector(testSetting.Key, testSetting.Label);
                SettingBatch batch    = await service.GetBatchAsync(selector, CancellationToken.None);

                Assert.AreEqual(1, batch.Count);
                Assert.AreEqual(testSetting.Key, batch[0].Key);
                Assert.AreEqual(testSetting.Label, batch[0].Label);
            }
            finally
            {
                await service.DeleteAsync(testSetting.Key, testSetting.Label);
            }
        }
Exemple #3
0
        public async Task UpdateKeyValue()
        {
            string connectionString = GetEnvironment();
            var    service          = new ConfigurationClient(connectionString);

            string key = string.Concat("key-", Guid.NewGuid().ToString("N"));
            await service.SetAsync(key, "my_value");

            try
            {
                string value = "my_value2";
                ConfigurationSetting responseSetting = await service.UpdateAsync(key, value);

                Assert.AreEqual(key, responseSetting.Key);
                Assert.AreEqual(value, responseSetting.Value);
            }
            finally
            {
                await service.DeleteAsync(key);
            }
        }
Exemple #4
0
        public async Task AddExistingSetting()
        {
            string connectionString = GetEnvironment();
            var    service          = new ConfigurationClient(connectionString);

            try
            {
                await service.AddAsync(s_testSetting);

                var exception = Assert.ThrowsAsync <RequestFailedException>(async() =>
                {
                    await service.AddAsync(s_testSetting);
                });

                Assert.AreEqual(412, exception.Status);
            }
            finally
            {
                await service.DeleteAsync(s_testSetting.Key, s_testSetting.Label);
            }
        }
        public async Task UpdateSettingIfETag()
        {
            ConfigurationClient  service     = GetClient();
            ConfigurationSetting testSetting = CreateSetting();

            await service.SetAsync(testSetting);

            ConfigurationSetting responseGet = await service.GetAsync(testSetting.Key, testSetting.Label);

            try
            {
                responseGet.Value = "test_value_diff";
                ConfigurationSetting responseSetting = await service.UpdateAsync(responseGet, CancellationToken.None);

                Assert.AreNotEqual(responseGet, responseSetting);
            }
            finally
            {
                await service.DeleteAsync(responseGet.Key, responseGet.Label);
            }
        }
        public async Task GetBatchSettingOnlyKey()
        {
            ConfigurationClient  service     = GetClient();
            ConfigurationSetting testSetting = CreateSetting();

            try
            {
                await service.SetAsync(testSetting);

                var selector = new SettingSelector(testSetting.Key);
                Response <ConfigurationSetting>[] batch = (await service.GetSettingsAsync(selector, CancellationToken.None).ToEnumerableAsync())
                                                          .ToArray();

                Assert.AreEqual(1, batch.Length);
                Assert.AreEqual(testSetting.Key, batch[0].Value.Key);
            }
            finally
            {
                await service.DeleteAsync(testSetting.Key, testSetting.Label);
            }
        }
Exemple #7
0
        public async Task SetKeyValueLabel()
        {
            ConfigurationClient service = GetClient();

            string key   = GenerateKeyId("key-");
            string value = "my_value";
            string label = "my_label";

            try
            {
                ConfigurationSetting setting = await service.SetAsync(key, value, label);

                Assert.AreEqual(key, setting.Key);
                Assert.AreEqual(value, setting.Value);
                Assert.AreEqual(label, setting.Label);
            }
            finally
            {
                await service.DeleteAsync(key, label);
            }
        }
        public async Task AddExistingSetting()
        {
            ConfigurationClient  service     = GetClient();
            ConfigurationSetting testSetting = CreateSetting();

            try
            {
                await service.AddAsync(testSetting);

                var exception = Assert.ThrowsAsync <RequestFailedException>(async() =>
                {
                    await service.AddAsync(testSetting);
                });

                Assert.AreEqual(412, exception.Status);
            }
            finally
            {
                await service.DeleteAsync(testSetting.Key, testSetting.Label);
            }
        }
Exemple #9
0
        public async Task GetBatchSettingKeyLabel()
        {
            string connectionString = GetEnvironment();
            var    service          = new ConfigurationClient(connectionString);

            try
            {
                await service.SetAsync(s_testSetting);

                var          selector = new SettingSelector(s_testSetting.Key, s_testSetting.Label);
                SettingBatch batch    = await service.GetBatchAsync(selector, CancellationToken.None);

                Assert.AreEqual(batch.Count, 1);
                Assert.AreEqual(batch[0].Key, s_testSetting.Key);
                Assert.AreEqual(batch[0].Label, s_testSetting.Label);
            }
            finally
            {
                await service.DeleteAsync(s_testSetting.Key, s_testSetting.Label);
            }
        }
Exemple #10
0
        public async Task UpdateSettingIfETag()
        {
            string connectionString = GetEnvironment();
            var    service          = new ConfigurationClient(connectionString);

            await service.SetAsync(s_testSetting);

            ConfigurationSetting responseGet = await service.GetAsync(s_testSetting.Key, s_testSetting.Label);

            try
            {
                responseGet.Value = "test_value_diff";
                ConfigurationSetting responseSetting = await service.UpdateAsync(responseGet, CancellationToken.None);

                Assert.AreNotEqual(responseGet, responseSetting);
            }
            finally
            {
                await service.DeleteAsync(responseGet.Key, responseGet.Label);
            }
        }
Exemple #11
0
        public async Task DeleteSettingWithETag()
        {
            ConfigurationClient  service     = GetClient();
            ConfigurationSetting testSetting = CreateSetting();

            // Prepare environment
            await service.SetAsync(testSetting);

            ConfigurationSetting setting = await service.GetAsync(testSetting.Key, testSetting.Label);

            // Test
            await service.DeleteAsync(setting.Key, setting.Label, setting.ETag, CancellationToken.None);

            //Try to get the non-existing setting
            RequestFailedException e = Assert.ThrowsAsync <RequestFailedException>(async() =>
            {
                await service.GetAsync(testSetting.Key, testSetting.Label);
            });

            Assert.AreEqual(404, e.Status);
        }
        public async Task UpdateTags()
        {
            var connectionString = Environment.GetEnvironmentVariable("AZ_CONFIG_CONNECTION");

            Assert.NotNull(connectionString, "Set AZ_CONFIG_CONNECTION environment variable to the connection string");
            var service = new ConfigurationClient(connectionString);

            await service.SetAsync(s_testSetting);

            ConfigurationSetting responseGet = await service.GetAsync(s_testSetting.Key, s_testSetting.Label);

            try
            {
                // Different tags
                var testSettingDiff = responseGet.Clone();
                var settingTags     = testSettingDiff.Tags;
                if (settingTags.ContainsKey("tag1"))
                {
                    settingTags["tag1"] = "value-updated";
                }
                settingTags.Add("tag3", "test_value3");
                testSettingDiff.Tags = settingTags;

                ConfigurationSetting responseSetting = await service.UpdateAsync(testSettingDiff, CancellationToken.None);

                Assert.AreEqual(testSettingDiff, responseSetting);

                // No tags
                var testSettingNoTags = responseGet.Clone();
                testSettingNoTags.Tags = null;

                responseSetting = await service.UpdateAsync(testSettingNoTags, CancellationToken.None);

                Assert.AreEqual(testSettingNoTags, responseSetting);
            }
            finally
            {
                await service.DeleteAsync(s_testSetting.Key, s_testSetting.Label);
            }
        }
        public async Task AddKeyValue()
        {
            var connectionString = Environment.GetEnvironmentVariable("AZ_CONFIG_CONNECTION");

            Assert.NotNull(connectionString, "Set AZ_CONFIG_CONNECTION environment variable to the connection string");
            var service = new ConfigurationClient(connectionString);

            string key = string.Concat("key-", Guid.NewGuid().ToString("N"));

            try
            {
                string value = "my_value";
                ConfigurationSetting setting = await service.AddAsync(key, value);

                Assert.AreEqual(key, setting.Key);
                Assert.AreEqual(value, setting.Value);
            }
            finally
            {
                await service.DeleteAsync(key);
            }
        }
Exemple #14
0
        // ConfigurationClient logs lots of useful information automatically to .NET's EventSource.
        // This sample illustrate how to control and access the log information.
        //[Test]
        public async Task Logging()
        {
            // Retrieve the connection string from the configuration store.
            // You can get the string from your Azure portal.
            var connectionString = Environment.GetEnvironmentVariable("AZ_CONFIG_CONNECTION");
            var client           = new ConfigurationClient(connectionString);

            // Setup a listener to monitor logged events.
            var listener = new ConsoleEventListener();

            listener.EnableEvents(EventLevel.LogAlways);

            Response <ConfigurationSetting> setResponse = await client.SetAsync(new ConfigurationSetting("some_key", "some_value"));

            if (setResponse.Status != 200)
            {
                throw new Exception("could not set configuration setting");
            }

            // Delete the setting when you don't need it anymore.
            await client.DeleteAsync("some_key");
        }
Exemple #15
0
        public async Task AddKeyValueLabel()
        {
            string connectionString = GetEnvironment();
            var    service          = new ConfigurationClient(connectionString);

            string key   = string.Concat("key-", Guid.NewGuid().ToString("N"));
            string value = "my_value";
            string label = "my_label";

            try
            {
                ConfigurationSetting setting = await service.AddAsync(key, value, label);

                Assert.AreEqual(key, setting.Key);
                Assert.AreEqual(value, setting.Value);
                Assert.AreEqual(label, setting.Label);
            }
            finally
            {
                await service.DeleteAsync(key, label);
            }
        }
Exemple #16
0
        public async Task HasChangedValuesDontMatch()
        {
            ConfigurationClient  service  = GetClient();
            ConfigurationSetting setting0 = CreateSetting();

            try
            {
                ConfigurationSetting setting1 = await service.SetAsync(setting0);

                setting0.Value = "test_value2";
                ConfigurationSetting setting2 = await service.SetAsync(setting0);

                // Test
                bool hasChanged = await service.HasChangedAsync(setting1);

                Assert.IsTrue(hasChanged);
            }
            finally
            {
                await service.DeleteAsync(setting0.Key, setting0.Label);
            }
        }
Exemple #17
0
        public async Task UpdateSettingTags()
        {
            ConfigurationClient  service     = GetClient();
            ConfigurationSetting testSetting = CreateSetting();

            await service.SetAsync(testSetting);

            ConfigurationSetting responseGet = await service.GetAsync(testSetting.Key, testSetting.Label);


            try
            {
                // Different tags
                var testSettingDiff = responseGet.Clone();
                var settingTags     = testSettingDiff.Tags;
                if (settingTags.ContainsKey("tag1"))
                {
                    settingTags["tag1"] = "value-updated";
                }
                settingTags.Add("tag3", "test_value3");
                testSettingDiff.Tags = settingTags;

                ConfigurationSetting responseSetting = await service.UpdateAsync(testSettingDiff, CancellationToken.None);

                Assert.AreEqual(testSettingDiff, responseSetting);

                // No tags
                var testSettingNoTags = responseGet.Clone();
                testSettingNoTags.Tags = null;

                responseSetting = await service.UpdateAsync(testSettingNoTags, CancellationToken.None);

                Assert.AreEqual(testSettingNoTags, responseSetting);
            }
            finally
            {
                await service.DeleteAsync(testSetting.Key, testSetting.Label);
            }
        }
Exemple #18
0
        public async Task HelloWorld()
        {
            // Retrieve the connection string from the configuration store.
            // You can get the string from your Azure portal.
            var connectionString = Environment.GetEnvironmentVariable("AZ_CONFIG_CONNECTION");

            // Instantiate a client that will be used to call the service.
            var client = new ConfigurationClient(connectionString);

            // Create a setting to be stored by the configuration service.
            var setting = new ConfigurationSetting();

            setting.Key   = "some_key";   // the key is a free form string, i.e. up to you what it is.
            setting.Value = "some_value"; // same with the value.

            // SetAsyc adds a new setting to the store or overrides an existing setting.
            // Alternativelly you can call AddAsync which only succeeds if the setting does not already exist in the store.
            // Or you can call UpdateAsync to update a setting that is already present in the store.
            Response <ConfigurationSetting> setResponse = await client.SetAsync(setting);

            if (setResponse.Status != 200)
            {
                throw new Exception("could not set configuration setting");
            }

            // Retrieve a previously stored setting by calling GetAsync.
            Response <ConfigurationSetting> getResponse = await client.GetAsync("some_key");

            if (getResponse.Status != 200)
            {
                throw new Exception("could not set configuration setting");
            }
            ConfigurationSetting retrieved = getResponse.Result;
            string retrievedValue          = retrieved.Value;

            // Delete the setting when you don't need it anymore.
            Response <ConfigurationSetting> deleteResponse = await client.DeleteAsync("some_key");
        }
Exemple #19
0
        public async Task ConfiguringRetries()
        {
            // specify retry policy options
            var options = new PipelineOptions();

            options.RetryPolicy = RetryPolicy.CreateFixed(
                maxRetries: 10,
                delay: TimeSpan.FromSeconds(1),
                retriableCodes: new int[] {
                500,     // Internal Server Error
                504      // Gateway Timeout
            }
                );

            var connectionString = Environment.GetEnvironmentVariable("AZ_CONFIG_CONNECTION");

            // pass the policy options to the client
            var client = new ConfigurationClient(connectionString, options);

            await client.SetAsync(new ConfigurationSetting("some_key", "some_value"));

            await client.DeleteAsync("some_key");
        }
        public async Task GetIfChangedSettingModified()
        {
            ConfigurationClient  service     = GetClient();
            ConfigurationSetting testSetting = CreateSetting();

            try
            {
                ConfigurationSetting setting = await service.AddAsync(testSetting);

                ConfigurationSetting modifiedSetting = setting.Clone();
                modifiedSetting.Value = "new_value";
                modifiedSetting       = await service.SetAsync(modifiedSetting);

                Response <ConfigurationSetting> response = await service.GetAsync(setting, onlyIfChanged : true).ConfigureAwait(false);

                Assert.AreEqual(200, response.GetRawResponse().Status);
                Assert.True(ConfigurationSettingEqualityComparer.Instance.Equals(modifiedSetting, response.Value));
            }
            finally
            {
                await service.DeleteAsync(testSetting.Key, testSetting.Label);
            }
        }
        public async Task SetIfUnchangedSettingModified()
        {
            ConfigurationClient  service     = GetClient();
            ConfigurationSetting testSetting = CreateSetting();

            try
            {
                ConfigurationSetting setting = await service.AddAsync(testSetting);

                ConfigurationSetting modifiedSetting = setting.Clone();
                modifiedSetting.Value = "new_value";
                modifiedSetting       = await service.SetAsync(modifiedSetting);

                // Test
                RequestFailedException exception = Assert.ThrowsAsync <RequestFailedException>(async() =>
                                                                                               await service.SetAsync(setting, onlyIfUnchanged: true));
                Assert.AreEqual(412, exception.Status);
            }
            finally
            {
                await service.DeleteAsync(testSetting.Key, testSetting.Label);
            }
        }
        public async Task SetIfUnchangedSettingUnmodified()
        {
            ConfigurationClient  service     = GetClient();
            ConfigurationSetting testSetting = CreateSetting();

            try
            {
                ConfigurationSetting setting = await service.AddAsync(testSetting);

                setting.Value = "new_value";

                // Test
                Response <ConfigurationSetting> response = await service.SetAsync(setting, onlyIfUnchanged : true);

                Assert.AreEqual(200, response.GetRawResponse().Status);
                Assert.AreEqual(setting.Value, response.Value.Value);
                Assert.AreNotEqual(setting.ETag, response.Value.ETag);
            }
            finally
            {
                await service.DeleteAsync(testSetting.Key, testSetting.Label);
            }
        }
        public async Task GetBatchSettingSpecialCharacters()
        {
            ConfigurationClient  service     = GetClient();
            ConfigurationSetting testSetting = CreateSettingSpecialCharacters();

            try
            {
                await service.SetAsync(testSetting);

                var selector = new SettingSelector(testSetting.Key);

                ConfigurationSetting[] settings = (await service.GetSettingsAsync(selector, CancellationToken.None).ToEnumerableAsync()).ToArray();

                // There should be at least one key available
                CollectionAssert.IsNotEmpty(settings);
                Assert.AreEqual(testSetting.Key, settings[0].Key);
                Assert.AreEqual(testSetting.Label, settings[0].Label);
            }
            finally
            {
                await service.DeleteAsync(testSetting.Key);
            }
        }
Exemple #24
0
        public async Task GetSetting()
        {
            ConfigurationClient service = TestEnvironment.GetClient();

            // Prepare environment
            var testSettingNoLabel = s_testSetting.Clone();

            testSettingNoLabel.Label = null;

            try
            {
                await service.SetAsync(testSettingNoLabel);

                // Test
                ConfigurationSetting setting = await service.GetAsync(testSettingNoLabel.Key);

                Assert.AreEqual(testSettingNoLabel, setting);
            }
            finally
            {
                await service.DeleteAsync(testSettingNoLabel.Key);
            }
        }
        public async Task DeleteWithETag()
        {
            var connectionString = Environment.GetEnvironmentVariable("AZ_CONFIG_CONNECTION");

            Assert.NotNull(connectionString, "Set AZ_CONFIG_CONNECTION environment variable to the connection string");
            var service = new ConfigurationClient(connectionString);

            // Prepare environment
            await service.SetAsync(s_testSetting);

            ConfigurationSetting setting = await service.GetAsync(s_testSetting.Key, s_testSetting.Label);

            // Test
            await service.DeleteAsync(setting.Key, setting.Label, setting.ETag, CancellationToken.None);

            //Try to get the non-existing setting
            var e = Assert.ThrowsAsync <RequestFailedException>(async() =>
            {
                await service.GetAsync(s_testSetting.Key, s_testSetting.Label);
            });

            Assert.AreEqual(404, e.Status);
        }
        public async Task AddExistingSetting()
        {
            var connectionString = Environment.GetEnvironmentVariable("AZ_CONFIG_CONNECTION");

            Assert.NotNull(connectionString, "Set AZ_CONFIG_CONNECTION environment variable to the connection string");
            var service = new ConfigurationClient(connectionString);

            try
            {
                await service.AddAsync(s_testSetting);

                var exception = Assert.ThrowsAsync <RequestFailedException>(async() =>
                {
                    await service.AddAsync(s_testSetting);
                });

                Assert.AreEqual(412, exception.Status);
            }
            finally
            {
                await service.DeleteAsync(s_testSetting.Key, s_testSetting.Label);
            }
        }
        public async Task UpdateIfETag()
        {
            var connectionString = Environment.GetEnvironmentVariable("AZ_CONFIG_CONNECTION");

            Assert.NotNull(connectionString, "Set AZ_CONFIG_CONNECTION environment variable to the connection string");
            var service = new ConfigurationClient(connectionString);

            await service.SetAsync(s_testSetting);

            ConfigurationSetting responseGet = await service.GetAsync(s_testSetting.Key, s_testSetting.Label);

            try
            {
                responseGet.Value = "test_value_diff";
                ConfigurationSetting responseSetting = await service.UpdateAsync(responseGet, CancellationToken.None);

                Assert.AreNotEqual(responseGet, responseSetting);
            }
            finally
            {
                await service.DeleteAsync(responseGet.Key, responseGet.Label);
            }
        }
Exemple #28
0
        public async Task ConfiguringPipeline()
        {
            // this instance will hold pipeline creation options
            var options = new ConfigurationClientOptions();

            // specify custon HttpClient
            options.Transport = new HttpClientTransport(s_client);

            // remove logging policy
            options.LoggingPolicy = null;

            // specify custom retry policy options
            options.RetryPolicy = new FixedRetryPolicy()
            {
                MaxRetries     = 10,
                Delay          = TimeSpan.FromSeconds(1),
                RetriableCodes = new [] {
                    500, // Internal Server Error
                    504  // Gateway Timeout
                }
            };

            // add a policy (custom behavior) that executes once per client call
            options.PerCallPolicies.Add(new AddHeaderPolicy());

            // add a policy that executes once per retry
            options.PerRetryPolicies.Add(new CustomLogPolicy());

            var connectionString = Environment.GetEnvironmentVariable("AZ_CONFIG_CONNECTION");
            // pass the policy options to the client
            var client = new ConfigurationClient(connectionString, options);

            await client.SetAsync(new ConfigurationSetting("some_key", "some_value"));

            await client.DeleteAsync("some_key");
        }
        public async Task GetSettingSpecialCharacters()
        {
            ConfigurationClient  service     = GetClient();
            ConfigurationSetting testSetting = CreateSettingSpecialCharacters();

            // Prepare environment
            ConfigurationSetting testSettingNoLabel = testSetting.Clone();

            testSettingNoLabel.Label = null;

            try
            {
                await service.SetAsync(testSettingNoLabel);

                // Test
                ConfigurationSetting setting = await service.GetAsync(testSettingNoLabel.Key);

                Assert.True(ConfigurationSettingEqualityComparer.Instance.Equals(testSettingNoLabel, setting));
            }
            finally
            {
                await service.DeleteAsync(testSettingNoLabel.Key);
            }
        }
        public async Task DeleteSettingReadOnly()
        {
            ConfigurationClient  service     = GetClient();
            ConfigurationSetting testSetting = CreateSetting();

            try
            {
                var setting = await service.AddAsync(testSetting);

                var readOnly = await service.SetReadOnlyAsync(testSetting.Key, testSetting.Label);

                // Test
                RequestFailedException exception = Assert.ThrowsAsync <RequestFailedException>(async() =>
                                                                                               await service.DeleteAsync(testSetting.Key, testSetting.Label)
                                                                                               );
                Assert.AreEqual(409, exception.Status);
            }
            finally
            {
                await service.ClearReadOnlyAsync(testSetting.Key, testSetting.Label);

                await service.DeleteAsync(testSetting.Key, testSetting.Label);
            }
        }