@ibm-cloud/platform-services
Version:
Node.js client library for IBM Cloud Platform Services
318 lines • 15.5 kB
JavaScript
;
/**
* (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.
* 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.
*/
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
/**
* IBM OpenAPI SDK Code Generator Version: 3.60.0-13f6e1ba-20221019-164457
*/
/* eslint-disable max-classes-per-file */
/* eslint-disable no-await-in-loop */
var extend = require("extend");
var ibm_cloud_sdk_core_1 = require("ibm-cloud-sdk-core");
var common_1 = require("../lib/common");
/**
* Usage reports for IBM Cloud enterprise entities
*
* API Version: 1.0.0-beta.1
*/
var EnterpriseUsageReportsV1 = /** @class */ (function (_super) {
__extends(EnterpriseUsageReportsV1, _super);
/**
* Construct a EnterpriseUsageReportsV1 object.
*
* @param {Object} options - Options for the service.
* @param {string} [options.serviceUrl] - The base url to use when contacting the service. The base url may differ between IBM Cloud regions.
* @param {OutgoingHttpHeaders} [options.headers] - Default headers that shall be included with every request to the service.
* @param {Authenticator} options.authenticator - The Authenticator object used to authenticate requests to the service
* @constructor
* @returns {EnterpriseUsageReportsV1}
*/
function EnterpriseUsageReportsV1(options) {
var _this = this;
options = options || {};
_this = _super.call(this, options) || this;
if (options.serviceUrl) {
_this.setServiceUrl(options.serviceUrl);
}
else {
_this.setServiceUrl(EnterpriseUsageReportsV1.DEFAULT_SERVICE_URL);
}
return _this;
}
/*************************
* Factory method
************************/
/**
* Constructs an instance of EnterpriseUsageReportsV1 with passed in options and external configuration.
*
* @param {UserOptions} [options] - The parameters to send to the service.
* @param {string} [options.serviceName] - The name of the service to configure
* @param {Authenticator} [options.authenticator] - The Authenticator object used to authenticate requests to the service
* @param {string} [options.serviceUrl] - The URL for the service
* @returns {EnterpriseUsageReportsV1}
*/
EnterpriseUsageReportsV1.newInstance = function (options) {
options = options || {};
if (!options.serviceName) {
options.serviceName = this.DEFAULT_SERVICE_NAME;
}
if (!options.authenticator) {
options.authenticator = (0, ibm_cloud_sdk_core_1.getAuthenticatorFromEnvironment)(options.serviceName);
}
var service = new EnterpriseUsageReportsV1(options);
service.configureService(options.serviceName);
if (options.serviceUrl) {
service.setServiceUrl(options.serviceUrl);
}
return service;
};
/*************************
* enterpriseUsageReports
************************/
/**
* Get usage reports for enterprise entities.
*
* Usage reports for entities in the IBM Cloud enterprise. These entities can be the enterprise, an account group, or
* an account.
*
* @param {Object} [params] - The parameters to send to the service.
* @param {string} [params.enterpriseId] - The ID of the enterprise for which the reports are queried. This parameter
* cannot be used with the `account_id` or `account_group_id` query parameters.
* @param {string} [params.accountGroupId] - The ID of the account group for which the reports are queried. This
* parameter cannot be used with the `account_id` or `enterprise_id` query parameters.
* @param {string} [params.accountId] - The ID of the account for which the reports are queried. This parameter cannot
* be used with the `account_group_id` or `enterprise_id` query parameters.
* @param {boolean} [params.children] - Returns the reports for the immediate child entities under the current account
* group or enterprise. This parameter cannot be used with the `account_id` query parameter.
* @param {string} [params.month] - The billing month for which the usage report is requested. The format is in
* yyyy-mm. Defaults to the month in which the report is queried.
* @param {string} [params.billingUnitId] - The ID of the billing unit by which to filter the reports.
* @param {number} [params.limit] - The maximum number of search results to be returned.
* @param {string} [params.offset] - An opaque value representing the offset of the first item to be returned by a
* search query. If not specified, then the first page of results is returned. To retrieve the next page of search
* results, use the 'offset' query parameter value within the 'next.href' URL found within a prior search query
* response.
* @param {OutgoingHttpHeaders} [params.headers] - Custom request headers
* @returns {Promise<EnterpriseUsageReportsV1.Response<EnterpriseUsageReportsV1.Reports>>}
*/
EnterpriseUsageReportsV1.prototype.getResourceUsageReport = function (params) {
var _params = __assign({}, params);
var _requiredParams = [];
var _validParams = [
'enterpriseId',
'accountGroupId',
'accountId',
'children',
'month',
'billingUnitId',
'limit',
'offset',
'headers',
];
var _validationErrors = (0, ibm_cloud_sdk_core_1.validateParams)(_params, _requiredParams, _validParams);
if (_validationErrors) {
return Promise.reject(_validationErrors);
}
var query = {
'enterprise_id': _params.enterpriseId,
'account_group_id': _params.accountGroupId,
'account_id': _params.accountId,
'children': _params.children,
'month': _params.month,
'billing_unit_id': _params.billingUnitId,
'limit': _params.limit,
'offset': _params.offset,
};
var sdkHeaders = (0, common_1.getSdkHeaders)(EnterpriseUsageReportsV1.DEFAULT_SERVICE_NAME, 'v1', 'getResourceUsageReport');
var parameters = {
options: {
url: '/v1/resource-usage-reports',
method: 'GET',
qs: query,
},
defaultOptions: extend(true, {}, this.baseOptions, {
headers: extend(true, sdkHeaders, {
'Accept': 'application/json',
}, _params.headers),
}),
};
return this.createRequest(parameters);
};
EnterpriseUsageReportsV1.DEFAULT_SERVICE_URL = 'https://enterprise.cloud.ibm.com';
EnterpriseUsageReportsV1.DEFAULT_SERVICE_NAME = 'enterprise_usage_reports';
return EnterpriseUsageReportsV1;
}(ibm_cloud_sdk_core_1.BaseService));
/*************************
* interfaces
************************/
(function (EnterpriseUsageReportsV1) {
/*************************
* pager classes
************************/
/**
* GetResourceUsageReportPager can be used to simplify the use of getResourceUsageReport().
*/
var GetResourceUsageReportPager = /** @class */ (function () {
/**
* 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}
*/
function GetResourceUsageReportPager(client, params) {
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}
*/
GetResourceUsageReportPager.prototype.hasNext = function () {
return this._hasNext;
};
/**
* Returns the next page of results by invoking getResourceUsageReport().
* @returns {Promise<EnterpriseUsageReportsV1.ResourceUsageReport[]>}
*/
GetResourceUsageReportPager.prototype.getNext = function () {
return __awaiter(this, void 0, void 0, function () {
var response, result, next;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!this.hasNext()) {
throw new Error('No more results available');
}
if (this.pageContext.next) {
this.params.offset = this.pageContext.next;
}
return [4 /*yield*/, this.client.getResourceUsageReport(this.params)];
case 1:
response = _a.sent();
result = response.result;
next = null;
if (result && result.next) {
if (result.next.href) {
next = (0, ibm_cloud_sdk_core_1.getQueryParam)(result.next.href, 'offset');
}
}
this.pageContext.next = next;
if (!this.pageContext.next) {
this._hasNext = false;
}
return [2 /*return*/, result.reports];
}
});
});
};
/**
* Returns all results by invoking getResourceUsageReport() repeatedly until all pages of results have been retrieved.
* @returns {Promise<EnterpriseUsageReportsV1.ResourceUsageReport[]>}
*/
GetResourceUsageReportPager.prototype.getAll = function () {
return __awaiter(this, void 0, void 0, function () {
var results, nextPage;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
results = [];
_a.label = 1;
case 1:
if (!this.hasNext()) return [3 /*break*/, 3];
return [4 /*yield*/, this.getNext()];
case 2:
nextPage = _a.sent();
results.push.apply(results, nextPage);
return [3 /*break*/, 1];
case 3: return [2 /*return*/, results];
}
});
});
};
return GetResourceUsageReportPager;
}());
EnterpriseUsageReportsV1.GetResourceUsageReportPager = GetResourceUsageReportPager;
})(EnterpriseUsageReportsV1 || (EnterpriseUsageReportsV1 = {}));
module.exports = EnterpriseUsageReportsV1;
//# sourceMappingURL=v1.js.map