-
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.
Adding in sample code for AppConfiguration
- Loading branch information
1 parent
091ae2d
commit 11de94e
Showing
10 changed files
with
139 additions
and
8 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
41 changes: 41 additions & 0 deletions
41
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,41 @@ | ||
import { getConnectionStringFromEnvironment, deleteKeyAndLabels } from "./sampleHelpers"; | ||
|
||
// 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"); | ||
|
||
let connectionString = getConnectionStringFromEnvironment(); | ||
const client = new AppConfigurationClient(connectionString); | ||
|
||
const greetingKey = "Samples:Greeting"; | ||
|
||
deleteKeyAndLabels([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`); | ||
|
||
deleteKeyAndLabels([greetingKey], client); | ||
} | ||
|
||
// 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}`); | ||
// }); |
36 changes: 36 additions & 0 deletions
36
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,36 @@ | ||
import { getConnectionStringFromEnvironment, deleteKeyAndLabels } from "./sampleHelpers"; | ||
|
||
// 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"); | ||
|
||
let connectionString = getConnectionStringFromEnvironment(); | ||
const client = new AppConfigurationClient(connectionString); | ||
|
||
const urlKey = "Samples:Endpoint:Url"; | ||
|
||
deleteKeyAndLabels([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}`); | ||
|
||
deleteKeyAndLabels([urlKey], client); | ||
} | ||
|
||
// 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(); | ||
} |
22 changes: 22 additions & 0 deletions
22
sdk/appconfiguration/app-configuration/samples/sampleHelpers.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,22 @@ | ||
import { AppConfigurationClient } from "../src" | ||
|
||
export function getConnectionStringFromEnvironment() : string { | ||
const connectionString = process.env["AZ_CONFIG_CONNECTION"]!; | ||
|
||
if (connectionString == null) { | ||
throw Error(`No connection string in environment - set AZ_CONFIG_CONNECTION with a connection string for your AppConfiguration instance.`); | ||
} | ||
|
||
return connectionString; | ||
} | ||
|
||
export async function deleteKeyAndLabels(keys: string[], client: AppConfigurationClient) { | ||
const existingSettings = await client.listConfigurationSettings({ | ||
key: keys | ||
}); | ||
|
||
for (const setting of existingSettings) { | ||
console.log(`Removing key ${setting.key} (and all labels)`); | ||
await client.deleteConfigurationSetting(setting.key!, { label: setting.label }); | ||
} | ||
} |
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
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 { runAll } from "../samples"; | ||
|
||
describe("AppConfiguration samples", () => { | ||
it("Make sure all the samples build and run", async () => { | ||
await runAll(); | ||
}); | ||
}); |
10 changes: 10 additions & 0 deletions
10
sdk/appconfiguration/app-configuration/test/testhelpers.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,10 @@ | ||
import * as assert from "assert"; | ||
import * as dotenv from "dotenv"; | ||
|
||
dotenv.config(); | ||
|
||
export function getConnectionString() { | ||
let connectionStringFromEnvironment = process.env["APPCONFIG_CONNECTION_STRING"]!; | ||
assert.ok(connectionStringFromEnvironment, "Connection string not set in environment variable APPCONFIG_CONNECTION_STRING"); | ||
return connectionStringFromEnvironment; | ||
} |