Skip to content

Commit

Permalink
[Service Bus] Update constructors to add overloads and use Azure Iden…
Browse files Browse the repository at this point in the history
…tity (#5436)
  • Loading branch information
ramya0820 authored Jan 15, 2020
1 parent b04922a commit 6947606
Show file tree
Hide file tree
Showing 26 changed files with 285 additions and 353 deletions.
6 changes: 3 additions & 3 deletions sdk/servicebus/service-bus/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ module.exports = function(config) {
// https://www.npmjs.com/package/karma-env-preprocessor
envPreprocessor: [
"SERVICEBUS_CONNECTION_STRING_BROWSER",
"AAD_CLIENT_ID",
"AAD_CLIENT_SECRET",
"AAD_TENANT_ID"
"AZURE_CLIENT_ID",
"AZURE_CLIENT_SECRET",
"AZURE_TENANT_ID"
],

// test results reporter to use
Expand Down
3 changes: 1 addition & 2 deletions sdk/servicebus/service-bus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"module": "dist-esm/src/index.js",
"browser": {
"./dist/index.js": "./browser/service-bus.js",
"./dist-esm/test/utils/aadUtils.js": "./dist-esm/test/utils/aadUtils.browser.js",
"./dist-esm/src/util/crypto.js": "./dist-esm/src/util/crypto.browser.js",
"./dist-esm/src/util/parseUrl.js": "./dist-esm/src/util/parseUrl.browser.js",
"buffer": "buffer",
Expand Down Expand Up @@ -78,7 +77,7 @@
"dependencies": {
"@azure/core-amqp": "^1.0.0",
"@azure/core-http": "^1.0.0",
"@azure/ms-rest-nodeauth": "^0.9.2",
"@azure/identity": "^1.0.0",
"@opentelemetry/types": "^0.2.0",
"@types/is-buffer": "^2.0.0",
"@types/long": "^4.0.0",
Expand Down
3 changes: 2 additions & 1 deletion sdk/servicebus/service-bus/review/service-bus.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,9 @@ export interface ServiceBusAtomManagementClientOptions {

// @public
export class ServiceBusClient {
constructor(connectionString: string, options?: ServiceBusClientOptions);
constructor(host: string, credential: TokenCredential, options?: ServiceBusClientOptions);
close(): Promise<any>;
static createFromConnectionString(connectionString: string, options?: ServiceBusClientOptions): ServiceBusClient;
createQueueClient(queueName: string): QueueClient;
createSubscriptionClient(topicName: string, subscriptionName: string): SubscriptionClient;
createTopicClient(topicName: string): TopicClient;
Expand Down
155 changes: 54 additions & 101 deletions sdk/servicebus/service-bus/src/serviceBusClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
ConnectionConfig,
DataTransformer,
TokenCredential,
SharedKeyCredential
SharedKeyCredential,
isTokenCredential
} from "@azure/core-amqp";
import { SubscriptionClient } from "./subscriptionClient";

Expand Down Expand Up @@ -56,23 +57,66 @@ export class ServiceBusClient {
*/
private _context: ConnectionContext;

/**
* Creates a ServiceBusClient for the Service Bus Namespace represented in the given connection
* string.
* @param connectionString - Connection string of the form
* 'Endpoint=sb://my-servicebus-namespace.servicebus.windows.net/;SharedAccessKeyName=my-SA-name;SharedAccessKey=my-SA-key'
* @param options Options to control ways to interact with the
* Service Bus Namespace.
* @returns ServiceBusClient
*/
constructor(connectionString: string, options?: ServiceBusClientOptions);

/**
* Instantiates a ServiceBusClient to interact with a Service Bus Namespace.
*
* @constructor
* @param {ConnectionConfig} config - The connection configuration needed to connect to the
* Service Bus Namespace.
* @param {TokenCredential} [tokenCredential] - SharedKeyCredential object or your
* credential that implements the TokenCredential interface.
* @param {ServiceBusClientOptions} - Options to control ways to interact with the Service Bus
* @param host - The host name for the Service Bus namespace. This is likely to be similar to
* <yournamespace>.servicebus.windows.net
* @param credential - credential that implements the TokenCredential interface.
* @param options - Options to control ways to interact with the Service Bus
* Namespace.
*/
private constructor(
config: ConnectionConfig,
credential: SharedKeyCredential | TokenCredential,
constructor(host: string, credential: TokenCredential, options?: ServiceBusClientOptions);

constructor(
hostOrConnectionString: string,
credentialOrServiceBusClientOptions?: TokenCredential | ServiceBusClientOptions,
options?: ServiceBusClientOptions
) {
if (!options) options = {};
let config;
let credential;

if (!isTokenCredential(credentialOrServiceBusClientOptions)) {
// connectionString and options based constructor was invoked
config = ConnectionConfig.create(hostOrConnectionString);

options = credentialOrServiceBusClientOptions as ServiceBusClientOptions;
config.webSocket = options && options.webSocket;
config.webSocketEndpointPath = "$servicebus/websocket";
config.webSocketConstructorOptions = options && options.webSocketConstructorOptions;

// Since connectionstring was passed, create a SharedKeyCredential
credential = new SharedKeyCredential(config.sharedAccessKeyName, config.sharedAccessKey);

ConnectionConfig.validate(config);
} else {
// host, credential and options based constructor was invoked
credential = credentialOrServiceBusClientOptions as TokenCredential;

hostOrConnectionString = String(hostOrConnectionString);
if (!hostOrConnectionString.endsWith("/")) {
hostOrConnectionString += "/";
}
const connectionString = `Endpoint=sb://${hostOrConnectionString};SharedAccessKeyName=defaultKeyName;SharedAccessKey=defaultKeyValue;`;
config = ConnectionConfig.create(connectionString);
}

if (!options) {
options = {};
}

this.name = config.endpoint;
this._context = ConnectionContext.create(config, credential, options);
}
Expand Down Expand Up @@ -152,95 +196,4 @@ export class ServiceBusClient {
throw errObj;
}
}

/**
* Creates a ServiceBusClient for the Service Bus Namespace represented in the given connection
* string.
* @param {string} connectionString - Connection string of the form
* 'Endpoint=sb://my-servicebus-namespace.servicebus.windows.net/;SharedAccessKeyName=my-SA-name;SharedAccessKey=my-SA-key'
* @param {ServiceBusClientOptions} [options] Options to control ways to interact with the
* Service Bus Namespace.
* @returns {ServiceBusClient}
*/
static createFromConnectionString(
connectionString: string,
options?: ServiceBusClientOptions
): ServiceBusClient {
const config = ConnectionConfig.create(connectionString);

config.webSocket = options && options.webSocket;
config.webSocketEndpointPath = "$servicebus/websocket";
config.webSocketConstructorOptions = options && options.webSocketConstructorOptions;

// Since connectionstring was passed, create a SharedKeyCredential
const credential = new SharedKeyCredential(config.sharedAccessKeyName, config.sharedAccessKey);

ConnectionConfig.validate(config);

return new ServiceBusClient(config, credential, options);
}

// /**
// * Creates a ServiceBusClient for the Service Bus Namespace represented by the given host using
// * the given TokenProvider.
// * @param {string} host - Fully qualified domain name for Servicebus. Most likely,
// * `<yournamespace>.servicebus.windows.net`.
// * @param {TokenProvider} tokenProvider - Your custom implementation of the {@link https://github.com/Azure/amqp-common-js/blob/master/lib/auth/token.ts Token Provider}
// * interface.
// * @param {ServiceBusClientOptions} options - Options to control ways to interact with the
// * Service Bus Namespace.
// * @returns {ServiceBusClient}
// */
// static createFromTokenProvider(
// host: string,
// tokenProvider: TokenProvider,
// options?: ServiceBusClientOptions
// ): ServiceBusClient {
// host = String(host);
// if (!tokenProvider) {
// throw new TypeError('Missing parameter "tokenProvider"');
// }
// if (!host.endsWith("/")) host += "/";
// const connectionString =
// `Endpoint=sb://${host};SharedAccessKeyName=defaultKeyName;` +
// `SharedAccessKey=defaultKeyValue`;
// const config = ConnectionConfig.create(connectionString);

// config.webSocket = options && options.webSocket;
// config.webSocketEndpointPath = "$servicebus/websocket";
// config.webSocketConstructorOptions = options && options.webSocketConstructorOptions;

// ConnectionConfig.validate(config);
// return new ServiceBusClient(config, tokenProvider, options);
// }

// /**
// * Creates a ServiceBusClient for the Service Bus Namespace represented by the given host using
// * the TokenCredentials generated using the `@azure/ms-rest-nodeauth` library.
// * @param {string} host - Fully qualified domain name for ServiceBus.
// * Most likely, {yournamespace}.servicebus.windows.net
// * @param {ServiceClientCredentials} credentials - The Token credentials generated by using the
// * `@azure/ms-rest-nodeauth` library. It can be one of the following:
// * - ApplicationTokenCredentials
// * - UserTokenCredentials
// * - DeviceTokenCredentials
// * - MSITokenCredentials
// * Token audience (or resource in case of MSI based credentials) to use when creating the credentials is https://servicebus.azure.net/
// * @param {ServiceBusClientOptions} options - Options to control ways to interact with the
// * Service Bus Namespace.
// * @returns {ServiceBusClient}
// */
// static createFromAadTokenCredentials(
// host: string,
// credentials:
// | ApplicationTokenCredentials
// | UserTokenCredentials
// | DeviceTokenCredentials
// | MSITokenCredentials,
// options?: ServiceBusClientOptions
// ): ServiceBusClient {
// host = String(host);
// const tokenProvider = new AadTokenProvider(credentials);
// return ServiceBusClient.createFromTokenProvider(host, tokenProvider, options);
// }
}
6 changes: 3 additions & 3 deletions sdk/servicebus/service-bus/test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ Go through the following setup in order to delete and create the required servic
Populate the following variables along with the above mentioned environment variables in the `.env`.
```
AAD_CLIENT_ID=""
AAD_CLIENT_SECRET=""
AAD_TENANT_ID=""
AZURE_CLIENT_ID=""
AZURE_CLIENT_SECRET=""
AZURE_TENANT_ID=""
```
**Note:**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ async function RunTest(
maxConcurrentCalls: number,
messages: number
): Promise<void> {
const ns = ServiceBusClient.createFromConnectionString(connectionString);
const ns = new ServiceBusClient(connectionString);

const client = ns.createQueueClient(entityPath);
const receiver = client.createReceiver(ReceiveMode.receiveAndDelete);
Expand Down
2 changes: 1 addition & 1 deletion sdk/servicebus/service-bus/test/perf/service-bus/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async function RunTest(
maxInflight: number,
messages: number
): Promise<void> {
const ns = ServiceBusClient.createFromConnectionString(connectionString);
const ns = new ServiceBusClient(connectionString);

const client = ns.createQueueClient(entityPath);
const sender = client.createSender();
Expand Down
Loading

0 comments on commit 6947606

Please sign in to comment.