From 4115068bc271fd7e6fd0c9c6ed57802dcddb8131 Mon Sep 17 00:00:00 2001 From: Shaun-Colley Date: Thu, 18 Mar 2021 12:55:18 -0500 Subject: [PATCH 1/3] fix(IAM Policy Management): updated examples and tests for recent changes in API --- examples/iam-policy-management.v1.test.js | 32 ++++- iam-policy-management/v1.ts | 110 +++++++++++++++--- .../iam-policy-management.v1.test.js | 32 +++++ test/unit/iam-policy-management.v1.test.js | 79 +++++++++++++ 4 files changed, 238 insertions(+), 15 deletions(-) diff --git a/examples/iam-policy-management.v1.test.js b/examples/iam-policy-management.v1.test.js index e18e566e..eb0d6c5c 100644 --- a/examples/iam-policy-management.v1.test.js +++ b/examples/iam-policy-management.v1.test.js @@ -292,7 +292,37 @@ describe('IamPolicyManagementV1', () => { }); // end-delete_policy - }) + }); + test('patchPolicy request example', done => { + expect(examplePolicyId).toBeDefined(); + expect(examplePolicyETag).toBeDefined(); + + consoleLogMock.mockImplementation(output => { + originalLog(output); + done(); + }); + consoleWarnMock.mockImplementation(output => { + done(output); + }); + + // begin-patch_policy + + const params = { + policyId: examplePolicyId, + ifMatch: examplePolicyETag, + state: 'active' + }; + + iamPolicyManagementService.patchPolicy(params) + .then(res => { + console.log(JSON.stringify(res.result, null, 2)); + }) + .catch(err => { + console.warn(err) + }); + + // end-patch_policy + }); test('createRole request example', done => { expect(exampleAccountId).not.toBeNull(); diff --git a/iam-policy-management/v1.ts b/iam-policy-management/v1.ts index ee5b7e59..7d47c06b 100644 --- a/iam-policy-management/v1.ts +++ b/iam-policy-management/v1.ts @@ -15,7 +15,7 @@ */ /** - * IBM OpenAPI SDK Code Generator Version: 99-SNAPSHOT-925238c4-20210122-102634 + * IBM OpenAPI SDK Code Generator Version: 3.29.1-b338fb38-20210313-010605 */ @@ -95,9 +95,9 @@ class IamPolicyManagementV1 extends BaseService { * * 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, 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. + * are supported: account_id, iam_id, access_group_id, type, service_type, sort, format and state. 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. @@ -111,6 +111,7 @@ class IamPolicyManagementV1 extends BaseService { * @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]. + * @param {string} [params.state] - The state of the policy, 'active' or 'deleted'. * @param {OutgoingHttpHeaders} [params.headers] - Custom request headers * @returns {Promise>} */ @@ -132,7 +133,8 @@ class IamPolicyManagementV1 extends BaseService { 'tag_name': _params.tagName, 'tag_value': _params.tagValue, 'sort': _params.sort, - 'format': _params.format + 'format': _params.format, + 'state': _params.state }; const sdkHeaders = getSdkHeaders(IamPolicyManagementV1.DEFAULT_SERVICE_NAME, 'v1', 'listPolicies'); @@ -161,17 +163,23 @@ class IamPolicyManagementV1 extends BaseService { * **authorization**. A policy administrator might want to create an access policy which grants access to a user, * service-id, or an access group. They might also want to create an authorization policy and setup access between * services. - * ### Access To create an access policy, use **`"type": "access"`** in the body. The possible subject attributes are + * + * ### Access + * + * To create an access policy, use **`"type": "access"`** in the body. The possible subject attributes are * **`iam_id`** and **`access_group_id`**. Use the **`iam_id`** subject attribute for assigning access for a user or * service-id. Use the **`access_group_id`** subject attribute for assigning access for an access group. The roles * must be a subset of a service's or the platform's supported roles. The resource attributes must be a subset of a * service's or the platform's supported attributes. The policy resource must include either the **`serviceType`**, * **`serviceName`**, or **`resourceGroupId`** attribute and the **`accountId`** attribute.` If the subject is a * locked service-id, the request will fail. - * ### Authorization Authorization policies are supported by services on a case by case basis. Refer to service - * documentation to verify their support of authorization policies. To create an authorization policy, use **`"type": - * "authorization"`** in the body. The subject attributes must match the supported authorization subjects of the - * resource. Multiple subject attributes might be provided. The following attributes are supported: + * + * ### Authorization + * + * Authorization policies are supported by services on a case by case basis. Refer to service documentation to verify + * their support of authorization policies. To create an authorization policy, use **`"type": "authorization"`** in + * the body. The subject attributes must match the supported authorization subjects of the resource. Multiple subject + * attributes might be provided. The following attributes are supported: * serviceName, serviceInstance, region, resourceType, resource, accountId The policy roles must be a subset of the * supported authorization roles supported by the target service. The user must also have the same level of access or * greater to the target resource in order to grant the role. The resource attributes must be a subset of a service's @@ -230,16 +238,22 @@ class IamPolicyManagementV1 extends BaseService { * * Update a policy to grant access between a subject and a resource. A policy administrator might want to update an * existing policy. The policy type cannot be changed (You cannot change an access policy to an authorization policy). - * ### Access To update an access policy, use **`"type": "access"`** in the body. The possible subject attributes are + * + * ### Access + * + * To update an access policy, use **`"type": "access"`** in the body. The possible subject attributes are * **`iam_id`** and **`access_group_id`**. Use the **`iam_id`** subject attribute for assigning access for a user or * service-id. Use the **`access_group_id`** subject attribute for assigning access for an access group. The roles * must be a subset of a service's or the platform's supported roles. The resource attributes must be a subset of a * service's or the platform's supported attributes. The policy resource must include either the **`serviceType`**, * **`serviceName`**, or **`resourceGroupId`** attribute and the **`accountId`** attribute.` If the subject is a * locked service-id, the request will fail. - * ### Authorization To update an authorization policy, use **`"type": "authorization"`** in the body. The subject - * attributes must match the supported authorization subjects of the resource. Multiple subject attributes might be - * provided. The following attributes are supported: + * + * ### Authorization + * + * To update an authorization policy, use **`"type": "authorization"`** in the body. The subject attributes must match + * the supported authorization subjects of the resource. Multiple subject attributes might be provided. The following + * attributes are supported: * serviceName, serviceInstance, region, resourceType, resource, accountId The policy roles must be a subset of the * supported authorization roles supported by the target service. The user must also have the same level of access or * greater to the target resource in order to grant the role. The resource attributes must be a subset of a service's @@ -383,6 +397,59 @@ class IamPolicyManagementV1 extends BaseService { return this.createRequest(parameters); }; + /** + * Restore a deleted policy by ID. + * + * Restore a policy that has recently been deleted. A policy administrator might want to restore a deleted policy. To + * restore a policy, use **`"state": "active"`** in the body. + * + * @param {Object} params - The parameters to send to the service. + * @param {string} params.policyId - The policy ID. + * @param {string} params.ifMatch - The revision number for updating a policy and must match the ETag value of the + * existing policy. The Etag can be retrieved using the GET /v1/policies/{policy_id} API and looking at the ETag + * response header. + * @param {string} [params.state] - The policy state; either 'active' or 'deleted'. + * @param {OutgoingHttpHeaders} [params.headers] - Custom request headers + * @returns {Promise>} + */ + public patchPolicy(params: IamPolicyManagementV1.PatchPolicyParams): Promise> { + const _params = Object.assign({}, params); + const requiredParams = ['policyId', 'ifMatch']; + + const missingParams = getMissingParams(_params, requiredParams); + if (missingParams) { + return Promise.reject(missingParams); + } + + const body = { + 'state': _params.state + }; + + const path = { + 'policy_id': _params.policyId + }; + + const sdkHeaders = getSdkHeaders(IamPolicyManagementV1.DEFAULT_SERVICE_NAME, 'v1', 'patchPolicy'); + + const parameters = { + options: { + url: '/v1/policies/{policy_id}', + method: 'PATCH', + body, + path, + }, + defaultOptions: extend(true, {}, this.baseOptions, { + headers: extend(true, sdkHeaders, { + 'Accept': 'application/json', + 'Content-Type': 'application/json', + 'If-Match': _params.ifMatch + }, _params.headers), + }), + }; + + return this.createRequest(parameters); + }; + /************************* * roles ************************/ @@ -680,6 +747,8 @@ namespace IamPolicyManagementV1 { sort?: string; /** Include additional data per policy returned [include_last_permit, display]. */ format?: string; + /** The state of the policy, 'active' or 'deleted'. */ + state?: string; headers?: OutgoingHttpHeaders; } @@ -735,6 +804,19 @@ namespace IamPolicyManagementV1 { headers?: OutgoingHttpHeaders; } + /** Parameters for the `patchPolicy` operation. */ + export interface PatchPolicyParams { + /** The policy ID. */ + policyId: string; + /** The revision number for updating a policy and must match the ETag value of the existing policy. The Etag can + * be retrieved using the GET /v1/policies/{policy_id} API and looking at the ETag response header. + */ + ifMatch: string; + /** The policy state; either 'active' or 'deleted'. */ + state?: string; + headers?: OutgoingHttpHeaders; + } + /** Parameters for the `listRoles` operation. */ export interface ListRolesParams { /** Translation language code. */ diff --git a/test/integration/iam-policy-management.v1.test.js b/test/integration/iam-policy-management.v1.test.js index 1387a89b..04ffcbfc 100644 --- a/test/integration/iam-policy-management.v1.test.js +++ b/test/integration/iam-policy-management.v1.test.js @@ -202,6 +202,38 @@ describe('IamPolicyManagementV1_integration', () => { expect(result['roles'][0]['role_id']).toEqual(updPolicyRoles[0]['role_id']); expect(result['resources']).toEqual(policyResources); + testPolicyETag = response.headers.etag; + + done(); + }); + + test('Patch an access policy', async done => { + expect(testPolicyId).toBeDefined(); + expect(testPolicyETag).toBeDefined(); + + const params = { + policyId: testPolicyId, + ifMatch: testPolicyETag, + state: 'active', + }; + + let response; + try { + response = await service.patchPolicy(params); + } catch (err) { + done(err); + } + + expect(response).toBeDefined(); + expect(response.status).toEqual(200); + const { result } = response || {}; + expect(result).toBeDefined(); + expect(result.id).toEqual(testPolicyId); + expect(result['type']).toEqual(policyType); + expect(result['subjects']).toEqual(policySubjects); + expect(result['state']).toEqual('active'); + expect(result['resources']).toEqual(policyResources); + done(); }); diff --git a/test/unit/iam-policy-management.v1.test.js b/test/unit/iam-policy-management.v1.test.js index 9c06736d..2d085bb6 100644 --- a/test/unit/iam-policy-management.v1.test.js +++ b/test/unit/iam-policy-management.v1.test.js @@ -114,6 +114,7 @@ describe('IamPolicyManagementV1', () => { const tagValue = 'testString'; const sort = 'testString'; const format = 'testString'; + const state = 'testString'; const params = { accountId: accountId, acceptLanguage: acceptLanguage, @@ -125,6 +126,7 @@ describe('IamPolicyManagementV1', () => { tagValue: tagValue, sort: sort, format: format, + state: state, }; const listPoliciesResult = iamPolicyManagementService.listPolicies(params); @@ -151,6 +153,7 @@ describe('IamPolicyManagementV1', () => { expect(options.qs['tag_value']).toEqual(tagValue); expect(options.qs['sort']).toEqual(sort); expect(options.qs['format']).toEqual(format); + expect(options.qs['state']).toEqual(state); }); test('should prioritize user-given headers', () => { @@ -592,6 +595,82 @@ describe('IamPolicyManagementV1', () => { }); }); }); + describe('patchPolicy', () => { + describe('positive tests', () => { + test('should pass the right params to createRequest', () => { + // Construct the params object for operation patchPolicy + const policyId = 'testString'; + const ifMatch = 'testString'; + const state = 'testString'; + const params = { + policyId: policyId, + ifMatch: ifMatch, + state: state, + }; + + const patchPolicyResult = iamPolicyManagementService.patchPolicy(params); + + // all methods should return a Promise + expectToBePromise(patchPolicyResult); + + // assert that create request was called + expect(createRequestMock).toHaveBeenCalledTimes(1); + + const options = getOptions(createRequestMock); + + checkUrlAndMethod(options, '/v1/policies/{policy_id}', 'PATCH'); + const expectedAccept = 'application/json'; + const expectedContentType = 'application/json'; + checkMediaHeaders(createRequestMock, expectedAccept, expectedContentType); + checkUserHeader(createRequestMock, 'If-Match', ifMatch); + expect(options.body['state']).toEqual(state); + expect(options.path['policy_id']).toEqual(policyId); + }); + + test('should prioritize user-given headers', () => { + // parameters + const policyId = 'testString'; + const ifMatch = 'testString'; + const userAccept = 'fake/accept'; + const userContentType = 'fake/contentType'; + const params = { + policyId, + ifMatch, + headers: { + Accept: userAccept, + 'Content-Type': userContentType, + }, + }; + + iamPolicyManagementService.patchPolicy(params); + checkMediaHeaders(createRequestMock, userAccept, userContentType); + }); + }); + + describe('negative tests', () => { + test('should enforce required parameters', async done => { + let err; + try { + await iamPolicyManagementService.patchPolicy({}); + } catch (e) { + err = e; + } + + expect(err.message).toMatch(/Missing required parameters/); + done(); + }); + + test('should reject promise when required params are not given', done => { + const patchPolicyPromise = iamPolicyManagementService.patchPolicy(); + expectToBePromise(patchPolicyPromise); + + patchPolicyPromise.catch(err => { + expect(err.message).toMatch(/Missing required parameters/); + done(); + }); + }); + }); + }); describe('listRoles', () => { describe('positive tests', () => { test('should pass the right params to createRequest', () => { From 581bbcb4dbbf530cea81c5560cf08ccc47191fc9 Mon Sep 17 00:00:00 2001 From: Shaun-Colley Date: Mon, 29 Mar 2021 13:41:21 -0500 Subject: [PATCH 2/3] fix(IAM Policy Management):move patch example to after update --- examples/iam-policy-management.v1.test.js | 51 ++++++++++++----------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/examples/iam-policy-management.v1.test.js b/examples/iam-policy-management.v1.test.js index 4d977351..f8633c4f 100644 --- a/examples/iam-policy-management.v1.test.js +++ b/examples/iam-policy-management.v1.test.js @@ -50,7 +50,7 @@ const originalWarn = console.warn; const consoleLogMock = jest.spyOn(console, 'log'); const consoleWarnMock = jest.spyOn(console, 'warn'); -describe('IamPolicyManagementV1', () => { +describe.only('IamPolicyManagementV1', () => { let exampleAccountId; let examplePolicyId; @@ -230,6 +230,7 @@ describe('IamPolicyManagementV1', () => { iamPolicyManagementService.updatePolicy(params) .then(res => { + examplePolicyETag = res.headers.etag; console.log('updatePolicy() result:\n' + JSON.stringify(res.result, null, 2)); }) .catch(err => { @@ -238,8 +239,9 @@ describe('IamPolicyManagementV1', () => { // end-update_policy }); - test('listPolicies request example', done => { - expect(exampleAccountId).not.toBeNull(); + test('patchPolicy request example', done => { + expect(examplePolicyId).toBeDefined(); + expect(examplePolicyETag).toBeDefined(); consoleLogMock.mockImplementation(output => { originalLog(output); @@ -249,25 +251,26 @@ describe('IamPolicyManagementV1', () => { done(output); }); - // begin-list_policies + // begin-patch_policy const params = { - accountId: exampleAccountId, - iamId: exampleUserId, - format: 'include_last_permit', + policyId: examplePolicyId, + ifMatch: examplePolicyETag, + state: 'active' }; - iamPolicyManagementService.listPolicies(params) + iamPolicyManagementService.patchPolicy(params) .then(res => { - console.log('listPolicies() result:\n' + JSON.stringify(res.result, null, 2)); + console.log(JSON.stringify(res.result, null, 2)); }) .catch(err => { console.warn(err) }); - // end-list_policies + // end-patch_policy }); - test('deletePolicy request example', done => { + test('listPolicies request example', done => { + expect(exampleAccountId).not.toBeNull(); consoleLogMock.mockImplementation(output => { originalLog(output); @@ -277,25 +280,25 @@ describe('IamPolicyManagementV1', () => { done(output); }); - // begin-delete_policy + // begin-list_policies const params = { - policyId: examplePolicyId, + accountId: exampleAccountId, + iamId: exampleUserId, + format: 'include_last_permit', }; - iamPolicyManagementService.deletePolicy(params) + iamPolicyManagementService.listPolicies(params) .then(res => { - console.log('deletePolicy() response status code: ' + res.status); + console.log('listPolicies() result:\n' + JSON.stringify(res.result, null, 2)); }) .catch(err => { console.warn(err) }); - // end-delete_policy + // end-list_policies }); - test('patchPolicy request example', done => { - expect(examplePolicyId).toBeDefined(); - expect(examplePolicyETag).toBeDefined(); + test('deletePolicy request example', done => { consoleLogMock.mockImplementation(output => { originalLog(output); @@ -305,23 +308,21 @@ describe('IamPolicyManagementV1', () => { done(output); }); - // begin-patch_policy + // begin-delete_policy const params = { policyId: examplePolicyId, - ifMatch: examplePolicyETag, - state: 'active' }; - iamPolicyManagementService.patchPolicy(params) + iamPolicyManagementService.deletePolicy(params) .then(res => { - console.log(JSON.stringify(res.result, null, 2)); + console.log('deletePolicy() response status code: ' + res.status); }) .catch(err => { console.warn(err) }); - // end-patch_policy + // end-delete_policy }); test('createRole request example', done => { expect(exampleAccountId).not.toBeNull(); From 2272a52878a228c6a534fdd8806e4a91e83225fb Mon Sep 17 00:00:00 2001 From: Shaun-Colley Date: Tue, 6 Apr 2021 08:29:08 -0500 Subject: [PATCH 3/3] fix(IAM Policy Management):fix test case --- examples/iam-policy-management.v1.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/iam-policy-management.v1.test.js b/examples/iam-policy-management.v1.test.js index f8633c4f..fedb20b0 100644 --- a/examples/iam-policy-management.v1.test.js +++ b/examples/iam-policy-management.v1.test.js @@ -50,7 +50,7 @@ const originalWarn = console.warn; const consoleLogMock = jest.spyOn(console, 'log'); const consoleWarnMock = jest.spyOn(console, 'warn'); -describe.only('IamPolicyManagementV1', () => { +describe('IamPolicyManagementV1', () => { let exampleAccountId; let examplePolicyId;