public async Task GetSettingWithLabel() { string connectionString = GetEnvironment(); var service = new ConfigurationClient(connectionString); // Prepare environment var testSettingNoLabel = s_testSetting.Clone(); testSettingNoLabel.Label = null; try { await service.SetAsync(testSettingNoLabel); await service.SetAsync(s_testSetting); // Test ConfigurationSetting responseSetting = await service.GetAsync(s_testSetting.Key, s_testSetting.Label); Assert.AreEqual(s_testSetting, responseSetting); } finally { await service.DeleteAsync(s_testSetting.Key, s_testSetting.Label); await service.DeleteAsync(testSettingNoLabel.Key); } }
private async Task <string> SetMultipleKeys(ConfigurationClient service, int expectedEvents) { string key = GenerateKeyId("key-"); /* * The configuration store contains a KV with the Key * that represents {expectedEvents} data points. * If not set, create the {expectedEvents} data points and the "BatchKey" */ const string batchKey = "BatchKey"; try { var responseGet = await service.GetAsync(batchKey); key = responseGet.Value.Value; responseGet.Dispose(); } catch { for (int i = 0; i < expectedEvents; i++) { await service.AddAsync(new ConfigurationSetting(key, "test_value", $"{i.ToString()}")); } await service.SetAsync(new ConfigurationSetting(batchKey, key)); } return(key); }
public async Task HelloWorld() { // Retrieve the connection string from the configuration store. // You can get the string from your Azure portal or using Azure CLI. 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 Configuration Setting to be stored in the Configuration Store. var setting = new ConfigurationSetting("some_key", "some_value"); // There are two ways to store a Configuration Setting: // -AddAsync creates a setting only if the setting does not already exist in the store. // -SetAsync creates a setting if it doesn't exist or overrides an existing setting await client.SetAsync(setting); // Retrieve a previously stored Configuration Setting by calling GetAsync. ConfigurationSetting gotSetting = await client.GetAsync("some_key"); Debug.WriteLine(gotSetting.Value); // Delete the Configuration Setting from the Configuration Store when you don't need it anymore. await client.DeleteAsync("some_key"); }
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("some_key", "some_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. await client.SetAsync(setting); // Retrieve a previously stored setting by calling GetAsync. ConfigurationSetting gotSetting = await client.GetAsync("some_key"); Debug.WriteLine(gotSetting.Value); // Delete the setting when you don't need it anymore. await client.DeleteAsync("some_key"); }
public async Task GetSettingWithLabel() { ConfigurationClient service = GetClient(); ConfigurationSetting testSetting = CreateSetting(); // Prepare environment var testSettingNoLabel = testSetting.Clone(); testSettingNoLabel.Label = null; try { await service.SetAsync(testSettingNoLabel); await service.SetAsync(testSetting); // Test ConfigurationSetting responseSetting = await service.GetAsync(testSetting.Key, testSetting.Label); Assert.AreEqual(testSetting, responseSetting); } finally { await service.DeleteAsync(testSetting.Key, testSetting.Label); await service.DeleteAsync(testSettingNoLabel.Key); } }
public async Task GetWithLabel() { 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 var testSettingNoLabel = s_testSetting.Clone(); testSettingNoLabel.Label = null; try { await service.SetAsync(testSettingNoLabel); await service.SetAsync(s_testSetting); // Test ConfigurationSetting responseSetting = await service.GetAsync(s_testSetting.Key, s_testSetting.Label); Assert.AreEqual(s_testSetting, responseSetting); } finally { await service.DeleteAsync(s_testSetting.Key, s_testSetting.Label); await service.DeleteAsync(testSettingNoLabel.Key); } }
public async Task GetIfChangedModified() { var requestSetting = s_testSetting.Clone(); requestSetting.ETag = new ETag("v1"); var responseSetting = s_testSetting.Clone(); responseSetting.ETag = new ETag("v2"); var mockResponse = new MockResponse(200); mockResponse.SetContent(SerializationHelpers.Serialize(responseSetting, SerializeSetting)); var mockTransport = new MockTransport(mockResponse); ConfigurationClient service = CreateTestService(mockTransport); Response <ConfigurationSetting> response = await service.GetAsync(requestSetting, onlyIfChanged : true); // TODO: Should this be response.Status? Assert.AreEqual(200, response.GetRawResponse().Status); ConfigurationSetting setting = new ConfigurationSetting(); Assert.DoesNotThrow(() => { setting = response; }); MockRequest request = mockTransport.SingleRequest; AssertRequestCommon(request); Assert.AreEqual(RequestMethod.Get, request.Method); Assert.AreEqual($"https://contoso.appconfig.io/kv/test_key?label=test_label&api-version={s_version}", request.Uri.ToString()); Assert.True(request.Headers.TryGetValue("If-None-Match", out var ifNoneMatch)); Assert.AreEqual("\"v1\"", ifNoneMatch); Assert.AreEqual(responseSetting, setting); }
public async Task DeleteSetting() { ConfigurationClient service = GetClient(); ConfigurationSetting testSetting = CreateSetting(); try { // Prepare environment var testSettingDiff = testSetting.Clone(); testSettingDiff.Label = null; await service.SetAsync(testSetting); await service.SetAsync(testSettingDiff); // Test await service.DeleteAsync(testSettingDiff.Key); //Try to get the non-existing setting var e = Assert.ThrowsAsync <RequestFailedException>(async() => { await service.GetAsync(testSettingDiff.Key); }); Assert.AreEqual(404, e.Status); } finally { await service.DeleteAsync(testSetting.Key, testSetting.Label); } }
public async Task Delete() { 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 { // Prepare environment var testSettingDiff = s_testSetting.Clone(); testSettingDiff.Label = "test_label_diff"; await service.SetAsync(s_testSetting); await service.SetAsync(testSettingDiff); // Test await service.DeleteAsync(testSettingDiff.Key, testSettingDiff.Label); //Try to get the non-existing setting var e = Assert.ThrowsAsync <RequestFailedException>(async() => { await service.GetAsync(testSettingDiff.Key, testSettingDiff.Label); }); Assert.AreEqual(404, e.Status); } finally { await service.DeleteAsync(s_testSetting.Key, s_testSetting.Label); } }
public async Task GetIfChangedUnmodified() { var requestSetting = s_testSetting.Clone(); requestSetting.ETag = new ETag("v1"); var mockTransport = new MockTransport(new MockResponse(304)); ConfigurationClient service = CreateTestService(mockTransport); Response <ConfigurationSetting> response = await service.GetAsync(requestSetting, onlyIfChanged : true); MockRequest request = mockTransport.SingleRequest; AssertRequestCommon(request); Assert.AreEqual(RequestMethod.Get, request.Method); Assert.AreEqual($"https://contoso.appconfig.io/kv/test_key?label=test_label&api-version={s_version}", request.Uri.ToString()); Assert.True(request.Headers.TryGetValue("If-None-Match", out var ifNoneMatch)); Assert.AreEqual("\"v1\"", ifNoneMatch); Assert.AreEqual(304, response.GetRawResponse().Status); bool throws = false; try { ConfigurationSetting setting = response.Value; } catch { throws = true; } Assert.True(throws); }
public async Task GetIfChangedSettingUnmodified() { ConfigurationClient service = GetClient(); ConfigurationSetting testSetting = CreateSetting(); try { ConfigurationSetting setting = await service.AddAsync(testSetting); // Test Response <ConfigurationSetting> response = await service.GetAsync(setting, onlyIfChanged : true).ConfigureAwait(false); Assert.AreEqual(304, response.GetRawResponse().Status); bool throws = false; try { ConfigurationSetting value = response.Value; } catch { throws = true; } Assert.IsTrue(throws); } finally { await service.DeleteAsync(testSetting.Key, testSetting.Label); } }
public async Task DeleteSettingWithLabel() { string connectionString = GetEnvironment(); var service = new ConfigurationClient(connectionString); try { // Prepare environment var testSettingDiff = s_testSetting.Clone(); testSettingDiff.Label = "test_label_diff"; await service.SetAsync(s_testSetting); await service.SetAsync(testSettingDiff); // Test await service.DeleteAsync(testSettingDiff.Key, testSettingDiff.Label); //Try to get the non-existing setting var e = Assert.ThrowsAsync <RequestFailedException>(async() => { await service.GetAsync(testSettingDiff.Key, testSettingDiff.Label); }); Assert.AreEqual(404, e.Status); } finally { await service.DeleteAsync(s_testSetting.Key, s_testSetting.Label); } }
/* * Sample demonstrates how to use Azure App Configuration to save information about two(2) environments * "beta" and "production". * To do this, we will create Configuration Settings with the same key, * but different labels: one for "beta" and one for "production". */ public async Task HelloWorldExtended() { // Retrieve the connection string from the configuration store. // You can get the string from your Azure portal or using Azure CLI. var connectionString = Environment.GetEnvironmentVariable("AZ_CONFIG_CONNECTION"); // Instantiate a client that will be used to call the service. var client = new ConfigurationClient(connectionString); // Create the Configuration Settings to be stored in the Configuration Store. var betaEndpoint = new ConfigurationSetting("endpoint", "https://beta.endpoint.com", "beta"); var betaInstances = new ConfigurationSetting("instances", "1", "beta"); var productionEndpoint = new ConfigurationSetting("endpoint", "https://production.endpoint.com", "production"); var productionInstances = new ConfigurationSetting("instances", "1", "production"); // There are two(2) ways to store a Configuration Setting: // -AddAsync creates a setting only if the setting does not already exist in the store. // -SetAsync creates a setting if it doesn't exist or overrides an existing setting await client.AddAsync(betaEndpoint); await client.AddAsync(betaInstances); await client.AddAsync(productionEndpoint); await client.AddAsync(productionInstances); // There is a need to increase the production instances from 1 to 5. // The UpdateSync will help us with this. ConfigurationSetting instancesToUpdate = await client.GetAsync(productionInstances.Key, productionInstances.Label); instancesToUpdate.Value = "5"; await client.UpdateAsync(instancesToUpdate); // We want to gather all the information available for the "production' environment. // By calling GetBatchSync with the proper filter for label "production", we will get // all the Configuration Settings that satisfy that condition. var selector = new SettingSelector(null, "production"); SettingBatch batch = await client.GetBatchAsync(selector); Debug.WriteLine("Settings for Production environmnet"); for (int i = 0; i < batch.Count; i++) { var value = batch[i]; Debug.WriteLine(batch[i]); } // Once we don't need the Configuration Setting, we can delete them. await client.DeleteAsync(betaEndpoint.Key, betaEndpoint.Label); await client.DeleteAsync(betaInstances.Key, betaInstances.Label); await client.DeleteAsync(productionEndpoint.Key, productionEndpoint.Label); await client.DeleteAsync(productionInstances.Key, productionInstances.Label); }
public void GetSettingNotFound() { ConfigurationClient service = TestEnvironment.GetClient(); var exception = Assert.ThrowsAsync <RequestFailedException>(async() => { await service.GetAsync(s_testSetting.Key); }); Assert.AreEqual(404, exception.Status); }
public async Task DeleteSettingWithETag() { ConfigurationClient service = TestEnvironment.GetClient(); // 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 void GetSettingNotFound() { ConfigurationClient service = GetClient(); ConfigurationSetting testSetting = CreateSetting(); RequestFailedException exception = Assert.ThrowsAsync <RequestFailedException>(async() => { await service.GetAsync(testSetting.Key); }); Assert.AreEqual(404, exception.Status); }
public void GetNotFound() { var response = new MockResponse(404); var mockTransport = new MockTransport(response); ConfigurationClient service = CreateTestService(mockTransport); RequestFailedException exception = Assert.ThrowsAsync <RequestFailedException>(async() => { await service.GetAsync(key: s_testSetting.Key); }); Assert.AreEqual(404, exception.Status); }
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 void GetNotFound() { 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); var exception = Assert.ThrowsAsync <RequestFailedException>(async() => { await service.GetAsync(s_testSetting.Key); }); Assert.AreEqual(404, exception.Status); }
public async Task MockClient() { // Create a mock response var mockResponse = new Mock <Response>(); // Create a client mock var mock = new Mock <ConfigurationClient>(); // Setup client method mock.Setup(c => c.GetAsync("Key", It.IsAny <string>(), It.IsAny <DateTimeOffset>(), It.IsAny <CancellationToken>())) .ReturnsAsync(new Response <ConfigurationSetting>(mockResponse.Object, ConfigurationClientModelFactory.ConfigurationSetting("Key", "Value"))); // Use the client mock ConfigurationClient client = mock.Object; Response <ConfigurationSetting> setting = await client.GetAsync("Key"); Assert.AreEqual("Value", setting.Value.Value); }
public async Task GetWithLabel() { var response = new MockResponse(200); response.SetContent(SerializationHelpers.Serialize(s_testSetting, SerializeSetting)); var mockTransport = new MockTransport(response); ConfigurationClient service = CreateTestService(mockTransport); ConfigurationSetting setting = await service.GetAsync(s_testSetting.Key, s_testSetting.Label); MockRequest request = mockTransport.SingleRequest; AssertRequestCommon(request); Assert.AreEqual(RequestMethod.Get, request.Method); Assert.AreEqual($"https://contoso.appconfig.io/kv/test_key?label=test_label&api-version={s_version}", request.Uri.ToString()); Assert.AreEqual(s_testSetting, setting); }
public async Task Get() { var response = new MockResponse(200); response.SetContent(SerializationHelpers.Serialize(s_testSetting, SerializeSetting)); var mockTransport = new MockTransport(response); ConfigurationClient service = CreateTestService(mockTransport); ConfigurationSetting setting = await service.GetAsync(s_testSetting.Key); MockRequest request = mockTransport.SingleRequest; AssertRequestCommon(request); Assert.AreEqual(HttpPipelineMethod.Get, request.Method); Assert.AreEqual("https://contoso.azconfig.io/kv/test_key", request.UriBuilder.ToString()); Assert.AreEqual(s_testSetting, setting); }
public async Task GetWithAcceptDateTime() { ConfigurationClient service = TestEnvironment.GetClient(); 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 GetSettingSpecialCharactersWithLabel() { ConfigurationClient service = GetClient(); ConfigurationSetting testSetting = CreateSettingSpecialCharacters(); try { await service.SetAsync(testSetting); // Test ConfigurationSetting setting = await service.GetAsync(testSetting.Key, testSetting.Label); Assert.True(ConfigurationSettingEqualityComparer.Instance.Equals(testSetting, setting)); } finally { await service.DeleteAsync(testSetting.Key); } }
public async Task GetWithAcceptDateTime() { ConfigurationClient service = GetClient(); ConfigurationSetting testSetting = CreateSetting(); try { await service.SetAsync(testSetting); // Test // TODO: add a test with a more granular timestamp. ConfigurationSetting responseSetting = await service.GetAsync(testSetting.Key, testSetting.Label, DateTimeOffset.MaxValue, requestOptions : default); Assert.True(ConfigurationSettingEqualityComparer.Instance.Equals(testSetting, responseSetting)); } finally { await service.DeleteAsync(testSetting.Key, testSetting.Label); } }
public async Task GetWithRequestOptions() { // Get If-Match is not an expected use case, but enabled for completeness. var testSetting = s_testSetting.Clone(); testSetting.ETag = new ETag("v1"); var mockResponse = new MockResponse(200); mockResponse.SetContent(SerializationHelpers.Serialize(testSetting, SerializeSetting)); var mockTransport = new MockTransport(mockResponse); ConfigurationClient service = CreateTestService(mockTransport); var requestOptions = new MatchConditions { IfMatch = new ETag("v1") }; ConfigurationSetting setting = await service.GetAsync(testSetting.Key, testSetting.Label, default, requestOptions);
public async Task ConfiguringTheClient() { var response = new MockResponse(200); response.SetContent(SerializationHelpers.Serialize(s_testSetting, SerializeSetting)); var mockTransport = new MockTransport(new MockResponse(503), response); var options = new ConfigurationClientOptions(); options.Diagnostics.ApplicationId = "test_application"; options.Transport = mockTransport; ConfigurationClient client = CreateClient <ConfigurationClient>(s_connectionString, options); ConfigurationSetting setting = await client.GetAsync(s_testSetting.Key); Assert.AreEqual(s_testSetting, setting); Assert.AreEqual(2, mockTransport.Requests.Count); }
public async Task UpdateSettingIfETag() { ConfigurationClient service = TestEnvironment.GetClient(); 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); } }
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 GetWithAcceptDateTime() { var mockResponse = new MockResponse(200); mockResponse.SetContent(SerializationHelpers.Serialize(s_testSetting, SerializeSetting)); var mockTransport = new MockTransport(mockResponse); ConfigurationClient service = CreateTestService(mockTransport); Response <ConfigurationSetting> response = await service.GetAsync(s_testSetting.Key, s_testSetting.Label, DateTimeOffset.MaxValue, requestOptions : default); MockRequest request = mockTransport.SingleRequest; AssertRequestCommon(request); Assert.AreEqual(RequestMethod.Get, request.Method); Assert.AreEqual($"https://contoso.appconfig.io/kv/test_key?label=test_label&api-version={s_version}", request.Uri.ToString()); Assert.True(request.Headers.TryGetValue("Accept-Datetime", out var acceptDateTime)); Assert.AreEqual(DateTimeOffset.MaxValue.UtcDateTime.ToString("R", CultureInfo.InvariantCulture), acceptDateTime); Assert.False(request.Headers.TryGetValue("If-Match", out var ifMatch)); Assert.False(request.Headers.TryGetValue("If-None-Match", out var ifNoneMatch)); }