Commit 60272fa3 authored by Paul Colin Hennig's avatar Paul Colin Hennig
Browse files

fix

parent 106a4057
# Powerdns API
<img draggable="none" src="https://git.y.gy/firstdorsal/powerdns-api/-/raw/master/logo.jpg" style="float:left; margin-right:10px;" height="100">
<img draggable="none" src="https://git.y.gy/firstdorsal/powerdns-api/-/raw/master/logo.jpg" style="float:left; margin-right:10px;" height="100">
## A Nodejs client for the PowerDns API with the most relevant functions.
......@@ -9,66 +10,79 @@
[![Website](https://ico.y.gy/website?down_color=red&down_message=offline&label=documentation&up_color=success&up_message=online&url=https%3A%2F%2Fdoc.y.gy%2Fpowerdns-api&style=flat-square)](https://doc.y.gy/powerdns-api/)
[![Website](https://ico.y.gy/website?down_color=red&down_message=offline&label=repository&up_color=success&up_message=online&url=https%3A%2F%2Fgit.y.gy%2Ffirstdorsal%2Fpowerdns-api&style=flat-square&logo=gitlab)](https://git.y.gy/firstdorsal/powerdns-api/)
# Install
```
npm i @firstdorsal/powerdns-api
```
# Usage
```js
(async () => {
//get enviroment variables from the .env file
require('dotenv').config();
require("dotenv").config();
//import the module
const {
PowerdnsClient
} = require('@firstdorsal/powerdns-api');
//create a powerdns client object
const { PowerdnsClient } = require("@firstdorsal/powerdns-api");
//create a powerdns client object
const pdns = new PowerdnsClient(process.env.PDNS_API_ENDPOINT, process.env.PDNS_API_KEY);
//use a function and return the results to console
console.log(await pdns.getZone('example.com'));
console.log(await pdns.getZone("example.com"));
})();
```
## What is dotenv?
The line "require('dotenv').config();" gets the contents of a file called ".env" in which you should store your global and secret variables.
**1. Install the module "dotenv" with**
```
npm i dotenv
```
**2. Create a file named ".env" in your applications root directory**
*.env*
_.env_
```c
PDNS_API_KEY='YOUR PDNS API KEY'
PDNS_API_ENDPOINT='https://example.com/api/v1/servers/localhost'
```
**3. Use your secret variables**
```
process.env.PDNS_API_ENDPOINT
process.env.PDNS_API_KEY
```
# IMPORTANT
if you are using domains with two labels for the TLD like yourdomain.com.au please use the setHomogeneousRecords function instead of setRecords
# Documentation
**[Here](https://doc.y.gy/powerdns-api/)**
# Need help or missing a feature?
Feel free to contact me via [xl9jthv_7bvgakv9o9wg0jabn2ylm91xxrzzgt0e@y.gy](mailto:xl9jthv_7bvgakv9o9wg0jabn2ylm91xxrzzgt0e@y.gy) in english or german
## Automatic Let's Encrypt certificates via DNS with greenlock and PDNS
[acme-dns-01-powerdns](https://www.npmjs.com/package/acme-dns-01-powerdns)
## PDNS WEB API Documentation
[On the Web](https://doc.powerdns.com/authoritative/http-api/index.html)
[And in full detail (Swagger)](https://raw.githubusercontent.com/PowerDNS/pdns/master/docs/http-api/swagger/authoritative-api-swagger.yaml)
## Links
[NPM](https://www.npmjs.com/package/@firstdorsal/powerdns-api)
[Documentation](https://doc.y.gy/powerdns-api/)
[Code](https://git.y.gy/firstdorsal/powerdns-api)
......@@ -142,8 +142,6 @@ module.exports.PowerdnsClient = class {
createZone(zoneName, kind = "Native") {
if (typeof zoneName !== "string") throw new TypeError("zoneName must be of type string");
const dname = this.absoluteName(zoneName);
const zoneNameSan = dname.substr(0, dname.length - 1).match(secondLevelRegex)[0];
return f(`${this.baseurl}/zones`, {
method: "POST",
headers: {
......@@ -151,7 +149,7 @@ module.exports.PowerdnsClient = class {
"Content-Type": "application/json"
},
body: JSON.stringify({
name: zoneNameSan + ".",
name: this.absoluteName(zoneName),
kind
})
})
......@@ -179,9 +177,8 @@ module.exports.PowerdnsClient = class {
*/
getZoneWithMeta(zoneName) {
if (typeof zoneName !== "string") throw new TypeError("zoneName must be of type string");
const dname = this.absoluteName(zoneName);
const zoneNameSan = dname.substr(0, dname.length - 1).match(secondLevelRegex)[0];
return f(this.baseurl + "/zones/" + zoneNameSan, {
return f(this.baseurl + "/zones/" + this.absoluteName(zoneName), {
method: "GET",
headers: {
"X-Api-Key": this.apikey
......@@ -222,9 +219,8 @@ module.exports.PowerdnsClient = class {
*/
deleteZone(zoneName) {
if (typeof zoneName !== "string") throw new TypeError("zoneName must be of type string");
const dname = this.absoluteName(zoneName);
const zoneNameSan = dname.substr(0, dname.length - 1).match(secondLevelRegex)[0];
return f(this.baseurl + "/zones/" + zoneNameSan, {
return f(this.baseurl + "/zones/" + this.absoluteName(zoneName), {
method: "DELETE",
headers: {
"X-Api-Key": this.apikey
......@@ -319,11 +315,11 @@ module.exports.PowerdnsClient = class {
type: "A"
}]);
*/
deleteRecords(records) {
deleteRecords(records, domain) {
if (!Array.isArray(records)) throw new TypeError("records must be of type array");
const dname = this.absoluteName(records[0].name);
const zoneName = dname.substr(0, dname.length - 1).match(secondLevelRegex)[0];
if (!domain) domain = zoneName;
let rrsets = [];
for (let i = 0; i < records.length; i++) {
rrsets.push({
......@@ -333,7 +329,7 @@ module.exports.PowerdnsClient = class {
});
}
return f(this.baseurl + "/zones/" + zoneName, {
return f(this.baseurl + "/zones/" + this.absoluteName(domain), {
method: "PATCH",
headers: {
"X-Api-Key": this.apikey
......@@ -436,9 +432,7 @@ module.exports.PowerdnsClient = class {
if (!zoneName) throw new Error("Missing zone/domain name");
if (!cryptokey.keytype) throw new Error("Missing keytype");
const dname = this.absoluteName(zoneName);
const zoneNameSan = dname.substr(0, dname.length - 1).match(secondLevelRegex)[0];
return f(`${this.baseurl}/zones/${zoneNameSan}/cryptokeys`, {
return f(`${this.baseurl}/zones/${this.absoluteName(zoneName)}/cryptokeys`, {
method: "POST",
headers: {
"X-Api-Key": this.apikey
......@@ -469,9 +463,7 @@ module.exports.PowerdnsClient = class {
getCryptoKeys(zoneName) {
if (!zoneName) throw new Error("Missing zone/domain name");
const dname = this.absoluteName(zoneName);
const zoneNameSan = dname.substr(0, dname.length - 1).match(secondLevelRegex)[0];
return f(`${this.baseurl}/zones/${zoneNameSan}/cryptokeys`, {
return f(`${this.baseurl}/zones/${this.absoluteName(zoneName)}/cryptokeys`, {
method: "GET",
headers: {
"X-Api-Key": this.apikey
......@@ -500,9 +492,7 @@ module.exports.PowerdnsClient = class {
deleteCryptoKey(zoneName, cryptokeyId) {
if (!zoneName) throw new Error("Missing zone/domain name");
const dname = this.absoluteName(zoneName);
const zoneNameSan = dname.substr(0, dname.length - 1).match(secondLevelRegex)[0];
return f(`${this.baseurl}/zones/${zoneNameSan}/cryptokeys/${cryptokeyId}`, {
return f(`${this.baseurl}/zones/${this.absoluteName(zoneName)}/cryptokeys/${cryptokeyId}`, {
method: "DELETE",
headers: {
"X-Api-Key": this.apikey
......@@ -712,7 +702,7 @@ await pdns.createAndSetupZone({
});
await this.setRecords([
{
name: zone.domain.match(secondLevelRegex)[0],
name: zone.domain,
type: "SOA",
ttl: 3600,
content: [
......@@ -723,7 +713,7 @@ await pdns.createAndSetupZone({
]
},
{
name: zone.domain.match(secondLevelRegex)[0],
name: zone.domain,
type: "NS",
ttl: 3600,
content: zone.nameserver.map(e => this.absoluteName(e))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment