Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add universe metadata handler #596

Merged
merged 4 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,20 +184,50 @@ async function fastFailMetadataRequest<T>(

/**
* Obtain metadata for the current GCE instance
*
* @see {@link https://cloud.google.com/compute/docs/metadata/predefined-metadata-keys}
*
* @example
* ```
* const serviceAccount: {} = await instance('service-accounts/');
* const serviceAccountEmail: string = await instance('service-accounts/default/email');
* ```
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function instance<T = any>(options?: string | Options) {
return metadataAccessor<T>('instance', options);
}

/**
* Obtain metadata for the current GCP Project.
* Obtain metadata for the current GCP project
*
* @see {@link https://cloud.google.com/compute/docs/metadata/predefined-metadata-keys}
*
* @example
* ```
* const projectId: string = await project('project-id');
* const numericProjectId: number = await project('numeric-project-id');
* ```
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function project<T = any>(options?: string | Options) {
return metadataAccessor<T>('project', options);
}

/**
* Obtain metadata for the current universe
*
* @see {@link https://cloud.google.com/compute/docs/metadata/predefined-metadata-keys}
*
* @example
* ```
* const universeDomain: string = await universe('universe_domain');
* ```
*/
export function universe<T>(options?: string | Options) {
return metadataAccessor<T>('universe', options);
}

/*
* How many times should we retry detecting GCP environment.
*/
Expand Down
13 changes: 13 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,19 @@ describe('unit test', () => {
scope.done();
});

it('should query the `universe` type', async () => {
const PROPERTY = 'universe_domain';
const VALUE = 'my-domain.com';

const scope = nock(HOST)
.get(`${PATH}/universe/${PROPERTY}`)
.reply(200, VALUE, HEADERS);

assert(await gcp.universe(PROPERTY), VALUE);

scope.done();
});

it('should return the request error', async () => {
const scope = nock(HOST)
.get(`${PATH}/${TYPE}`)
Expand Down
Loading