Skip to content

Commit

Permalink
Adding in sample code for AppConfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
richardpark-msft committed Sep 11, 2019
1 parent 091ae2d commit 11de94e
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 8 deletions.
12 changes: 7 additions & 5 deletions sdk/appconfiguration/app-configuration/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Azure App Configuration client library for JS

This package contains an isomorphic SDK for ConfigurationClient.
Azure App Configuration is a managed service that helps developers centralize their application configurations simply and securely.

Modern programs, especially programs running in a cloud, generally have many components that are distributed in nature. Spreading configuration settings across these components can lead to hard-to-troubleshoot errors during an application deployment. Use App Configuration to securely store all the settings for your application in one place.

Use the client library for App Configuration to create and manage application configuration settings.

## Getting started

Expand All @@ -12,7 +16,7 @@ This package contains an isomorphic SDK for ConfigurationClient.
### How to Install

```bash
npm install @azure/app-config
npm install @azure/app-configuration
```
## Key concepts

Expand All @@ -31,10 +35,8 @@ npm install @azure/ms-rest-nodeauth
##### Sample code

```typescript
import * as coreHttp from "@azure/core-http";
import * as coreArm from "@azure/core-arm";
import * as msRestNodeAuth from "@azure/ms-rest-nodeauth";
import { ConfigurationClient, ConfigurationModels, ConfigurationMappers } from "@azure/app-config";
import { ConfigurationClient, ConfigurationModels, ConfigurationMappers } from "@azure/app-configuration";
const subscriptionId = process.env["AZURE_SUBSCRIPTION_ID"];

msRestNodeAuth.interactiveLogin().then((creds) => {
Expand Down
1 change: 1 addition & 0 deletions sdk/appconfiguration/app-configuration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
"prebuild": "npm run clean",
"pack": "npm pack 2>&1",
"test": "npm run build:test && mocha -t 1200000 test-dist/index.node.js --reporter mocha-multi --reporter-options spec=-,mocha-junit-reporter=-",
"test:watch": "mocha \"test/**/*\" -t 1200000 --require ts-node/register --watch-extensions ts,tsx --reporter mocha-multi --reporter-options spec=-,mocha-junit-reporter=- --watch",
"unit-test:browser": "echo skipped",
"unit-test:node": "echo skipped",
"unit-test": "npm run unit-test:node && npm run unit-test:browser"
Expand Down
41 changes: 41 additions & 0 deletions sdk/appconfiguration/app-configuration/samples/helloworld.ts
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}`);
// });
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}`);
// });
7 changes: 7 additions & 0 deletions sdk/appconfiguration/app-configuration/samples/index.ts
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 sdk/appconfiguration/app-configuration/samples/sampleHelpers.ts
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 });
}
}
6 changes: 6 additions & 0 deletions sdk/appconfiguration/app-configuration/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ export class AppConfigurationClient {
*/
constructor(uri: string, credential: TokenCredential);
constructor(uriOrConnectionString: string, credential?: TokenCredential) {
if (uriOrConnectionString == null) {
throw new Error(
"You must provide a connection string or the URL for your AppConfiguration instance"
);
}

const regexMatch = uriOrConnectionString.match(ConnectionStringRegex);
if (regexMatch) {
const credential = new AppConfigCredential(regexMatch[2], regexMatch[3]);
Expand Down
5 changes: 2 additions & 3 deletions sdk/appconfiguration/app-configuration/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

import * as assert from "assert";
import { getConnectionString } from "./testhelpers";
import * as dotenv from "dotenv";
import { AppConfigurationClient } from "../src";

Expand All @@ -14,9 +15,7 @@ describe("AppConfigurationClient", () => {
let client: AppConfigurationClient;

before("validate environment variables", () => {
if (!connectionString) {
throw new Error("APPCONFIG_CONNECTION_STRING not defined.");
}
let connectionString = getConnectionString();
client = new AppConfigurationClient(connectionString);
});

Expand Down
7 changes: 7 additions & 0 deletions sdk/appconfiguration/app-configuration/test/samples.spec.ts
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 sdk/appconfiguration/app-configuration/test/testhelpers.ts
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;
}

0 comments on commit 11de94e

Please sign in to comment.