Constructor

Service

new Service(compute, name)

An HTTP(S) load balancing backend service is a centralized service for managing backends, which in turn manage instances that handle user requests. You configure your load balancing service to route requests to your backend service. The backend service in turn knows which instances it can use, how much traffic they can handle, and how much traffic they are currently handling. In addition, the backend service monitors health checking and does not send traffic to unhealthy instances.

Parameter

compute

Compute

The Compute instance this service inherits from.

name

string

Name of the service.

See also

Backend Services Overview

Example

const Compute = require('@google-cloud/compute');
const compute = new Compute();
const service = const.service('service-name');

Properties

compute

Compute

The parent Compute instance of this Service instance.

id

string

name

string

Methods

create

create(config)

Create a backend service.

Parameter

config

object

See Compute#createService.

Example

const Compute = require('@google-cloud/compute');
const compute = new Compute();
const service = const.service('service-name');

const config = {
  backends: [
    {
      group: 'URL of an Instance Group resource'
    }
  ],
  healthChecks: [
    'URL of an HTTP/HTTPS health check resource'
  ]
};

service.create(config, function(err, service, operation, apiResponse) {
  // `service` is a Service object.

  // `operation` is an Operation object that can be used to check the
  // of the request.
});

//-
// If the callback is omitted, we'll return a Promise.
//-
service.create(config).then(function(data) {
  const service = data[0];
  const operation = data[1];
  const apiResponse = data[2];
});

delete

delete(callback)

Delete the backend service.

Parameter

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.

operation

Operation

An operation object that can be used to check the status of the request.

apiResponse

object

The full API response.

See also

BackendServices: delete API Documentation

Example

const Compute = require('@google-cloud/compute');
const compute = new Compute();
const service = const.service('service-name');

service.delete(function(err, operation, apiResponse) {
  // `operation` is an Operation object that can be used to check the status
  // of the request.
});

//-
// If the callback is omitted, we'll return a Promise.
//-
service.delete().then(function(data) {
  const operation = data[0];
  const apiResponse = data[1];
});

exists

exists(callback)

Check if the backend service exists.

Parameter

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.

exists

boolean

Whether the backend service exists or not.

Example

const Compute = require('@google-cloud/compute');
const compute = new Compute();
const service = const.service('service-name');

service.exists(function(err, exists) {});

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

get

get(options)

Get a Service object if it exists.

You may optionally use this to "get or create" an object by providing an object with autoCreate set to true. Any extra configuration that is normally required for the create method must be contained within this object as well.

Parameter

options

Optional

options

Configuration object.

Values in options have the following properties:

Parameter

autoCreate

boolean

Automatically create the object if it does not exist. Default: false

Example

const Compute = require('@google-cloud/compute');
const compute = new Compute();
const service = const.service('service-name');

service.get(function(err, service, apiResponse) {
  // `service` is a Service object.
});

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

getHealth

getHealth(group, callback)

Get the most recent health check results.

Parameter

group

(string or object)

The fully-qualified URL of an Instance Group resource.

Values in group have the following properties:

Parameter

name

string

The name of the Instance Group resource.

zone

(Zone or string)

The name of the zone or a Zone object.

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.

status

Array of object

A list of health checks and their corresponding status.

apiResponse

object

The full API response.

See also

BackendServices: getHealth API Documentation

Example

const Compute = require('@google-cloud/compute');
const compute = new Compute();
const service = const.service('service-name');

const group = {
  name: 'instance-group-name',
  zone: 'us-central1-a'
};

service.getHealth(group, function(err, status, apiResponse) {
  if (!err) {
    // status = [
    //   {
    //      ipAddress: '...',
    //      instance: '...',
    //      healthState: '...',
    //      port: '...'
    //   }
    // ]
  }
});

//-
// If the callback is omitted, we'll return a Promise.
//-
service.getHealth(group).then(function(data) {
  const status = data[0];
  const apiResponse = data[1];
});

getMetadata

getMetadata(callback)

Get the metadata of this backend service.

Parameter

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.

metadata

object

The service's metadata.

apiResponse

object

The full API response.

See also

BackendService Resource

BackendService: get API Documentation

Example

const Compute = require('@google-cloud/compute');
const compute = new Compute();
const service = const.service('service-name');

service.getMetadata(function(err, metadata, apiResponse) {});

//-
// If the callback is omitted, we'll return a Promise.
//-
service.getMetadata().then(function(data) {
  const metadata = data[0];
  const apiResponse = data[1];
});

setMetadata

setMetadata(metadata, callback)

Set the backend service's metadata.

Parameter

metadata

object

See a BackendService 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.

operation

Operation

An operation object that can be used to check the status of the request.

apiResponse

object

The full API response.

See also

BackendService Resource

Example

const Compute = require('@google-cloud/compute');
const compute = new Compute();
const service = const.service('service-name');

const metadata = {
  description: 'New description'
};

service.setMetadata(metadata, function(err, operation, apiResponse) {
  // `operation` is an Operation object that can be used to check the status
  // of the request.
});

//-
// If the callback is omitted, we'll return a Promise.
//-
service.setMetadata(metadata).then(function(data) {
  const operation = data[0];
  const apiResponse = data[1];
});