Resource

Resource

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.

Constructor

new Resource(optionsopt)

Parameters:
Name Type Attributes Description
options ClientConfig <optional>

Configuration options.

See:
Examples

Import the client library

const {Resource} = require('@google-cloud/resource');
<caption>Create a client that uses <a
href="https://cloud.google.com/docs/authentication/production#providing_credentials_to_your_application">Application
Default Credentials (ADC)</a>:</caption> const resource = new Resource();
<caption>Create a client with <a
href="https://cloud.google.com/docs/authentication/production#obtaining_and_providing_service_account_credentials_manually">explicit
credentials</a>:</caption> 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');

  // Creates a client
  const resource = new Resource();

  async function quickstart() {
    // Lists current projects
    const [projects] = await resource.getProjects();

    console.log('Projects:');
    projects.forEach(project => console.log(project.id));
  }
  quickstart();

Members

getProjectsStream

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

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

resource.getProjectsStream()
  .on('error', console.error)
  .on('data', project => {
    // `project` is a `Project` object.
  })
  .on('end', () => {
    // 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

operation(name)

Get a reference to an existing operation.

Parameters:
Name Type Description
name string

The name of the operation.

Throws:

If a name is not provided.

Type
Error
Example
const {Resource} = require('@google-cloud/resource');
const resource = new Resource();

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

project(id) → {Project}

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

Parameters:
Name Type Description
id string

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

Throws:

If an ID is not provided.

Type
Error
Example
const {Resource} = require('@google-cloud/resource');
const resource = new Resource();

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