Constructor

Resource

new Resource(options)

The Cloud Resource Manager provides methods that you can use to programmatically manage your projects in the Google Cloud Platform. With this API, you can do the following:

  • Get a list of all projects associated with an account.
  • Create new projects.
  • Update existing projects.
  • Delete projects.
  • Recover projects.

Parameter

options

Optional

ClientConfig

Configuration options.

See also

What is the Cloud Resource Manager?

Examples

Import the client library

const Resource = require('@google-cloud/resource');

Create a client that uses Application Default Credentials (ADC):

const resource = new Resource();

Create a client with explicit credentials:

const resource = new Resource({
  projectId: 'your-project-id',
  keyFilename: '/path/to/keyfile.json'
});

Full quickstart example:

// Imports the Google Cloud client library
const Resource = require('@google-cloud/resource');

// Your Google Cloud Platform project ID
const projectId = 'YOUR_PROJECT_ID';

// Creates a client
const resourceClient = new Resource({
  projectId: projectId,
});

// Lists current projects
resourceClient
  .getProjects()
  .then(results => {
    const projects = results[0];

    console.log('Projects:');
    projects.forEach(project => console.log(project.id));
  })
  .catch(err => {
    console.error('ERROR:', err);
  });

Properties

Project

static

constructor

Project class.

See also
Project

getProjectsStream

Get a list of Resource/project objects as a readable object stream.

Parameter

query

object

Configuration object. See Resource#getProjects for a complete list of options.

Returns

stream 

Example

const Resource = require('@google-cloud/resource');
const resource = new Resource();

resource.getProjectsStream()
  .on('error', console.error)
  .on('data', function(project) {
    // `project` is a `Project` object.
  })
  .on('end', function() {
    // All projects retrieved.
  });

//-
// If you anticipate many results, you can end a stream early to prevent
// unnecessary processing and API requests.
//-
resource.getProjectsStream()
  .on('data', function(project) {
    this.end();
  });

Methods

createProject

createProject(id, options, callback)

Create a project.

This method only works if you are authenticated as yourself, e.g. using the gcloud SDK.

Parameter

id

string

ID of the project.

options

Optional

object

See a Project resource.

callback

Optional

function()

The callback function.

Values in callback have the following properties:

Parameter

err

error

An error returned while making this request.

Value may be null.

project

Project

The created Project object.

apiResponse

object

The full API response.

See also

Projects Overview

projects: create API Documentation

Example

const Resource = require('@google-cloud/resource');
const resource = new Resource();

const id = 'new-project-id';

resource.createProject(id, function(err, project, operation, apiResponse) {
  if (err) {
    // Error handling omitted.
  }

  // `project` is a new Project instance.
  // `operation` will emit `error` or `complete` when the status updates.

  operation
    .on('error', function(err) {})
    .on('complete', function() {
      // Project was created successfully!
    });
});

//-
// If the callback is omitted, we'll return a Promise.
//-
resource.createProject(id)
  .then(function(data) {
    const project = data[0];
    const operation = data[1];
    const apiResponse = data[2];

    return operation.promise();
  })
  .then(function(data) {
    const operationMetadata = data[0];

    // Project created successfully!
  });

getProjects

getProjects(options, callback)

Get a list of projects.

Parameter

options

Optional

object

Operation search options.

Values in options have the following properties:

Parameter

autoPaginate

Optional

boolean

Have pagination handled automatically. Default: true.

filter

Optional

string

An expression for filtering the results.

maxApiCalls

Optional

number

Maximum number of API calls to make.

maxResults

Optional

number

Maximum number of results to return.

pageSize

Optional

number

Maximum number of projects to return.

pageToken

Optional

string

A previously-returned page token representing part of the larger set of results to view.

callback

function()

The callback function.

Values in callback have the following properties:

Parameter

err

error

An error returned while making this request.

Value may be null.

operations

Array of Project

Project objects from your account.

apiResponse

object

The full API response.

See also

Projects Overview

projects: list API Documentation

Example

const Resource = require('@google-cloud/resource');
const resource = new Resource();

resource.getProjects(function(err, projects) {
  // `projects` is an array of `Project` objects.
});

//-
// To control how many API requests are made and page through the results
// manually, set `autoPaginate` to `false`.
//-
function callback(err, projects, nextQuery, apiResponse) {
  if (nextQuery) {
    // More results exist.
    resource.getProjects(nextQuery, callback);
  }
}

resource.getProjects({
  autoPaginate: false
}, callback);

//-
// If the callback is omitted, we'll return a Promise.
//-
resource.getProjects().then(function(data) {
  const projects = data[0];
});

operation

operation(name)

Get a reference to an existing operation.

Parameter

name

string

The name of the operation.

Throws

Error 

If a name is not provided.

Example

const Resource = require('@google-cloud/resource');
const resource = new Resource();

const operation = resource.operation('68850831366825');

project

project(id) returns Project

Create a Project object. See Resource#createProject to create a project.

Parameter

id

string

The ID of the project (eg: grape-spaceship-123).

Throws

Error 

If an ID is not provided.

Returns

Project 

Example

const Resource = require('@google-cloud/resource');
const resource = new Resource();

const project = resource.project('grape-spaceship-123');