How to use gcp-metadata - 10 common examples

To help you get started, we’ve selected a few gcp-metadata examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github googleapis / gcp-metadata / system-test / fixtures / kitchen / src / index.ts View on Github external
* 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);
github googleapis / gcp-metadata / system-test / fixtures / kitchen / src / index.ts View on Github external
/**
 * Copyright 2018 Google LLC
 *
 * 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);
github googleapis / gcp-metadata / system-test / fixtures / kitchen / src / index.ts View on Github external
/**
 * Copyright 2018 Google LLC
 *
 * 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);
github googleapis / gcp-metadata / samples / quickstart.js View on Github external
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);
    }
github googleapis / gcp-metadata / samples / quickstart.js View on Github external
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);
    }
  }
github googleapis / nodejs-logging / src / metadata.ts View on Github external
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,
    },
  };
}
github googleapis / nodejs-logging / src / metadata.ts View on Github external
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,
    },
  };
}
github googleapis / cloud-debug-nodejs / src / agent / debuglet.js View on Github external
'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});
        });
      });
};
github googleapis / cloud-debug-nodejs / src / agent / debuglet.js View on Github external
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);
      });
};
github googleapis / cloud-debug-nodejs / src / agent / debuglet.js View on Github external
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,

gcp-metadata

Get the metadata from a Google Cloud Platform environment

Apache-2.0
Latest version published 5 months ago

Package Health Score

87 / 100
Full package analysis