Skip to content

Commit b911064

Browse files
committed
Add list method to generic KubernetesObjectApi
1 parent 32c1163 commit b911064

File tree

2 files changed

+113
-2
lines changed

2 files changed

+113
-2
lines changed

src/object.ts

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type KubernetesObjectResponseBody =
2020
| V1APIResourceList;
2121

2222
/** Kubernetes API verbs. */
23-
type KubernetesApiAction = 'create' | 'delete' | 'patch' | 'read' | 'replace';
23+
type KubernetesApiAction = 'create' | 'delete' | 'patch' | 'read' | 'list' | 'replace';
2424

2525
/**
2626
* Valid Content-Type header values for patch operations. See
@@ -315,6 +315,91 @@ export class KubernetesObjectApi extends ApisApi {
315315
return this.requestPromise(localVarRequestOptions);
316316
}
317317

318+
/**
319+
* List any Kubernetes resources.
320+
* @param apiVersion api group and version of the form <apiGroup>/<version>
321+
* @param kind Kubernetes resource kind
322+
* @param namespace list resources in this namespace
323+
* @param pretty If \&#39;true\&#39;, then the output is pretty printed.
324+
* @param exact Should the export be exact. Exact export maintains cluster-specific fields like
325+
* \&#39;Namespace\&#39;. Deprecated. Planned for removal in 1.18.
326+
* @param exportt Should this value be exported. Export strips fields that a user can not
327+
* specify. Deprecated. Planned for removal in 1.18.
328+
* @param fieldSelector A selector to restrict the list of returned objects by their fields. Defaults to everything.
329+
* @param labelSelector A selector to restrict the list of returned objects by their labels. Defaults to everything.
330+
* @param limit Number of returned resources.
331+
* @param options Optional headers to use in the request.
332+
* @return Promise containing the request response and [[KubernetesListObject<KubernetesObject>]].
333+
*/
334+
public async list(
335+
apiVersion: string,
336+
kind: string,
337+
namespace?: string,
338+
pretty?: string,
339+
exact?: boolean,
340+
exportt?: boolean,
341+
fieldSelector?: string,
342+
labelSelector?: string,
343+
limit?: number,
344+
options: { headers: { [name: string]: string } } = { headers: {} },
345+
): Promise<{ body: KubernetesListObject<KubernetesObject>; response: http.IncomingMessage }> {
346+
// verify required parameters 'apiVersion', 'kind' is not null or undefined
347+
if (apiVersion === null || apiVersion === undefined) {
348+
throw new Error('Required parameter apiVersion was null or undefined when calling list.');
349+
}
350+
if (kind === null || kind === undefined) {
351+
throw new Error('Required parameter kind was null or undefined when calling list.');
352+
}
353+
354+
const localVarPath = await this.specUriPath(
355+
{
356+
apiVersion,
357+
kind,
358+
metadata: {
359+
namespace,
360+
},
361+
},
362+
'list',
363+
);
364+
const localVarQueryParameters: any = {};
365+
const localVarHeaderParams = this.generateHeaders(options.headers);
366+
367+
if (pretty !== undefined) {
368+
localVarQueryParameters.pretty = ObjectSerializer.serialize(pretty, 'string');
369+
}
370+
371+
if (exact !== undefined) {
372+
localVarQueryParameters.exact = ObjectSerializer.serialize(exact, 'boolean');
373+
}
374+
375+
if (exportt !== undefined) {
376+
localVarQueryParameters.export = ObjectSerializer.serialize(exportt, 'boolean');
377+
}
378+
379+
if (fieldSelector !== undefined) {
380+
localVarQueryParameters.fieldSelector = ObjectSerializer.serialize(fieldSelector, 'string');
381+
}
382+
383+
if (labelSelector !== undefined) {
384+
localVarQueryParameters.labelSelector = ObjectSerializer.serialize(labelSelector, 'string');
385+
}
386+
387+
if (limit !== undefined) {
388+
localVarQueryParameters.limit = ObjectSerializer.serialize(limit, 'number');
389+
}
390+
391+
const localVarRequestOptions: request.Options = {
392+
method: 'GET',
393+
qs: localVarQueryParameters,
394+
headers: localVarHeaderParams,
395+
uri: localVarPath,
396+
useQuerystring: this._useQuerystring,
397+
json: true,
398+
};
399+
400+
return this.requestPromise(localVarRequestOptions);
401+
}
402+
318403
/**
319404
* Replace any Kubernetes resource.
320405
* @param spec Kubernetes resource spec
@@ -411,7 +496,7 @@ export class KubernetesObjectApi extends ApisApi {
411496
parts.push('namespaces', encodeURIComponent(String(spec.metadata.namespace)));
412497
}
413498
parts.push(resource.name);
414-
if (action !== 'create') {
499+
if (action !== 'create' && action !== 'list') {
415500
if (!spec.metadata.name) {
416501
throw new Error('Required spec property name is not set');
417502
}

src/object_test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1921,5 +1921,31 @@ describe('KubernetesObject', () => {
19211921
expect(thrown).to.be.true;
19221922
scope.done();
19231923
});
1924+
1925+
it('should throw error if no apiVersion', async () => {
1926+
let thrown = false;
1927+
try {
1928+
await (client.list as any)(undefined, undefined);
1929+
expect.fail('should have thrown an error');
1930+
} catch (e) {
1931+
thrown = true;
1932+
expect(e.message).to.contain(
1933+
'Required parameter apiVersion was null or undefined when calling ',
1934+
);
1935+
}
1936+
expect(thrown).to.be.true;
1937+
});
1938+
1939+
it('should throw error if no kind', async () => {
1940+
let thrown = false;
1941+
try {
1942+
await (client.list as any)('', undefined);
1943+
expect.fail('should have thrown an error');
1944+
} catch (e) {
1945+
thrown = true;
1946+
expect(e.message).to.contain('Required parameter kind was null or undefined when calling ');
1947+
}
1948+
expect(thrown).to.be.true;
1949+
});
19241950
});
19251951
});

0 commit comments

Comments
 (0)