diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..1ceb529 --- /dev/null +++ b/.jshintrc @@ -0,0 +1,17 @@ +{ "node": true +, "browser": true +, "jquery": true +, "globals": { "Promise": true } + +, "indent": 2 +, "onevar": true +, "laxcomma": true +, "laxbreak": true +, "curly": true +, "nonbsp": true + +, "eqeqeq": true +, "immed": true +, "undef": true +, "latedef": "nofunc" +} diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..1ceb529 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,17 @@ +{ "node": true +, "browser": true +, "jquery": true +, "globals": { "Promise": true } + +, "indent": 2 +, "onevar": true +, "laxcomma": true +, "laxbreak": true +, "curly": true +, "nonbsp": true + +, "eqeqeq": true +, "immed": true +, "undef": true +, "latedef": "nofunc" +} diff --git a/lib/index.js b/lib/index.js index 81f602e..23e3256 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,107 +1,128 @@ -'use strict'; +"use strict"; //var auth = require('./auth.js'); var defaults = { - baseUrl: 'https://www.googleapis.com/dns/v1/' + baseUrl: "https://www.googleapis.com/dns/v1/" }; module.exports.create = function(config) { - var request; - var baseUrl = (config.baseUrl || defaults.baseUrl).replace(/\/$/, ''); - var token = config.token; - var sa = getServiceAccount(config); + var request; + var baseUrl = (config.baseUrl || defaults.baseUrl).replace(/\/$/, ""); + var token = config.token; + var sa = getServiceAccount(config); - return { - init: function(opts) { - request = opts.request; - return null; - }, - zones: function(data) { - //console.info('List Zones', data); - return api({ - url: baseUrl + '/projects/' + sa.project_id + '/managedZones', - json: true - }).then(function(resp) { - return resp.body.managedZones.map(function(zone) { - // slice out the leading and trailing single quotes, and the trailing dot - // (assuming that all 'dnsName's probably look the same) - var name = zone.dnsName.slice(0, zone.dnsName.length - 1); - console.log(`the is name ${name}`); - return name; - }); - }); - }, - set: function(data) { - console.info('Add TXT', data); - var ch = data.challenge; - return api({ - method: 'POST', - url: baseUrl + '/projects/' + sa.project_id + '/managedZones/' + ch.dnsZone + '/changes', - json: { - "kind": "dns#change", - "additions": [ - { - "kind": "dns#resourceRecordSet", - "name": ch.dnsHost, - "type": "TXT", - "ttl": 300, // TODO test for lowest allowed value - "rrdatas": [ ch.dnsAuthorization ], - "signatureRrdatas": [] - } - ], - "deletions": [], - //"startTime": "string", - //"id": "string", - //"status": "string", - //"isServing": true - } - }) - throw Error('setting TXT not implemented'); - - }, - remove: function(data) { - // console.info('Remove TXT', data); - throw Error('removing TXT not implemented'); - }, - get: function(data) { - // console.info('List TXT', data); - throw Error('listing TXTs not implemented'); - } - }; + var plugin = { + init: function(opts) { + request = opts.request; + return null; + }, + zones: function(data) { + //console.info('List Zones', data); + return api({ + url: baseUrl + "/projects/" + sa.project_id + "/managedZones", + json: true + }).then(function(resp) { + return resp.body.managedZones.map(function(zone) { + // slice out the trailing dot. + // ex: "example.com." => "example.com" + // (assuming that all 'dnsName's probably look the same) + var name = zone.dnsName.slice(0, zone.dnsName.length - 1); + console.log("the is name", name); + return name; + }); + }); + }, + set: function(data) { + console.info("Add TXT", data); + var ch = data.challenge; + return getZoneNameByDnsName(ch.dnsZone) + .then(function(gZoneName) { + return api({ + method: "POST", + url: + baseUrl + + "/projects/" + + sa.project_id + + "/managedZones/" + + gZoneName + + "/changes", + json: { + kind: "dns#change", + additions: [ + { + kind: "dns#resourceRecordSet", + name: ch.dnsHost, + type: "TXT", + ttl: 300, // TODO test for lowest allowed value + rrdatas: [ch.dnsAuthorization], + signatureRrdatas: [] + } + ], + deletions: [] + //"startTime": "string", + //"id": "string", + //"status": "string", + //"isServing": true + } + }); + }) + .then(function(resp) { + if (resp.body.error) { + console.error(resp.headers); + console.error(resp.body); + throw new Error(resp.body.error); + } + }); + }, + remove: function(data) { + // console.info('Remove TXT', data); + throw Error("removing TXT not implemented"); + }, + get: function(data) { + // console.info('List TXT', data); + throw Error("listing TXTs not implemented"); + } + }; - function api(opts) { - //return auth.getToken(sa).then(function(token) { - opts.headers = opts.headers || {}; - opts.headers.Authorization = 'Bearer ' + token; - return request(opts).then(function(resp){ - console.log(resp.headers); - console.log(resp.body); - return resp - } - - ); - //}); - } + return plugin; - function getServiceAccount(config) { - var saPath = - config.serviceAccountPath || - process.env.GOOGLE_APPLICATION_CREDENTIALS; - var sa = config.serviceAccount || require(saPath); + function api(opts) { + //return auth.getToken(sa).then(function(token) { + opts.headers = opts.headers || {}; + opts.headers.Authorization = "Bearer " + token; + return request(opts).then(function(resp) { + console.log(resp.headers); + console.log(resp.body); + return resp; + }); + //}); + } - if ( - !sa || - !( - sa.private_key && - sa.private_key_id && - sa.client_email && - sa.project_id - ) - ) { - throw new Error( - 'missing or incomplete service_account.json: set serviceAccount serviceAccountPath' - ); - } - return sa; - } + function getZoneNameByDnsName(dnsZone) { + return api({ + url: baseUrl + "/projects/" + sa.project_id + "/managedZones", + json: true + }).then(function(resp) { + // Google Zone Name is NOT the DNS Name, but an arbitrary string + return resp.body.managedZones.filter(function(zone) { + return dnsZone + "." === zone.dnsName; + })[0].name; + }); + } + + function getServiceAccount(config) { + var saPath = + config.serviceAccountPath || process.env.GOOGLE_APPLICATION_CREDENTIALS; + var sa = config.serviceAccount || require(saPath); + + if ( + !sa || + !(sa.private_key && sa.private_key_id && sa.client_email && sa.project_id) + ) { + throw new Error( + "missing or incomplete service_account.json: set serviceAccount serviceAccountPath" + ); + } + return sa; + } };