diff --git a/examples/resource-manager.v2.test.js b/examples/resource-manager.v2.test.js new file mode 100644 index 00000000..a3d74d84 --- /dev/null +++ b/examples/resource-manager.v2.test.js @@ -0,0 +1,258 @@ +/** + * @jest-environment node + */ +/** + * (C) Copyright IBM Corp. 2021. + * + * 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'; + +const ResourceManagerV2 = require('../dist/resource-manager/v2'); +const { readExternalSources } = require('ibm-cloud-sdk-core'); +const authHelper = require('../test/resources/auth-helper.js'); + +// +// This file provides an example of how to use the Resource Manager service. +// +// The following configuration properties are assumed to be defined: +// The following configuration properties are assumed to be defined: +// RESOURCE_MANAGER_URL= +// RESOURCE_MANAGER_AUTH_TYPE=iam +// RESOURCE_MANAGER_APIKEY= +// RESOURCE_MANAGER_AUTH_URL= +// RESOURCE_MANAGER_QUOTA_ID= +// RESOURCE_MANAGER_USER_ACCOUNT_ID= +// +// ALT_RESOURCE_MANAGER_URL= +// ALT_RESOURCE_MANAGER_AUTH_TYPE=iam +// ALT_RESOURCE_MANAGER_AUTH_URL= +// ALT_RESOURCE_MANAGER_APIKEY= +// +// These configuration properties can be exported as environment variables, or stored +// in a configuration file and then: +// export IBM_CREDENTIALS_FILE= +// +const configFile = 'resource_manager.env'; + +const describe = authHelper.prepareTests(configFile); + +// Save original console.log and console.warn +const originalLog = console.log; +const originalWarn = console.warn; + +// Mocks for console.log and console.warn +const consoleLogMock = jest.spyOn(console, 'log'); +const consoleWarnMock = jest.spyOn(console, 'warn'); + +describe('ResourceManagerV2', () => { + + // begin-common + + const resourceManagerService = ResourceManagerV2.newInstance({ serviceName: ResourceManagerV2.DEFAULT_SERVICE_NAME}); + const deleteResourceManagerService = ResourceManagerV2.newInstance( + {serviceName: 'ALT_RESOURCE_MANAGER'} + ); + + // end-common + + const config = readExternalSources(ResourceManagerV2.DEFAULT_SERVICE_NAME); + const exampleUserAccountId = config.userAccountId; + const exampleQuotaId = config.quotaId; + let resourceGroupId = null; + + test('createResourceGroup request example', done => { + + consoleLogMock.mockImplementation(output => { + originalLog(output); + done(); + }); + consoleWarnMock.mockImplementation(output => { + done(output); + }); + + // begin-create_resource_group + const params = { + accountId: exampleUserAccountId, + name: "ExampleGroup" + }; + + resourceManagerService.createResourceGroup(params) + .then(res => { + resourceGroupId = res.result.id; + console.log(JSON.stringify(res.result, null, 2)); + }) + .catch(err => { + console.warn(err) + }); + + // end-create_resource_group + }); + test('getResourceGroup request example', done => { + + consoleLogMock.mockImplementation(output => { + originalLog(output); + done(); + }); + consoleWarnMock.mockImplementation(output => { + done(output); + }); + + // begin-get_resource_group + + const params = { + id: resourceGroupId, + }; + + resourceManagerService.getResourceGroup(params) + .then(res => { + console.log(JSON.stringify(res.result, null, 2)); + }) + .catch(err => { + console.warn(err) + }); + + // end-get_resource_group + }); + test('updateResourceGroup request example', done => { + + consoleLogMock.mockImplementation(output => { + originalLog(output); + done(); + }); + consoleWarnMock.mockImplementation(output => { + done(output); + }); + + // begin-update_resource_group + + const params = { + id: resourceGroupId, + state: 'ACTIVE', + name: 'RenamedExampleGroup' + }; + + resourceManagerService.updateResourceGroup(params) + .then(res => { + console.log(JSON.stringify(res.result, null, 2)); + }) + .catch(err => { + console.warn(err) + }); + + // end-update_resource_group + }); + + test('listResourceGroups request example', done => { + + consoleLogMock.mockImplementation(output => { + originalLog(output); + done(); + }); + consoleWarnMock.mockImplementation(output => { + done(output); + }); + + // begin-list_resource_groups + const params = { + accountId: exampleUserAccountId, + includeDeleted: true, + } + + resourceManagerService.listResourceGroups(params) + .then(res => { + console.log(JSON.stringify(res.result, null, 2)); + }) + .catch(err => { + console.warn(err) + }); + + // end-list_resource_groups + }); + test('deleteResourceGroup request example', done => { + + consoleLogMock.mockImplementation(output => { + originalLog(output); + done(); + }); + consoleWarnMock.mockImplementation(output => { + done(output); + }); + + // begin-delete_resource_group + + const params = { + id: resourceGroupId, + }; + + deleteResourceManagerService.deleteResourceGroup(params) + .then(res => { + console.log(JSON.stringify(res.result, null, 2)); + }) + .catch(err => { + console.warn(err) + }); + + // end-delete_resource_group + }); + + test('listQuotaDefinitions request example', done => { + + consoleLogMock.mockImplementation(output => { + originalLog(output); + done(); + }); + consoleWarnMock.mockImplementation(output => { + done(output); + }); + + // begin-list_quota_definitions + + resourceManagerService.listQuotaDefinitions({}) + .then(res => { + console.log(JSON.stringify(res.result, null, 2)); + }) + .catch(err => { + console.warn(err) + }); + + // end-list_quota_definitions + }); + test('getQuotaDefinition request example', done => { + + consoleLogMock.mockImplementation(output => { + originalLog(output); + done(); + }); + consoleWarnMock.mockImplementation(output => { + done(output); + }); + + // begin-get_quota_definition + + const params = { + id: exampleQuotaId, + }; + + resourceManagerService.getQuotaDefinition(params) + .then(res => { + console.log(JSON.stringify(res.result, null, 2)); + }) + .catch(err => { + console.warn(err) + }); + + // end-get_quota_definition + }); + +}); diff --git a/resource-manager/v2.ts b/resource-manager/v2.ts index 7d31b9a4..06ef0a22 100644 --- a/resource-manager/v2.ts +++ b/resource-manager/v2.ts @@ -1,5 +1,5 @@ /** - * (C) Copyright IBM Corp. 2020. + * (C) Copyright IBM Corp. 2021. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,7 +15,7 @@ */ /** - * IBM OpenAPI SDK Code Generator Version: 99-SNAPSHOT-d753183b-20201209-163011 + * IBM OpenAPI SDK Code Generator Version: 99-SNAPSHOT-4883cbcd-20210301-143711 */ @@ -97,8 +97,11 @@ class ResourceManagerV2 extends BaseService { * * @param {Object} [params] - The parameters to send to the service. * @param {string} [params.accountId] - The ID of the account that contains the resource groups that you want to get. - * @param {string} [params.date] - The date would be in a format of YYYY-MM which returns resource groups exclude the - * deleted ones before this month. + * @param {string} [params.date] - The date in the format of YYYY-MM which returns resource groups. Deleted resource + * groups will be excluded before this month. + * @param {string} [params.name] - The name of the resource group. + * @param {boolean} [params._default] - Boolean value to specify whether or not to list default resource groups. + * @param {boolean} [params.includeDeleted] - Boolean value to specify whether or not to list default resource groups. * @param {OutgoingHttpHeaders} [params.headers] - Custom request headers * @returns {Promise>} */ @@ -107,7 +110,10 @@ class ResourceManagerV2 extends BaseService { const query = { 'account_id': _params.accountId, - 'date': _params.date + 'date': _params.date, + 'name': _params.name, + 'default': _params._default, + 'include_deleted': _params.includeDeleted }; const sdkHeaders = getSdkHeaders(ResourceManagerV2.DEFAULT_SERVICE_NAME, 'v2', 'listResourceGroups'); @@ -406,10 +412,16 @@ namespace ResourceManagerV2 { export interface ListResourceGroupsParams { /** The ID of the account that contains the resource groups that you want to get. */ accountId?: string; - /** The date would be in a format of YYYY-MM which returns resource groups exclude the deleted ones before this - * month. + /** The date in the format of YYYY-MM which returns resource groups. Deleted resource groups will be excluded + * before this month. */ date?: string; + /** The name of the resource group. */ + name?: string; + /** Boolean value to specify whether or not to list default resource groups. */ + _default?: boolean; + /** Boolean value to specify whether or not to list default resource groups. */ + includeDeleted?: boolean; headers?: OutgoingHttpHeaders; } @@ -504,7 +516,7 @@ namespace ResourceManagerV2 { /** An alpha-numeric value identifying the resource group. */ id?: string; /** The full CRN (cloud resource name) associated with the resource group. For more on this format, see [Cloud - * Resource Names](https://cloud.ibm.com/docs/resources?topic=resources-crn). + * Resource Names](https://cloud.ibm.com/docs/account?topic=account-crn). */ crn?: string; } @@ -514,7 +526,7 @@ namespace ResourceManagerV2 { /** An alpha-numeric value identifying the resource group. */ id?: string; /** The full CRN (cloud resource name) associated with the resource group. For more on this format, see [Cloud - * Resource Names](https://cloud.ibm.com/docs/resources?topic=resources-crn). + * Resource Names](https://cloud.ibm.com/docs/account?topic=account-crn). */ crn?: string; /** An alpha-numeric value identifying the account ID. */ @@ -554,7 +566,7 @@ namespace ResourceManagerV2 { /** The human-readable name of the quota. */ resource_id?: string; /** The full CRN (cloud resource name) associated with the quota. For more on this format, see - * https://cloud.ibm.com/docs/resources?topic=resources-crn#crn. + * https://cloud.ibm.com/docs/account?topic=account-crn. */ crn?: string; /** The limit number of this resource. */ diff --git a/test/integration/resource-manager.v2.test.js b/test/integration/resource-manager.v2.test.js index 51a7d399..633a1459 100644 --- a/test/integration/resource-manager.v2.test.js +++ b/test/integration/resource-manager.v2.test.js @@ -18,6 +18,7 @@ 'use strict'; const ResourceManagerV2 = require('../../dist/resource-manager/v2'); const authHelper = require('../resources/auth-helper.js'); +const { readExternalSources } = require('ibm-cloud-sdk-core'); // testcase timeout value (200s). const timeout = 200000; @@ -30,28 +31,52 @@ const describe = authHelper.prepareTests(configFile); describe('ResourceManagerV2_integration', () => { jest.setTimeout(timeout); - let service1; - let service2; - let new_resource_group_id; - const test_quota_id = '7ce89f4a-4381-4600-b814-3cd9a4f4bdf4'; - const test_user_account_id = '60ce10d1d94749bf8dceff12065db1b0'; - - it('should successfully complete initialization', done => { - service1 = ResourceManagerV2.newInstance({ serviceName: 'RMGR1' }); - expect(service1).not.toBeNull(); - - service2 = ResourceManagerV2.newInstance({ serviceName: 'RMGR2' }); - expect(service2).not.toBeNull(); + let resourceManagerService = null; + let deleteResourceManagerService = null; + let newResourceGroupId = null; + let url = null; + let authType = null; + let apiKey = null; + let authUrl = null; + let quotaId = null; + let userAccountId = null; + + beforeAll(done => { + resourceManagerService = ResourceManagerV2.newInstance({ serviceName: ResourceManagerV2.DEFAULT_SERVICE_NAME }); + expect(resourceManagerService).not.toBeNull(); + deleteResourceManagerService = ResourceManagerV2.newInstance({ serviceName: 'ALT_RESOURCE_MANAGER' }); + expect(deleteResourceManagerService).not.toBeNull(); + + const config = readExternalSources(ResourceManagerV2.DEFAULT_SERVICE_NAME); + expect(config).not.toBeNull(); + url = config.url; + authType = config.authType; + apiKey = config.apiKey; + authUrl = config.authUrl; + quotaId = config.quotaId; + userAccountId = config.userAccountId; + + expect(url).not.toBeNull(); + expect(authType).not.toBeNull(); + expect(apiKey).not.toBeNull(); + expect(authUrl).not.toBeNull(); + expect(quotaId).not.toBeNull(); + expect(userAccountId).not.toBeNull(); done(); }); - it('should get a list of all quota definitions', done => { - service1 - .listQuotaDefinitions() + it('should create a new resource group in an account', done => { + const params = { + accountId: userAccountId, + name: 'TestGroup', + }; + resourceManagerService + .createResourceGroup(params) .then(response => { expect(response.hasOwnProperty('status')).toBe(true); - expect(response.status).toBe(200); + expect(response.status).toBe(201); + newResourceGroupId = response.result.id; done(); }) .catch(err => { @@ -59,12 +84,12 @@ describe('ResourceManagerV2_integration', () => { }); }); - it('should get a quota definition by quota id', done => { + it('should get a list of all resource groups in an account', done => { const params = { - id: test_quota_id, + accountId: userAccountId, }; - service1 - .getQuotaDefinition(params) + resourceManagerService + .listResourceGroups(params) .then(response => { expect(response.hasOwnProperty('status')).toBe(true); expect(response.status).toBe(200); @@ -75,12 +100,14 @@ describe('ResourceManagerV2_integration', () => { }); }); - it('should get a list of all resource groups in an account', done => { + it('should update a resource group by id', done => { const params = { - accountId: test_user_account_id, + id: newResourceGroupId, + name: 'TestGroup2', + state: 'ACTIVE', }; - service1 - .listResourceGroups(params) + resourceManagerService + .updateResourceGroup(params) .then(response => { expect(response.hasOwnProperty('status')).toBe(true); expect(response.status).toBe(200); @@ -91,17 +118,15 @@ describe('ResourceManagerV2_integration', () => { }); }); - it('should create a new resource group in an account', done => { + it('should retrieve a resource group by id', done => { const params = { - accountId: test_user_account_id, - name: 'TestGroup', + id: newResourceGroupId, }; - service1 - .createResourceGroup(params) + resourceManagerService + .getResourceGroup(params) .then(response => { expect(response.hasOwnProperty('status')).toBe(true); - expect(response.status).toBe(201); - new_resource_group_id = response.result.id; + expect(response.status).toBe(200); done(); }) .catch(err => { @@ -109,15 +134,15 @@ describe('ResourceManagerV2_integration', () => { }); }); - it('should retrieve a resource group by id', done => { + it('should delete a resource group by id', done => { const params = { - id: new_resource_group_id, + id: newResourceGroupId, }; - service1 - .getResourceGroup(params) + deleteResourceManagerService + .deleteResourceGroup(params) .then(response => { expect(response.hasOwnProperty('status')).toBe(true); - expect(response.status).toBe(200); + expect(response.status).toBe(204); done(); }) .catch(err => { @@ -125,14 +150,9 @@ describe('ResourceManagerV2_integration', () => { }); }); - it('should update a resource group by id', done => { - const params = { - id: new_resource_group_id, - name: 'TestGroup2', - state: 'ACTIVE', - }; - service1 - .updateResourceGroup(params) + it('should get a list of all quota definitions', done => { + resourceManagerService + .listQuotaDefinitions() .then(response => { expect(response.hasOwnProperty('status')).toBe(true); expect(response.status).toBe(200); @@ -143,15 +163,15 @@ describe('ResourceManagerV2_integration', () => { }); }); - it('should delete a resource group by id', done => { + it('should get a quota definition by quota id', done => { const params = { - id: new_resource_group_id, + id: quotaId, }; - service2 - .deleteResourceGroup(params) + resourceManagerService + .getQuotaDefinition(params) .then(response => { expect(response.hasOwnProperty('status')).toBe(true); - expect(response.status).toBe(204); + expect(response.status).toBe(200); done(); }) .catch(err => { diff --git a/test/unit/resource-manager.v2.test.js b/test/unit/resource-manager.v2.test.js index c5441c2b..c422de84 100644 --- a/test/unit/resource-manager.v2.test.js +++ b/test/unit/resource-manager.v2.test.js @@ -1,5 +1,5 @@ /** - * (C) Copyright IBM Corp. 2020. + * (C) Copyright IBM Corp. 2021. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -105,9 +105,15 @@ describe('ResourceManagerV2', () => { // Construct the params object for operation listResourceGroups const accountId = 'testString'; const date = 'testString'; + const name = 'testString'; + const _default = true; + const includeDeleted = true; const params = { accountId: accountId, date: date, + name: name, + _default: _default, + includeDeleted: includeDeleted, }; const listResourceGroupsResult = resourceManagerService.listResourceGroups(params); @@ -126,6 +132,9 @@ describe('ResourceManagerV2', () => { checkMediaHeaders(createRequestMock, expectedAccept, expectedContentType); expect(options.qs['account_id']).toEqual(accountId); expect(options.qs['date']).toEqual(date); + expect(options.qs['name']).toEqual(name); + expect(options.qs['default']).toEqual(_default); + expect(options.qs['include_deleted']).toEqual(includeDeleted); }); test('should prioritize user-given headers', () => {