diff --git a/examples/iam-policy-management.v1.test.js b/examples/iam-policy-management.v1.test.js new file mode 100644 index 00000000..87fce144 --- /dev/null +++ b/examples/iam-policy-management.v1.test.js @@ -0,0 +1,317 @@ +/** +* @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 IamPolicyManagementV1 = require('../dist/iam-policy-management/v1'); +const { readExternalSources } = require('ibm-cloud-sdk-core'); +const authHelper = require('../test/resources/auth-helper.js'); + +// Location of our config file. +const configFile = 'iam_policy_management_v1.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('IamPolicyManagementV1', () => { + + // begin-common + + const iamPolicyManagementService = IamPolicyManagementV1.newInstance({}); + + // end-common + + const config = readExternalSources(IamPolicyManagementV1.DEFAULT_SERVICE_NAME); + + test('listPolicies request example', done => { + + consoleLogMock.mockImplementation(output => { + originalLog(output); + done(); + }); + consoleWarnMock.mockImplementation(output => { + done(output); + }); + + // begin-list_policies + + const params = { + accountId: 'testString', + }; + + iamPolicyManagementService.listPolicies(params) + .then(res => { + console.log(JSON.stringify(res.result, null, 2)); + }) + .catch(err => { + console.warn(err) + }); + + // end-list_policies + }); + test('createPolicy request example', done => { + + consoleLogMock.mockImplementation(output => { + originalLog(output); + done(); + }); + consoleWarnMock.mockImplementation(output => { + done(output); + }); + + // begin-create_policy + + const params = { + type: 'testString', + subjects: [ { } ], + roles: [policyRoleModel], + resources: [ { } ], + }; + + iamPolicyManagementService.createPolicy(params) + .then(res => { + console.log(JSON.stringify(res.result, null, 2)); + }) + .catch(err => { + console.warn(err) + }); + + // end-create_policy + }); + test('updatePolicy request example', done => { + + consoleLogMock.mockImplementation(output => { + originalLog(output); + done(); + }); + consoleWarnMock.mockImplementation(output => { + done(output); + }); + + // begin-update_policy + + const params = { + policyId: 'testString', + ifMatch: 'testString', + type: 'testString', + subjects: [ { } ], + roles: [policyRoleModel], + resources: [ { } ], + }; + + iamPolicyManagementService.updatePolicy(params) + .then(res => { + console.log(JSON.stringify(res.result, null, 2)); + }) + .catch(err => { + console.warn(err) + }); + + // end-update_policy + }); + test('getPolicy request example', done => { + + consoleLogMock.mockImplementation(output => { + originalLog(output); + done(); + }); + consoleWarnMock.mockImplementation(output => { + done(output); + }); + + // begin-get_policy + + const params = { + policyId: 'testString', + }; + + iamPolicyManagementService.getPolicy(params) + .then(res => { + console.log(JSON.stringify(res.result, null, 2)); + }) + .catch(err => { + console.warn(err) + }); + + // end-get_policy + }); + test('listRoles request example', done => { + + consoleLogMock.mockImplementation(output => { + originalLog(output); + done(); + }); + consoleWarnMock.mockImplementation(output => { + done(output); + }); + + // begin-list_roles + + iamPolicyManagementService.listRoles({}) + .then(res => { + console.log(JSON.stringify(res.result, null, 2)); + }) + .catch(err => { + console.warn(err) + }); + + // end-list_roles + }); + test('createRole request example', done => { + + consoleLogMock.mockImplementation(output => { + originalLog(output); + done(); + }); + consoleWarnMock.mockImplementation(output => { + done(output); + }); + + // begin-create_role + + const params = { + displayName: 'testString', + actions: ['testString'], + name: 'testString', + accountId: 'testString', + serviceName: 'testString', + }; + + iamPolicyManagementService.createRole(params) + .then(res => { + console.log(JSON.stringify(res.result, null, 2)); + }) + .catch(err => { + console.warn(err) + }); + + // end-create_role + }); + test('updateRole request example', done => { + + consoleLogMock.mockImplementation(output => { + originalLog(output); + done(); + }); + consoleWarnMock.mockImplementation(output => { + done(output); + }); + + // begin-update_role + + const params = { + roleId: 'testString', + ifMatch: 'testString', + }; + + iamPolicyManagementService.updateRole(params) + .then(res => { + console.log(JSON.stringify(res.result, null, 2)); + }) + .catch(err => { + console.warn(err) + }); + + // end-update_role + }); + test('getRole request example', done => { + + consoleLogMock.mockImplementation(output => { + originalLog(output); + done(); + }); + consoleWarnMock.mockImplementation(output => { + done(output); + }); + + // begin-get_role + + const params = { + roleId: 'testString', + }; + + iamPolicyManagementService.getRole(params) + .then(res => { + console.log(JSON.stringify(res.result, null, 2)); + }) + .catch(err => { + console.warn(err) + }); + + // end-get_role + }); + test('deleteRole request example', done => { + + consoleLogMock.mockImplementation(output => { + originalLog(output); + done(); + }); + consoleWarnMock.mockImplementation(output => { + done(output); + }); + + // begin-delete_role + + const params = { + roleId: 'testString', + }; + + iamPolicyManagementService.deleteRole(params) + .then(res => { + console.log(JSON.stringify(res.result, null, 2)); + }) + .catch(err => { + console.warn(err) + }); + + // end-delete_role + }); + test('deletePolicy request example', done => { + + consoleLogMock.mockImplementation(output => { + originalLog(output); + done(); + }); + consoleWarnMock.mockImplementation(output => { + done(output); + }); + + // begin-delete_policy + + const params = { + policyId: 'testString', + }; + + iamPolicyManagementService.deletePolicy(params) + .then(res => { + console.log(JSON.stringify(res.result, null, 2)); + }) + .catch(err => { + console.warn(err) + }); + + // end-delete_policy + }); +}); diff --git a/iam-policy-management/v1.ts b/iam-policy-management/v1.ts index 687a740d..ee5b7e59 100644 --- a/iam-policy-management/v1.ts +++ b/iam-policy-management/v1.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-925238c4-20210122-102634 */ @@ -94,10 +94,10 @@ class IamPolicyManagementV1 extends BaseService { * Get policies by attributes. * * Get policies and filter by attributes. While managing policies, you may want to retrieve policies in the account - * and filter by attribute values. This can be done through query parameters. Currently, we only support the following - * attributes: account_id, iam_id, access_group_id, type, and service_type. account_id is a required query parameter. - * Only policies that have the specified attributes and that the caller has read access to are returned. If the caller - * does not have read access to any policies an empty array is returned. + * and filter by attribute values. This can be done through query parameters. Currently, only the following attributes + * are supported: account_id, iam_id, access_group_id, type, service_type, sort and format. account_id is a required + * query parameter. Only policies that have the specified attributes and that the caller has read access to are + * returned. If the caller does not have read access to any policies an empty array is returned. * * @param {Object} params - The parameters to send to the service. * @param {string} params.accountId - The account GUID in which the policies belong to. @@ -106,6 +106,8 @@ class IamPolicyManagementV1 extends BaseService { * @param {string} [params.accessGroupId] - The access group id. * @param {string} [params.type] - The type of policy (access or authorization). * @param {string} [params.serviceType] - The type of service. + * @param {string} [params.tagName] - The name of the access management tag in the policy. + * @param {string} [params.tagValue] - The value of the access management tag in the policy. * @param {string} [params.sort] - Sort the results by any of the top level policy fields (id, created_at, * created_by_id, last_modified_at, etc). * @param {string} [params.format] - Include additional data per policy returned [include_last_permit, display]. @@ -127,6 +129,8 @@ class IamPolicyManagementV1 extends BaseService { 'access_group_id': _params.accessGroupId, 'type': _params.type, 'service_type': _params.serviceType, + 'tag_name': _params.tagName, + 'tag_value': _params.tagValue, 'sort': _params.sort, 'format': _params.format }; @@ -666,6 +670,10 @@ namespace IamPolicyManagementV1 { type?: string; /** The type of service. */ serviceType?: string; + /** The name of the access management tag in the policy. */ + tagName?: string; + /** The value of the access management tag in the policy. */ + tagValue?: string; /** Sort the results by any of the top level policy fields (id, created_at, created_by_id, last_modified_at, * etc). */ @@ -858,6 +866,8 @@ namespace IamPolicyManagementV1 { export interface PolicyResource { /** List of resource attributes. */ attributes?: ResourceAttribute[]; + /** List of access management tags. */ + tags?: ResourceTag[]; } /** A role associated with a policy. */ @@ -886,6 +896,16 @@ namespace IamPolicyManagementV1 { operator?: string; } + /** A tag associated with a resource. */ + export interface ResourceTag { + /** The name of an access management tag. */ + name: string; + /** The value of an access management tag. */ + value: string; + /** The operator of an access management tag. */ + operator?: string; + } + /** A role resource. */ export interface Role { /** The display name of the role that is shown in the console. */ diff --git a/test/unit/iam-policy-management.v1.test.js b/test/unit/iam-policy-management.v1.test.js index 3a8426fd..bd0159b6 100644 --- a/test/unit/iam-policy-management.v1.test.js +++ b/test/unit/iam-policy-management.v1.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. @@ -110,6 +110,8 @@ describe('IamPolicyManagementV1', () => { const accessGroupId = 'testString'; const type = 'testString'; const serviceType = 'testString'; + const tagName = 'testString'; + const tagValue = 'testString'; const sort = 'testString'; const format = 'testString'; const params = { @@ -119,6 +121,8 @@ describe('IamPolicyManagementV1', () => { accessGroupId: accessGroupId, type: type, serviceType: serviceType, + tagName: tagName, + tagValue: tagValue, sort: sort, format: format, }; @@ -143,6 +147,8 @@ describe('IamPolicyManagementV1', () => { expect(options.qs['access_group_id']).toEqual(accessGroupId); expect(options.qs['type']).toEqual(type); expect(options.qs['service_type']).toEqual(serviceType); + expect(options.qs['tag_name']).toEqual(tagName); + expect(options.qs['tag_value']).toEqual(tagValue); expect(options.qs['sort']).toEqual(sort); expect(options.qs['format']).toEqual(format); }); @@ -216,9 +222,17 @@ describe('IamPolicyManagementV1', () => { operator: 'testString', }; + // ResourceTag + const resourceTagModel = { + name: 'project', + value: 'moonshot', + operator: 'stringEquals', + }; + // PolicyResource const policyResourceModel = { attributes: [resourceAttributeModel], + tags: [resourceTagModel], }; test('should pass the right params to createRequest', () => { @@ -335,9 +349,17 @@ describe('IamPolicyManagementV1', () => { operator: 'testString', }; + // ResourceTag + const resourceTagModel = { + name: 'project', + value: 'moonshot', + operator: 'stringEquals', + }; + // PolicyResource const policyResourceModel = { attributes: [resourceAttributeModel], + tags: [resourceTagModel], }; test('should pass the right params to createRequest', () => {