new Zone()

Example

const {DNS} = require('@google-cloud/dns');
const dns = new DNS();

const zone = dns.zone('zone-id');

Properties

baseUrl  string

Default value
"/changes"

baseUrl  string

Default value
"/managedZones"

id  string

metadata  object

name  string

Methods

addRecords(record[, callback]) → Promise containing CreateChangeResponse

Add records to this zone. This is a convenience wrapper around Zone#createChange.

Parameters

Name Type Optional Description

record

(Record or Array of Record)

 

The Record object(s) to add.

callback

CreateChangeCallback

Yes

Callback function.

See also

ManagedZones: create API Documentation

Returns

Promise containing CreateChangeResponse 

change(id) → Change

Create a reference to a Change object in this zone.

Example

const {DNS} = require('@google-cloud/dns');
const dns = new DNS();
const zone = dns.zone('zone-id');
const change = zone.change('change-id');

Parameter

Name Type Optional Description

id

string

 

The change id.

Returns

Change 

create(metadata[, callback]) → Promise containing CreateZoneResponse

Create a zone.

Example

const {DNS} = require('@google-cloud/dns');
const dns = new DNS();

const config = {
  dnsName: 'example.com.',
  // ...
};

zone.create(config, (err, zone, apiResponse) => {
  if (!err) {
    // The zone was created successfully.
  }
});

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

Parameters

Name Type Optional Description

metadata

CreateZoneRequest

 

Metadata to set for the zone.

callback

CreateZoneCallback

Yes

Callback function.

Returns

Promise containing CreateZoneResponse 

createChange(config[, callback]) → Promise containing CreateChangeResponse

Create a change of resource record sets for the zone.

Example

const {DNS} = require('@google-cloud/dns');
const dns = new DNS();
const zone = dns.zone('zone-id');

const oldARecord = zone.record('a', {
  name: 'example.com.',
  data: '1.2.3.4',
  ttl: 86400
});

const newARecord = zone.record('a', {
  name: 'example.com.',
  data: '5.6.7.8',
  ttl: 86400
});

const config = {
  add: newARecord,
  delete: oldARecord
};

zone.createChange(config, (err, change, apiResponse) => {
  if (!err) {
    // The change was created successfully.
  }
});

//-
// If the callback is omitted, we'll return a Promise.
//-
zone.createChange(config).then((data) => {
  const change = data[0];
  const apiResponse = data[1];
});

Parameters

Name Type Optional Description

config

CreateChangeRequest

 

The configuration object.

callback

CreateChangeCallback

Yes

Callback function.

See also

ManagedZones: create API Documentation

Returns

Promise containing CreateChangeResponse 

delete([options][, callback]) → Promise containing DeleteZoneResponse

Delete the zone.

Only empty zones can be deleted. Set options.force to true to call Zone#empty before deleting the zone. Two API calls will then be made (one to empty, another to delete), which means this is not an atomic request.

Example

const {DNS} = require('@google-cloud/dns');
const dns = new DNS();
const zone = dns.zone('zone-id');

zone.delete((err, apiResponse) => {
  if (!err) {
    // The zone is now deleted.
  }
});

//-
// Use `force` to first empty the zone before deleting it.
//-
zone.delete({
  force: true
}, (err, apiResponse) => {
  if (!err) {
    // The zone is now deleted.
  }
});

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

Parameters

Name Type Optional Description

options

object

Yes

Configuration object.

Values in options have the following properties:

Name Type Optional Description

force

boolean

Yes

Empty the zone before deleting.

Defaults to false.

callback

DeleteZoneCallback

Yes

Callback function.

See also

ManagedZones: delete API Documentation

Returns

Promise containing DeleteZoneResponse 

deleteRecords(record[, callback]) → Promise containing CreateChangeResponse

Delete records from this zone. This is a convenience wrapper around Zone#createChange.

This method accepts Record objects or string record types in its place. This means that you can delete all A records or NS records, etc. If used this way, two API requests are made (one to get, then another to delete), which means the operation is not atomic and could result in unexpected changes.

Example

const {DNS} = require('@google-cloud/dns');
const dns = new DNS();
const zone = dns.zone('zone-id');

const oldARecord = zone.record('a', {
  name: 'example.com.',
  data: '1.2.3.4',
  ttl: 86400
});

const callback = (err, change, apiResponse) => {
  if (!err) {
    // Delete change modification was created.
  }
};

zone.deleteRecords(oldARecord, callback);

//-
// Delete multiple records at once.
//-
const oldNs1Record = zone.record('ns', {
  name: 'example.com.',
  data: 'ns-cloud1.googledomains.com.',
  ttl: 86400
});

const oldNs2Record = zone.record('ns', {
  name: 'example.com.',
  data: 'ns-cloud2.googledomains.com.',
  ttl: 86400
});

zone.deleteRecords([
  oldNs1Record,
  oldNs2Record
], callback);

//-
// Possibly a simpler way to perform the above change is deleting records
by
// type.
//-
zone.deleteRecords('ns', callback);

//-
// You can also delete records of multiple types.
//-
zone.deleteRecords(['ns', 'a'], callback);

//-
// If the callback is omitted, we'll return a Promise.
//-
zone.deleteRecords(oldARecord).then((data) => {
  const change = data[0];
  const apiResponse = data[1];
});

Parameters

Name Type Optional Description

record

(Record, Array of Record, or string)

 

If given a string, it is interpreted as a record type. All records that match that type will be retrieved and then deleted. You can also provide a Record object or array of Record objects.

callback

CreateChangeCallback

Yes

Callback function.

See also

ManagedZones: create API Documentation

Returns

Promise containing CreateChangeResponse 

empty([callback]) → Promise containing CreateChangeResponse

Emptying your zone means leaving only 'NS' and 'SOA' records. This method will first fetch the non-NS and non-SOA records from your zone using Zone#getRecords, then use Zone#createChange to create a deletion change.

Parameter

Name Type Optional Description

callback

CreateChangeCallback

Yes

Callback function.

See also

ManagedZones: create API Documentation

Returns

Promise containing CreateChangeResponse 

exists([callback]) → Promise containing ZoneExistsResponse

Check if the zone exists.

Example

const {DNS} = require('@google-cloud/dns');
const dns = new DNS();
const zone = dns.zone('zone-id');

zone.exists((err, exists) => {});

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

Parameter

Name Type Optional Description

callback

ZoneExistsCallback

Yes

Callback function.

Returns

Promise containing ZoneExistsResponse 

export(localPath[, callback]) → Promise containing ZoneExportResponse

Provide a path to a zone file to copy records into the zone.

Example

const {DNS} = require('@google-cloud/dns');
const dns = new DNS();
const zone = dns.zone('zone-id');

const zoneFilename = '/Users/stephen/zonefile.zone';

zone.export(zoneFilename, err => {
  if (!err) {
    // The zone file was created successfully.
  }
});

//-
// If the callback is omitted, we'll return a Promise.
//-
zone.export(zoneFilename).then(() => {});

Parameters

Name Type Optional Description

localPath

string

 

The fully qualified path to the zone file.

callback

ZoneExportCallback

Yes

Callback function.

See also

ResourceRecordSets: list API Documentation

Returns

Promise containing ZoneExportResponse 

get([options][, callback]) → Promise containing GetZoneResponse

Get a zone 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.

Example

const {DNS} = require('@google-cloud/dns');
const dns = new DNS();
const zone = dns.zone('zone-id');

zone.get((err, zone, apiResponse) => {
  // `zone.metadata` has been populated.
});

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

Parameters

Name Type Optional Description

options

options

Yes

Configuration object.

Values in options have the following properties:

Name Type Optional Description

autoCreate

boolean

Yes

Automatically create the object if it does not exist.

Defaults to false.

callback

GetZoneCallback

Yes

Callback function.

Returns

Promise containing GetZoneResponse 

getChanges([query][, callback]) → Promise containing GetChangesResponse

Get the list of changes associated with this zone. A change is an atomic update to a collection of records.

Example

const {DNS} = require('@google-cloud/dns');
const dns = new DNS();

const callback = (err, changes, nextQuery, apiResponse) => {
  // The `metadata` property is populated for you with the metadata at the
  // time of fetching.
  changes[0].metadata;

  // However, in cases where you are concerned the metadata could have
  // changed, use the `getMetadata` method.
  changes[0].getMetadata((err, metadata) => {});
  if (nextQuery) {
    // nextQuery will be non-null if there are more results.
    zone.getChanges(nextQuery, callback);
  }
};

const zone = dns.zone('zone-id');

zone.getChanges(callback);

//-
// If the callback is omitted, we'll return a Promise.
//-
zone.getChanges().then((data) => {
  const changes = data[0];
});

Parameters

Name Type Optional Description

query

GetChangesRequest

Yes

Query object for listing changes.

callback

GetChangesCallback

Yes

Callback function.

See also

Changes: get API Documentation

Returns

Promise containing GetChangesResponse 

getChangesStream([query]) → ReadableStream

Get the list of Change objects associated with this zone as a readable object stream.

Example

zone.getChangesStream()
  .on('error', console.error)
  .on('data', change => {
    // change is a Change object.
  })
  .on('end', () => {
    // All changes retrieved.
  });

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

Parameter

Name Type Optional Description

query

GetChangesRequest

Yes

Query object for listing changes.

Returns

ReadableStream 

A readable stream that emits Change instances.

getMetadata([callback]) → Promise containing GetZoneMetadataResponse

Get the metadata for the zone.

Example

const {DNS} = require('@google-cloud/dns');
const dns = new DNS();
const zone = dns.zone('zone-id');

zone.getMetadata((err, metadata, apiResponse) => {});

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

Parameter

Name Type Optional Description

callback

GetZoneMetadataCallback

Yes

Callback function.

See also

ManagedZones: get API Documentation

Returns

Promise containing GetZoneMetadataResponse 

getRecords([query][, callback]) → Promise containing GetRecordsResponse

Get the list of records for this zone.

Example

const {DNS} = require('@google-cloud/dns');
const dns = new DNS();

const callback = (err, records, nextQuery, apiResponse) => {
  if (!err) {
    // records is an array of Record objects.
  }

  if (nextQuery) {
    zone.getRecords(nextQuery, callback);
  }
};

const zone = dns.zone('zone-id');

zone.getRecords(callback);

//-
// Provide a query for further customization.
//-

// Get the namespace records for example.com.
const query = {
  name: 'example.com.',
  type: 'NS'
};

zone.getRecords(query, callback);

//-
// If you only want records of a specific type or types, provide them in
// place of the query object.
//-
zone.getRecords('ns', (err, records) => {
  if (!err) {
    // records is an array of NS Record objects in your zone.
  }
});

//-
// You can also specify multiple record types.
//-
zone.getRecords(['ns', 'a', 'cname'], (err, records) => {
  if (!err) {
    // records is an array of NS, A, and CNAME records in your zone.
  }
});

//-
// If the callback is omitted, we'll return a Promise.
//-
zone.getRecords(query).then(data => {
  const records = data[0];
});

Parameters

Name Type Optional Description

query

GetRecordsRequest

Yes

Query object for listing records.

callback

GetRecordsCallback

Yes

Callback function.

See also

ResourceRecordSets: list API Documentation

Returns

Promise containing GetRecordsResponse 

getRecordsStream([query]) → ReadableStream

Get the list of {module:dns/record} objects for this zone as a readable object stream.

Example

const {DNS} = require('@google-cloud/dns');
const dns = new DNS();
const zone = dns.zone('zone-id');

zone.getRecordsStream()
  .on('error', console.error)
  .on('data', record => {
    // record is a Record object.
  })
  .on('end', () => {
    // All records retrieved.
  });

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

Parameter

Name Type Optional Description

query

GetRecordsRequest

Yes

Query object for listing records.

Returns

ReadableStream 

A readable stream that emits Record instances.

import(localPath[, callback]) → Promise containing CreateChangeResponse

Copy the records from a zone file into this zone.

Example

const {DNS} = require('@google-cloud/dns');
const dns = new DNS();
const zone = dns.zone('zone-id');

const zoneFilename = '/Users/dave/zonefile.zone';

zone.import(zoneFilename, (err, change, apiResponse) => {
  if (!err) {
    // The change was created successfully.
  }
});

//-
// If the callback is omitted, we'll return a Promise.
//-
zone.import(zoneFilename).then(data => {
  const change = data[0];
  const apiResponse = data[1];
});

Parameters

Name Type Optional Description

localPath

string

 

The fully qualified path to the zone file.

callback

CreateChangeCallback

Yes

Callback function.

See also

ManagedZones: create API Documentation

Returns

Promise containing CreateChangeResponse 

record(type, metadata) → Record

A Record object can be used to construct a record you want to add to your zone, or to refer to an existing one.

Note that using this method will not itself make any API requests. You will use the object returned in other API calls, for example to add a record to your zone or to delete an existing one.

Example

const {DNS} = require('@google-cloud/dns');
const dns = new DNS();

const zone = dns.zone('zone-id');

//-
// Reference an existing record to delete from your zone.
//-
const oldARecord = zone.record('a', {
  name: 'example.com.',
  data: '1.2.3.4',
  ttl: 86400
});

//-
// Construct a record to add to your zone.
//-
const newARecord = zone.record('a', {
  name: 'example.com.',
  data: '5.6.7.8',
  ttl: 86400
});

//-
// Use these records together to create a change.
//-
zone.createChange({
  add: newARecord,
  delete: oldARecord
}, (err, change, apiResponse) => {});

Parameters

Name Type Optional Description

type

string

 

The type of record to construct or the type of record you are referencing.

metadata

object

 

The metadata of this record.

Values in metadata have the following properties:

Name Type Optional Description

name

string

 

The name of the record, e.g. www.example.com..

data

Array of string

 

Defined in RFC 1035, section 5 and RFC 1034, section 3.6.1.

ttl

number

 

Seconds that the resource is cached by resolvers.

Returns

Record 

replaceRecords(recordTypes, newRecords[, callback]) → Promise containing CreateChangeResponse

Provide a record type that should be deleted and replaced with other records.

This is not an atomic request. Two API requests are made (one to get records of the type that you've requested, then another to replace them), which means the operation is not atomic and could result in unexpected changes.

Example

const {DNS} = require('@google-cloud/dns');
const dns = new DNS();

const zone = dns.zone('zone-id');

const newNs1Record = zone.record('ns', {
  name: 'example.com.',
  data: 'ns-cloud1.googledomains.com.',
  ttl: 86400
});

const newNs2Record = zone.record('ns', {
  name: 'example.com.',
  data: 'ns-cloud2.googledomains.com.',
  ttl: 86400
});

const newNsRecords = [
  newNs1Record,
  newNs2Record
];

zone.replaceRecords('ns', newNsRecords, (err, change, apiResponse) => {
  if (!err) {
    // The change was created successfully.
  }
});

//-
// If the callback is omitted, we'll return a Promise.
//-
zone.replaceRecords('ns', newNsRecords).then(data => {
  const change = data[0];
  const apiResponse = data[1];
});

Parameters

Name Type Optional Description

recordTypes

(string or Array of string)

 

The type(s) of records to replace.

newRecords

(Record or Array of Record)

 

The Record object(s) to add.

callback

CreateChangeCallback

Yes

Callback function.

See also

ManagedZones: create API Documentation

Returns

Promise containing CreateChangeResponse