From a62b6159078784adf9559fb910e3fd0b97f8be9c Mon Sep 17 00:00:00 2001 From: Phil Adams Date: Fri, 22 Jan 2021 13:21:19 -0600 Subject: [PATCH 1/7] fix(IAM Policy Management): re-gen service and add examples after recent API changes --- examples/iam-policy-management.v1.test.js | 317 +++++++++++++++++++++ iam-policy-management/v1.ts | 32 ++- test/unit/iam-policy-management.v1.test.js | 24 +- 3 files changed, 366 insertions(+), 7 deletions(-) create mode 100644 examples/iam-policy-management.v1.test.js diff --git a/examples/iam-policy-management.v1.test.js b/examples/iam-policy-management.v1.test.js new file mode 100644 index 00000000..5056cbc4 --- /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.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..9c06736d 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: 'testString', + value: 'testString', + operator: 'testString', + }; + // 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: 'testString', + value: 'testString', + operator: 'testString', + }; + // PolicyResource const policyResourceModel = { attributes: [resourceAttributeModel], + tags: [resourceTagModel], }; test('should pass the right params to createRequest', () => { From 68ef5fa613aff1eff027ae15ce13b6f498034953 Mon Sep 17 00:00:00 2001 From: Guillermo Cabrera Date: Mon, 1 Feb 2021 16:14:36 -0600 Subject: [PATCH 2/7] fix(IAM Policy Management): fixed examples and integration tests --- examples/iam-policy-management.v1.test.js | 226 +++++++++++++----- .../iam-policy-management.v1.test.js | 6 + 2 files changed, 177 insertions(+), 55 deletions(-) diff --git a/examples/iam-policy-management.v1.test.js b/examples/iam-policy-management.v1.test.js index 5056cbc4..3727aefe 100644 --- a/examples/iam-policy-management.v1.test.js +++ b/examples/iam-policy-management.v1.test.js @@ -22,6 +22,18 @@ const IamPolicyManagementV1 = require('../dist/iam-policy-management/v1'); const { readExternalSources } = require('ibm-cloud-sdk-core'); const authHelper = require('../test/resources/auth-helper.js'); +/** + * Below are examples on how to use IAM Policy Management service + * + * The following environment variables are assumed to be defined when running examples below: + * + * IAM_POLICY_MANAGEMENT_URL=https://iam.cloud.ibm.com + * IAM_POLICY_MANAGEMENT_AUTH_TYPE=iam + * IAM_POLICY_MANAGEMENT_AUTH_URL=https://iam.cloud.ibm.com/identity/token + * IAM_POLICY_MANAGEMENT_APIKEY= + * IAM_POLICY_MANAGEMENT_TEST_ACCOUNT_ID= + */ + // Location of our config file. const configFile = 'iam_policy_management.env'; @@ -37,6 +49,15 @@ const consoleWarnMock = jest.spyOn(console, 'warn'); describe('IamPolicyManagementV1', () => { + let testAccountId; + let testPolicyId; + let testPolicyETag; + let testCustomRoleId; + let testCustomRoleEtag; + const testUniqueId = Math.floor(Math.random() * 100000); + const testIamId = 'IBMid-SDKNode' + testUniqueId; + const testServiceName = 'iam-groups'; + // begin-common const iamPolicyManagementService = IamPolicyManagementV1.newInstance({}); @@ -45,7 +66,13 @@ describe('IamPolicyManagementV1', () => { const config = readExternalSources(IamPolicyManagementV1.DEFAULT_SERVICE_NAME); - test('listPolicies request example', done => { + expect(iamPolicyManagementService).not.toBeNull(); + expect(config).not.toBeNull(); + expect(config).toHaveProperty('testAccountId'); + testAccountId = config.testAccountId; + + test('createPolicy request example', done => { + expect(testAccountId).not.toBeNull(); consoleLogMock.mockImplementation(output => { originalLog(output); @@ -55,23 +82,64 @@ describe('IamPolicyManagementV1', () => { done(output); }); - // begin-list_policies + // begin-create_policy + const policySubjects = [ + { + attributes: [ + { + name: 'iam_id', + value: testIamId, + }, + ], + }, + ]; + const policyRoles = [ + { + role_id: 'crn:v1:bluemix:public:iam::::role:Viewer', + }, + ]; + const policyResourceAccountAttribute = { + name: 'accountId', + value: testAccountId, + operator: 'stringEquals', + }; + const policyResourceServiceAttribute = { + name: 'serviceName', + value: testServiceName, + operator: 'stringEquals', + }; + const policyResourceTag = { + name: 'project', + operator: 'stringEquals', + value: 'moonshoot', + }; + const samplePolicyResources = [ + { + attributes: [policyResourceAccountAttribute, policyResourceServiceAttribute], + tags: [policyResourceTag], + }, + ]; const params = { - accountId: 'testString', + type: 'access', + subjects: policySubjects, + roles: policyRoles, + resources: samplePolicyResources, }; - iamPolicyManagementService.listPolicies(params) + iamPolicyManagementService.createPolicy(params) .then(res => { + testPolicyId = res.result.id; console.log(JSON.stringify(res.result, null, 2)); }) .catch(err => { console.warn(err) }); - // end-list_policies + // end-create_policy }); - test('createPolicy request example', done => { + test('getPolicy request example', done => { + expect(testPolicyId).toBeDefined(); consoleLogMock.mockImplementation(output => { originalLog(output); @@ -81,26 +149,27 @@ describe('IamPolicyManagementV1', () => { done(output); }); - // begin-create_policy + // begin-get_policy const params = { - type: 'testString', - subjects: [{}], - roles: [policyRoleModel], - resources: [{}], + policyId: testPolicyId, }; - iamPolicyManagementService.createPolicy(params) + iamPolicyManagementService.getPolicy(params) .then(res => { + testPolicyETag = res.headers.etag; console.log(JSON.stringify(res.result, null, 2)); }) .catch(err => { console.warn(err) }); - // end-create_policy + // end-get_policy }); test('updatePolicy request example', done => { + expect(testAccountId).not.toBeNull(); + expect(testPolicyId).toBeDefined(); + expect(testPolicyETag).toBeDefined(); consoleLogMock.mockImplementation(output => { originalLog(output); @@ -112,13 +181,43 @@ describe('IamPolicyManagementV1', () => { // begin-update_policy + const policySubjects = [ + { + attributes: [ + { + name: 'iam_id', + value: testIamId, + }, + ], + }, + ]; + const policyResourceAccountAttribute = { + name: 'accountId', + value: testAccountId, + operator: 'stringEquals', + }; + const policyResourceServiceAttribute = { + name: 'serviceName', + value: testServiceName, + operator: 'stringEquals', + }; + const samplePolicyResources = [ + { + attributes: [policyResourceAccountAttribute, policyResourceServiceAttribute], + }, + ]; + const updatedPolicyRoles = [ + { + role_id: 'crn:v1:bluemix:public:iam::::role:Editor', + }, + ]; const params = { - policyId: 'testString', - ifMatch: 'testString', - type: 'testString', - subjects: [{}], - roles: [policyRoleModel], - resources: [{}], + policyId: testPolicyId, + ifMatch: testPolicyETag, + type: 'access', + subjects: policySubjects, + roles: updatedPolicyRoles, + resources: samplePolicyResources, }; iamPolicyManagementService.updatePolicy(params) @@ -131,7 +230,8 @@ describe('IamPolicyManagementV1', () => { // end-update_policy }); - test('getPolicy request example', done => { + test('listPolicies request example', done => { + expect(testAccountId).not.toBeNull(); consoleLogMock.mockImplementation(output => { originalLog(output); @@ -141,13 +241,15 @@ describe('IamPolicyManagementV1', () => { done(output); }); - // begin-get_policy + // begin-list_policies const params = { - policyId: 'testString', + accountId: testAccountId, + iamId: testIamId, + format: 'include_last_permit', }; - iamPolicyManagementService.getPolicy(params) + iamPolicyManagementService.listPolicies(params) .then(res => { console.log(JSON.stringify(res.result, null, 2)); }) @@ -155,9 +257,9 @@ describe('IamPolicyManagementV1', () => { console.warn(err) }); - // end-get_policy + // end-list_policies }); - test('listRoles request example', done => { + test('deletePolicy request example', done => { consoleLogMock.mockImplementation(output => { originalLog(output); @@ -167,9 +269,13 @@ describe('IamPolicyManagementV1', () => { done(output); }); - // begin-list_roles + // begin-delete_policy - iamPolicyManagementService.listRoles({}) + const params = { + policyId: testPolicyId, + }; + + iamPolicyManagementService.deletePolicy(params) .then(res => { console.log(JSON.stringify(res.result, null, 2)); }) @@ -177,9 +283,10 @@ describe('IamPolicyManagementV1', () => { console.warn(err) }); - // end-list_roles - }); + // end-delete_policy + }) test('createRole request example', done => { + expect(testAccountId).not.toBeNull(); consoleLogMock.mockImplementation(output => { originalLog(output); @@ -192,15 +299,16 @@ describe('IamPolicyManagementV1', () => { // begin-create_role const params = { - displayName: 'testString', - actions: ['testString'], - name: 'testString', - accountId: 'testString', - serviceName: 'testString', + displayName: 'IAM Groups read access', + actions: ['iam-groups.groups.read'], + name: 'ExampleRoleIAMGroups', + accountId: testAccountId, + serviceName: testServiceName, }; iamPolicyManagementService.createRole(params) .then(res => { + testCustomRoleId = res.result.id; console.log(JSON.stringify(res.result, null, 2)); }) .catch(err => { @@ -209,7 +317,8 @@ describe('IamPolicyManagementV1', () => { // end-create_role }); - test('updateRole request example', done => { + test('getRole request example', done => { + expect(testCustomRoleId).toBeDefined(); consoleLogMock.mockImplementation(output => { originalLog(output); @@ -219,24 +328,26 @@ describe('IamPolicyManagementV1', () => { done(output); }); - // begin-update_role + // begin-get_role const params = { - roleId: 'testString', - ifMatch: 'testString', + roleId: testCustomRoleId, }; - iamPolicyManagementService.updateRole(params) + iamPolicyManagementService.getRole(params) .then(res => { + testCustomRoleEtag = res.headers.etag; console.log(JSON.stringify(res.result, null, 2)); }) .catch(err => { console.warn(err) }); - // end-update_role + // end-get_role }); - test('getRole request example', done => { + test('updateRole request example', done => { + expect(testCustomRoleId).toBeDefined(); + expect(testCustomRoleEtag).toBeDefined(); consoleLogMock.mockImplementation(output => { originalLog(output); @@ -246,13 +357,16 @@ describe('IamPolicyManagementV1', () => { done(output); }); - // begin-get_role + // begin-update_role + const updatedRoleId = ['iam-groups.groups.read', 'iam-groups.groups.list']; const params = { - roleId: 'testString', + roleId: testCustomRoleId, + ifMatch: testCustomRoleEtag, + actions: updatedRoleId, }; - iamPolicyManagementService.getRole(params) + iamPolicyManagementService.updateRole(params) .then(res => { console.log(JSON.stringify(res.result, null, 2)); }) @@ -260,9 +374,9 @@ describe('IamPolicyManagementV1', () => { console.warn(err) }); - // end-get_role + // end-update_role }); - test('deleteRole request example', done => { + test('listRoles request example', done => { consoleLogMock.mockImplementation(output => { originalLog(output); @@ -272,13 +386,14 @@ describe('IamPolicyManagementV1', () => { done(output); }); - // begin-delete_role + // begin-list_roles const params = { - roleId: 'testString', + accountId: testAccountId, + serviceName: testServiceName, }; - iamPolicyManagementService.deleteRole(params) + iamPolicyManagementService.listRoles(params) .then(res => { console.log(JSON.stringify(res.result, null, 2)); }) @@ -286,9 +401,10 @@ describe('IamPolicyManagementV1', () => { console.warn(err) }); - // end-delete_role + // end-list_roles }); - test('deletePolicy request example', done => { + test('deleteRole request example', done => { + expect(testCustomRoleId).toBeDefined(); consoleLogMock.mockImplementation(output => { originalLog(output); @@ -298,13 +414,13 @@ describe('IamPolicyManagementV1', () => { done(output); }); - // begin-delete_policy + // begin-delete_role const params = { - policyId: 'testString', + roleId: testCustomRoleId, }; - iamPolicyManagementService.deletePolicy(params) + iamPolicyManagementService.deleteRole(params) .then(res => { console.log(JSON.stringify(res.result, null, 2)); }) @@ -312,6 +428,6 @@ describe('IamPolicyManagementV1', () => { console.warn(err) }); - // end-delete_policy + // end-delete_role }); }); diff --git a/test/integration/iam-policy-management.v1.test.js b/test/integration/iam-policy-management.v1.test.js index 5125275c..d28b5472 100644 --- a/test/integration/iam-policy-management.v1.test.js +++ b/test/integration/iam-policy-management.v1.test.js @@ -69,9 +69,15 @@ describe('IamPolicyManagementV1_integration', () => { value: testServiceName, operator: 'stringEquals', }; + const policyResourceTag = { + name: 'project', + operator: 'stringEquals', + value: 'moonshoot', + }; const policyResources = [ { attributes: [policyResourceAccountAttribute, policyResourceServiceAttribute], + tags: [policyResourceTag], }, ]; From ff7f9a622b777e478b39de293859f65f7ebe2bfa Mon Sep 17 00:00:00 2001 From: Guillermo Cabrera Date: Tue, 16 Feb 2021 14:09:28 -0600 Subject: [PATCH 3/7] fix(IAM Policy Management): minor refactor to unify across all languages --- examples/iam-policy-management.v1.test.js | 96 +++++++++---------- .../iam-policy-management.v1.test.js | 13 +-- 2 files changed, 48 insertions(+), 61 deletions(-) diff --git a/examples/iam-policy-management.v1.test.js b/examples/iam-policy-management.v1.test.js index 3727aefe..20166729 100644 --- a/examples/iam-policy-management.v1.test.js +++ b/examples/iam-policy-management.v1.test.js @@ -49,14 +49,13 @@ const consoleWarnMock = jest.spyOn(console, 'warn'); describe('IamPolicyManagementV1', () => { - let testAccountId; - let testPolicyId; - let testPolicyETag; - let testCustomRoleId; - let testCustomRoleEtag; - const testUniqueId = Math.floor(Math.random() * 100000); - const testIamId = 'IBMid-SDKNode' + testUniqueId; - const testServiceName = 'iam-groups'; + let exampleAccountId; + let examplePolicyId; + let examplePolicyETag; + let exampleCustomRoleId; + let exampleCustomRoleEtag; + const exampleUserId = 'IBMid-user1'; + const exampleServiceName = 'iam-groups'; // begin-common @@ -69,10 +68,10 @@ describe('IamPolicyManagementV1', () => { expect(iamPolicyManagementService).not.toBeNull(); expect(config).not.toBeNull(); expect(config).toHaveProperty('testAccountId'); - testAccountId = config.testAccountId; + exampleAccountId = config.testAccountId; test('createPolicy request example', done => { - expect(testAccountId).not.toBeNull(); + expect(exampleAccountId).not.toBeNull(); consoleLogMock.mockImplementation(output => { originalLog(output); @@ -89,7 +88,7 @@ describe('IamPolicyManagementV1', () => { attributes: [ { name: 'iam_id', - value: testIamId, + value: exampleUserId, }, ], }, @@ -101,18 +100,18 @@ describe('IamPolicyManagementV1', () => { ]; const policyResourceAccountAttribute = { name: 'accountId', - value: testAccountId, + value: exampleAccountId, operator: 'stringEquals', }; const policyResourceServiceAttribute = { name: 'serviceName', - value: testServiceName, + value: exampleServiceName, operator: 'stringEquals', }; const policyResourceTag = { name: 'project', operator: 'stringEquals', - value: 'moonshoot', + value: 'prototype', }; const samplePolicyResources = [ { @@ -129,7 +128,7 @@ describe('IamPolicyManagementV1', () => { iamPolicyManagementService.createPolicy(params) .then(res => { - testPolicyId = res.result.id; + examplePolicyId = res.result.id; console.log(JSON.stringify(res.result, null, 2)); }) .catch(err => { @@ -139,7 +138,7 @@ describe('IamPolicyManagementV1', () => { // end-create_policy }); test('getPolicy request example', done => { - expect(testPolicyId).toBeDefined(); + expect(examplePolicyId).toBeDefined(); consoleLogMock.mockImplementation(output => { originalLog(output); @@ -152,12 +151,12 @@ describe('IamPolicyManagementV1', () => { // begin-get_policy const params = { - policyId: testPolicyId, + policyId: examplePolicyId, }; iamPolicyManagementService.getPolicy(params) .then(res => { - testPolicyETag = res.headers.etag; + examplePolicyETag = res.headers.etag; console.log(JSON.stringify(res.result, null, 2)); }) .catch(err => { @@ -167,9 +166,9 @@ describe('IamPolicyManagementV1', () => { // end-get_policy }); test('updatePolicy request example', done => { - expect(testAccountId).not.toBeNull(); - expect(testPolicyId).toBeDefined(); - expect(testPolicyETag).toBeDefined(); + expect(exampleAccountId).not.toBeNull(); + expect(examplePolicyId).toBeDefined(); + expect(examplePolicyETag).toBeDefined(); consoleLogMock.mockImplementation(output => { originalLog(output); @@ -186,19 +185,19 @@ describe('IamPolicyManagementV1', () => { attributes: [ { name: 'iam_id', - value: testIamId, + value: exampleUserId, }, ], }, ]; const policyResourceAccountAttribute = { name: 'accountId', - value: testAccountId, + value: exampleAccountId, operator: 'stringEquals', }; const policyResourceServiceAttribute = { name: 'serviceName', - value: testServiceName, + value: exampleServiceName, operator: 'stringEquals', }; const samplePolicyResources = [ @@ -212,9 +211,9 @@ describe('IamPolicyManagementV1', () => { }, ]; const params = { - policyId: testPolicyId, - ifMatch: testPolicyETag, type: 'access', + policyId: examplePolicyId, + ifMatch: examplePolicyETag, subjects: policySubjects, roles: updatedPolicyRoles, resources: samplePolicyResources, @@ -231,7 +230,7 @@ describe('IamPolicyManagementV1', () => { // end-update_policy }); test('listPolicies request example', done => { - expect(testAccountId).not.toBeNull(); + expect(exampleAccountId).not.toBeNull(); consoleLogMock.mockImplementation(output => { originalLog(output); @@ -244,8 +243,8 @@ describe('IamPolicyManagementV1', () => { // begin-list_policies const params = { - accountId: testAccountId, - iamId: testIamId, + accountId: exampleAccountId, + iamId: exampleUserId, format: 'include_last_permit', }; @@ -272,12 +271,12 @@ describe('IamPolicyManagementV1', () => { // begin-delete_policy const params = { - policyId: testPolicyId, + policyId: examplePolicyId, }; iamPolicyManagementService.deletePolicy(params) .then(res => { - console.log(JSON.stringify(res.result, null, 2)); + console.log(JSON.stringify(res, null, 2)); }) .catch(err => { console.warn(err) @@ -286,7 +285,7 @@ describe('IamPolicyManagementV1', () => { // end-delete_policy }) test('createRole request example', done => { - expect(testAccountId).not.toBeNull(); + expect(exampleAccountId).not.toBeNull(); consoleLogMock.mockImplementation(output => { originalLog(output); @@ -302,13 +301,13 @@ describe('IamPolicyManagementV1', () => { displayName: 'IAM Groups read access', actions: ['iam-groups.groups.read'], name: 'ExampleRoleIAMGroups', - accountId: testAccountId, - serviceName: testServiceName, + accountId: exampleAccountId, + serviceName: exampleServiceName, }; iamPolicyManagementService.createRole(params) .then(res => { - testCustomRoleId = res.result.id; + exampleCustomRoleId = res.result.id; console.log(JSON.stringify(res.result, null, 2)); }) .catch(err => { @@ -318,7 +317,7 @@ describe('IamPolicyManagementV1', () => { // end-create_role }); test('getRole request example', done => { - expect(testCustomRoleId).toBeDefined(); + expect(exampleCustomRoleId).toBeDefined(); consoleLogMock.mockImplementation(output => { originalLog(output); @@ -331,12 +330,12 @@ describe('IamPolicyManagementV1', () => { // begin-get_role const params = { - roleId: testCustomRoleId, + roleId: exampleCustomRoleId, }; iamPolicyManagementService.getRole(params) .then(res => { - testCustomRoleEtag = res.headers.etag; + exampleCustomRoleEtag = res.headers.etag; console.log(JSON.stringify(res.result, null, 2)); }) .catch(err => { @@ -346,8 +345,8 @@ describe('IamPolicyManagementV1', () => { // end-get_role }); test('updateRole request example', done => { - expect(testCustomRoleId).toBeDefined(); - expect(testCustomRoleEtag).toBeDefined(); + expect(exampleCustomRoleId).toBeDefined(); + expect(exampleCustomRoleEtag).toBeDefined(); consoleLogMock.mockImplementation(output => { originalLog(output); @@ -359,11 +358,11 @@ describe('IamPolicyManagementV1', () => { // begin-update_role - const updatedRoleId = ['iam-groups.groups.read', 'iam-groups.groups.list']; + const updatedRoleActions = ['iam-groups.groups.read', 'iam-groups.groups.list']; const params = { - roleId: testCustomRoleId, - ifMatch: testCustomRoleEtag, - actions: updatedRoleId, + roleId: exampleCustomRoleId, + ifMatch: exampleCustomRoleEtag, + actions: updatedRoleActions, }; iamPolicyManagementService.updateRole(params) @@ -389,8 +388,7 @@ describe('IamPolicyManagementV1', () => { // begin-list_roles const params = { - accountId: testAccountId, - serviceName: testServiceName, + accountId: exampleAccountId, }; iamPolicyManagementService.listRoles(params) @@ -404,7 +402,7 @@ describe('IamPolicyManagementV1', () => { // end-list_roles }); test('deleteRole request example', done => { - expect(testCustomRoleId).toBeDefined(); + expect(exampleCustomRoleId).toBeDefined(); consoleLogMock.mockImplementation(output => { originalLog(output); @@ -417,12 +415,12 @@ describe('IamPolicyManagementV1', () => { // begin-delete_role const params = { - roleId: testCustomRoleId, + roleId: exampleCustomRoleId, }; iamPolicyManagementService.deleteRole(params) .then(res => { - console.log(JSON.stringify(res.result, null, 2)); + console.log(JSON.stringify(res, null, 2)); }) .catch(err => { console.warn(err) diff --git a/test/integration/iam-policy-management.v1.test.js b/test/integration/iam-policy-management.v1.test.js index d28b5472..43455da9 100644 --- a/test/integration/iam-policy-management.v1.test.js +++ b/test/integration/iam-policy-management.v1.test.js @@ -72,7 +72,7 @@ describe('IamPolicyManagementV1_integration', () => { const policyResourceTag = { name: 'project', operator: 'stringEquals', - value: 'moonshoot', + value: 'prototype', }; const policyResources = [ { @@ -114,7 +114,6 @@ describe('IamPolicyManagementV1_integration', () => { subjects: policySubjects, roles: policyRoles, resources: policyResources, - headers: { 'transaction-Id': 'SDKNode-' + testUniqueId }, }; let response; @@ -143,7 +142,6 @@ describe('IamPolicyManagementV1_integration', () => { const params = { policyId: testPolicyId, - headers: { 'transaction-Id': 'SDKNode-' + testUniqueId }, }; let response; @@ -185,7 +183,6 @@ describe('IamPolicyManagementV1_integration', () => { subjects: policySubjects, roles: updPolicyRoles, resources: policyResources, - headers: { 'transaction-Id': 'SDKNode-' + testUniqueId }, }; let response; @@ -214,7 +211,6 @@ describe('IamPolicyManagementV1_integration', () => { const params = { accountId: testAccountId, iamId: testUserId, - headers: { 'transaction-Id': 'SDKNode-' + testUniqueId }, }; let response; @@ -248,7 +244,6 @@ describe('IamPolicyManagementV1_integration', () => { const params = { accountId: testAccountId, iamId: testUserId, - headers: { 'transaction-Id': 'SDKNode-' + testUniqueId }, }; let response; @@ -274,7 +269,6 @@ describe('IamPolicyManagementV1_integration', () => { if (policy.id === testPolicyId || createdAt < fiveMinutesAgo) { const params = { policyId: policy.id, - headers: { 'transaction-Id': 'SDKNode-' + testUniqueId }, }; let response; @@ -302,7 +296,6 @@ describe('IamPolicyManagementV1_integration', () => { accountId: testAccountId, serviceName: testServiceName, actions: testCustomRoleActions, - headers: { 'transaction-Id': 'SDKNode-' + testUniqueId }, }; let response; @@ -333,7 +326,6 @@ describe('IamPolicyManagementV1_integration', () => { const params = { roleId: testCustomRoleId, - headers: { 'transaction-Id': 'SDKNode-' + testUniqueId }, }; let response; @@ -401,7 +393,6 @@ describe('IamPolicyManagementV1_integration', () => { const params = { accountId: testAccountId, serviceName: testServiceName, - headers: { 'transaction-Id': 'SDKNode-' + testUniqueId }, }; let response; @@ -435,7 +426,6 @@ describe('IamPolicyManagementV1_integration', () => { const params = { accountId: testAccountId, serviceName: testServiceName, - headers: { 'transaction-Id': 'SDKNode-' + testUniqueId }, }; let response; @@ -461,7 +451,6 @@ describe('IamPolicyManagementV1_integration', () => { if (role.id === testCustomRoleId || createdAt < fiveMinutesAgo) { const params = { roleId: role.id, - headers: { 'transaction-Id': 'SDKNode-' + testUniqueId }, }; let response; From ccf65f5a7647a8392764b37b21ff1b562f7eb92b Mon Sep 17 00:00:00 2001 From: Guillermo Cabrera Date: Tue, 16 Feb 2021 14:29:24 -0600 Subject: [PATCH 4/7] fix(IAM Policy Management): change of variable names for consistency --- examples/iam-policy-management.v1.test.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/examples/iam-policy-management.v1.test.js b/examples/iam-policy-management.v1.test.js index 20166729..5f8a8913 100644 --- a/examples/iam-policy-management.v1.test.js +++ b/examples/iam-policy-management.v1.test.js @@ -98,12 +98,12 @@ describe('IamPolicyManagementV1', () => { role_id: 'crn:v1:bluemix:public:iam::::role:Viewer', }, ]; - const policyResourceAccountAttribute = { + const accountIdResourceAttribute = { name: 'accountId', value: exampleAccountId, operator: 'stringEquals', }; - const policyResourceServiceAttribute = { + const serviceNameResourceAttribute = { name: 'serviceName', value: exampleServiceName, operator: 'stringEquals', @@ -113,9 +113,9 @@ describe('IamPolicyManagementV1', () => { operator: 'stringEquals', value: 'prototype', }; - const samplePolicyResources = [ + const policyResources = [ { - attributes: [policyResourceAccountAttribute, policyResourceServiceAttribute], + attributes: [accountIdResourceAttribute, serviceNameResourceAttribute], tags: [policyResourceTag], }, ]; @@ -123,7 +123,7 @@ describe('IamPolicyManagementV1', () => { type: 'access', subjects: policySubjects, roles: policyRoles, - resources: samplePolicyResources, + resources: policyResources, }; iamPolicyManagementService.createPolicy(params) @@ -190,19 +190,19 @@ describe('IamPolicyManagementV1', () => { ], }, ]; - const policyResourceAccountAttribute = { + const accountIdResourceAttribute = { name: 'accountId', value: exampleAccountId, operator: 'stringEquals', }; - const policyResourceServiceAttribute = { + const serviceNameResourceAttribute = { name: 'serviceName', value: exampleServiceName, operator: 'stringEquals', }; - const samplePolicyResources = [ + const policyResources = [ { - attributes: [policyResourceAccountAttribute, policyResourceServiceAttribute], + attributes: [accountIdResourceAttribute, serviceNameResourceAttribute], }, ]; const updatedPolicyRoles = [ @@ -216,7 +216,7 @@ describe('IamPolicyManagementV1', () => { ifMatch: examplePolicyETag, subjects: policySubjects, roles: updatedPolicyRoles, - resources: samplePolicyResources, + resources: policyResources, }; iamPolicyManagementService.updatePolicy(params) From 975c184d6bdaf12b3e47ae10d0ea3d79ca25653f Mon Sep 17 00:00:00 2001 From: Phil Adams Date: Fri, 19 Feb 2021 11:00:16 -0600 Subject: [PATCH 5/7] chore: fix comments in examples file --- examples/iam-policy-management.v1.test.js | 27 +++++++++++++---------- package-lock.json | 2 +- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/examples/iam-policy-management.v1.test.js b/examples/iam-policy-management.v1.test.js index 5f8a8913..d83da38e 100644 --- a/examples/iam-policy-management.v1.test.js +++ b/examples/iam-policy-management.v1.test.js @@ -22,18 +22,21 @@ const IamPolicyManagementV1 = require('../dist/iam-policy-management/v1'); const { readExternalSources } = require('ibm-cloud-sdk-core'); const authHelper = require('../test/resources/auth-helper.js'); -/** - * Below are examples on how to use IAM Policy Management service - * - * The following environment variables are assumed to be defined when running examples below: - * - * IAM_POLICY_MANAGEMENT_URL=https://iam.cloud.ibm.com - * IAM_POLICY_MANAGEMENT_AUTH_TYPE=iam - * IAM_POLICY_MANAGEMENT_AUTH_URL=https://iam.cloud.ibm.com/identity/token - * IAM_POLICY_MANAGEMENT_APIKEY= - * IAM_POLICY_MANAGEMENT_TEST_ACCOUNT_ID= - */ - +// +// This file provides an example of how to use the IAM Policy Management service. +// +// The following configuration properties are assumed to be defined: +// +// IAM_POLICY_MANAGEMENT_URL=https://iam.cloud.ibm.com +// IAM_POLICY_MANAGEMENT_AUTH_TYPE=iam +// IAM_POLICY_MANAGEMENT_AUTH_URL=https://iam.cloud.ibm.com/identity/token +// IAM_POLICY_MANAGEMENT_APIKEY= +// IAM_POLICY_MANAGEMENT_TEST_ACCOUNT_ID= +// +// These configuration properties can be exported as environment variables, or stored +// in a configuration file and then: +// export IBM_CREDENTIALS_FILE= +// // Location of our config file. const configFile = 'iam_policy_management.env'; diff --git a/package-lock.json b/package-lock.json index 43885e35..eb3d11e2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6,7 +6,7 @@ "packages": { "": { "name": "ibm-platform-services", - "version": "0.17.9", + "version": "0.17.10", "license": "Apache-2.0", "dependencies": { "@types/node": "^12.0.8", From 216206ac76deec7c899d68fa1913790a16815ec7 Mon Sep 17 00:00:00 2001 From: Phil Adams Date: Fri, 19 Feb 2021 13:23:09 -0600 Subject: [PATCH 6/7] chore: one more tweak to comments --- examples/iam-policy-management.v1.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/iam-policy-management.v1.test.js b/examples/iam-policy-management.v1.test.js index d83da38e..fc486b3d 100644 --- a/examples/iam-policy-management.v1.test.js +++ b/examples/iam-policy-management.v1.test.js @@ -27,9 +27,9 @@ const authHelper = require('../test/resources/auth-helper.js'); // // The following configuration properties are assumed to be defined: // -// IAM_POLICY_MANAGEMENT_URL=https://iam.cloud.ibm.com +// IAM_POLICY_MANAGEMENT_URL= // IAM_POLICY_MANAGEMENT_AUTH_TYPE=iam -// IAM_POLICY_MANAGEMENT_AUTH_URL=https://iam.cloud.ibm.com/identity/token +// IAM_POLICY_MANAGEMENT_AUTH_URL= // IAM_POLICY_MANAGEMENT_APIKEY= // IAM_POLICY_MANAGEMENT_TEST_ACCOUNT_ID= // From fda72e1cfbc9f4ce7ec5cf7d5a9ba26d3a9fd31f Mon Sep 17 00:00:00 2001 From: Guillermo Cabrera Date: Fri, 19 Feb 2021 15:32:49 -0600 Subject: [PATCH 7/7] fix(IAM Policy Management): update payload to fix policy creation error --- examples/iam-policy-management.v1.test.js | 14 ++++++++++---- test/integration/iam-policy-management.v1.test.js | 4 ++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/examples/iam-policy-management.v1.test.js b/examples/iam-policy-management.v1.test.js index fc486b3d..e18e566e 100644 --- a/examples/iam-policy-management.v1.test.js +++ b/examples/iam-policy-management.v1.test.js @@ -107,8 +107,8 @@ describe('IamPolicyManagementV1', () => { operator: 'stringEquals', }; const serviceNameResourceAttribute = { - name: 'serviceName', - value: exampleServiceName, + name: 'serviceType', + value: 'service', operator: 'stringEquals', }; const policyResourceTag = { @@ -199,13 +199,19 @@ describe('IamPolicyManagementV1', () => { operator: 'stringEquals', }; const serviceNameResourceAttribute = { - name: 'serviceName', - value: exampleServiceName, + name: 'serviceType', + value: 'service', operator: 'stringEquals', }; + const policyResourceTag = { + name: 'project', + operator: 'stringEquals', + value: 'prototype', + }; const policyResources = [ { attributes: [accountIdResourceAttribute, serviceNameResourceAttribute], + tags: [policyResourceTag], }, ]; const updatedPolicyRoles = [ diff --git a/test/integration/iam-policy-management.v1.test.js b/test/integration/iam-policy-management.v1.test.js index 43455da9..1387a89b 100644 --- a/test/integration/iam-policy-management.v1.test.js +++ b/test/integration/iam-policy-management.v1.test.js @@ -65,8 +65,8 @@ describe('IamPolicyManagementV1_integration', () => { operator: 'stringEquals', }; const policyResourceServiceAttribute = { - name: 'serviceName', - value: testServiceName, + name: 'serviceType', + value: 'service', operator: 'stringEquals', }; const policyResourceTag = {