Skip to content

Commit 5499c1e

Browse files
committed
feat: add ibm-cloud-code-engine service
1 parent 7166984 commit 5499c1e

File tree

3 files changed

+378
-1
lines changed

3 files changed

+378
-1
lines changed

ibm-cloud-code-engine/v1.ts

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
/**
2+
* (C) Copyright IBM Corp. 2020.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* IBM OpenAPI SDK Code Generator Version: 3.12.0-64fe8d3f-20200820-144050
19+
*/
20+
21+
22+
import * as extend from 'extend';
23+
import { IncomingHttpHeaders, OutgoingHttpHeaders } from 'http';
24+
import { Authenticator, BaseService, getAuthenticatorFromEnvironment, getMissingParams, UserOptions } from 'ibm-cloud-sdk-core';
25+
import { getSdkHeaders } from '../lib/common';
26+
27+
/**
28+
* The purpose is to provide an API to get Kubeconfig for IBM Cloud Code Engine Project
29+
*/
30+
31+
class IbmCloudCodeEngineV1 extends BaseService {
32+
33+
static DEFAULT_SERVICE_URL: string = 'https://ibm-cloud-code-engine.cloud.ibm.com/api/v1';
34+
static DEFAULT_SERVICE_NAME: string = 'ibm_cloud_code_engine';
35+
36+
/*************************
37+
* Factory method
38+
************************/
39+
40+
/**
41+
* Constructs an instance of IbmCloudCodeEngineV1 with passed in options and external configuration.
42+
*
43+
* @param {UserOptions} [options] - The parameters to send to the service.
44+
* @param {string} [options.serviceName] - The name of the service to configure
45+
* @param {Authenticator} [options.authenticator] - The Authenticator object used to authenticate requests to the service
46+
* @param {string} [options.serviceUrl] - The URL for the service
47+
* @returns {IbmCloudCodeEngineV1}
48+
*/
49+
50+
public static newInstance(options: UserOptions): IbmCloudCodeEngineV1 {
51+
options = options || {};
52+
53+
if (!options.serviceName) {
54+
options.serviceName = this.DEFAULT_SERVICE_NAME;
55+
}
56+
if (!options.authenticator) {
57+
options.authenticator = getAuthenticatorFromEnvironment(options.serviceName);
58+
}
59+
const service = new IbmCloudCodeEngineV1(options);
60+
service.configureService(options.serviceName);
61+
if (options.serviceUrl) {
62+
service.setServiceUrl(options.serviceUrl);
63+
}
64+
return service;
65+
}
66+
67+
68+
/**
69+
* Construct a IbmCloudCodeEngineV1 object.
70+
*
71+
* @param {Object} options - Options for the service.
72+
* @param {string} [options.serviceUrl] - The base url to use when contacting the service (e.g. 'https://gateway.watsonplatform.net/api/v1'). The base url may differ between IBM Cloud regions.
73+
* @param {OutgoingHttpHeaders} [options.headers] - Default headers that shall be included with every request to the service.
74+
* @param {Authenticator} options.authenticator - The Authenticator object used to authenticate requests to the service
75+
* @constructor
76+
* @returns {IbmCloudCodeEngineV1}
77+
*/
78+
constructor(options: UserOptions) {
79+
options = options || {};
80+
81+
super(options);
82+
if (options.serviceUrl) {
83+
this.setServiceUrl(options.serviceUrl);
84+
} else {
85+
this.setServiceUrl(IbmCloudCodeEngineV1.DEFAULT_SERVICE_URL);
86+
}
87+
}
88+
89+
/*************************
90+
* getKubeconfig
91+
************************/
92+
93+
/**
94+
* Retrieve KUBECONFIG for a specified project.
95+
*
96+
* Returns the KUBECONFIG, similar to the output of `kubectl config view --minify=true`.
97+
*
98+
* @param {Object} params - The parameters to send to the service.
99+
* @param {string} params.refreshToken - The IAM Refresh token associated with the IBM Cloud account.
100+
* @param {string} params.id - The id of the IBM Cloud Code Engine project.
101+
* @param {string} [params.accept] - The type of the response: application/json or text/html. A character encoding can
102+
* be specified by including a `charset` parameter. For example, 'text/html;charset=utf-8'.
103+
* @param {OutgoingHttpHeaders} [params.headers] - Custom request headers
104+
* @returns {Promise<IbmCloudCodeEngineV1.Response<IbmCloudCodeEngineV1.Empty>>}
105+
*/
106+
public listKubeconfig(params: IbmCloudCodeEngineV1.ListKubeconfigParams): Promise<IbmCloudCodeEngineV1.Response<IbmCloudCodeEngineV1.Empty>> {
107+
const _params = Object.assign({}, params);
108+
const requiredParams = ['refreshToken', 'id'];
109+
110+
return new Promise((resolve, reject) => {
111+
const missingParams = getMissingParams(_params, requiredParams);
112+
if (missingParams) {
113+
return reject(missingParams);
114+
}
115+
116+
const path = {
117+
'id': _params.id
118+
};
119+
120+
const sdkHeaders = getSdkHeaders(IbmCloudCodeEngineV1.DEFAULT_SERVICE_NAME, 'v1', 'listKubeconfig');
121+
122+
const parameters = {
123+
options: {
124+
url: '/namespaces/{id}/config',
125+
method: 'GET',
126+
path,
127+
},
128+
defaultOptions: extend(true, {}, this.baseOptions, {
129+
headers: extend(true, sdkHeaders, {
130+
'Refresh-Token': _params.refreshToken,
131+
'Accept': _params.accept
132+
}, _params.headers),
133+
}),
134+
};
135+
136+
return resolve(this.createRequest(parameters));
137+
});
138+
};
139+
140+
}
141+
142+
/*************************
143+
* interfaces
144+
************************/
145+
146+
namespace IbmCloudCodeEngineV1 {
147+
148+
/** An operation response. */
149+
export interface Response<T = any> {
150+
result: T;
151+
status: number;
152+
statusText: string;
153+
headers: IncomingHttpHeaders;
154+
}
155+
156+
/** The callback for a service request. */
157+
export type Callback<T> = (error: any, response?: Response<T>) => void;
158+
159+
/** The body of a service request that returns no response data. */
160+
export interface Empty { }
161+
162+
/** A standard JS object, defined to avoid the limitations of `Object` and `object` */
163+
export interface JsonObject {
164+
[key: string]: any;
165+
}
166+
167+
/*************************
168+
* request interfaces
169+
************************/
170+
171+
/** Parameters for the `listKubeconfig` operation. */
172+
export interface ListKubeconfigParams {
173+
/** The IAM Refresh token associated with the IBM Cloud account. */
174+
refreshToken: string;
175+
/** The id of the IBM Cloud Code Engine project. */
176+
id: string;
177+
/** The type of the response: application/json or text/html. A character encoding can be specified by including
178+
* a `charset` parameter. For example, 'text/html;charset=utf-8'.
179+
*/
180+
accept?: ListKubeconfigConstants.Accept | string;
181+
headers?: OutgoingHttpHeaders;
182+
}
183+
184+
/** Constants for the `listKubeconfig` operation. */
185+
export namespace ListKubeconfigConstants {
186+
/** The type of the response: application/json or text/html. A character encoding can be specified by including a `charset` parameter. For example, 'text/html;charset=utf-8'. */
187+
export enum Accept {
188+
APPLICATION_JSON = 'application/json',
189+
TEXT_HTML = 'text/html',
190+
}
191+
}
192+
193+
/*************************
194+
* model interfaces
195+
************************/
196+
197+
}
198+
199+
export = IbmCloudCodeEngineV1;

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
/**
2+
* (C) Copyright IBM Corp. 2020.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
'use strict';
17+
18+
// need to import the whole package to mock getAuthenticatorFromEnvironment
19+
const core = require('ibm-cloud-sdk-core');
20+
const { NoAuthAuthenticator, unitTestUtils } = core;
21+
22+
const IbmCloudCodeEngineV1 = require('../../dist/ibm-cloud-code-engine/v1');
23+
24+
const {
25+
getOptions,
26+
checkUrlAndMethod,
27+
checkMediaHeaders,
28+
expectToBePromise,
29+
checkUserHeader,
30+
} = unitTestUtils;
31+
32+
const service = {
33+
authenticator: new NoAuthAuthenticator(),
34+
url: 'https://ibm-cloud-code-engine.cloud.ibm.com/api/v1',
35+
};
36+
37+
const ibmCloudCodeEngineService = new IbmCloudCodeEngineV1(service);
38+
39+
// dont actually create a request
40+
const createRequestMock = jest.spyOn(ibmCloudCodeEngineService, 'createRequest');
41+
createRequestMock.mockImplementation(() => Promise.resolve());
42+
43+
// dont actually construct an authenticator
44+
const getAuthenticatorMock = jest.spyOn(core, 'getAuthenticatorFromEnvironment');
45+
getAuthenticatorMock.mockImplementation(() => new NoAuthAuthenticator());
46+
47+
afterEach(() => {
48+
createRequestMock.mockClear();
49+
getAuthenticatorMock.mockClear();
50+
});
51+
52+
describe('IbmCloudCodeEngineV1', () => {
53+
describe('the newInstance method', () => {
54+
test('should use defaults when options not provided', () => {
55+
const testInstance = IbmCloudCodeEngineV1.newInstance();
56+
57+
expect(getAuthenticatorMock).toHaveBeenCalled();
58+
expect(testInstance.baseOptions.authenticator).toBeInstanceOf(NoAuthAuthenticator);
59+
expect(testInstance.baseOptions.serviceName).toBe(IbmCloudCodeEngineV1.DEFAULT_SERVICE_NAME);
60+
expect(testInstance.baseOptions.serviceUrl).toBe(IbmCloudCodeEngineV1.DEFAULT_SERVICE_URL);
61+
expect(testInstance).toBeInstanceOf(IbmCloudCodeEngineV1);
62+
});
63+
64+
test('should set serviceName, serviceUrl, and authenticator when provided', () => {
65+
const options = {
66+
authenticator: new NoAuthAuthenticator(),
67+
serviceUrl: 'custom.com',
68+
serviceName: 'my-service',
69+
};
70+
71+
const testInstance = IbmCloudCodeEngineV1.newInstance(options);
72+
73+
expect(getAuthenticatorMock).not.toHaveBeenCalled();
74+
expect(testInstance.baseOptions.authenticator).toBeInstanceOf(NoAuthAuthenticator);
75+
expect(testInstance.baseOptions.serviceUrl).toBe('custom.com');
76+
expect(testInstance.baseOptions.serviceName).toBe('my-service');
77+
expect(testInstance).toBeInstanceOf(IbmCloudCodeEngineV1);
78+
});
79+
});
80+
describe('the constructor', () => {
81+
test('use user-given service url', () => {
82+
const options = {
83+
authenticator: new NoAuthAuthenticator(),
84+
serviceUrl: 'custom.com',
85+
};
86+
87+
const testInstance = new IbmCloudCodeEngineV1(options);
88+
89+
expect(testInstance.baseOptions.serviceUrl).toBe('custom.com');
90+
});
91+
92+
test('use default service url', () => {
93+
const options = {
94+
authenticator: new NoAuthAuthenticator(),
95+
};
96+
97+
const testInstance = new IbmCloudCodeEngineV1(options);
98+
99+
expect(testInstance.baseOptions.serviceUrl).toBe(IbmCloudCodeEngineV1.DEFAULT_SERVICE_URL);
100+
});
101+
});
102+
describe('listKubeconfig', () => {
103+
describe('positive tests', () => {
104+
test('should pass the right params to createRequest', () => {
105+
// Construct the params object for operation listKubeconfig
106+
const refreshToken = 'testString';
107+
const id = 'testString';
108+
const accept = 'application/json';
109+
const params = {
110+
refreshToken: refreshToken,
111+
id: id,
112+
accept: accept,
113+
};
114+
115+
const listKubeconfigResult = ibmCloudCodeEngineService.listKubeconfig(params);
116+
117+
// all methods should return a Promise
118+
expectToBePromise(listKubeconfigResult);
119+
120+
// assert that create request was called
121+
expect(createRequestMock).toHaveBeenCalledTimes(1);
122+
123+
const options = getOptions(createRequestMock);
124+
125+
checkUrlAndMethod(options, '/namespaces/{id}/config', 'GET');
126+
const expectedAccept = accept;
127+
const expectedContentType = undefined;
128+
checkMediaHeaders(createRequestMock, expectedAccept, expectedContentType);
129+
checkUserHeader(createRequestMock, 'Refresh-Token', refreshToken);
130+
checkUserHeader(createRequestMock, 'Accept', accept);
131+
expect(options.path['id']).toEqual(id);
132+
});
133+
134+
test('should prioritize user-given headers', () => {
135+
// parameters
136+
const refreshToken = 'testString';
137+
const id = 'testString';
138+
const userAccept = 'fake/accept';
139+
const userContentType = 'fake/contentType';
140+
const params = {
141+
refreshToken,
142+
id,
143+
headers: {
144+
Accept: userAccept,
145+
'Content-Type': userContentType,
146+
},
147+
};
148+
149+
ibmCloudCodeEngineService.listKubeconfig(params);
150+
checkMediaHeaders(createRequestMock, userAccept, userContentType);
151+
});
152+
});
153+
154+
describe('negative tests', () => {
155+
test('should enforce required parameters', async done => {
156+
let err;
157+
try {
158+
await ibmCloudCodeEngineService.listKubeconfig({});
159+
} catch (e) {
160+
err = e;
161+
}
162+
163+
expect(err.message).toMatch(/Missing required parameters/);
164+
done();
165+
});
166+
167+
test('should reject promise when required params are not given', done => {
168+
const listKubeconfigPromise = ibmCloudCodeEngineService.listKubeconfig();
169+
expectToBePromise(listKubeconfigPromise);
170+
171+
listKubeconfigPromise.catch(err => {
172+
expect(err.message).toMatch(/Missing required parameters/);
173+
done();
174+
});
175+
});
176+
});
177+
});
178+
});

0 commit comments

Comments
 (0)