Skip to content

Commit

Permalink
feat: Add universe metadata handler (#596)
Browse files Browse the repository at this point in the history
* feat: Add `universe` metadata handler

* fix: test
  • Loading branch information
danielbankhead authored Nov 10, 2023
1 parent 470a872 commit 0c02016
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
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

0 comments on commit 0c02016

Please sign in to comment.