Update 'lib/index.js'

Replaced dummy text with gandi DNS API methods for zones, set, remove, get functionality
This commit is contained in:
jarombridges 2019-07-11 02:39:15 +00:00
parent 93a6c3bda3
commit 780d4af407
1 changed files with 95 additions and 15 deletions

View File

@ -1,29 +1,109 @@
'use strict'; 'use strict';
var request; var request;
var defaults = {}; var defaults = {
baseUrl: 'https://dns.api.gandi.net/api/v5/'
};
module.exports.create = function(config) { module.exports.create = function(config) {
var baseUrl = (config.baseUrl || defaults.baseUrl).replace(/\/$/, '');
var authtoken = config.token;
var request;
return { return {
init: function(opts) { init: function(opts) {
request = opts.request; request = opts.request;
return null; return null;
}, },
zones: function(data) {
//console.info('List Zones', data); zones: function(opts) {
throw Error('listing zones not implemented'); console.log(opts);
return request({
method: 'GET',
url: baseUrl + '/zones',
headers: { 'X-Api-Key': authtoken },
json: true
}).then(function(resp) {
return resp.body.map(function (zone) { return zone.name; });;
});;
}, },
set: function(data) {
// console.info('Add TXT', data); set: function(opts) {
throw Error('setting TXT not implemented'); console.log(opts);
},
remove: function(data) { return request({
// console.info('Remove TXT', data); method: 'GET',
throw Error('removing TXT not implemented'); url: baseUrl + '/domains/' + opts.challenge.dnsZone + '/records/' + opts.challenge.dnsPrefix + '/TXT',
}, headers: { 'X-Api-Key': authtoken},
get: function(data) { json: true
// console.info('List TXT', data); }).then(function(resp) {
throw Error('listing TXTs not implemented'); if (resp.body.cause === 'Not Found') {
return request({
method: 'POST',
url: baseUrl + '/domains/' + opts.challenge.dnsZone + '/records',
headers: { 'X-Api-Key': authtoken },
json: {'rrset_name': opts.challenge.dnsPrefix,
'rrset_type': 'TXT',
'rrset_ttl': 300,
'rrset_values': [opts.challenge.dnsAuthorization] }
})
} }
else {
const body = resp.body
let value = body.rrset_values.map(x => JSON.parse(x))
if (body.rrset_values) {
return request({
method: 'PUT',
url: baseUrl + '/domains/' + opts.challenge.dnsZone + '/records/' + opts.challenge.dnsPrefix + '/TXT',
headers: { 'X-Api-Key': authtoken},
json: {'rrset_ttl': 300,
'rrset_values': value.concat([opts.challenge.dnsAuthorization] )
}
})
}
}
})
;;
},
remove: function(opts) {
console.log(opts);
return request({
method: 'DELETE',
url: baseUrl + '/domains/' + opts.challenge.dnsZone + '/records/' + opts.challenge.dnsPrefix + '/TXT',
headers: { 'X-Api-Key': authtoken },
json: true
})
}
,
get: function(opts) {
console.log(opts);
return request({
method: 'GET',
url: baseUrl + '/domains/' + opts.challenge.dnsZone + '/records/' + opts.challenge.dnsPrefix,
headers: { 'X-Api-Key': authtoken},
json: true
}).then(function(resp) {
const body = resp.body
if (body.length > 0) {
let value = body[0].rrset_values.map(x => JSON.parse(x)).filter(field => field === opts.challenge.dnsAuthorization)
if (value !== []) {
return { dnsAuthorization: value[0]}
}
else {
return null
}
}
else {
return null
}
})
}
})
}; };
}; };