diff --git a/README.md b/README.md index 8846bd4..1020990 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,7 @@ IBM Cloud services: Service Name | Import Path --- | --- +[Account Management](https://test.cloud.ibm.com/apidocs/account-management) | @ibm-cloud/platform-services/account-management/v4 [Case Management](https://cloud.ibm.com/apidocs/case-management?code=node) | @ibm-cloud/platform-services/case-management/v1 [Catalog Management](https://cloud.ibm.com/apidocs/resource-catalog/private-catalog?code=node) | @ibm-cloud/platform-services/catalog-management/v1 [Context Based Restrictions](https://cloud.ibm.com/apidocs/context-based-restrictions?code=node) | @ibm-cloud/platform-services/context-based-restrictions/v1 diff --git a/account-management/v4.ts b/account-management/v4.ts new file mode 100644 index 0000000..0831928 --- /dev/null +++ b/account-management/v4.ts @@ -0,0 +1,222 @@ +/** + * (C) Copyright IBM Corp. 2026. + * + * 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. + */ + +/** + * IBM OpenAPI SDK Code Generator Version: 3.111.0-1bfb72c2-20260206-185521 + */ + +import * as extend from 'extend'; +import { IncomingHttpHeaders, OutgoingHttpHeaders } from 'http'; +import { + AbortSignal, + Authenticator, + BaseService, + UserOptions, + getAuthenticatorFromEnvironment, + validateParams, +} from 'ibm-cloud-sdk-core'; +import { getSdkHeaders } from '../lib/common'; + +/** + * The Account Management API allows for the management of Account + * + * API Version: 4.0.0 + */ + +class AccountManagementV4 extends BaseService { + static DEFAULT_SERVICE_URL: string = 'https://accounts.test.cloud.ibm.com'; + + static DEFAULT_SERVICE_NAME: string = 'account_management'; + + /************************* + * Factory method + ************************/ + + /** + * Constructs an instance of AccountManagementV4 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 base URL for the service + * @returns {AccountManagementV4} + */ + + public static newInstance(options: UserOptions): AccountManagementV4 { + options = options || {}; + + if (!options.serviceName) { + options.serviceName = this.DEFAULT_SERVICE_NAME; + } + if (!options.authenticator) { + options.authenticator = getAuthenticatorFromEnvironment(options.serviceName); + } + const service = new AccountManagementV4(options); + service.configureService(options.serviceName); + if (options.serviceUrl) { + service.setServiceUrl(options.serviceUrl); + } + return service; + } + + /** + * Construct a AccountManagementV4 object. + * + * @param {Object} options - Options for the service. + * @param {string} [options.serviceUrl] - The base URL for the service + * @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 {AccountManagementV4} + */ + constructor(options: UserOptions) { + options = options || {}; + + super(options); + if (options.serviceUrl) { + this.setServiceUrl(options.serviceUrl); + } else { + this.setServiceUrl(AccountManagementV4.DEFAULT_SERVICE_URL); + } + } + + /************************* + * default + ************************/ + + /** + * Get Account by Account ID. + * + * Returns the details of an account. + * + * @param {Object} params - The parameters to send to the service. + * @param {string} params.accountId - The unique identifier of the account you want to retrieve. + * @param {OutgoingHttpHeaders} [params.headers] - Custom request headers + * @returns {Promise>} + */ + public getAccount( + params: AccountManagementV4.GetAccountParams + ): Promise> { + const _params = { ...params }; + const _requiredParams = ['accountId']; + const _validParams = ['accountId', 'signal', 'headers']; + const _validationErrors = validateParams(_params, _requiredParams, _validParams); + if (_validationErrors) { + return Promise.reject(_validationErrors); + } + + const path = { + 'account_id': _params.accountId, + }; + + const sdkHeaders = getSdkHeaders(AccountManagementV4.DEFAULT_SERVICE_NAME, 'v4', 'getAccount'); + + const parameters = { + options: { + url: '/v4/accounts/{account_id}', + method: 'GET', + path, + }, + defaultOptions: extend(true, {}, this.baseOptions, { + headers: extend( + true, + sdkHeaders, + this.baseOptions.headers, + { + 'Accept': 'application/json', + }, + _params.headers + ), + axiosOptions: { + signal: _params.signal, + }, + }), + }; + + return this.createRequest(parameters); + } +} + +/************************* + * interfaces + ************************/ + +namespace AccountManagementV4 { + /** An operation response. */ + export interface Response { + result: T; + status: number; + statusText: string; + headers: IncomingHttpHeaders; + } + + /** The callback for a service request. */ + export type Callback = (error: any, response?: Response) => void; + + /** The body of a service request that returns no response data. */ + export interface EmptyObject {} + + /** A standard JS object, defined to avoid the limitations of `Object` and `object` */ + export interface JsonObject { + [key: string]: any; + } + + /************************* + * request interfaces + ************************/ + + interface DefaultParams { + headers?: OutgoingHttpHeaders; + signal?: AbortSignal; + } + + /** Parameters for the `getAccount` operation. */ + export interface GetAccountParams extends DefaultParams { + /** The unique identifier of the account you want to retrieve. */ + accountId: string; + } + + /************************* + * model interfaces + ************************/ + + /** + * AccountResponseTraits. + */ + export interface AccountResponseTraits { + eu_supported: boolean; + poc: boolean; + hippa: boolean; + } + + /** + * AccountResponse. + */ + export interface AccountResponse { + name: string; + id: string; + owner: string; + owner_userid: string; + owner_iamid: string; + type: string; + status: string; + linked_softlayer_account: string; + team_directory_enabled: boolean; + traits: AccountResponseTraits; + } +} + +export = AccountManagementV4; diff --git a/examples/account-management.v4.test.js b/examples/account-management.v4.test.js new file mode 100644 index 0000000..3da141b --- /dev/null +++ b/examples/account-management.v4.test.js @@ -0,0 +1,95 @@ +/** + * @jest-environment node + */ +/** + * (C) Copyright IBM Corp. 2026. + * + * 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. + */ + +/* eslint-disable no-console */ + +const AccountManagementV4 = require('../dist/account-management/v4'); +// eslint-disable-next-line node/no-unpublished-require +const authHelper = require('../test/resources/auth-helper.js'); +// You can use the readExternalSources method to access additional configuration values +// const { readExternalSources } = require('ibm-cloud-sdk-core'); + +// +// This file provides an example of how to use the account_management service. +// +// The following configuration properties are assumed to be defined: +// ACCOUNT_MANAGEMENT_URL= +// ACCOUNT_MANAGEMENT_AUTH_TYPE=iam +// ACCOUNT_MANAGEMENT_APIKEY= +// ACCOUNT_MANAGEMENT_AUTH_URL= +// +// These configuration properties can be exported as environment variables, or stored +// in a configuration file and then: +// export IBM_CREDENTIALS_FILE= +// +const configFile = 'account_management_v4.env'; + +const describe = authHelper.prepareTests(configFile); + +// Save original console.log +const originalLog = console.log; +const originalWarn = console.warn; + +// Mocks for console.log and console.warn +const consoleLogMock = jest.spyOn(console, 'log'); +const consoleWarnMock = jest.spyOn(console, 'warn'); + +describe('AccountManagementV4', () => { + // Service instance + let accountManagementService; + + // To access additional configuration values, uncomment this line and extract the values from config + // const config = readExternalSources(AccountManagementV4.DEFAULT_SERVICE_NAME); + + test('Initialize service', async () => { + // begin-common + + accountManagementService = AccountManagementV4.newInstance(); + + // end-common + }); + + test('getAccount request example', async () => { + consoleLogMock.mockImplementation((output) => { + originalLog(output); + }); + consoleWarnMock.mockImplementation((output) => { + // if an error occurs, display the message and then fail the test + originalWarn(output); + expect(true).toBeFalsy(); + }); + + originalLog('getAccount() result:'); + // begin-getAccount + + const params = { + accountId: 'testString', + }; + + let res; + try { + res = await accountManagementService.getAccount(params); + console.log(JSON.stringify(res.result, null, 2)); + } catch (err) { + console.warn(err); + } + + // end-getAccount + }); +}); diff --git a/test/integration/account-management.v4.test.js b/test/integration/account-management.v4.test.js new file mode 100644 index 0000000..4aaf49a --- /dev/null +++ b/test/integration/account-management.v4.test.js @@ -0,0 +1,60 @@ +/** + * (C) Copyright IBM Corp. 2026. + * + * 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. + */ + +/* eslint-disable no-console */ + +const { readExternalSources } = require('ibm-cloud-sdk-core'); +const AccountManagementV4 = require('../../dist/account-management/v4'); +const authHelper = require('../resources/auth-helper.js'); + +// testcase timeout value (200s). +const timeout = 200000; + +// Location of our config file. +const configFile = 'account_management_v4.env'; + +const describe = authHelper.prepareTests(configFile); + +describe('AccountManagementV4_integration', () => { + jest.setTimeout(timeout); + + // global values used in various test cases + let accountId; + let accountManagementService; + + test('Initialize service', async () => { + accountManagementService = AccountManagementV4.newInstance(); + + expect(accountManagementService).not.toBeNull(); + + const config = readExternalSources(AccountManagementV4.DEFAULT_SERVICE_NAME); + expect(config).not.toBeNull(); + accountId = config.accountId; + expect(accountId).not.toBeNull(); + accountManagementService.enableRetries(); + }); + + test('getAccount()', async () => { + const params = { + accountId, + }; + + const res = await accountManagementService.getAccount(params); + expect(res).toBeDefined(); + expect(res.status).toBe(200); + expect(res.result).toBeDefined(); + }); +}); diff --git a/test/unit/account-management.v4.test.js b/test/unit/account-management.v4.test.js new file mode 100644 index 0000000..e5e4cd7 --- /dev/null +++ b/test/unit/account-management.v4.test.js @@ -0,0 +1,195 @@ +/** + * (C) Copyright IBM Corp. 2026. + * + * 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. + */ + +// need to import the whole package to mock getAuthenticatorFromEnvironment +const sdkCorePackage = require('ibm-cloud-sdk-core'); + +const { NoAuthAuthenticator } = sdkCorePackage; +const AccountManagementV4 = require('../../dist/account-management/v4'); + +const { + getOptions, + checkUrlAndMethod, + checkMediaHeaders, + expectToBePromise, +} = require('@ibm-cloud/sdk-test-utilities'); + +const accountManagementServiceOptions = { + authenticator: new NoAuthAuthenticator(), + url: 'https://accounts.test.cloud.ibm.com', +}; + +const accountManagementService = new AccountManagementV4(accountManagementServiceOptions); + +let createRequestMock = null; +function mock_createRequest() { + if (!createRequestMock) { + createRequestMock = jest.spyOn(accountManagementService, 'createRequest'); + createRequestMock.mockImplementation(() => Promise.resolve()); + } +} + +// dont actually construct an authenticator +const getAuthenticatorMock = jest.spyOn(sdkCorePackage, 'getAuthenticatorFromEnvironment'); +getAuthenticatorMock.mockImplementation(() => new NoAuthAuthenticator()); + +describe('AccountManagementV4', () => { + beforeEach(() => { + mock_createRequest(); + }); + + afterEach(() => { + if (createRequestMock) { + createRequestMock.mockClear(); + } + getAuthenticatorMock.mockClear(); + }); + + describe('the newInstance method', () => { + test('should use defaults when options not provided', () => { + const testInstance = AccountManagementV4.newInstance(); + + expect(getAuthenticatorMock).toHaveBeenCalled(); + expect(testInstance.baseOptions.authenticator).toBeInstanceOf(NoAuthAuthenticator); + expect(testInstance.baseOptions.serviceName).toBe(AccountManagementV4.DEFAULT_SERVICE_NAME); + expect(testInstance.baseOptions.serviceUrl).toBe(AccountManagementV4.DEFAULT_SERVICE_URL); + expect(testInstance).toBeInstanceOf(AccountManagementV4); + }); + + test('should set serviceName, serviceUrl, and authenticator when provided', () => { + const options = { + authenticator: new NoAuthAuthenticator(), + serviceUrl: 'custom.com', + serviceName: 'my-service', + }; + + const testInstance = AccountManagementV4.newInstance(options); + + expect(getAuthenticatorMock).not.toHaveBeenCalled(); + expect(testInstance.baseOptions.authenticator).toBeInstanceOf(NoAuthAuthenticator); + expect(testInstance.baseOptions.serviceUrl).toBe('custom.com'); + expect(testInstance.baseOptions.serviceName).toBe('my-service'); + expect(testInstance).toBeInstanceOf(AccountManagementV4); + }); + }); + + describe('the constructor', () => { + test('use user-given service url', () => { + const options = { + authenticator: new NoAuthAuthenticator(), + serviceUrl: 'custom.com', + }; + + const testInstance = new AccountManagementV4(options); + + expect(testInstance.baseOptions.serviceUrl).toBe('custom.com'); + }); + + test('use default service url', () => { + const options = { + authenticator: new NoAuthAuthenticator(), + }; + + const testInstance = new AccountManagementV4(options); + + expect(testInstance.baseOptions.serviceUrl).toBe(AccountManagementV4.DEFAULT_SERVICE_URL); + }); + }); + + describe('getAccount', () => { + describe('positive tests', () => { + function __getAccountTest() { + // Construct the params object for operation getAccount + const accountId = 'testString'; + const getAccountParams = { + accountId, + }; + + const getAccountResult = accountManagementService.getAccount(getAccountParams); + + // all methods should return a Promise + expectToBePromise(getAccountResult); + + // assert that create request was called + expect(createRequestMock).toHaveBeenCalledTimes(1); + + const mockRequestOptions = getOptions(createRequestMock); + + checkUrlAndMethod(mockRequestOptions, '/v4/accounts/{account_id}', 'GET'); + const expectedAccept = 'application/json'; + const expectedContentType = undefined; + checkMediaHeaders(createRequestMock, expectedAccept, expectedContentType); + expect(mockRequestOptions.path.account_id).toEqual(accountId); + } + + test('should pass the right params to createRequest with enable and disable retries', () => { + // baseline test + __getAccountTest(); + + // enable retries and test again + createRequestMock.mockClear(); + accountManagementService.enableRetries(); + __getAccountTest(); + + // disable retries and test again + createRequestMock.mockClear(); + accountManagementService.disableRetries(); + __getAccountTest(); + }); + + test('should prioritize user-given headers', () => { + // parameters + const accountId = 'testString'; + const userAccept = 'fake/accept'; + const userContentType = 'fake/contentType'; + const getAccountParams = { + accountId, + headers: { + Accept: userAccept, + 'Content-Type': userContentType, + }, + }; + + accountManagementService.getAccount(getAccountParams); + checkMediaHeaders(createRequestMock, userAccept, userContentType); + }); + }); + + describe('negative tests', () => { + test('should enforce required parameters', async () => { + let err; + try { + await accountManagementService.getAccount({}); + } catch (e) { + err = e; + } + + expect(err.message).toMatch(/Missing required parameters/); + }); + + test('should reject promise when required params are not given', async () => { + let err; + try { + await accountManagementService.getAccount(); + } catch (e) { + err = e; + } + + expect(err.message).toMatch(/Missing required parameters/); + }); + }); + }); +});