Constructor

DNS

new DNS(options)

Parameter

options

Optional

ClientConfig

Configuration options.

See also

What is Cloud DNS?

Examples

Import the client library

const {DNS} = require('@google-cloud/dns');
<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 dns = new DNS();
<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 dns = new DNS({ projectId:
'your-project-id', keyFilename: '/path/to/keyfile.json'
});

Full quickstart example:

async function quickstart(
  projectId = 'YOUR_PROJECT_ID' // Your GCP project Id
) {
  // Imports the Google Cloud client library
  const {DNS} = require('@google-cloud/dns');

  // Creates a client
  const dns = new DNS({projectId});

  // Lists all zones in the current project
  const [zones] = await dns.getZones();
  console.log('Zones:');
  zones.forEach(zone => console.log(zone.name));
}

Methods

createZone

createZone(name, config, callback) returns Promise containing CreateZoneResponse

Create a managed zone.

Parameter

name

string

Name of the zone to create, e.g. "my-zone".

config

CreateZoneRequest

Config to set for the zone.

callback

Optional

CreateZoneCallback

Callback function.

See also

ManagedZones: create API Documentation

Zone#create
Throws

error 

If a zone name is not provided.

error 

If a zone dnsName is not provided.

Error 

If a name is not provided.

Returns

Promise containing CreateZoneResponse 

Example

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

const config = {
  dnsName: 'example.com.', // note the period at the end of the domain.
  description: 'This zone is awesome!'
};

dns.createZone('my-awesome-zone', config, (err, zone, apiResponse) => {
  if (!err) {
    // The zone was created successfully.
  }
});

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

getZones

getZones(query, callback) returns Promise containing GetZonesResponse

Gets a list of managed zones for the project.

Parameter

query

Optional

GetZonesRequest

Query object for listing zones.

callback

Optional

GetZonesCallback

Callback function.

See also

ManagedZones: list API Documentation

Returns

Promise containing GetZonesResponse 

Example

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

dns.getZones((err, zones, apiResponse) {});

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

getZonesStream

getZonesStream(query) returns ReadableStream

Get Zone objects for all of the zones in your project as a readable object stream.

Parameter

query

Optional

GetZonesRequest

Query object for listing zones.

Returns

ReadableStream 

A readable stream that emits Zone instances.

Example

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

dns.getZonesStream()
  .on('error', console.error)
  .on('data', function(zone) {
    // zone is a Zone object.
  })
  .on('end', () => {
    // All zones retrieved.
  });

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

zone

zone(name) returns Zone

Get a reference to a Zone.

Parameter

name

string

The unique name of the zone.

See also
Zone
Throws

error 

If a zone name is not provided.

Returns

Zone 

Example

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

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