@ibm-cloud/platform-services
Version:
Node.js client library for IBM Cloud Platform Services
516 lines • 23.9 kB
JavaScript
"use strict";
/**
* (C) Copyright IBM Corp. 2023.
*
* 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.64.1-cee95189-20230124-211647
*/
/* 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");
/**
* Billing units for IBM Cloud Enterprise
*
* API Version: 1.0.0
*/
var EnterpriseBillingUnitsV1 = /** @class */ (function (_super) {
__extends(EnterpriseBillingUnitsV1, _super);
/**
* Construct a EnterpriseBillingUnitsV1 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 {EnterpriseBillingUnitsV1}
*/
function EnterpriseBillingUnitsV1(options) {
var _this = this;
options = options || {};
_this = _super.call(this, options) || this;
if (options.serviceUrl) {
_this.setServiceUrl(options.serviceUrl);
}
else {
_this.setServiceUrl(EnterpriseBillingUnitsV1.DEFAULT_SERVICE_URL);
}
return _this;
}
/*************************
* Factory method
************************/
/**
* Constructs an instance of EnterpriseBillingUnitsV1 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 {EnterpriseBillingUnitsV1}
*/
EnterpriseBillingUnitsV1.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 EnterpriseBillingUnitsV1(options);
service.configureService(options.serviceName);
if (options.serviceUrl) {
service.setServiceUrl(options.serviceUrl);
}
return service;
};
/*************************
* billingUnits
************************/
/**
* Get billing unit by ID.
*
* Return the billing unit information if it exists.
*
* @param {Object} params - The parameters to send to the service.
* @param {string} params.billingUnitId - The ID of the requested billing unit.
* @param {OutgoingHttpHeaders} [params.headers] - Custom request headers
* @returns {Promise<EnterpriseBillingUnitsV1.Response<EnterpriseBillingUnitsV1.BillingUnit>>}
*/
EnterpriseBillingUnitsV1.prototype.getBillingUnit = function (params) {
var _params = __assign({}, params);
var _requiredParams = ['billingUnitId'];
var _validParams = ['billingUnitId', 'headers'];
var _validationErrors = (0, ibm_cloud_sdk_core_1.validateParams)(_params, _requiredParams, _validParams);
if (_validationErrors) {
return Promise.reject(_validationErrors);
}
var path = {
'billing_unit_id': _params.billingUnitId,
};
var sdkHeaders = (0, common_1.getSdkHeaders)(EnterpriseBillingUnitsV1.DEFAULT_SERVICE_NAME, 'v1', 'getBillingUnit');
var parameters = {
options: {
url: '/v1/billing-units/{billing_unit_id}',
method: 'GET',
path: path,
},
defaultOptions: extend(true, {}, this.baseOptions, {
headers: extend(true, sdkHeaders, {
'Accept': 'application/json',
}, _params.headers),
}),
};
return this.createRequest(parameters);
};
/**
* List billing units.
*
* Return matching billing unit information if any exists. Omits internal properties and enterprise account ID from
* the billing unit.
*
* @param {Object} [params] - The parameters to send to the service.
* @param {string} [params.accountId] - The enterprise account ID.
* @param {string} [params.enterpriseId] - The enterprise ID.
* @param {string} [params.accountGroupId] - The account group ID.
* @param {number} [params.limit] - Return results up to this limit. Valid values are between 0 and 100.
* @param {string} [params.start] - The pagination offset. This represents the index of the first returned result.
* @param {OutgoingHttpHeaders} [params.headers] - Custom request headers
* @returns {Promise<EnterpriseBillingUnitsV1.Response<EnterpriseBillingUnitsV1.BillingUnitsList>>}
*/
EnterpriseBillingUnitsV1.prototype.listBillingUnits = function (params) {
var _params = __assign({}, params);
var _requiredParams = [];
var _validParams = [
'accountId',
'enterpriseId',
'accountGroupId',
'limit',
'start',
'headers',
];
var _validationErrors = (0, ibm_cloud_sdk_core_1.validateParams)(_params, _requiredParams, _validParams);
if (_validationErrors) {
return Promise.reject(_validationErrors);
}
var query = {
'account_id': _params.accountId,
'enterprise_id': _params.enterpriseId,
'account_group_id': _params.accountGroupId,
'limit': _params.limit,
'start': _params.start,
};
var sdkHeaders = (0, common_1.getSdkHeaders)(EnterpriseBillingUnitsV1.DEFAULT_SERVICE_NAME, 'v1', 'listBillingUnits');
var parameters = {
options: {
url: '/v1/billing-units',
method: 'GET',
qs: query,
},
defaultOptions: extend(true, {}, this.baseOptions, {
headers: extend(true, sdkHeaders, {
'Accept': 'application/json',
}, _params.headers),
}),
};
return this.createRequest(parameters);
};
/*************************
* billingOptions
************************/
/**
* List billing options.
*
* Return matching billing options if any exist. Show subscriptions and promotional offers that are available to a
* billing unit.
*
* @param {Object} params - The parameters to send to the service.
* @param {string} params.billingUnitId - The billing unit ID.
* @param {number} [params.limit] - Return results up to this limit. Valid values are between 0 and 100.
* @param {string} [params.start] - The pagination offset. This represents the index of the first returned result.
* @param {OutgoingHttpHeaders} [params.headers] - Custom request headers
* @returns {Promise<EnterpriseBillingUnitsV1.Response<EnterpriseBillingUnitsV1.BillingOptionsList>>}
*/
EnterpriseBillingUnitsV1.prototype.listBillingOptions = function (params) {
var _params = __assign({}, params);
var _requiredParams = ['billingUnitId'];
var _validParams = ['billingUnitId', 'limit', 'start', 'headers'];
var _validationErrors = (0, ibm_cloud_sdk_core_1.validateParams)(_params, _requiredParams, _validParams);
if (_validationErrors) {
return Promise.reject(_validationErrors);
}
var query = {
'billing_unit_id': _params.billingUnitId,
'limit': _params.limit,
'start': _params.start,
};
var sdkHeaders = (0, common_1.getSdkHeaders)(EnterpriseBillingUnitsV1.DEFAULT_SERVICE_NAME, 'v1', 'listBillingOptions');
var parameters = {
options: {
url: '/v1/billing-options',
method: 'GET',
qs: query,
},
defaultOptions: extend(true, {}, this.baseOptions, {
headers: extend(true, sdkHeaders, {
'Accept': 'application/json',
}, _params.headers),
}),
};
return this.createRequest(parameters);
};
/*************************
* creditPools
************************/
/**
* Get credit pools.
*
* Get credit pools for a billing unit. Credit pools can be either platform or support credit pools. The platform
* credit pool contains credit from platform subscriptions and promotional offers. The support credit pool contains
* credit from support subscriptions.
*
* @param {Object} params - The parameters to send to the service.
* @param {string} params.billingUnitId - The ID of the billing unit.
* @param {string} [params.date] - The date in the format of YYYY-MM.
* @param {string} [params.type] - Filters the credit pool by type, either `PLATFORM` or `SUPPORT`.
* @param {number} [params.limit] - Return results up to this limit. Valid values are between 0 and 100.
* @param {string} [params.start] - The pagination offset. This represents the index of the first returned result.
* @param {OutgoingHttpHeaders} [params.headers] - Custom request headers
* @returns {Promise<EnterpriseBillingUnitsV1.Response<EnterpriseBillingUnitsV1.CreditPoolsList>>}
*/
EnterpriseBillingUnitsV1.prototype.getCreditPools = function (params) {
var _params = __assign({}, params);
var _requiredParams = ['billingUnitId'];
var _validParams = ['billingUnitId', 'date', 'type', 'limit', 'start', 'headers'];
var _validationErrors = (0, ibm_cloud_sdk_core_1.validateParams)(_params, _requiredParams, _validParams);
if (_validationErrors) {
return Promise.reject(_validationErrors);
}
var query = {
'billing_unit_id': _params.billingUnitId,
'date': _params.date,
'type': _params.type,
'limit': _params.limit,
'start': _params.start,
};
var sdkHeaders = (0, common_1.getSdkHeaders)(EnterpriseBillingUnitsV1.DEFAULT_SERVICE_NAME, 'v1', 'getCreditPools');
var parameters = {
options: {
url: '/v1/credit-pools',
method: 'GET',
qs: query,
},
defaultOptions: extend(true, {}, this.baseOptions, {
headers: extend(true, sdkHeaders, {
'Accept': 'application/json',
}, _params.headers),
}),
};
return this.createRequest(parameters);
};
EnterpriseBillingUnitsV1.DEFAULT_SERVICE_URL = 'https://billing.cloud.ibm.com';
EnterpriseBillingUnitsV1.DEFAULT_SERVICE_NAME = 'enterprise_billing_units';
return EnterpriseBillingUnitsV1;
}(ibm_cloud_sdk_core_1.BaseService));
/*************************
* interfaces
************************/
(function (EnterpriseBillingUnitsV1) {
/*************************
* pager classes
************************/
/**
* BillingUnitsPager can be used to simplify the use of listBillingUnits().
*/
var BillingUnitsPager = /** @class */ (function () {
/**
* Construct a BillingUnitsPager object.
*
* @param {EnterpriseBillingUnitsV1} client - The service client instance used to invoke listBillingUnits()
* @param {Object} [params] - The parameters to be passed to listBillingUnits()
* @constructor
* @returns {BillingUnitsPager}
*/
function BillingUnitsPager(client, params) {
if (params && params.start) {
throw new Error("the params.start 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}
*/
BillingUnitsPager.prototype.hasNext = function () {
return this._hasNext;
};
/**
* Returns the next page of results by invoking listBillingUnits().
* @returns {Promise<EnterpriseBillingUnitsV1.BillingUnit[]>}
*/
BillingUnitsPager.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.start = this.pageContext.next;
}
return [4 /*yield*/, this.client.listBillingUnits(this.params)];
case 1:
response = _a.sent();
result = response.result;
next = null;
if (result && result.next_url) {
next = (0, ibm_cloud_sdk_core_1.getQueryParam)(result.next_url, 'start');
}
this.pageContext.next = next;
if (!this.pageContext.next) {
this._hasNext = false;
}
return [2 /*return*/, result.resources];
}
});
});
};
/**
* Returns all results by invoking listBillingUnits() repeatedly until all pages of results have been retrieved.
* @returns {Promise<EnterpriseBillingUnitsV1.BillingUnit[]>}
*/
BillingUnitsPager.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 BillingUnitsPager;
}());
EnterpriseBillingUnitsV1.BillingUnitsPager = BillingUnitsPager;
/**
* BillingOptionsPager can be used to simplify the use of listBillingOptions().
*/
var BillingOptionsPager = /** @class */ (function () {
/**
* Construct a BillingOptionsPager object.
*
* @param {EnterpriseBillingUnitsV1} client - The service client instance used to invoke listBillingOptions()
* @param {Object} params - The parameters to be passed to listBillingOptions()
* @constructor
* @returns {BillingOptionsPager}
*/
function BillingOptionsPager(client, params) {
if (params && params.start) {
throw new Error("the params.start 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}
*/
BillingOptionsPager.prototype.hasNext = function () {
return this._hasNext;
};
/**
* Returns the next page of results by invoking listBillingOptions().
* @returns {Promise<EnterpriseBillingUnitsV1.BillingOption[]>}
*/
BillingOptionsPager.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.start = this.pageContext.next;
}
return [4 /*yield*/, this.client.listBillingOptions(this.params)];
case 1:
response = _a.sent();
result = response.result;
next = null;
if (result && result.next_url) {
next = (0, ibm_cloud_sdk_core_1.getQueryParam)(result.next_url, 'start');
}
this.pageContext.next = next;
if (!this.pageContext.next) {
this._hasNext = false;
}
return [2 /*return*/, result.resources];
}
});
});
};
/**
* Returns all results by invoking listBillingOptions() repeatedly until all pages of results have been retrieved.
* @returns {Promise<EnterpriseBillingUnitsV1.BillingOption[]>}
*/
BillingOptionsPager.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 BillingOptionsPager;
}());
EnterpriseBillingUnitsV1.BillingOptionsPager = BillingOptionsPager;
})(EnterpriseBillingUnitsV1 || (EnterpriseBillingUnitsV1 = {}));
module.exports = EnterpriseBillingUnitsV1;
//# sourceMappingURL=v1.js.map