From 5b58d104a74767694f31d78592e0791142dea467 Mon Sep 17 00:00:00 2001 From: Cecilia Avila <44245136+ceciliaavila@users.noreply.github.com> Date: Tue, 8 Aug 2023 17:52:44 -0300 Subject: [PATCH 1/3] Replace Map with WeakMap to avoid memory leak (#4517) --- .../adaptive-expressions/src/parser/expressionParser.ts | 8 ++++---- .../src/languageGeneratorExtensions.ts | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/libraries/adaptive-expressions/src/parser/expressionParser.ts b/libraries/adaptive-expressions/src/parser/expressionParser.ts index ce5d2d4ac9..e93fc7458b 100644 --- a/libraries/adaptive-expressions/src/parser/expressionParser.ts +++ b/libraries/adaptive-expressions/src/parser/expressionParser.ts @@ -27,7 +27,7 @@ export class ExpressionParser implements ExpressionParserInterface { */ readonly EvaluatorLookup: EvaluatorLookup; - private static expressionDict: Map = new Map(); + private static expressionDict: WeakMap = new WeakMap(); private readonly ExpressionTransformer = class extends AbstractParseTreeVisitor @@ -279,8 +279,8 @@ export class ExpressionParser implements ExpressionParserInterface { * @returns A ParseTree. */ protected static antlrParse(expression: string): ParseTree { - if (ExpressionParser.expressionDict.has(expression)) { - return ExpressionParser.expressionDict.get(expression); + if (ExpressionParser.expressionDict.has({ key: expression })) { + return ExpressionParser.expressionDict.get({ key: expression }); } const inputStream: ANTLRInputStream = new ANTLRInputStream(expression); @@ -297,7 +297,7 @@ export class ExpressionParser implements ExpressionParserInterface { if (file !== undefined) { expressionContext = file.expression(); } - ExpressionParser.expressionDict.set(expression, expressionContext); + ExpressionParser.expressionDict.set({ key: expression }, expressionContext); return expressionContext; } diff --git a/libraries/botbuilder-dialogs-adaptive/src/languageGeneratorExtensions.ts b/libraries/botbuilder-dialogs-adaptive/src/languageGeneratorExtensions.ts index d82a554a99..aac5b75e39 100644 --- a/libraries/botbuilder-dialogs-adaptive/src/languageGeneratorExtensions.ts +++ b/libraries/botbuilder-dialogs-adaptive/src/languageGeneratorExtensions.ts @@ -36,7 +36,7 @@ export const languagePolicyKey = Symbol('LanguagePolicy'); * Extension methods for language generator. */ export class LanguageGeneratorExtensions { - private static readonly _languageGeneratorManagers = new Map(); + private static readonly _languageGeneratorManagers = new WeakMap(); /** * Register default LG file or a language generator as default language generator. From 447c81c094a4a613dacce5085f032ad51ae3b3d9 Mon Sep 17 00:00:00 2001 From: Greg Borelly Date: Thu, 17 Aug 2023 10:53:41 -0400 Subject: [PATCH 2/3] fix: Upgrading restify to fix error on Node version 18+ (#4515) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Current version leads to the following error on Node 18+: ➜ my-echo-bot npm start > my-echo-bot@1.0.0 start > node ./index.js /home/gregborrelly/development/myJsBots/my-echo-bot/node_modules/restify/lib/request.js:848 Request.prototype.closed = function closed() { ^ TypeError: Cannot set property closed of # which has only a getter at patch (/home/gregborrelly/development/myJsBots/my-echo-bot/node_modules/restify/lib/request.js:848:30) at Object. (/home/gregborrelly/development/myJsBots/my-echo-bot/node_modules/restify/lib/server.js:33:1) --- .../generators/app/templates/echo/package.json.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generators/generator-botbuilder/generators/app/templates/echo/package.json.js b/generators/generator-botbuilder/generators/app/templates/echo/package.json.js index 0afa6b69d1..be8587b19d 100644 --- a/generators/generator-botbuilder/generators/app/templates/echo/package.json.js +++ b/generators/generator-botbuilder/generators/app/templates/echo/package.json.js @@ -18,7 +18,7 @@ "dependencies": { "botbuilder": "~4.15.0", "dotenv": "~8.2.0", - "restify": "~10.0.0" + "restify": "~11.1.0" }, "devDependencies": { "eslint": "^7.0.0", From 59e5d9600e9919c7e5d121ac432049ed04269ae8 Mon Sep 17 00:00:00 2001 From: Cecilia Avila <44245136+ceciliaavila@users.noreply.github.com> Date: Fri, 18 Aug 2023 10:35:28 -0300 Subject: [PATCH 3/3] fix: remove ms-rest-azure package (#4521) * remove ms-rest-azure package * replace node-auth with azure/identity * fix unit tests --------- Co-authored-by: JhontSouth --- package.json | 2 - tools/framework/authentication.js | 88 ------- tools/framework/subscriptionUtils.js | 123 --------- tools/framework/suite-base.js | 67 ++--- tools/package.json | 3 +- .../lib/resource/models/baseResource.js | 51 ++++ .../lib/resource/models/cloudError.js | 243 ++++++++++++++++++ .../lib/resource/models/index.js | 6 +- .../operations/deploymentOperations.js | 8 +- .../lib/resource/operations/deployments.js | 20 +- .../lib/resource/operations/providers.js | 12 +- .../lib/resource/operations/resourceGroups.js | 18 +- .../lib/resource/operations/resources.js | 31 +-- .../lib/resource/operations/tags.js | 14 +- .../lib/resource/resourceManagementClient.js | 62 ++++- tools/resourceManagement/package.json | 3 +- yarn.lock | 22 +- 17 files changed, 444 insertions(+), 329 deletions(-) delete mode 100644 tools/framework/authentication.js delete mode 100644 tools/framework/subscriptionUtils.js create mode 100644 tools/resourceManagement/lib/resource/models/baseResource.js create mode 100644 tools/resourceManagement/lib/resource/models/cloudError.js diff --git a/package.json b/package.json index 1d3cb2da16..1256b4032b 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,6 @@ "async": "3.2.3", "minimist": "^1.2.6", "mixme": "0.5.2", - "ms-rest-azure": "2.6.2", "node-fetch": "2.6.7", "underscore": "1.13.1", "json-schema": "0.4.0", @@ -83,7 +82,6 @@ "exorcist": "^1.0.1", "mocha": "^6.2.3", "mocha-junit-reporter": "^2.0.0", - "ms-rest-azure": "^2.6.2", "npm-run-all": "^4.1.5", "nyc": "^15.1.0", "prettier": "^2.1.2", diff --git a/tools/framework/authentication.js b/tools/framework/authentication.js deleted file mode 100644 index 08f0cd79a4..0000000000 --- a/tools/framework/authentication.js +++ /dev/null @@ -1,88 +0,0 @@ -// -// Copyright (c) Microsoft and contributors. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the 'License'); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an 'AS IS' BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// -// See the License for the specific language governing permissions and -// limitations under the License. -// - -'use strict'; - -var adal = require('adal-node'); -var fs = require('fs'); -var https = require('https'); -var util = require('util'); -var msRestAzure = require('ms-rest-azure'); -var AuthenticationContext = adal.AuthenticationContext; - -function turnOnLogging() { - var log = adal.Logging; - log.setLoggingOptions( - { - level : log.LOGGING_LEVEL.VERBOSE, - log : function (level, message, error) { - console.log(message); - if (error) { - console.log(error); - } - } - }); -} - -turnOnLogging(); - -var authenticationParameters = { - tenant: 'common', - authorityHostUrl: 'https://login.windows.net', - clientId: '04b07795-8ddb-461a-bbee-02f9e1bf7b46' -}; - -/** - * Authenticate with username and password - * @param {string} username - Username - * @param {string} password - Password - * @param {function} callback - The callback to handle the error and result. - */ -exports.authenticateWithUsernamePassword = function (username, password, callback) { - var authorityUrl = authenticationParameters.authorityHostUrl + '/' + authenticationParameters.tenant; - var resource = 'https://management.core.windows.net/'; - var context = new AuthenticationContext(authorityUrl); - context.acquireTokenWithUsernamePassword(resource, authenticationParameters.username, authenticationParameters.password, authenticationParameters.clientId, function (err, tokenResponse) { - if (err) { - callback('Authentication failed. The error is: ' + util.inspect(err)); - } else { - callback(null, tokenResponse); - } - }); -}; - -/** - * Authenticate with Service Principal - * @param {string} clientId - Id of the service principal - * @param {string} clientSecret - Password/Secret of the service principal - * @param {string} tenant - The tenant id of the service principal. - * @param {function} callback - The callback to handle the error and result. - */ -exports.authenticateWithServicePrincipal = function (clientId, clientSecret, tenant, callback) { - var authorityUrl = authenticationParameters.authorityHostUrl + '/' + tenant; - var resource = 'https://management.core.windows.net/'; - var context = new AuthenticationContext(authorityUrl); - context.acquireTokenWithClientCredentials(resource, authenticationParameters.clientId, authenticationParameters.clientSecret, function (err, tokenResponse) { - if (err) { - callback('Authentication failed. The error is: ' + util.inspect(err)); - } else { - //new msRestAzure.SubscriptionCredentials(tokenResponse, - callback(null, tokenResponse); - } - }); -}; - -exports = module.exports; \ No newline at end of file diff --git a/tools/framework/subscriptionUtils.js b/tools/framework/subscriptionUtils.js deleted file mode 100644 index 3985802ca5..0000000000 --- a/tools/framework/subscriptionUtils.js +++ /dev/null @@ -1,123 +0,0 @@ -/** -* Copyright (c) Microsoft. All rights reserved. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var __ = require('underscore'); -var async = require('async'); -var util = require('util'); -var msRestAzure = require('ms-rest-azure'); -var SubscriptionClient = require('../../lib/services/resourceManagement/lib/subscriptionClient/SubscriptionClient'); - - -exports.getSubscriptions = function (environment, username, password, tenant, callback) { - environment.acquireToken(username, password, tenant, function (err, authContext) { - if (err) return callback(err); - username = _crossCheckUserNameWithToken(username, authContext.userId); - - async.waterfall([ - function (callback) { - _buildTenantList(environment, username, password, tenant, authContext, callback); - }, - function (tenantList, callback) { - getSubscriptionsFromTenants(environment, username, tenantList, callback); - }, - ], function (err, subscriptions) { - callback(err, subscriptions); - }); - }); -}; - -exports.getSubscriptionsFromTenants = function (username, authContext, callback) { - _buildTenantList(username, password, tenant, authContext, function (err, tenants) { - if (err) { - callback(err); - } - var subscriptions = []; - async.eachSeries(tenants, function (tenant, cb) { - var tenantId = tenant.tenantId; - var subscriptionClient = new SubscriptionClient(_createCredential(tenant.authContext)); - subscriptionClient.subscriptions.list(function (err, result) { - if (!err) { - subscriptions = subscriptions.concat(result.value.map(function (s) { - return new msRestAzure.SubscriptionCredential(tenant.authContext, s.subscriptionId); - })); - } - cb(err); - }); - }, function (err) { - callback(err, subscriptions); - }); - }); -}; - -function _buildTenantList(environment, username, password, tenant, authContext, callback) { - var tenants = []; - if (tenant) { - tenants = [{ - tenantId: tenant,//'tenant' could be a logical name, interchangable with a tenant guid though - authContext: authContext - }]; - return callback(null, tenants); - } - var armClient = environment.getArmClient(_createCredential(authContext)); - armClient.tenants.list(function (err, result) { - if (err) return callback(err); - async.eachSeries(result.tenantIds/*'tenantInfos' could be a better name*/, function (tenantInfo, cb) { - environment.acquireToken(username, password, tenantInfo.tenantId, function (err, authContext) { - //TODO: verify - if (err && err.match(new RegExp('.*\"error_codes\":\\[50034|50000\\].*'))) { - console.log(util.format('Due to current limitation, we will skip retrieving subscriptions from the external tenant \'%s\'', tenantInfo.tenantId)); - err = null; - } - if (!err) { - tenants.push({ - tenantId: tenantInfo.tenantId, - authContext: authContext - }); - } - cb(err); - }); - }, function (err) { - callback(err, tenants); - }); - }); -} - -function _ignoreCaseAndSpaceEquals(a, b) { - return a === b || (_toLowerCaseAndRemoveSpace(a) === _toLowerCaseAndRemoveSpace(b)); -} - -function _toLowerCaseAndRemoveSpace(str) { - if (!str) { - return str; - } - - return str.replace(/ /gi, '').toLowerCase(); -} - -function _crossCheckUserNameWithToken(usernameFromCommandline, userIdFromToken) { - if (_ignoreCaseAndSpaceEquals(usernameFromCommandline, userIdFromToken)) { - return userIdFromToken; - } else { - throw new Error(util.format('invalid user name %s', usernameFromCommandline)); - } -} - -function _createCredential(authContext) { - return new msRestAzure.SubscriptionCredentials(authContext, 'notUsed'); -} - -exports = module.exports; \ No newline at end of file diff --git a/tools/framework/suite-base.js b/tools/framework/suite-base.js index f2151cdb19..4e1060a4e8 100644 --- a/tools/framework/suite-base.js +++ b/tools/framework/suite-base.js @@ -23,7 +23,8 @@ var _ = require('underscore'); var util = require('util'); var uuid = require('uuid'); var msRest = require('ms-rest'); -var msRestAzure = require('ms-rest-azure'); +var identity = require("@azure/identity"); +var {Environment} = require("@azure/ms-rest-azure-env") var MicrosoftAppCredentials = require('botframework-connector/lib/auth/microsoftAppCredentials'); var TokenApiClient = require('botframework-connector/lib/tokenApi/tokenApiClient'); var FileTokenCache = require('../util/fileTokenCache'); @@ -33,7 +34,8 @@ var ResourceManagementClient = require('../resourceManagement/lib/resource/resou var async = require('async'); var adal = require('adal-node'); -var DEFAULT_ADAL_CLIENT_ID = '04b07795-8ddb-461a-bbee-02f9e1bf7b46'; +var DEFAULT_ADAL_CLIENT_ID = '04b07795-8ddb-461a-bbee-02f9e1bf7b46'; +var DEFAULT_ADAL_TENANT_ID = 'd4058e97-3782-4874-bc12-c975407af782'; /** * @class @@ -54,7 +56,7 @@ var DEFAULT_ADAL_CLIENT_ID = '04b07795-8ddb-461a-bbee-02f9e1bf7b46'; */ function SuiteBase(mochaSuiteObject, testPrefix, env, libraryPath) { this.mochaSuiteObject = mochaSuiteObject; - this.testPrefix = this.normalizeTestName(testPrefix); + this.testPrefix = this.normalizeTestName(testPrefix); this.mockServerClient; this.currentTest = ''; //Recording info @@ -68,12 +70,13 @@ function SuiteBase(mochaSuiteObject, testPrefix, env, libraryPath) { //authentication info this.subscriptionId = process.env['AZURE_SUBSCRIPTION_ID'] || 'subscription-id'; this.clientId = process.env['CLIENT_ID'] || DEFAULT_ADAL_CLIENT_ID; + this.tenantId = process.env['TENANT_ID'] || DEFAULT_ADAL_TENANT_ID; this.domain = process.env['DOMAIN'] || 'domain'; this.username = process.env['AZURE_USERNAME'] || 'username@example.com'; this.password = process.env['AZURE_PASSWORD'] || 'dummypassword'; this.secret = process.env['CLIENT_SECRET'] || 'dummysecret'; this.tokenCache = new adal.MemoryCache(); - + this._setCredentials(); //subscriptionId should be recorded for playback if (!env) { @@ -101,11 +104,11 @@ _.extend(SuiteBase.prototype, { let token = process.env['AZURE_ACCESS_TOKEN'] || 'token'; this.credentials = new msRest.TokenCredentials('token'); } else if ((process.env['AZURE_PASSWORD'] && process.env['CLIENT_SECRET']) || - (!process.env['AZURE_PASSWORD'] && !process.env['CLIENT_SECRET'])) { + (!process.env['AZURE_PASSWORD'] && !process.env['CLIENT_SECRET'])) { throw new Error('You must either set the envt. variables \'AZURE_USERNAME\' ' + - 'and \'AZURE_PASSWORD\' for running tests as a user or set the ' + - 'envt. variable \'CLIENT_ID\' and \'CLIENT_SECRET\' ' + - 'for running tests as a service-principal, but not both.'); + 'and \'AZURE_PASSWORD\' for running tests as a user or set the ' + + 'envt. variable \'CLIENT_ID\' and \'CLIENT_SECRET\' ' + + 'for running tests as a service-principal, but not both.'); } if (process.env['AZURE_PASSWORD']) { @@ -123,7 +126,7 @@ _.extend(SuiteBase.prototype, { /** * Creates the UserTokenCredentials object. * - * @returns {ms-rest-azure.UserTokenCredentials} The user token credentials object. + * @returns {@azure/identity.UsernamePasswordCredential} The user token credentials object. */ _createUserCredentials: function() { if(process.env['AZURE_ENVIRONMENT'] && process.env['AZURE_ENVIRONMENT'].toUpperCase() === 'DOGFOOD') { @@ -135,19 +138,19 @@ _.extend(SuiteBase.prototype, { managementEndpointUrl: 'https://management-preview.core.windows-int.net/', resourceManagerEndpointUrl: 'https://api-dogfood.resources.windows-int.net/' }; - var env = msRestAzure.AzureEnvironment.add(df); - return new msRestAzure.UserTokenCredentials(this.clientId, this.domain, this.username, + var env = Environment.add(df); + return new identity.UsernamePasswordCredential(this.tenantId, this.clientId, this.username, this.password, { 'tokenCache': this.tokenCache, 'environment': env }); } - - return new msRestAzure.UserTokenCredentials(this.clientId, this.domain, this.username, + + return new identity.UsernamePasswordCredential(this.tenantId, this.clientId, this.username, this.password, { 'tokenCache': this.tokenCache }); }, /** * Creates the ApplicationTokenCredentials object. * - * @returns {ms-rest-azure.ApplicationTokenCredentials} The application token credentials object. + * @returns {@azure/identity.ClientSecretCredential} The application token credentials object. */ _createApplicationCredentials: function() { if(process.env['AZURE_ENVIRONMENT'] && process.env['AZURE_ENVIRONMENT'].toUpperCase() === 'DOGFOOD') { @@ -159,14 +162,14 @@ _.extend(SuiteBase.prototype, { managementEndpointUrl: 'https://management-preview.core.windows-int.net/', resourceManagerEndpointUrl: 'https://api-dogfood.resources.windows-int.net/' }; - var env = msRestAzure.AzureEnvironment.add(df); - return new msRestAzure.ApplicationTokenCredentials(this.clientId, this.domain, this.secret, { + var env = Environment.add(df); + return new identity.ClientSecretCredential(this.tenantId, this.clientId, this.secret, { 'tokenCache': this.tokenCache, 'environment': env }); } - - return new msRestAzure.ApplicationTokenCredentials(this.clientId, this.domain, this.secret, { + + return new identity.ClientSecretCredential(this.tenantId, this.clientId, this.secret, { 'tokenCache': this.tokenCache }); }, @@ -242,7 +245,7 @@ _.extend(SuiteBase.prototype, { */ getTestRecordingsFile: function() { this.testRecordingsFile = this.getRecordingsDirectory() + - this.normalizeTestName(this.currentTest) + '.nock.js'; + this.normalizeTestName(this.currentTest) + '.nock.js'; return this.testRecordingsFile; }, @@ -278,7 +281,7 @@ _.extend(SuiteBase.prototype, { if (missing.length > 0) { messages.push('This test requires the following environment variables which are not set: ' + - missing.join(', ')); + missing.join(', ')); } if (messages.length > 0) { @@ -438,7 +441,7 @@ _.extend(SuiteBase.prototype, { }); } else { throw new Error('It appears the ' + this.getTestRecordingsFile() + ' file has more tests than there are mocked tests. ' + - 'You may need to re-generate it.'); + 'You may need to re-generate it.'); } } @@ -484,13 +487,13 @@ _.extend(SuiteBase.prototype, { line = line.replace(/(\.get\('.*\/microsoft.insights\/eventtypes\/management\/values\?api-version=[0-9-]+)[^)]+\)/, '.filteringPath(function (path) { return path.slice(0, path.indexOf(\'&\')); })\n$1\')'); if (line.match(/\/oauth2\/token\//ig) === null && - line.match(/login\.windows\.net/ig) === null && + line.match(/login\.windows\.net/ig) === null && line.match(/login\.windows-ppe\.net/ig) === null && line.match(/login\.microsoftonline\.com/ig) === null && line.match(/login\.chinacloudapi\.cn/ig) === null && line.match(/login\.microsoftonline\.de/ig) === null) { scope += (lineWritten ? ',\n' : '') + 'function (nock) { \n' + - 'var result = ' + line + ' return result; }'; + 'var result = ' + line + ' return result; }'; lineWritten = true; } } @@ -656,7 +659,7 @@ _.extend(SuiteBase.prototype, { * in 'Record' mode or keeps it in memory when the test is run in 'Live' mode. */ saveMockVariable: function(mockVariableName, mockVariable) { - //record or live + //record or live if (!this.isPlayback) { this.mockVariables[mockVariableName] = mockVariable; } @@ -708,21 +711,7 @@ _.extend(SuiteBase.prototype, { * Stubs certain methods. */ _stubMethods: function() { - if (this.isPlayback) { - if (msRestAzure.UserTokenCredentials.prototype.signRequest.restore) { - msRestAzure.UserTokenCredentials.prototype.signRequest.restore(); - } - sinon.stub(msRestAzure.UserTokenCredentials.prototype, 'signRequest').callsFake(function(webResource, callback) { - return callback(null); - }); - - if (msRestAzure.ApplicationTokenCredentials.prototype.signRequest.restore) { - msRestAzure.ApplicationTokenCredentials.prototype.signRequest.restore(); - } - sinon.stub(msRestAzure.ApplicationTokenCredentials.prototype, 'signRequest').callsFake(function(webResource, callback) { - return callback(null); - }); - + if (this.isPlayback) { if (this.createResourcegroup.restore) { this.createResourcegroup.restore(); } diff --git a/tools/package.json b/tools/package.json index 28d2fad03e..de5542c269 100644 --- a/tools/package.json +++ b/tools/package.json @@ -29,12 +29,13 @@ "main": "./lib/azure.js", "license": "(MIT OR Apache-2.0)", "dependencies": { + "@azure/identity": "^2.0.4", + "@azure/ms-rest-azure-env":"^2.0.0", "@types/request": "^2.47.1", "botframework-connector": "4.1.6", "dotenv": "^4.0.0", "mime": "^1.4.1", "ms-rest": "^2.3.6", - "ms-rest-azure": "^2.5.7", "underscore": "^1.13.1", "uuid": "^3.3.2" }, diff --git a/tools/resourceManagement/lib/resource/models/baseResource.js b/tools/resourceManagement/lib/resource/models/baseResource.js new file mode 100644 index 0000000000..178d50596e --- /dev/null +++ b/tools/resourceManagement/lib/resource/models/baseResource.js @@ -0,0 +1,51 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. + +'use strict'; + +/** + * @class + * Initializes a new instance of the Resource class. + * @constructor + */ +class BaseResource { + /** + * Defines the metadata of BaseResource + * + * @returns {object} metadata of BaseResource + * + */ + mapper() { + return { + required: false, + serializedName: 'BaseResource', + type: { + name: 'Composite', + className: 'BaseResource', + modelProperties: {} + } + }; + } + + /** + * Validate the payload against the Resource schema + * + * @param {JSON} payload + * + */ + serialize() { + return {}; + } + + /** + * Deserialize the instance to Resource schema + * + * @param {JSON} instance + * + */ + deserialize(instance) { + return instance; + } +} + +module.exports = BaseResource; diff --git a/tools/resourceManagement/lib/resource/models/cloudError.js b/tools/resourceManagement/lib/resource/models/cloudError.js new file mode 100644 index 0000000000..7a5d2f29a9 --- /dev/null +++ b/tools/resourceManagement/lib/resource/models/cloudError.js @@ -0,0 +1,243 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. + +'use strict'; + +/** + * @class + * Provides additional information about an http error response returned from a Microsoft Azure service. + * @constructor + * @member {string} [code] The error code parsed from the body of the http error response + * + * @member {string} [message] The error message parsed from the body of the http error response + * + * @member {string} [target] The target of the error + * + * @member {array} [details] An array of CloudError objects specifying the details + * + * @member {Object} [innererror] The inner error parsed from the body of the http error response + * + * @member {Object} [additionalInfo] The additional error information + */ +class CloudError extends Error { + constructor(parameters) { + super(); + this.name = this.constructor.name; + + if (parameters) { + if (parameters.code) { + this.code = parameters.code; + } + + if (parameters.message) { + this.message = parameters.message; + } + + if (parameters.target) { + this.target = parameters.target; + } + + if (parameters.details) { + var tempDetails = []; + parameters.details.forEach(function (element) { + if (element) { + element = new CloudError(element); + } + tempDetails.push(element); + }); + this.details = tempDetails; + } + + if (parameters.innererror) { + this.innererror = JSON.parse(JSON.stringify(parameters.innererror)); + } + + if (parameters.additionalInfo) { + this.additionalInfo = parameters.additionalInfo; + } + } + } + + /** + * Defines the metadata of CloudError + * + * @returns {object} metadata of CloudError + * + */ + mapper() { + return { + required: false, + serializedName: 'CloudError', + type: { + name: 'Composite', + className: 'CloudError', + modelProperties: { + code: { + required: false, + serializedName: 'code', + type: { + name: 'String' + } + }, + message: { + required: false, + serializedName: 'message', + type: { + name: 'String' + } + }, + target: { + required: false, + serializedName: 'target', + type: { + name: 'String' + } + }, + details: { + required: false, + serializedName: 'details', + type: { + name: 'Sequence', + element: { + required: false, + serializedName: 'CloudErrorElementType', + type: { + name: 'Composite', + className: 'CloudError' + } + } + } + }, + innererror: { + required: false, + serializedName: 'innererror', + type: { + name: 'Object' + } + }, + additionalInfo: { + required: false, + serializedName: 'additionalInfo', + type: { + name: 'Sequence', + element: { + required: false, + serializedName: 'AdditionalInfoElementType', + type: { + name: 'Composite', + className: 'AdditionalInfoElement', + modelProperties: { + type: { + required: true, + serializedName: 'type', + type: { + name: 'String' + } + }, + info: { + required: false, + serializedName: 'info', + type: { + name: 'Object' + } + } + } + } + } + } + } + } + } + }; + } + + /** + * Serialize the instance to CloudError schema + * + * @param {JSON} instance + * + */ + serialize() { + var payload = { error: {} }; + if (this.code) { + payload.error.code = this.code; + } + + if (this.message) { + payload.error.message = this.message; + } + + if (this.target) { + payload.error.target = this.target; + } + + if (this.details) { + var deserializedArray = []; + this.details.forEach(function (element1) { + if (element1) { + element1 = this.serialize(element1); + } + deserializedArray.push(element1); + }); + payload.error.details = deserializedArray; + } + + if (this.innererror) { + payload.error.innererror = JSON.parse(JSON.stringify(this.innererror)); + } + + if (this.additionalInfo) { + payload.error.additionalInfo = JSON.parse(JSON.stringify(this.additionalInfo)); + } + + return payload; + } + + /** + * Deserialize the instance to CloudError schema + * + * @param {JSON} instance + * + */ + deserialize(instance) { + if (instance) { + + if (instance.error) { + + if (instance.error.code) { + this.code = instance.error.code; + } + + if (instance.error.message) { + this.message = instance.error.message; + } + + if (instance.error.target) { + this.target = instance.error.target; + } + + if (instance.error.details) { + var deserializedArray = []; + instance.error.details.forEach(function (element1) { + if (element1) { + element1 = this.deserialize(element1); + } + deserializedArray.push(element1); + }); + this.details = deserializedArray; + } + + if (instance.error.innererror) { + this.innererror = JSON.parse(JSON.stringify(instance.error.innererror)); + } + + if (instance.error.additionalInfo) { + this.additionalInfo = JSON.parse(JSON.stringify(instance.error.additionalInfo)); + } + } + } + return this; + } +} + +module.exports = CloudError; diff --git a/tools/resourceManagement/lib/resource/models/index.js b/tools/resourceManagement/lib/resource/models/index.js index d3190d8dc9..bd292d0bf8 100644 --- a/tools/resourceManagement/lib/resource/models/index.js +++ b/tools/resourceManagement/lib/resource/models/index.js @@ -14,10 +14,8 @@ 'use strict'; -var msRestAzure = require('ms-rest-azure'); - -exports.BaseResource = msRestAzure.BaseResource; -exports.CloudError = msRestAzure.CloudError; +exports.BaseResource = require('./baseResource'); +exports.CloudError = require('./cloudError'); exports.DeploymentExtendedFilter = require('./deploymentExtendedFilter'); exports.GenericResourceFilter = require('./genericResourceFilter'); exports.ResourceGroupFilter = require('./resourceGroupFilter'); diff --git a/tools/resourceManagement/lib/resource/operations/deploymentOperations.js b/tools/resourceManagement/lib/resource/operations/deploymentOperations.js index 278d90b590..e5a2c637fb 100644 --- a/tools/resourceManagement/lib/resource/operations/deploymentOperations.js +++ b/tools/resourceManagement/lib/resource/operations/deploymentOperations.js @@ -11,7 +11,7 @@ 'use strict'; const msRest = require('ms-rest'); -const msRestAzure = require('ms-rest-azure'); +const uuid = require('uuid'); const WebResource = msRest.WebResource; /** @@ -125,7 +125,7 @@ function _get(resourceGroupName, deploymentName, operationId, options, callback) // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; @@ -310,7 +310,7 @@ function _list(resourceGroupName, deploymentName, options, callback) { // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; @@ -438,7 +438,7 @@ function _listNext(nextPageLink, options, callback) { // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; diff --git a/tools/resourceManagement/lib/resource/operations/deployments.js b/tools/resourceManagement/lib/resource/operations/deployments.js index 2232d1ec2a..270b838671 100644 --- a/tools/resourceManagement/lib/resource/operations/deployments.js +++ b/tools/resourceManagement/lib/resource/operations/deployments.js @@ -11,7 +11,7 @@ 'use strict'; const msRest = require('ms-rest'); -const msRestAzure = require('ms-rest-azure'); +const uuid = require('uuid'); const WebResource = msRest.WebResource; @@ -193,7 +193,7 @@ function _checkExistence(resourceGroupName, deploymentName, options, callback) { // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; @@ -492,7 +492,7 @@ function _get(resourceGroupName, deploymentName, options, callback) { // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; @@ -670,7 +670,7 @@ function _cancel(resourceGroupName, deploymentName, options, callback) { // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; @@ -890,7 +890,7 @@ function _validate(resourceGroupName, deploymentName, parameters, options, callb // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; @@ -1097,7 +1097,7 @@ function _exportTemplate(resourceGroupName, deploymentName, options, callback) { // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; @@ -1271,7 +1271,7 @@ function _listByResourceGroup(resourceGroupName, options, callback) { // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; @@ -1455,7 +1455,7 @@ function _beginDeleteMethod(resourceGroupName, deploymentName, options, callback // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; @@ -1677,7 +1677,7 @@ function _beginCreateOrUpdate(resourceGroupName, deploymentName, parameters, opt // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; @@ -1835,7 +1835,7 @@ function _listByResourceGroupNext(nextPageLink, options, callback) { // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; diff --git a/tools/resourceManagement/lib/resource/operations/providers.js b/tools/resourceManagement/lib/resource/operations/providers.js index 46daa81baa..10c0b8f9ec 100644 --- a/tools/resourceManagement/lib/resource/operations/providers.js +++ b/tools/resourceManagement/lib/resource/operations/providers.js @@ -11,7 +11,7 @@ 'use strict'; const msRest = require('ms-rest'); -const msRestAzure = require('ms-rest-azure'); +const uuid = require('uuid'); const WebResource = msRest.WebResource; /** @@ -85,7 +85,7 @@ function _unregister(resourceProviderNamespace, options, callback) { // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; @@ -225,7 +225,7 @@ function _register(resourceProviderNamespace, options, callback) { // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; @@ -380,7 +380,7 @@ function _list(options, callback) { // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; @@ -530,7 +530,7 @@ function _get(resourceProviderNamespace, options, callback) { // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; @@ -657,7 +657,7 @@ function _listNext(nextPageLink, options, callback) { // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; diff --git a/tools/resourceManagement/lib/resource/operations/resourceGroups.js b/tools/resourceManagement/lib/resource/operations/resourceGroups.js index a5dffe088c..4f7248e29e 100644 --- a/tools/resourceManagement/lib/resource/operations/resourceGroups.js +++ b/tools/resourceManagement/lib/resource/operations/resourceGroups.js @@ -11,7 +11,7 @@ 'use strict'; const msRest = require('ms-rest'); -const msRestAzure = require('ms-rest-azure'); +const uuid = require('uuid'); const WebResource = msRest.WebResource; /** @@ -98,7 +98,7 @@ function _checkExistence(resourceGroupName, options, callback) { // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; @@ -255,7 +255,7 @@ function _createOrUpdate(resourceGroupName, parameters, options, callback) { // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; @@ -506,7 +506,7 @@ function _get(resourceGroupName, options, callback) { // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; @@ -678,7 +678,7 @@ function _update(resourceGroupName, parameters, options, callback) { // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; @@ -860,7 +860,7 @@ function _exportTemplate(resourceGroupName, parameters, options, callback) { // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; @@ -1027,7 +1027,7 @@ function _list(options, callback) { // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; @@ -1184,7 +1184,7 @@ function _beginDeleteMethod(resourceGroupName, options, callback) { // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; @@ -1295,7 +1295,7 @@ function _listNext(nextPageLink, options, callback) { // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; diff --git a/tools/resourceManagement/lib/resource/operations/resources.js b/tools/resourceManagement/lib/resource/operations/resources.js index c154fb5b08..7b85d2a5d8 100644 --- a/tools/resourceManagement/lib/resource/operations/resources.js +++ b/tools/resourceManagement/lib/resource/operations/resources.js @@ -10,8 +10,9 @@ 'use strict'; +const uuid = require('uuid'); const msRest = require('ms-rest'); -const msRestAzure = require('ms-rest-azure'); + const WebResource = msRest.WebResource; /** @@ -127,7 +128,7 @@ function _listByResourceGroup(resourceGroupName, options, callback) { // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; @@ -439,7 +440,7 @@ function _list(options, callback) { // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; @@ -620,7 +621,7 @@ function _checkExistence(resourceGroupName, resourceProviderNamespace, parentRes // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; @@ -988,7 +989,7 @@ function _get(resourceGroupName, resourceProviderNamespace, parentResourcePath, // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; @@ -1126,7 +1127,7 @@ function _checkExistenceById(resourceId, apiVersion, options, callback) { // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; @@ -1436,7 +1437,7 @@ function _getById(resourceId, apiVersion, options, callback) { // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; @@ -1604,7 +1605,7 @@ function _beginMoveResources(sourceResourceGroupName, parameters, options, callb // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; @@ -1772,7 +1773,7 @@ function _beginValidateMoveResources(sourceResourceGroupName, parameters, option // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; @@ -1949,7 +1950,7 @@ function _beginDeleteMethod(resourceGroupName, resourceProviderNamespace, parent // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; @@ -2158,7 +2159,7 @@ function _beginCreateOrUpdate(resourceGroupName, resourceProviderNamespace, pare // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; @@ -2327,7 +2328,7 @@ function _beginDeleteById(resourceId, apiVersion, options, callback) { // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; @@ -2494,7 +2495,7 @@ function _beginCreateOrUpdateById(resourceId, apiVersion, parameters, options, c // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; @@ -2652,7 +2653,7 @@ function _listByResourceGroupNext(nextPageLink, options, callback) { // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; @@ -2779,7 +2780,7 @@ function _listNext(nextPageLink, options, callback) { // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; diff --git a/tools/resourceManagement/lib/resource/operations/tags.js b/tools/resourceManagement/lib/resource/operations/tags.js index 28cebfcf69..46b3d53fcb 100644 --- a/tools/resourceManagement/lib/resource/operations/tags.js +++ b/tools/resourceManagement/lib/resource/operations/tags.js @@ -11,7 +11,7 @@ 'use strict'; const msRest = require('ms-rest'); -const msRestAzure = require('ms-rest-azure'); +const uuid = require('uuid'); const WebResource = msRest.WebResource; /** @@ -89,7 +89,7 @@ function _deleteValue(tagName, tagValue, options, callback) { // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; @@ -217,7 +217,7 @@ function _createOrUpdateValue(tagName, tagValue, options, callback) { // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; @@ -377,7 +377,7 @@ function _createOrUpdate(tagName, options, callback) { // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; @@ -534,7 +534,7 @@ function _deleteMethod(tagName, options, callback) { // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; @@ -651,7 +651,7 @@ function _list(options, callback) { // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; @@ -779,7 +779,7 @@ function _listNext(nextPageLink, options, callback) { // Set Headers httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; if (this.client.generateClientRequestId) { - httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid(); + httpRequest.headers['x-ms-client-request-id'] = uuid.v4(); } if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) { httpRequest.headers['accept-language'] = this.client.acceptLanguage; diff --git a/tools/resourceManagement/lib/resource/resourceManagementClient.js b/tools/resourceManagement/lib/resource/resourceManagementClient.js index 9e7be32a70..c39aabef91 100644 --- a/tools/resourceManagement/lib/resource/resourceManagementClient.js +++ b/tools/resourceManagement/lib/resource/resourceManagementClient.js @@ -15,8 +15,9 @@ 'use strict'; const msRest = require('ms-rest'); -const msRestAzure = require('ms-rest-azure'); -const ServiceClient = msRestAzure.AzureServiceClient; +const fs = require('fs'); +const path = require('path'); +const ServiceClient = msRest.ServiceClient; const models = require('./models'); const operations = require('./operations'); @@ -61,15 +62,18 @@ class ResourceManagementClient extends ServiceClient { this.credentials = credentials; this.subscriptionId = subscriptionId; + //Property to store various pieces of information we would finally concat to produce a user-agent header. + this.userAgentInfo = { value: [] }; + let packageInfo = this.getPackageJsonInfo(__dirname); this.addUserAgentInfo(`${packageInfo.name}/${packageInfo.version}`); - if(options.acceptLanguage !== null && options.acceptLanguage !== undefined) { + if (options.acceptLanguage !== null && options.acceptLanguage !== undefined) { this.acceptLanguage = options.acceptLanguage; } - if(options.longRunningOperationRetryTimeout !== null && options.longRunningOperationRetryTimeout !== undefined) { + if (options.longRunningOperationRetryTimeout !== null && options.longRunningOperationRetryTimeout !== undefined) { this.longRunningOperationRetryTimeout = options.longRunningOperationRetryTimeout; } - if(options.generateClientRequestId !== null && options.generateClientRequestId !== undefined) { + if (options.generateClientRequestId !== null && options.generateClientRequestId !== undefined) { this.generateClientRequestId = options.generateClientRequestId; } this.deployments = new operations.Deployments(this); @@ -82,6 +86,54 @@ class ResourceManagementClient extends ServiceClient { msRest.addSerializationMixin(this); } + addUserAgentInfo(additionalUserAgentInfo) { + if (this.userAgentInfo.value.indexOf(additionalUserAgentInfo) === -1) { + this.userAgentInfo.value.push(additionalUserAgentInfo); + } + } + + getPackageJsonInfo(managementClientDir) { + + // algorithm: + // package.json is placed next to the lib directory. So we try to find the lib directory first. + // In most packages we generate via autorest, the management client directly lives in the lib directory + // so, package.json could be found just one level above where management client lives. + // In some packages (azure-arm-resource), management client lives at one level deeper in the lib directory + // so, we have to traverse at least two levels higher to locate package.json. + // The algorithm for locating package.json would then be, start at the current directory where management client lives + // and keep searching up until the file is located. We also limit the search depth to 2, since we know the structure of + // the clients we generate. + + let packageJsonInfo = { + name: 'NO_NAME', + version: '0.0.0' + }; + + // private helper + function _getLibPath(currentDir, searchDepth) { + if (searchDepth < 1) { + return; + } + + // if current directory is lib, return current dir, otherwise search one level up. + return (currentDir.endsWith('lib') || currentDir.endsWith('lib' + path.sep)) ? + currentDir : + _getLibPath(path.join(currentDir, '..'), searchDepth - 1); + } + + let libPath = _getLibPath(managementClientDir, 2); + if (libPath) { + let packageJsonPath = path.join(libPath, '..', 'package.json'); + if (fs.existsSync(packageJsonPath)) { + let data = require(packageJsonPath); + packageJsonInfo.name = data.name; + packageJsonInfo.version = data.version; + } + } + + return packageJsonInfo; + } + } module.exports = ResourceManagementClient; diff --git a/tools/resourceManagement/package.json b/tools/resourceManagement/package.json index 6dd7eb7994..eb98438b53 100644 --- a/tools/resourceManagement/package.json +++ b/tools/resourceManagement/package.json @@ -20,8 +20,7 @@ "types": "./lib/resource.d.ts", "license": "MIT", "dependencies": { - "ms-rest": "^2.0.0", - "ms-rest-azure": "^2.0.0" + "ms-rest": "^2.0.0" }, "homepage": "http://github.com/Azure/azure-sdk-for-node", "repository": { diff --git a/yarn.lock b/yarn.lock index b463b1b274..09bc110e34 100644 --- a/yarn.lock +++ b/yarn.lock @@ -209,6 +209,11 @@ dependencies: tslib "^2.0.0" +"@azure/ms-rest-azure-env@^2.0.0": + version "2.0.0" + resolved "https://registry.yarnpkg.com/@azure/ms-rest-azure-env/-/ms-rest-azure-env-2.0.0.tgz#45809f89763a480924e21d3c620cd40866771625" + integrity sha512-dG76W7ElfLi+fbTjnZVGj+M9e0BIEJmRxU6fHaUQ12bZBe8EJKYb2GV50YWNaP2uJiVQ5+7nXEVj1VN1UQtaEw== + "@azure/ms-rest-js@^1.6.0", "@azure/ms-rest-js@^2.7.0": version "2.7.0" resolved "https://registry.yarnpkg.com/@azure/ms-rest-js/-/ms-rest-js-2.7.0.tgz#8639065577ffdf4946951e1d246334ebfd72d537" @@ -2680,7 +2685,7 @@ acorn@^8.5.0: resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.10.0.tgz#8be5b3907a67221a81ab23c7889c4c5526b62ec5" integrity sha512-F0SAmZ8iUtS//m8DmCTA0jlh6TDKkHQyK6xc6V4KDTyZKA9dnvX9/3sRTVQrWm79glUAZbnmmNcdYwUIHWVybw== -adal-node@0.2.3, adal-node@^0.1.28, adal-node@^0.2.2: +adal-node@0.2.3, adal-node@^0.1.28: version "0.2.3" resolved "https://registry.yarnpkg.com/adal-node/-/adal-node-0.2.3.tgz#87ed3dbed344f6e114e36bf18fe1c4e7d3cc6069" integrity sha512-gMKr8RuYEYvsj7jyfCv/4BfKToQThz20SP71N3AtFn3ia3yAR8Qt2T3aVQhuJzunWs2b38ZsQV0qsZPdwZr7VQ== @@ -3131,7 +3136,7 @@ async-listener@^0.6.0: semver "^5.3.0" shimmer "^1.1.0" -async@3.2.3, async@^1.4.0, async@^1.5.2, async@^2.6.1, async@^2.6.3, async@^2.6.4, async@^3.2.3: +async@3.2.3, async@^1.4.0, async@^1.5.2, async@^2.6.1, async@^2.6.3, async@^3.2.3: version "3.2.3" resolved "https://registry.yarnpkg.com/async/-/async-3.2.3.tgz#ac53dafd3f4720ee9e8a160628f18ea91df196c9" integrity sha512-spZRyzKL5l5BZQrr/6m/SqFdBN0q3OCI0f9rjfBzCMBIP4p75P620rR3gTmaksNOhmzgdxcaxdNfMy6anrbM0g== @@ -9534,18 +9539,7 @@ move-concurrently@^1.0.1: rimraf "^2.5.4" run-queue "^1.0.3" -ms-rest-azure@2.6.2, ms-rest-azure@^2.5.7, ms-rest-azure@^2.6.2: - version "2.6.2" - resolved "https://registry.yarnpkg.com/ms-rest-azure/-/ms-rest-azure-2.6.2.tgz#65a08799adb417678ce5722669bc15ca740b429e" - integrity sha512-h74ezkiMQ1y8tD0kf0MK2kdCONAP70i032Dt8kBrFsvj3ZqGRj+DHHNRCGLDfkBkSXhILGMeiF8Ji/iSCUJLJg== - dependencies: - adal-node "^0.2.2" - async "^2.6.4" - ms-rest "^2.3.2" - request "^2.88.0" - uuid "^3.2.1" - -ms-rest@^2.3.2, ms-rest@^2.3.6: +ms-rest@^2.3.6: version "2.5.4" resolved "https://registry.yarnpkg.com/ms-rest/-/ms-rest-2.5.4.tgz#57b42299cf302e45d5e1a734220bf7d4a110167a" integrity sha512-VeqCbawxRM6nhw0RKNfj7TWL7SL8PB6MypqwgylXCi+u412uvYoyY/kSmO8n06wyd8nIcnTbYToCmSKFMI1mCg==