This repository has been archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgandi.js
64 lines (60 loc) · 1.8 KB
/
gandi.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"use strict";
const request = require('request');
const messages = {
"e401": "Gandi Authorization declined, check GANDI_API_KEY",
"e404": "Record doesn't exists, Please create record in zone first (update only)",
"e4xx": "Unknown Error: ",
"error_domain": "Configuration error missing domain or record to manage",
"error_unknown": "Unknown Error",
};
module.exports = function(config) {
let callAPI = function(data, func) {
/*
curl -H"X-Api-Key: $APIKEY" \
https://dns.api.gandi.net/api/v5/domains/example.com/records
*/
let method = (func == undefined) ? 'GET': 'PUT';
let callback = (func == undefined) ? data: func;
if ( typeof callback !== 'function' ) {
throw("Unexpected null callback");
}
if (config.domain == undefined || config.record == undefined) {
callback(messages.error_domain);
}
let options = {
method: method,
url: config.endpoint + "/domains/" + config.domain + "/records/" + config.record + "/A",
headers: {
'X-Api-Key': config.api_key
},
json: true
};
if (method == 'PUT') {
options.body = data;
}
if (config.debug) console.debug(method, options.url);
request(options, function(err, res, body) {
if (config.debug) console.debug(err, body);
if(!res || err) {
callback(messages.error_unknown + ": " + err);
} else if (res.statusCode && res.statusCode == 401) {
callback(messages.e401);
} else if (res && res.statusCode && res.statusCode == 404) {
callback(messages.e404);
} else if (res.statusCode > 399) {
let error = body.message || body;
callback(messages.e4xx + error + " (" + res.statusCode + ")" , body);
} else {
callback(false, body);
}
});
};
return {
getRecord: function(callback) {
callAPI(callback);
},
updateRecord: function(data, callback) {
callAPI(data, callback);
}
};
};