Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(IAM Policy Management): add restore policy functionality #88

Merged
merged 8 commits into from
Apr 6, 2021
35 changes: 33 additions & 2 deletions examples/iam-policy-management.v1.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
swcolley marked this conversation as resolved.
Show resolved Hide resolved

let exampleAccountId;
let examplePolicyId;
Expand Down Expand Up @@ -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 => {
Expand All @@ -238,6 +239,36 @@ describe('IamPolicyManagementV1', () => {

// end-update_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('listPolicies request example', done => {
expect(exampleAccountId).not.toBeNull();

Expand Down Expand Up @@ -292,7 +323,7 @@ describe('IamPolicyManagementV1', () => {
});

// end-delete_policy
})
});
test('createRole request example', done => {
expect(exampleAccountId).not.toBeNull();

Expand Down
110 changes: 96 additions & 14 deletions iam-policy-management/v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/


Expand Down Expand Up @@ -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.
Expand All @@ -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<IamPolicyManagementV1.Response<IamPolicyManagementV1.PolicyList>>}
*/
Expand All @@ -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');
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<IamPolicyManagementV1.Response<IamPolicyManagementV1.Policy>>}
*/
public patchPolicy(params: IamPolicyManagementV1.PatchPolicyParams): Promise<IamPolicyManagementV1.Response<IamPolicyManagementV1.Policy>> {
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
************************/
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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. */
Expand Down
32 changes: 32 additions & 0 deletions test/integration/iam-policy-management.v1.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand Down
79 changes: 79 additions & 0 deletions test/unit/iam-policy-management.v1.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ describe('IamPolicyManagementV1', () => {
const tagValue = 'testString';
const sort = 'testString';
const format = 'testString';
const state = 'testString';
const params = {
accountId: accountId,
acceptLanguage: acceptLanguage,
Expand All @@ -125,6 +126,7 @@ describe('IamPolicyManagementV1', () => {
tagValue: tagValue,
sort: sort,
format: format,
state: state,
};

const listPoliciesResult = iamPolicyManagementService.listPolicies(params);
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down