Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export async function getGCEDescriptor() {
const idResponse = await gcpMetadata.instance('id');
const zoneResponse = await gcpMetadata.instance('zone');
// Some parsing is necessary. Metadata service returns a fully
// qualified zone name: 'projects/{projectId}/zones/{zone}'. Logging
// wants just the zone part.
//
const zone = zoneFromQualifiedZone(zoneResponse);
return {
type: 'gce_instance',
labels: {
// idResponse can be BigNumber when the id too large for JavaScript
// numbers. Use a toString() to uniformly convert to a string.
instance_id: idResponse.toString(),
zone,
},
};
}
export async function getGCEDescriptor() {
const idResponse = await gcpMetadata.instance('id');
const zoneResponse = await gcpMetadata.instance('zone');
// Some parsing is necessary. Metadata service returns a fully
// qualified zone name: 'projects/{projectId}/zones/{zone}'. Logging
// wants just the zone part.
//
const zone = zoneFromQualifiedZone(zoneResponse);
return {
type: 'gce_instance',
labels: {
// idResponse can be BigNumber when the id too large for JavaScript
// numbers. Use a toString() to uniformly convert to a string.
instance_id: idResponse.toString(),
zone,
},
};
}
'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,
{ project: project, clusterName: metadataCluster, onGCP: onGCP});
});
});
};
async function main() {
const isAvailable = await gcpMetadata.isAvailable();
console.log(`isAvailable: ${isAvailable}`);
await gcpMetadata.instance(`service-accounts/default/token`);
const svc = await gcpMetadata.instance({
property: 'service-accounts/',
params: {recursive: 'true'},
});
console.log('serviceAccounts:');
console.log(
JSON.stringify(svc)
.split('\n')
.join()
);
}
async function getMetadataInstanceField(field: string): Promise {
return gcpMetadata.instance(field);
}
async function getInstanceId() {
try {
const id = await gcpMetadata.instance('id');
return id.toString();
} catch {
return '';
}
}
isContainerEngine (callback) {
var env = this.environment;
if (typeof env.IS_CONTAINER_ENGINE !== 'undefined') {
setImmediate(() => {
callback(null, env.IS_CONTAINER_ENGINE);
});
return;
}
gcpMetadata.instance('/attributes/cluster-name')
.then(() => {
env.IS_CONTAINER_ENGINE = true;
callback(null, env.IS_CONTAINER_ENGINE);
})
.catch(() => {
env.IS_CONTAINER_ENGINE = false
callback(null, env.IS_CONTAINER_ENGINE);
});
}
async function getClusterName() {
try {
return await gcpMetadata.instance('attributes/cluster-name');
} catch {
return '';
}
}