2016-05-24 21:36:06 +00:00
|
|
|
import "typings-global";
|
2016-04-27 00:20:27 +00:00
|
|
|
import plugins = require("./cflare.plugins");
|
|
|
|
import helpers = require("./cflare.classes.helpers");
|
|
|
|
|
2016-05-15 17:51:48 +00:00
|
|
|
export class CflareAccount {
|
2016-04-27 00:20:27 +00:00
|
|
|
private authEmail:string;
|
|
|
|
private authKey:string;
|
2016-04-27 01:08:14 +00:00
|
|
|
private authCheck(){
|
|
|
|
return (this.authEmail && this.authKey); //check if auth is available
|
|
|
|
}
|
2016-04-27 00:20:27 +00:00
|
|
|
constructor(){
|
|
|
|
|
|
|
|
};
|
2016-04-27 01:08:14 +00:00
|
|
|
auth(optionsArg:{email:string,key:string}){
|
|
|
|
this.authEmail = optionsArg.email;
|
|
|
|
this.authKey = optionsArg.key;
|
|
|
|
}
|
2016-05-16 01:29:29 +00:00
|
|
|
getZoneId(domainName:string){
|
|
|
|
let done = plugins.q.defer();
|
|
|
|
this.listZones(domainName)
|
|
|
|
.then((responseArg) => {
|
|
|
|
let filteredResponse = responseArg.result.filter((zoneArg)=>{
|
|
|
|
return zoneArg.name === domainName;
|
|
|
|
});
|
|
|
|
if (filteredResponse.length >= 1){
|
|
|
|
done.resolve(filteredResponse[0].id);
|
|
|
|
} else {
|
|
|
|
plugins.beautylog.error("the domain " + domainName.blue + " does not appear to be in this account!");
|
|
|
|
done.reject(undefined);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return done.promise;
|
|
|
|
}
|
|
|
|
getRecord(domainNameArg:string,typeArg:string){
|
|
|
|
let done = plugins.q.defer();
|
2016-05-25 04:32:56 +00:00
|
|
|
let domain = new plugins.smartstring.Domain(domainNameArg);
|
2016-05-25 05:06:06 +00:00
|
|
|
this.listRecords(domain.zoneName)
|
2016-05-16 01:29:29 +00:00
|
|
|
.then((responseArg) => {
|
|
|
|
let filteredResponse = responseArg.result.filter((recordArg) => {
|
|
|
|
return (recordArg.type == typeArg && recordArg.name == domainNameArg);
|
|
|
|
})
|
2016-06-21 17:04:43 +00:00
|
|
|
done.resolve(filteredResponse[0]);
|
2016-05-16 01:29:29 +00:00
|
|
|
})
|
|
|
|
return done.promise;
|
|
|
|
};
|
2016-05-24 21:36:06 +00:00
|
|
|
createRecord(domainNameArg:string,typeArg:string,contentArg:string){
|
2016-04-27 01:08:14 +00:00
|
|
|
let done = plugins.q.defer();
|
2016-05-25 04:26:48 +00:00
|
|
|
let domain = new plugins.smartstring.Domain(domainNameArg);
|
2016-05-25 05:06:06 +00:00
|
|
|
this.getZoneId(domain.zoneName)
|
2016-05-24 21:36:06 +00:00
|
|
|
.then((domainIdArg)=>{
|
|
|
|
let dataObject = {
|
2016-05-25 04:26:48 +00:00
|
|
|
name: domain.fullName,
|
2016-05-24 21:36:06 +00:00
|
|
|
type: typeArg,
|
|
|
|
content: contentArg
|
|
|
|
};
|
|
|
|
this.request("POST","/zones/" + domainIdArg + "/dns_records",dataObject)
|
|
|
|
.then(function(responseArg){
|
|
|
|
done.resolve(responseArg);
|
|
|
|
});
|
|
|
|
});
|
2016-04-27 01:08:14 +00:00
|
|
|
return done.promise;
|
|
|
|
};
|
2016-05-24 21:36:06 +00:00
|
|
|
removeRecord(domainNameArg:string,typeArg:string){
|
2016-04-27 01:08:14 +00:00
|
|
|
let done = plugins.q.defer();
|
2016-05-25 04:26:48 +00:00
|
|
|
let domain = new plugins.smartstring.Domain(domainNameArg);
|
2016-06-21 17:04:43 +00:00
|
|
|
this.getRecord(domain.fullName,typeArg)
|
2016-05-25 04:26:48 +00:00
|
|
|
.then((responseArg) => {
|
2016-06-21 17:04:43 +00:00
|
|
|
if(responseArg){
|
|
|
|
let requestRoute:string = "/zones/" + responseArg.zone_id + "/dns_records/" + responseArg.id;
|
|
|
|
this.request("DELETE",requestRoute)
|
|
|
|
.then((responseArg) => {
|
|
|
|
done.resolve(responseArg);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
done.reject();
|
|
|
|
}
|
2016-05-25 04:26:48 +00:00
|
|
|
});
|
2016-04-27 01:08:14 +00:00
|
|
|
return done.promise;
|
|
|
|
};
|
2016-05-16 01:29:29 +00:00
|
|
|
updateRecord(domainNameArg:string,typeArg:string,valueArg){
|
|
|
|
let done = plugins.q.defer();
|
2016-05-25 05:06:06 +00:00
|
|
|
let domain = new plugins.smartstring.Domain(domainNameArg);
|
2016-05-16 01:29:29 +00:00
|
|
|
return done.promise;
|
|
|
|
};
|
2016-05-24 21:36:06 +00:00
|
|
|
listRecords(domainNameArg:string){
|
2016-04-27 01:08:14 +00:00
|
|
|
let done = plugins.q.defer();
|
2016-05-25 05:06:06 +00:00
|
|
|
let domain = new plugins.smartstring.Domain(domainNameArg);
|
|
|
|
this.getZoneId(domain.zoneName)
|
2016-05-16 01:29:29 +00:00
|
|
|
.then((domainIdArg)=>{
|
|
|
|
this.request("GET","/zones/" + domainIdArg + "/dns_records?per_page=100")
|
|
|
|
.then(function(responseArg){
|
|
|
|
done.resolve(responseArg);
|
|
|
|
});
|
|
|
|
});
|
2016-04-27 01:08:14 +00:00
|
|
|
return done.promise;
|
|
|
|
}
|
2016-05-16 01:29:29 +00:00
|
|
|
listZones(domainName?:string){ // TODO: handle pagination
|
2016-04-27 01:08:14 +00:00
|
|
|
let done = plugins.q.defer();
|
2016-05-16 01:29:29 +00:00
|
|
|
let requestRoute = "/zones?per_page=50"
|
|
|
|
if(domainName) requestRoute = requestRoute + "&name=" + domainName;
|
|
|
|
let result = {};
|
|
|
|
this.request("GET",requestRoute)
|
2016-04-27 04:01:50 +00:00
|
|
|
.then(function(responseArg){
|
2016-05-16 01:29:29 +00:00
|
|
|
result = responseArg;
|
|
|
|
done.resolve(result);
|
2016-04-27 04:01:50 +00:00
|
|
|
});
|
2016-04-27 01:08:14 +00:00
|
|
|
return done.promise;
|
|
|
|
};
|
2016-05-24 21:36:06 +00:00
|
|
|
request(methodArg:string,routeArg:string,dataArg = {}){
|
2016-04-27 01:08:14 +00:00
|
|
|
let done = plugins.q.defer();
|
2016-05-24 21:36:06 +00:00
|
|
|
let jsonArg:string = JSON.stringify(dataArg);
|
2016-04-27 04:01:50 +00:00
|
|
|
let options = {
|
|
|
|
method:methodArg,
|
|
|
|
url:"https://api.cloudflare.com/client/v4" + routeArg,
|
|
|
|
headers:{
|
|
|
|
"Content-Type":"application/json",
|
|
|
|
"X-Auth-Email":this.authEmail,
|
|
|
|
"X-Auth-Key":this.authKey
|
2016-05-16 01:29:29 +00:00
|
|
|
},
|
2016-05-24 21:36:06 +00:00
|
|
|
body:jsonArg
|
2016-05-16 01:29:29 +00:00
|
|
|
};
|
2016-06-21 17:04:43 +00:00
|
|
|
//console.log(options);
|
2016-04-27 04:01:50 +00:00
|
|
|
plugins.request(options,function(err, res, body){
|
|
|
|
if (!err && res.statusCode == 200) {
|
|
|
|
var responseObj = JSON.parse(body);
|
|
|
|
done.resolve(responseObj);
|
|
|
|
} else {
|
|
|
|
console.log(err);
|
|
|
|
console.log(res);
|
|
|
|
done.reject(err);
|
|
|
|
};
|
|
|
|
});
|
2016-04-27 01:08:14 +00:00
|
|
|
return done.promise;
|
|
|
|
}
|
2016-04-27 00:20:27 +00:00
|
|
|
};
|