Exemplo n.º 1
0
        public async Task CreateSecretReference()
        {
            var connectionString = TestEnvironment.ConnectionString;
            var client           = new ConfigurationClient(connectionString);

            #region Snippet:Sample_CreateSecretReference
            var secretId = "https://keyvault_name.vault.azure.net/secrets/<secret_name>";
#if !SNIPPET
            secretId = TestEnvironment.SecretId;
#endif
            var secretReferenceSetting = new SecretReferenceConfigurationSetting("setting", new Uri(secretId));
            #endregion

            #region Snippet:Sample_SetSecretReference
            client.SetConfigurationSetting(secretReferenceSetting);
            #endregion

            #region Snippet:Sample_GetSecretReference
            Response <ConfigurationSetting> response = client.GetConfigurationSetting("setting");
            if (response.Value is SecretReferenceConfigurationSetting secretReference)
            {
                var identifier   = new KeyVaultSecretIdentifier(secretReference.SecretId);
                var secretClient = new SecretClient(identifier.VaultUri, new DefaultAzureCredential());
                var secret       = await secretClient.GetSecretAsync(identifier.Name, identifier.Version);

                Console.WriteLine($"Setting {secretReference.Key} references {secretReference.SecretId} Secret Value: {secret.Value.Value}");
            }
            #endregion

            #region Snippet:Sample_DeleteSecretReference
            client.DeleteConfigurationSetting(secretReferenceSetting);
            #endregion
        }
Exemplo n.º 2
0
        public void UpdateSettingIfUnchanged()
        {
            var connectionString = Environment.GetEnvironmentVariable("APPCONFIGURATION_CONNECTION_STRING");

            #region Snippet:AzConfigSample6_CreateConfigurationClient
            var client = new ConfigurationClient(connectionString);
            #endregion

            #region Snippet:AzConfigSample6_SetInitialVMs
            client.SetConfigurationSetting("available_vms", "10");
            #endregion

            #region Snippet:AzConfigSample6_CallReleaseVMs
            int releasedVMs = ReleaseVMs(vmsToRelease: 2);
            #endregion

            #region Snippet:AzConfigSample6_CallUpdateAvailableVms
            var availableVms = UpdateAvailableVms(client, releasedVMs);
            Console.WriteLine($"Available VMs after update: {availableVms}");
            #endregion

            #region Snippet:AzConfigSample6_DeleteConfigurationSetting
            client.DeleteConfigurationSetting("available_vms");
            #endregion
        }
Exemplo n.º 3
0
        public void CreateFeatureFlag()
        {
            var connectionString = TestEnvironment.ConnectionString;
            var client           = new ConfigurationClient(connectionString);

            #region Snippet:Sample_CreateFeatureFlag
            var featureFlagSetting = new FeatureFlagConfigurationSetting("feature_id", isEnabled: true);
            #endregion

            #region Snippet:Sample_SetFeatureFlag
            client.SetConfigurationSetting(featureFlagSetting);
            #endregion

            #region Snippet:Sample_GetFeatureFlag
            Response <ConfigurationSetting> response = client.GetConfigurationSetting(FeatureFlagConfigurationSetting.KeyPrefix + "feature_id");
            if (response.Value is FeatureFlagConfigurationSetting featureFlag)
            {
                Console.WriteLine($"Feature flag {featureFlag.FeatureId} IsEnabled: {featureFlag.IsEnabled}");
            }
            #endregion

            #region Snippet:Sample_DeleteFeatureFlag
            client.DeleteConfigurationSetting(featureFlagSetting);
            #endregion
        }
        public void HelloWorld()
        {
            var connectionString = Environment.GetEnvironmentVariable("APPCONFIGURATION_CONNECTION_STRING");

            #region Snippet:AzConfigSample1_CreateConfigurationClient
            var client = new ConfigurationClient(connectionString);
            #endregion

            #region Snippet:AzConfigSample1_CreateConfigurationSetting
            var setting = new ConfigurationSetting("some_key", "some_value");
            #endregion

            #region Snippet:AzConfigSample1_SetConfigurationSetting
            client.SetConfigurationSetting(setting);
            #endregion

            #region Snippet:AzConfigSample1_RetrieveConfigurationSetting
            ConfigurationSetting retrievedSetting = client.GetConfigurationSetting("some_key");
            Console.WriteLine($"The value of the configuration setting is: {retrievedSetting.Value}");
            #endregion

            #region Snippet:AzConfigSample1_DeleteConfigurationSetting
            client.DeleteConfigurationSetting("some_key");
            #endregion
        }
Exemplo n.º 5
0
        /*
         * This sample ilustrates the simple scenario of adding a setting to a
         * configuration store, retrieving it from the configuration store, and
         * finally, deleting it from the configuration store.
         */
        public void HelloWorld()
        {
            // Retrieve the connection string from the environment.
            // The connection string is available from the App Configuration Access Keys view in the Azure Portal.
            var connectionString = Environment.GetEnvironmentVariable("APPCONFIGURATION_CONNECTION_STRING");

            // 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 create a Configuration Setting:
            //   - AddConfigurationSetting creates a setting only if the setting does not already exist in the store.
            //   - SetConfigurationSetting creates a setting if it doesn't exist or overrides an existing setting with the same key and label.
            client.SetConfigurationSetting(setting);

            // Retrieve a previously stored Configuration Setting by calling GetConfigurationSetting.
            ConfigurationSetting retrievedSetting = client.GetConfigurationSetting("some_key");

            Debug.WriteLine($"The value of the configuration setting is: {retrievedSetting.Value}");

            // Delete the Configuration Setting from the Configuration Store.
            client.DeleteConfigurationSetting("some_key");
        }
        public void HelloWorld()
        {
            // Retrieve the connection string from the configuration store.
            // You can get the string from your Azure portal.
            var connectionString = Environment.GetEnvironmentVariable("APPCONFIGURATION_CONNECTION_STRING");

            // 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
            client.SetConfigurationSetting(setting);

            // Retrieve a previously stored Configuration Setting by calling GetAsync.
            ConfigurationSetting gotSetting = client.GetConfigurationSetting("some_key");

            Debug.WriteLine(gotSetting.Value);

            // Delete the Configuration Setting from the Configuration Store when you don't need it anymore.
            client.DeleteConfigurationSetting("some_key");
        }
Exemplo n.º 7
0
        public void DeleteSetting()
        {
            var connectionString = Environment.GetEnvironmentVariable("APPCONFIGURATION_CONNECTION_STRING");

            #region Snippet:DeleteConfigurationSetting
            //@@ string connectionString = "<connection_string>";
            var client = new ConfigurationClient(connectionString);
            client.DeleteConfigurationSetting("some_key");
            #endregion Snippet:DeleteConfigurationSetting
        }
Exemplo n.º 8
0
        public void DeleteSetting()
        {
            var connectionString = TestEnvironment.ConnectionString;

            #region Snippet:DeleteConfigurationSetting
            //@@ string connectionString = "<connection_string>";
            var client = new ConfigurationClient(connectionString);
            client.DeleteConfigurationSetting("some_key");
            #endregion Snippet:DeleteConfigurationSetting
        }
Exemplo n.º 9
0
        /*
         * This sample ilustrates how to update a setting in the configuration
         * store only when the store holds same version it did when you last
         * retrieved it from the configuration store, as determined by whether
         * the client and service setting ETags match.  This ensures our client
         * will not overwrite updates applied to the setting from other sources.
         */
        public void UpdateSettingIfUnchanged()
        {
            // Retrieve the connection string from the environment.
            // The connection string is available from the App Configuration Access Keys view in the Azure Portal.
            var connectionString = Environment.GetEnvironmentVariable("APPCONFIGURATION_CONNECTION_STRING");

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

            // Create a Configuration Setting for this sample.
            ConfigurationSetting setting = new ConfigurationSetting("available_vms", "10");

            // Add the setting to the Configuration Store.
            setting = client.SetConfigurationSetting(setting);

            // Here we invoke code that releases VMs from our application's pool of resources.
            // After this completes, we will update the total number of available VMs
            int releasedVMs = ReleaseVMs(vmsToRelease: 2);

            // Modify the setting before updating it in the Configuration Store.
            int availableVMCount = int.Parse(setting.Value);

            setting.Value = (availableVMCount + releasedVMs).ToString();

            // Update the value only if it hasn't been updated by another client,
            // so that updates from other sources don't get overwritten.
            ConfigurationSetting updatedSetting = null;
            bool hasChanged;

            do
            {
                try
                {
                    updatedSetting = client.SetConfigurationSetting(setting, onlyIfUnchanged: true);
                    hasChanged     = false;
                }
                catch (RequestFailedException e) when(e.Status == 412)
                {
                    hasChanged = true;

                    // The setting has been modified since the last time our client retrieved it from the service.
                    // Get the latest value and re-apply our update logic before attempting to set it again on the service.
                    setting = client.GetConfigurationSetting(setting);

                    availableVMCount = int.Parse(setting.Value);
                    setting.Value    = (availableVMCount + releasedVMs).ToString();
                }
            }while (hasChanged);

            Debug.WriteLine($"Setting after update was applied: {updatedSetting}");

            // Delete the Configuration Setting from the Configuration Store.
            client.DeleteConfigurationSetting("available_vms");
        }
Exemplo n.º 10
0
        public void SetClearReadOnly()
        {
            var connectionString = Environment.GetEnvironmentVariable("APPCONFIGURATION_CONNECTION_STRING");

            #region Snippet:AzConfigSample3_CreateConfigurationClient
            var client = new ConfigurationClient(connectionString);
            #endregion

            #region Snippet:AzConfigSample3_SetConfigurationSetting
            var setting = new ConfigurationSetting("some_key", "some_value");
            client.SetConfigurationSetting(setting);
            #endregion

            #region Snippet:AzConfigSample3_SetReadOnly
            client.SetReadOnly(setting.Key, true);
            #endregion

            #region Snippet:AzConfigSample3_SetConfigurationSettingReadOnly
            setting.Value = "new_value";

            try
            {
                client.SetConfigurationSetting(setting);
            }
            catch (RequestFailedException e)
            {
                Console.WriteLine(e.Message);
            }
            #endregion

            // Make the setting read write again.
            #region Snippet:AzConfigSample3_SetReadWrite
            client.SetReadOnly(setting.Key, false);
            #endregion

            #region Snippet:AzConfigSample3_SetConfigurationSettingReadWrite
            client.SetConfigurationSetting(setting);
            #endregion

            #region Snippet:AzConfigSample3_DeleteConfigurationSetting
            client.DeleteConfigurationSetting("some_key");
            #endregion
        }
        /*
         * This sample demonstrates how to use Azure App Configuration to make
         * a configuration value read only and then set it back to read-write.
         * This corresponds to the lock and unlock operations in the Azure portal.
         */
        public void SetClearReadOnly()
        {
            // Retrieve the connection string from the environment.
            // The connection string is available from the App Configuration Access Keys view in the Azure Portal.
            var connectionString = Environment.GetEnvironmentVariable("APPCONFIGURATION_CONNECTION_STRING");

            // 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
            // to illustrate the set and clear read only scenario.
            var setting = new ConfigurationSetting("some_key", "some_value");

            // Add the setting to the Configuration Store.
            client.SetConfigurationSetting(setting);

            // Make the setting read only.
            client.SetReadOnly(setting.Key);

            // Modify the value to attempt to update it.
            setting.Value = "new_value";

            try
            {
                // SetConfigurationSetting should throw an exception because
                // the setting is read only and cannot be updated.
                client.SetConfigurationSetting(setting);
            }
            catch (RequestFailedException e)
            {
                Console.WriteLine(e.Message);
            }

            // Make the setting read write again.
            client.ClearReadOnly(setting.Key);

            // Try to update to the new value again.
            // SetConfigurationSetting should now succeed because the setting is read write.
            client.SetConfigurationSetting(setting);

            // Delete the Configuration Setting from the Configuration Store.
            client.DeleteConfigurationSetting("some_key");
        }
Exemplo n.º 12
0
        /*
         * This sample ilustrates how to get a setting from the configuration
         * store only if the version in the configuration store is different
         * from the one held by your client application, as determined by whether
         * the setting ETags match.  Getting a setting only if it has changed
         * allows you to avoid downloading a setting if your client application
         * is already holding the latest value, which saves cost and bandwidth.
         */
        public void GetSettingIfChanged()
        {
            // Retrieve the connection string from the environment.
            // The connection string is available from the App Configuration Access Keys view in the Azure Portal.
            var connectionString = Environment.GetEnvironmentVariable("APPCONFIGURATION_CONNECTION_STRING");

            // 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.
            ConfigurationSetting setting = new ConfigurationSetting("some_key", "initial_value");

            // Add the setting to the Configuration Store.
            ConfigurationSetting settingV1 = client.SetConfigurationSetting(setting);

            // Download the setting only if it's changed.
            Response <ConfigurationSetting> response = client.GetConfigurationSetting(settingV1, onlyIfChanged: true);

            // This ConfigurationSetting object will hold the most up-to-date version of the setting.
            ConfigurationSetting latestSetting = null;

            // Check to see whether the setting was changed in the configuration store before accessing its value.
            // If response.Value is accessed when no value was returned, an exception will be thrown.
            Debug.WriteLine($"Received a response code of {response.GetRawResponse().Status}");
            if (response.GetRawResponse().Status == 304)
            {
                // We already have the Nothing to change in local version.
                latestSetting = settingV1;
            }
            else if (response.GetRawResponse().Status == 200)
            {
                // The configuration store held a newer version of this setting.
                // Update the client's version.
                latestSetting = response.Value;
            }

            Debug.WriteLine($"Latest version of setting is {latestSetting}.");

            // Delete the Configuration Setting from the Configuration Store when you don't need it anymore.
            client.DeleteConfigurationSetting("some_key");
        }
Exemplo n.º 13
0
        public void GetSettingIfChanged()
        {
            string connectionString = Environment.GetEnvironmentVariable("APPCONFIGURATION_CONNECTION_STRING");

            #region Snippet:AzConfigSample5_CreateConfigurationClient
            ConfigurationClient client = new ConfigurationClient(connectionString);
            #endregion

            #region Snippet:AzConfigSample5_SetConfigurationSetting
            ConfigurationSetting setting = client.SetConfigurationSetting("some_key", "initial_value");
            Console.WriteLine($"setting.ETag is '{setting.ETag}'");
            #endregion

            #region Snippet:AzConfigSample5_GetLatestConfigurationSetting
            ConfigurationSetting latestSetting = GetConfigurationSettingIfChanged(client, setting);
            Console.WriteLine($"Latest version of setting is {latestSetting}.");
            #endregion

            #region Snippet:AzConfigSample5_DeleteConfigurationSetting
            client.DeleteConfigurationSetting("some_key");
            #endregion
        }