Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
* Distributed under MIT license.
* See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
*/
import * as gcp from 'gcp-metadata';
// uncomment the line below during development
//import * as gcp from '../../../../build/src/index';
const header = gcp.HEADER_NAME;
const headers = gcp.HEADERS;
async function main() {
const v = await gcp.instance('/somepath');
}
gcp.project('something').then(console.log);
main().catch(console.error);
async function quickstart() {
// check to see if this code can access a metadata server
const isAvailable = await gcpMetadata.isAvailable();
console.log(`Is available: ${isAvailable}`);
// Instance and Project level metadata will only be available if
// running inside of a Google Cloud compute environment such as
// Cloud Functions, App Engine, Kubernetes Engine, or Compute Engine.
if (isAvailable) {
// grab all top level metadata from the service
const instanceMetadata = await gcpMetadata.instance();
console.log('Instance metadata:');
console.log(instanceMetadata);
// get all project level metadata
const projectMetadata = await gcpMetadata.project();
console.log('Project metadata:');
console.log(projectMetadata);
}
}
Debuglet.prototype.getProjectId_ = function(callback) {
var that = this;
// We need to figure out whether we are running on GCP. We can use our ability
// to access the metadata service as a test for that.
// TODO: change this to getProjectId in the future.
metadata.project(
'project-id', function(err, response, metadataProject) {
// We should get an error if we are not on GCP.
var onGCP = !err;
// We perfer to use the locally available projectId as that is least
// surprising to users.
var project = that.debug_.options.projectId ||
process.env.GCLOUD_PROJECT || metadataProject;
// We if don't have a projectId by now, we fail with an error.
if (!project) {
return callback(err);
}
return callback(null, project, onGCP);
});
};
Debuglet.prototype.getMetadataValues_ = function(callback) {
var that = this;
// We need to figure out whether we are running on GCP. We can use our ability
// to access the metadata service as a test for that.
// TODO: change this to getProjectId in the future.
metadata.project(
'project-id', function(err, response, metadataProject) {
// We should get an error if we are not on GCP.
var onGCP = !err;
// We perfer to use the locally available projectId as that is least
// surprising to users.
var project = that.debug_.options.projectId ||
process.env.GCLOUD_PROJECT || metadataProject;
// We if don't have a projectId by now, we fail with an error.
if (!project) {
return callback(err);
}
metadata.instance(
'attributes/cluster-name', function(err, response, metadataCluster) {
return callback(null,
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, gcpMetadata.project('project-id')];
case 1:
r = _a.sent();
return [2 /*return*/, r.data];
case 2:
e_3 = _a.sent();
// Ignore any errors
return [2 /*return*/, null];
case 3: return [2 /*return*/];
}
});
});
async function getProjectId() {
try {
return await gcpMetadata.project('project-id');
} catch {
return '';
}
}