-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[app-configuration] Adding in sample code and readme (#5091)
* Adding in sample code for AppConfiguration * Remove test:watch command until I get it working reliably * * Updating samples and adding one that demonstrates etags * Removing the browser examples since we're not yet supporting browsers. * Updating readme and expanding out the summary * Fix the sample code so it works * Remove the wrapper * Fixed sample code and also added in link to additional samples * Updated readme with appropriate text * * Flattening out samples so they aren't referencing any separate files * Updating readme with links to the samples and instructions on how to build and test it for contributions * Removing troubleshooting section until we add in logging * * Ignore missing sections in readme until we get troubleshooting steps * Add in a section for examples * Update path * Update .docsettings.yml to the right path * Update to a simpler function for the example code * * Added back in the connection string test * Fixed an issue where I wasn't awaiting on the result of the clean, causing the samples to be flaky when they ran
- Loading branch information
1 parent
3133eb2
commit 386cc43
Showing
10 changed files
with
295 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
sdk/appconfiguration/app-configuration/samples/helloworld.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// NOTE: replace with import { AppConfigurationClient } from "@azure/app-configuration" | ||
// in a standalone project | ||
import { AppConfigurationClient } from "../src" | ||
|
||
export async function run() { | ||
console.log("Running helloworld sample"); | ||
|
||
// You will need to set this environment variable | ||
const connectionString = process.env["AZ_CONFIG_CONNECTION"]!; | ||
const client = new AppConfigurationClient(connectionString); | ||
|
||
const greetingKey = "Samples:Greeting"; | ||
|
||
await cleanupSampleValues([greetingKey], client); | ||
|
||
// creating a new setting | ||
console.log(`Adding in new setting ${greetingKey}`); | ||
await client.addConfigurationSetting(greetingKey, { value: "Hello!" }); | ||
|
||
const newSetting = await client.getConfigurationSetting(greetingKey); | ||
console.log(`${greetingKey} has been set to ${newSetting.value}`); | ||
|
||
// changing the value of a setting | ||
await client.setConfigurationSetting(greetingKey, { value: "Goodbye!" }); | ||
|
||
const updatedSetting = await client.getConfigurationSetting(greetingKey); | ||
console.log(`${greetingKey} has been set to ${updatedSetting.value}`); | ||
|
||
// removing the setting | ||
await client.deleteConfigurationSetting(greetingKey, {}); | ||
console.log(`${greetingKey} has been deleted`); | ||
|
||
await cleanupSampleValues([greetingKey], client); | ||
} | ||
|
||
async function cleanupSampleValues(keys: string[], client: AppConfigurationClient) { | ||
const existingSettings = await client.listConfigurationSettings({ | ||
key: keys | ||
}); | ||
|
||
for (const setting of existingSettings) { | ||
await client.deleteConfigurationSetting(setting.key!, { label: setting.label }); | ||
} | ||
} | ||
|
||
// If you want to run this sample from a console | ||
// uncomment these lines so run() will get called | ||
// run().catch(err => { | ||
// console.log(`ERROR: ${err}`); | ||
// }); |
88 changes: 88 additions & 0 deletions
88
sdk/appconfiguration/app-configuration/samples/helloworldWithETag.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// NOTE: replace with import { AppConfigurationClient } from "@azure/app-configuration" | ||
// in a standalone project | ||
import { AppConfigurationClient } from "../src" | ||
|
||
export async function run() { | ||
console.log("Running helloworld sample using etags"); | ||
|
||
// You will need to set this environment variable | ||
const connectionString = process.env["AZ_CONFIG_CONNECTION"]!; | ||
const client = new AppConfigurationClient(connectionString); | ||
|
||
const greetingKey = "Samples:Greeting"; | ||
|
||
await cleanupSampleValues([greetingKey], client); | ||
|
||
// create a new setting as Client Alpha | ||
console.log(`Client Alpha: adding in new setting ${greetingKey}`); | ||
let initialSettingFromClientA = await client.addConfigurationSetting(greetingKey, { value: "Created for Client Alpha" }); | ||
console.log(`Client Alpha: ${greetingKey} has been set to '${initialSettingFromClientA.value}' with etag of ${initialSettingFromClientA.etag}`); | ||
|
||
// Each setting, when added, will come back with an etag (https://en.wikipedia.org/wiki/HTTP_ETag) | ||
// This allows you to update a value but only if it hasn't changed from the last time you read it. | ||
// | ||
// Let's simulate two processes attempting to update the value | ||
|
||
// if you don't specify an etag the update is unconditional | ||
let updateFromClientBeta = await client.setConfigurationSetting(greetingKey, { | ||
value: "Update from Client Beta" | ||
}); | ||
|
||
console.log(`Client Beta: updated the value of ${greetingKey} without specifying an etag (unconditional update).`); | ||
console.log(` Client Beta's etag is ${updateFromClientBeta.etag}`); | ||
console.log(` Client Alpha's etag from the initial creation is ${initialSettingFromClientA.etag}`); | ||
|
||
// at this point we've got this sequence of events | ||
// | ||
// 1. Client Alpha created the setting (and stored off its etag) | ||
// 2. Client Beta updated the setting, ignoring the etag | ||
|
||
// Now Client Alpha wants to update the value _but_ Client Alpha will pay attention to the | ||
// etag and only update the value if the value currently stored is the same as when we | ||
// initially updated the setting. | ||
// | ||
// This allows us to prevent unintentional overwrites and allows you to implement | ||
// optimistic concurrency (https://en.wikipedia.org/wiki/Optimistic_concurrency_control) | ||
// within your application. | ||
|
||
console.log("Client Alpha: attempting update that doesn't include an etag"); | ||
await client.setConfigurationSetting(greetingKey, { | ||
value: "Update from Client Alpha that should only get set if the value has not changed from the last time we loaded it", | ||
etag: initialSettingFromClientA.etag | ||
}).catch(err => { | ||
console.log(" Update failed - etag didn't match"); | ||
}); | ||
|
||
// if we want to update then we need to retrieve the new setting and determine if our update makes sense | ||
let actualStoredSetting = await client.getConfigurationSetting(greetingKey); | ||
|
||
console.log("Client Alpha: getting current value and merging/updating based on it") | ||
// now we can figure out if we want to merge our value, overwrite with our value, etc... | ||
// in this case we'll just update the value to what we want (again, specifying the etag to | ||
// prevent unintended overwrite) | ||
await client.updateConfigurationSetting(greetingKey, { | ||
value: "Theoretical update from Client Alpha that takes Client Beta's changes into account", | ||
etag: actualStoredSetting.etag | ||
}); | ||
|
||
let currentSetting = await client.getConfigurationSetting(greetingKey); | ||
console.log(`The value is now updated to '${currentSetting.value}'`); | ||
|
||
await cleanupSampleValues([greetingKey], client); | ||
} | ||
|
||
async function cleanupSampleValues(keys: string[], client: AppConfigurationClient) { | ||
const existingSettings = await client.listConfigurationSettings({ | ||
key: keys | ||
}); | ||
|
||
for (const setting of existingSettings) { | ||
await client.deleteConfigurationSetting(setting.key!, { label: setting.label }); | ||
} | ||
} | ||
|
||
// If you want to run this sample from a console | ||
// uncomment these lines so run() will get called | ||
// run().catch(err => { | ||
// console.log("ERROR", err); | ||
// }); |
45 changes: 45 additions & 0 deletions
45
sdk/appconfiguration/app-configuration/samples/helloworldWithLabels.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// NOTE: replace with import { AppConfigurationClient } from "@azure/app-configuration" | ||
// in a standalone project | ||
import { AppConfigurationClient } from "../src" | ||
|
||
export async function run() { | ||
console.log("Running helloworldWithLabels sample"); | ||
|
||
// You will need to set this environment variable | ||
const connectionString = process.env["AZ_CONFIG_CONNECTION"]!; | ||
const client = new AppConfigurationClient(connectionString); | ||
|
||
const urlKey = "Samples:Endpoint:Url"; | ||
|
||
await cleanupSampleValues([urlKey], client); | ||
|
||
// labels allow you to use the same key with different values for separate environments | ||
// or clients | ||
console.log("Adding in endpoint with two labels - beta and production"); | ||
await client.addConfigurationSetting(urlKey, { label: "beta", value: "https://beta.example.com" }); | ||
await client.addConfigurationSetting(urlKey, { label: "production", value: "https://example.com" }); | ||
|
||
const betaEndpoint = await client.getConfigurationSetting(urlKey, { label: "beta" }); | ||
console.log(`Endpoint with beta label: ${betaEndpoint.value}`); | ||
|
||
const productionEndpoint = await client.getConfigurationSetting(urlKey, { label: "production" }); | ||
console.log(`Endpoint with production label: ${productionEndpoint.value}`); | ||
|
||
await cleanupSampleValues([urlKey], client); | ||
} | ||
|
||
async function cleanupSampleValues(keys: string[], client: AppConfigurationClient) { | ||
const existingSettings = await client.listConfigurationSettings({ | ||
key: keys | ||
}); | ||
|
||
for (const setting of existingSettings) { | ||
await client.deleteConfigurationSetting(setting.key!, { label: setting.label }); | ||
} | ||
} | ||
|
||
// If you want to run this sample from a console | ||
// uncomment these lines so run() will get called | ||
// run().catch(err => { | ||
// console.log(`ERROR: ${err}`); | ||
// }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import * as helloworld from "./helloworld"; | ||
import * as helloworldWithLabels from "./helloworldWithLabels"; | ||
|
||
export async function runAll() { | ||
await helloworld.run(); | ||
await helloworldWithLabels.run(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.