gcp-metadata
Advanced tools
Comparing version 6.0.0 to 6.1.0
@@ -35,11 +35,79 @@ /** | ||
} | ||
export interface MetadataAccessor { | ||
/** | ||
* | ||
* @example | ||
* | ||
* // equivalent to `project('project-id')`; | ||
* const metadataKey = 'project/project-id'; | ||
*/ | ||
metadataKey: string; | ||
params?: Options['params']; | ||
headers?: Options['headers']; | ||
noResponseRetries?: number; | ||
fastFail?: boolean; | ||
} | ||
export type BulkResults<T extends readonly MetadataAccessor[]> = { | ||
[key in T[number]['metadataKey']]: ReturnType<JSON['parse']>; | ||
}; | ||
/** | ||
* Obtain metadata for the current GCE instance | ||
* 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'); | ||
* ``` | ||
*/ | ||
export declare function instance<T = any>(options?: string | Options): Promise<T>; | ||
/** | ||
* 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'); | ||
* ``` | ||
*/ | ||
export declare function project<T = any>(options?: string | Options): Promise<T>; | ||
/** | ||
* 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 declare function universe<T>(options?: string | Options): Promise<T>; | ||
/** | ||
* Retrieve metadata items in parallel. | ||
* | ||
* @see {@link https://cloud.google.com/compute/docs/metadata/predefined-metadata-keys} | ||
* | ||
* @example | ||
* ``` | ||
* const data = await bulk([ | ||
* { | ||
* metadataKey: 'instance', | ||
* }, | ||
* { | ||
* metadataKey: 'project/project-id', | ||
* }, | ||
* ] as const); | ||
* | ||
* // data.instance; | ||
* // data['project/project-id']; | ||
* ``` | ||
* | ||
* @param properties The metadata properties to retrieve | ||
* @returns The metadata in `metadatakey:value` format | ||
*/ | ||
export declare function bulk<T extends readonly Readonly<MetadataAccessor>[], R extends BulkResults<T> = BulkResults<T>>(properties: T): Promise<R>; | ||
/** | ||
* Determine if the metadata server is currently available. | ||
@@ -46,0 +114,0 @@ */ |
@@ -23,3 +23,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.requestTimeout = exports.setGCPResidency = exports.getGCPResidency = exports.gcpResidencyCache = exports.resetIsAvailableCache = exports.isAvailable = exports.project = exports.instance = exports.METADATA_SERVER_DETECTION = exports.HEADERS = exports.HEADER_VALUE = exports.HEADER_NAME = exports.SECONDARY_HOST_ADDRESS = exports.HOST_ADDRESS = exports.BASE_PATH = void 0; | ||
exports.requestTimeout = exports.setGCPResidency = exports.getGCPResidency = exports.gcpResidencyCache = exports.resetIsAvailableCache = exports.isAvailable = exports.bulk = exports.universe = exports.project = exports.instance = exports.METADATA_SERVER_DETECTION = exports.HEADERS = exports.HEADER_VALUE = exports.HEADER_NAME = exports.SECONDARY_HOST_ADDRESS = exports.HOST_ADDRESS = exports.BASE_PATH = void 0; | ||
const gaxios_1 = require("gaxios"); | ||
@@ -83,19 +83,35 @@ const jsonBigint = require("json-bigint"); | ||
} | ||
async function metadataAccessor(type, options, noResponseRetries = 3, fastFail = false) { | ||
options = options || {}; | ||
async function metadataAccessor(type, options = {}, noResponseRetries = 3, fastFail = false) { | ||
let metadataKey = ''; | ||
let params = {}; | ||
let headers = {}; | ||
if (typeof type === 'object') { | ||
const metadataAccessor = type; | ||
metadataKey = metadataAccessor.metadataKey; | ||
params = metadataAccessor.params || params; | ||
headers = metadataAccessor.headers || headers; | ||
noResponseRetries = metadataAccessor.noResponseRetries || noResponseRetries; | ||
fastFail = metadataAccessor.fastFail || fastFail; | ||
} | ||
else { | ||
metadataKey = type; | ||
} | ||
if (typeof options === 'string') { | ||
options = { property: options }; | ||
metadataKey += `/${options}`; | ||
} | ||
let property = ''; | ||
if (typeof options === 'object' && options.property) { | ||
property = '/' + options.property; | ||
else { | ||
validate(options); | ||
if (options.property) { | ||
metadataKey += `/${options.property}`; | ||
} | ||
headers = options.headers || headers; | ||
params = options.params || params; | ||
} | ||
validate(options); | ||
try { | ||
const requestMethod = fastFail ? fastFailMetadataRequest : gaxios_1.request; | ||
const res = await requestMethod({ | ||
url: `${getBaseUrl()}/${type}${property}`, | ||
headers: Object.assign({}, exports.HEADERS, options.headers), | ||
url: `${getBaseUrl()}/${metadataKey}`, | ||
headers: { ...exports.HEADERS, ...headers }, | ||
retryConfig: { noResponseRetries }, | ||
params: options.params, | ||
params, | ||
responseType: 'text', | ||
@@ -181,3 +197,11 @@ timeout: requestTimeout(), | ||
/** | ||
* Obtain metadata for the current GCE instance | ||
* 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'); | ||
* ``` | ||
*/ | ||
@@ -190,3 +214,11 @@ // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
/** | ||
* 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'); | ||
* ``` | ||
*/ | ||
@@ -198,2 +230,51 @@ // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
exports.project = project; | ||
/** | ||
* 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'); | ||
* ``` | ||
*/ | ||
function universe(options) { | ||
return metadataAccessor('universe', options); | ||
} | ||
exports.universe = universe; | ||
/** | ||
* Retrieve metadata items in parallel. | ||
* | ||
* @see {@link https://cloud.google.com/compute/docs/metadata/predefined-metadata-keys} | ||
* | ||
* @example | ||
* ``` | ||
* const data = await bulk([ | ||
* { | ||
* metadataKey: 'instance', | ||
* }, | ||
* { | ||
* metadataKey: 'project/project-id', | ||
* }, | ||
* ] as const); | ||
* | ||
* // data.instance; | ||
* // data['project/project-id']; | ||
* ``` | ||
* | ||
* @param properties The metadata properties to retrieve | ||
* @returns The metadata in `metadatakey:value` format | ||
*/ | ||
async function bulk(properties) { | ||
const r = {}; | ||
await Promise.all(properties.map(item => { | ||
return (async () => { | ||
const res = await metadataAccessor(item); | ||
const key = item.metadataKey; | ||
r[key] = res; | ||
})(); | ||
})); | ||
return r; | ||
} | ||
exports.bulk = bulk; | ||
/* | ||
@@ -200,0 +281,0 @@ * How many times should we retry detecting GCP environment. |
@@ -7,2 +7,15 @@ # Changelog | ||
## [6.1.0](https://github.com/googleapis/gcp-metadata/compare/v6.0.0...v6.1.0) (2023-11-10) | ||
### Features | ||
* Add `universe` metadata handler ([#596](https://github.com/googleapis/gcp-metadata/issues/596)) ([0c02016](https://github.com/googleapis/gcp-metadata/commit/0c02016756754cddde6c4402fac1ceb6a318e82d)) | ||
* Bulk Metadata Requests ([#598](https://github.com/googleapis/gcp-metadata/issues/598)) ([0a51378](https://github.com/googleapis/gcp-metadata/commit/0a513788537173570f9910d368dd36717de7233b)) | ||
### Bug Fixes | ||
* Repo Metadata ([#595](https://github.com/googleapis/gcp-metadata/issues/595)) ([470a872](https://github.com/googleapis/gcp-metadata/commit/470a8722df2b2fb2da1b076b73414d2e28a3ff4e)) | ||
## [6.0.0](https://github.com/googleapis/gcp-metadata/compare/v5.3.0...v6.0.0) (2023-07-17) | ||
@@ -9,0 +22,0 @@ |
{ | ||
"name": "gcp-metadata", | ||
"version": "6.0.0", | ||
"version": "6.1.0", | ||
"description": "Get the metadata from a Google Cloud Platform environment", | ||
@@ -44,11 +44,10 @@ "repository": "googleapis/gcp-metadata", | ||
"devDependencies": { | ||
"@babel/plugin-proposal-private-methods": "^7.18.6", | ||
"@compodoc/compodoc": "^1.1.10", | ||
"@google-cloud/functions": "^2.0.0", | ||
"@google-cloud/functions": "^3.0.0", | ||
"@types/json-bigint": "^1.0.0", | ||
"@types/mocha": "^9.0.0", | ||
"@types/ncp": "^2.0.1", | ||
"@types/node": "^18.0.0", | ||
"@types/sinon": "^10.0.13", | ||
"@types/tmp": "0.2.3", | ||
"@types/node": "^20.0.0", | ||
"@types/sinon": "^17.0.0", | ||
"@types/tmp": "0.2.6", | ||
"@types/uuid": "^9.0.0", | ||
@@ -59,3 +58,3 @@ "c8": "^8.0.0", | ||
"gcx": "^1.0.0", | ||
"gts": "^3.1.1", | ||
"gts": "^5.0.0", | ||
"linkinator": "^4.0.0", | ||
@@ -65,3 +64,3 @@ "mocha": "^8.0.0", | ||
"nock": "^13.0.0", | ||
"sinon": "^15.0.0", | ||
"sinon": "^17.0.0", | ||
"tmp": "^0.2.0", | ||
@@ -68,0 +67,0 @@ "typescript": "^5.1.6", |
@@ -7,3 +7,3 @@ [//]: # "This README.md file is auto-generated, all changes to this file will be lost." | ||
[![release level](https://img.shields.io/badge/release%20level-general%20availability%20%28GA%29-brightgreen.svg?style=flat)](https://cloud.google.com/terms/launch-stages) | ||
[![release level](https://img.shields.io/badge/release%20level-stable-brightgreen.svg?style=flat)](https://cloud.google.com/terms/launch-stages) | ||
[![npm version](https://img.shields.io/npm/v/gcp-metadata.svg)](https://www.npmjs.org/package/gcp-metadata) | ||
@@ -145,3 +145,3 @@ | ||
``` | ||
export GCE_METADATA_HOST = '169.254.169.254' | ||
export GCE_METADATA_HOST='169.254.169.254' | ||
``` | ||
@@ -176,3 +176,3 @@ | ||
Our client libraries follow the [Node.js release schedule](https://nodejs.org/en/about/releases/). | ||
Our client libraries follow the [Node.js release schedule](https://github.com/nodejs/release#release-schedule). | ||
Libraries are compatible with all current _active_ and _maintenance_ versions of | ||
@@ -201,6 +201,6 @@ Node.js. | ||
This library is considered to be **General Availability (GA)**. This means it | ||
is stable; the code surface will not change in backwards-incompatible ways | ||
This library is considered to be **stable**. The code surface will not change in backwards-incompatible ways | ||
unless absolutely necessary (e.g. because of critical security issues) or with | ||
an extensive deprecation period. Issues and requests against **GA** libraries | ||
an extensive deprecation period. Issues and requests against **stable** libraries | ||
are addressed with the highest priority. | ||
@@ -213,3 +213,2 @@ | ||
More Information: [Google Cloud Platform Launch Stages][launch_stages] | ||
@@ -216,0 +215,0 @@ |
Sorry, the diff of this file is not supported yet
81347
22
723
236