Skip to content

Commit

Permalink
Implement Service & Service Object for DNS
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Nov 14, 2015
1 parent 7374122 commit ce2dce4
Show file tree
Hide file tree
Showing 7 changed files with 462 additions and 477 deletions.
117 changes: 91 additions & 26 deletions lib/dns/change.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@

'use strict';

var nodeutil = require('util');

/**
* @type {module:common/serviceObject}
* @private
*/
var ServiceObject = require('../common/service-object.js');

/**
* @constructor
* @alias module:dns/change
Expand All @@ -39,50 +47,107 @@
* var change = zone.change('change-id');
*/
function Change(zone, id) {
this.zoneName = zone.name;
this.id = id;
var methods = {
/**
* Check if the change exists.
*
* @param {function} callback - The callback function.
* @param {?error} callback.err - An error returned while making this
* request.
* @param {boolean} callback.exists - Whether the change exists or not.
*
* @example
* change.exists(function(err, exists) {});
*/
exists: true,

/**
* Get a change 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.
*
* @param {options=} options - Configuration object.
* @param {boolean} options.autoCreate - Automatically create the object if
* it does not exist. Default: `false`
*
* @example
* change.get(function(err, change, apiResponse) {
* // `change.metadata` has been populated.
* });
*/
get: true,

/**
* Get the metadata for the change in the zone.
*
* @resource [Changes: get API Documentation]{@link https://cloud.google.com/dns/api/v1/changes/get}
*
* @param {function} callback - The callback function.
* @param {?error} callback.err - An API error.
* @param {?object} callback.metadata - Metadata of the change from the API.
* @param {object} callback.apiResponse - Raw API response.
*
* @example
* change.getMetadata(function(err, metadata, apiResponse) {
* if (!err) {
* // metadata = {
* // kind: 'dns#change',
* // additions: [{...}],
* // deletions: [{...}],
* // startTime: '2015-07-21T14:40:06.056Z',
* // id: '1',
* // status: 'done'
* // }
* }
* });
*/
getMetadata: true
};

this.metadata = {};
this.makeReq_ = zone.dns.makeReq_.bind(zone.dns);
ServiceObject.call(this, {
parent: zone,
baseUrl: '/changes',
id: id,
methods: methods
});
}

nodeutil.inherits(Change, ServiceObject);

/**
* Get the metadata for the change in the zone.
*
* @resource [Changes: get API Documentation]{@link https://cloud.google.com/dns/api/v1/changes/get}
* Create a change.
*
* @param {function} callback - The callback function.
* @param {?error} callback.err - An API error.
* @param {?object} callback.metadata - Metadata of the change from the API.
* @param {object} callback.apiResponse - Raw API response.
* @param {object} config - See {module:dns/zone#createChange}.
*
* @example
* change.getMetadata(function(err, metadata, apiResponse) {
* var config = {
* add: {
* // ...
* }
* };
*
* change.create(config, function(err, change, apiResponse) {
* if (!err) {
* // metadata = {
* // kind: 'dns#change',
* // additions: [{...}],
* // deletions: [{...}],
* // startTime: '2015-07-21T14:40:06.056Z',
* // id: '1',
* // status: 'done'
* // }
* // The change was created successfully.
* }
* });
*/
Change.prototype.getMetadata = function(callback) {
Change.prototype.create = function(config, callback) {
var self = this;
var path = '/managedZones/' + this.zoneName + '/changes/' + this.id;

this.makeReq_('GET', path, null, null, function(err, resp) {
this.parent.createChange(config, function(err, change, apiResponse) {
if (err) {
callback(err, null, resp);
callback(err, null, apiResponse);
return;
}

self.metadata = resp;
self.id = change.id;
self.metadata = change.metadata;

callback(null, self.metadata, resp);
callback(null, self, apiResponse);
});
};

Expand Down
81 changes: 29 additions & 52 deletions lib/dns/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,16 @@

'use strict';

var arrify = require('arrify');
var extend = require('extend');
var is = require('is');
var nodeutil = require('util');

/**
* @type {module:common/service}
* @private
*/
var Service = require('../common/service.js');

/**
* @type {module:common/streamrouter}
Expand All @@ -41,21 +49,6 @@ var util = require('../common/util.js');
*/
var Zone = require('./zone.js');

/**
* @const {string} Base URL for DNS API.
* @private
*/
var DNS_BASE_URL = 'https://www.googleapis.com/dns/v1/projects/';

/**
* @const {array} Required scopes for the DNS API.
* @private
*/
var SCOPES = [
'https://www.googleapis.com/auth/ndev.clouddns.readwrite',
'https://www.googleapis.com/auth/cloud-platform'
];

/**
* [Google Cloud DNS](https://cloud.google.com/dns/what-is-cloud-dns) is a high-
* performance, resilient, global DNS service that provides a cost-effective way
Expand All @@ -82,16 +75,19 @@ function DNS(options) {
return new DNS(options);
}

this.makeAuthenticatedRequest_ = util.makeAuthenticatedRequestFactory({
credentials: options.credentials,
keyFile: options.keyFilename,
scopes: SCOPES,
email: options.email
});
var config = {
baseUrl: 'https://www.googleapis.com/dns/v1',
scopes: [
'https://www.googleapis.com/auth/ndev.clouddns.readwrite',
'https://www.googleapis.com/auth/cloud-platform'
]
};

this.projectId_ = options.projectId;
Service.call(this, config, options);
}

nodeutil.inherits(DNS, Service);

/**
* Create a managed zone.
*
Expand Down Expand Up @@ -135,7 +131,11 @@ DNS.prototype.createZone = function(name, config, callback) {
// Required by the API.
config.description = config.description || '';

this.makeReq_('POST', '/managedZones', null, config, function(err, resp) {
this.request({
method: 'POST',
uri: '/managedZones',
json: config
}, function(err, resp) {
if (err) {
callback(err, null, resp);
return;
Expand Down Expand Up @@ -173,13 +173,16 @@ DNS.prototype.getZones = function(query, callback) {
query = {};
}

this.makeReq_('GET', '/managedZones', query, null, function(err, resp) {
this.request({
uri: '/managedZones',
qs: query
}, function(err, resp) {
if (err) {
callback(err, null, null, resp);
return;
}

var zones = (resp.managedZones || []).map(function(zone) {
var zones = arrify(resp.managedZones).map(function(zone) {
var zoneInstance = self.zone(zone.name);
zoneInstance.metadata = zone;
return zoneInstance;
Expand All @@ -198,7 +201,7 @@ DNS.prototype.getZones = function(query, callback) {
};

/**
* Create a zone object representing an existing managed zone.
* Create a zone object representing a managed zone.
*
* @throws {error} If a zone name is not provided.
*
Expand All @@ -216,32 +219,6 @@ DNS.prototype.zone = function(name) {
return new Zone(this, name);
};

/**
* Make a new request object from the provided arguments and wrap the callback
* to intercept non-successful responses.
*
* @private
*
* @param {string} method - Action.
* @param {string} path - Request path.
* @param {*} query - Request query object.
* @param {*} body - Request body contents.
* @param {function} callback - The callback function.
*/
DNS.prototype.makeReq_ = function(method, path, query, body, callback) {
var reqOpts = {
method: method,
qs: query,
uri: DNS_BASE_URL + this.projectId_ + path
};

if (body) {
reqOpts.json = body;
}

this.makeAuthenticatedRequest_(reqOpts, callback);
};

/*! Developer Documentation
*
* These methods can be used with either a callback or as a readable object
Expand Down
Loading

0 comments on commit ce2dce4

Please sign in to comment.