Skip to content

Commit

Permalink
fix(Enterprise Usage Reports): regen service to get Pager (#170)
Browse files Browse the repository at this point in the history
* fix(Enterprise Usage Reports): regen service to get Pager

This commit contains a re-gen of the service to
leverage the new Pagers emitted by the SDK generator.

Signed-off-by: Phil Adams <[email protected]>
  • Loading branch information
padamstx authored Oct 26, 2022
1 parent 90d7b27 commit e55c8cc
Show file tree
Hide file tree
Showing 5 changed files with 338 additions and 92 deletions.
50 changes: 33 additions & 17 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"files": "package-lock.json|go.sum|^.secrets.baseline$",
"lines": null
},
"generated_at": "2022-03-17T11:35:57Z",
"generated_at": "2022-10-26T12:15:59Z",
"plugins_used": [
{
"name": "AWSKeyDetector"
Expand Down Expand Up @@ -88,7 +88,7 @@
"hashed_secret": "a2190c299b60e882d9fb33736d5e6ab6ffe42708",
"is_secret": false,
"is_verified": false,
"line_number": 1004,
"line_number": 1009,
"type": "Secret Keyword",
"verified_result": null
}
Expand All @@ -98,31 +98,31 @@
"hashed_secret": "aa4ad361672f4c98fd64bf0db80127dd79be59d0",
"is_secret": false,
"is_verified": false,
"line_number": 271,
"line_number": 305,
"type": "Secret Keyword",
"verified_result": null
},
{
"hashed_secret": "0a49d517da94f14c36ac92806c1d15cf95fbca67",
"is_secret": false,
"is_verified": false,
"line_number": 633,
"line_number": 693,
"type": "Secret Keyword",
"verified_result": null
},
{
"hashed_secret": "835e124f126ae02c1c18b3c992a28dde441f5e04",
"is_secret": false,
"is_verified": false,
"line_number": 1803,
"line_number": 1915,
"type": "Secret Keyword",
"verified_result": null
},
{
"hashed_secret": "e058a1c493ad749bd67d368340e9056ed1c2f3ed",
"is_secret": false,
"is_verified": false,
"line_number": 2469,
"line_number": 2736,
"type": "Secret Keyword",
"verified_result": null
}
Expand All @@ -132,7 +132,7 @@
"hashed_secret": "c9208613cbf7c65f191e34c0ecc0a24c7e98c471",
"is_secret": false,
"is_verified": false,
"line_number": 38,
"line_number": 39,
"type": "Hex High Entropy String",
"verified_result": null
}
Expand All @@ -142,7 +142,7 @@
"hashed_secret": "a2190c299b60e882d9fb33736d5e6ab6ffe42708",
"is_secret": false,
"is_verified": false,
"line_number": 1285,
"line_number": 1289,
"type": "Secret Keyword",
"verified_result": null
}
Expand Down Expand Up @@ -198,49 +198,65 @@
"hashed_secret": "f0b0b291c4d22d76db59faaeb94802714dd6d884",
"is_secret": false,
"is_verified": false,
"line_number": 654,
"line_number": 666,
"type": "Hex High Entropy String",
"verified_result": null
},
{
"hashed_secret": "4c41e7a08e8895da0c6066adf33bf389c202b4e2",
"is_secret": false,
"is_verified": false,
"line_number": 962,
"line_number": 1001,
"type": "Hex High Entropy String",
"verified_result": null
},
{
"hashed_secret": "ca8b3e9d1445b3218e3512da63b05c8f26f181e5",
"is_secret": false,
"is_verified": false,
"line_number": 973,
"line_number": 1012,
"type": "Hex High Entropy String",
"verified_result": null
}
],
"test/unit/enterprise-usage-reports.v1.test.js": [
{
"hashed_secret": "953fd8a5aac905b12bf5d8cff3710d345c209632",
"is_secret": false,
"is_verified": false,
"line_number": 214,
"type": "Hex High Entropy String",
"verified_result": null
},
{
"hashed_secret": "a0138a9944cb15ff025195706f98f95d9a544eb6",
"is_secret": false,
"is_verified": false,
"line_number": 214,
"type": "Hex High Entropy String",
"verified_result": null
},
{
"hashed_secret": "5bfabda9d31f1270745654e91a23ca1ff9b26a95",
"is_secret": false,
"is_verified": false,
"line_number": 106,
"line_number": 253,
"type": "Hex High Entropy String",
"verified_result": null
},
{
"hashed_secret": "d06f0318f9eff64a13844ce2fa66cf9fb37a8cad",
"is_secret": false,
"is_verified": false,
"line_number": 107,
"line_number": 254,
"type": "Hex High Entropy String",
"verified_result": null
},
{
"hashed_secret": "820b2eb95f60d828f82a984ce0321feab7868833",
"is_secret": false,
"is_verified": false,
"line_number": 108,
"line_number": 255,
"type": "Hex High Entropy String",
"verified_result": null
}
Expand All @@ -250,21 +266,21 @@
"hashed_secret": "b8473b86d4c2072ca9b08bd28e373e8253e865c4",
"is_secret": false,
"is_verified": false,
"line_number": 736,
"line_number": 880,
"type": "Secret Keyword",
"verified_result": null
},
{
"hashed_secret": "cf4d2385b84329a52ca542285b93d9c4618420df",
"is_secret": false,
"is_verified": false,
"line_number": 2376,
"line_number": 2862,
"type": "Secret Keyword",
"verified_result": null
}
]
},
"version": "0.13.1+ibm.47.dss",
"version": "0.13.1+ibm.55.dss",
"word_list": {
"file": null,
"hash": null
Expand Down
118 changes: 114 additions & 4 deletions enterprise-usage-reports/v1.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* (C) Copyright IBM Corp. 2020.
* (C) Copyright IBM Corp. 2022.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,22 +15,28 @@
*/

/**
* IBM OpenAPI SDK Code Generator Version: 99-SNAPSHOT-629bbb97-20201207-171303
* IBM OpenAPI SDK Code Generator Version: 3.60.0-13f6e1ba-20221019-164457
*/

/* eslint-disable max-classes-per-file */
/* eslint-disable no-await-in-loop */

import * as extend from 'extend';
import { IncomingHttpHeaders, OutgoingHttpHeaders } from 'http';
import {
Authenticator,
BaseService,
getAuthenticatorFromEnvironment,
getMissingParams,
validateParams,
UserOptions,
getQueryParam,
} from 'ibm-cloud-sdk-core';
import { getSdkHeaders } from '../lib/common';

/**
* Usage reports for IBM Cloud enterprise entities
*
* API Version: 1.0.0-beta.1
*/

class EnterpriseUsageReportsV1 extends BaseService {
Expand Down Expand Up @@ -124,6 +130,22 @@ class EnterpriseUsageReportsV1 extends BaseService {
params?: EnterpriseUsageReportsV1.GetResourceUsageReportParams
): Promise<EnterpriseUsageReportsV1.Response<EnterpriseUsageReportsV1.Reports>> {
const _params = { ...params };
const _requiredParams = [];
const _validParams = [
'enterpriseId',
'accountGroupId',
'accountId',
'children',
'month',
'billingUnitId',
'limit',
'offset',
'headers',
];
const _validationErrors = validateParams(_params, _requiredParams, _validParams);
if (_validationErrors) {
return Promise.reject(_validationErrors);
}

const query = {
'enterprise_id': _params.enterpriseId,
Expand Down Expand Up @@ -181,7 +203,7 @@ namespace EnterpriseUsageReportsV1 {
export type Callback<T> = (error: any, response?: Response<T>) => void;

/** The body of a service request that returns no response data. */
export interface Empty {}
export interface EmptyObject {}

/** A standard JS object, defined to avoid the limitations of `Object` and `object` */
export interface JsonObject {
Expand Down Expand Up @@ -333,6 +355,94 @@ namespace EnterpriseUsageReportsV1 {
/** Details about all the resources that are included in the aggregated charges. */
resources: ResourceUsage[];
}

/*************************
* pager classes
************************/

/**
* GetResourceUsageReportPager can be used to simplify the use of getResourceUsageReport().
*/
export class GetResourceUsageReportPager {
protected _hasNext: boolean;

protected pageContext: any;

protected client: EnterpriseUsageReportsV1;

protected params: EnterpriseUsageReportsV1.GetResourceUsageReportParams;

/**
* Construct a GetResourceUsageReportPager object.
*
* @param {EnterpriseUsageReportsV1} client - The service client instance used to invoke getResourceUsageReport()
* @param {Object} [params] - The parameters to be passed to getResourceUsageReport()
* @constructor
* @returns {GetResourceUsageReportPager}
*/
constructor(
client: EnterpriseUsageReportsV1,
params?: EnterpriseUsageReportsV1.GetResourceUsageReportParams
) {
if (params && params.offset) {
throw new Error(`the params.offset field should not be set`);
}

this._hasNext = true;
this.pageContext = { next: undefined };
this.client = client;
this.params = JSON.parse(JSON.stringify(params || {}));
}

/**
* Returns true if there are potentially more results to be retrieved by invoking getNext().
* @returns {boolean}
*/
public hasNext(): boolean {
return this._hasNext;
}

/**
* Returns the next page of results by invoking getResourceUsageReport().
* @returns {Promise<EnterpriseUsageReportsV1.ResourceUsageReport[]>}
*/
public async getNext(): Promise<EnterpriseUsageReportsV1.ResourceUsageReport[]> {
if (!this.hasNext()) {
throw new Error('No more results available');
}

if (this.pageContext.next) {
this.params.offset = this.pageContext.next;
}
const response = await this.client.getResourceUsageReport(this.params);
const { result } = response;

let next = null;
if (result && result.next) {
if (result.next.href) {
next = getQueryParam(result.next.href, 'offset');
}
}
this.pageContext.next = next;
if (!this.pageContext.next) {
this._hasNext = false;
}
return result.reports;
}

/**
* Returns all results by invoking getResourceUsageReport() repeatedly until all pages of results have been retrieved.
* @returns {Promise<EnterpriseUsageReportsV1.ResourceUsageReport[]>}
*/
public async getAll(): Promise<EnterpriseUsageReportsV1.ResourceUsageReport[]> {
const results: ResourceUsageReport[] = [];
while (this.hasNext()) {
const nextPage = await this.getNext();
results.push(...nextPage);
}
return results;
}
}
}

export = EnterpriseUsageReportsV1;
22 changes: 13 additions & 9 deletions examples/enterprise-usage-reports.v1.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @jest-environment node
*/
/**
* (C) Copyright IBM Corp. 2020.
* (C) Copyright IBM Corp. 2020, 2022.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -56,7 +56,7 @@ describe('EnterpriseUsageReportsV1', () => {

// begin-common

const enterpriseUsageReportsService = EnterpriseUsageReportsV1.newInstance({});
const enterpriseUsageReportsService = EnterpriseUsageReportsV1.newInstance();

// end-common

Expand All @@ -65,13 +65,12 @@ describe('EnterpriseUsageReportsV1', () => {
let billingMonth = config.billingMonth;

test('getResourceUsageReport request example', async () => {

consoleLogMock.mockImplementation(output => {
consoleLogMock.mockImplementation((output) => {
originalLog(output);
});
consoleWarnMock.mockImplementation(output => {
consoleWarnMock.mockImplementation((output) => {
// if an error occurs, display the message and then fail the test
originalWarn(output);
// when the test fails we need to print out the error message and stop execution right after it
expect(true).toBeFalsy();
});

Expand All @@ -81,12 +80,17 @@ describe('EnterpriseUsageReportsV1', () => {
const params = {
enterpriseId: enterpriseId,
month: billingMonth,
limit: 10,
};

const allResults = [];
try {
const res = await enterpriseUsageReportsService.getResourceUsageReport(params);
console.log(JSON.stringify(res.result, null, 2));
const pager = new EnterpriseUsageReportsV1.GetResourceUsageReportPager(enterpriseUsageReportsService, params);
while (pager.hasNext()) {
const nextPage = await pager.getNext();
expect(nextPage).not.toBeNull();
allResults.push(...nextPage);
}
console.log(JSON.stringify(allResults, null, 2));
} catch (err) {
console.warn(err);
}
Expand Down
Loading

0 comments on commit e55c8cc

Please sign in to comment.