Ver código fonte

now lists all domains' A and CNAME records

Benoît Hubert 8 anos atrás
pai
commit
5aa9110311
2 arquivos alterados com 58 adições e 5 exclusões
  1. 56 5
      app.js
  2. 2 0
      package.json

+ 56 - 5
app.js

@@ -1,5 +1,7 @@
 let { appKey, appSecret, consumerKey } = require('./credentials.json');
 const fs = require('fs');
+const Promise = require('bluebird');
+const _ = require('lodash');
 const ovh = require('ovh')({
   endpoint: 'ovh-eu',
   appKey,
@@ -12,7 +14,11 @@ function requestConsumerKey() {
     'accessRules': [
       { 'method': 'GET', 'path': '/me'},
       { 'method': 'GET', 'path': '/domain/zone'},
-      // { 'method': 'POST', 'path': '/domain/*'},
+      { 'method': 'GET', 'path': '/domain/zone/*'}, 
+      { 'method': 'GET', 'path': '/domain/zone/*/record'},
+      { 'method': 'GET', 'path': '/domain/zone/*/record/*'},
+      { 'method': 'POST', 'path': '/domain/zone/*/record'},
+      { 'method': 'POST', 'path': '/domain/zone/*/refresh'},
       // { 'method': 'PUT', 'path': '/domain/*'},
       // { 'method': 'DELETE', 'path': '/domain/*'}
     ]
@@ -34,12 +40,34 @@ function readDomains() {
   console.log('readDomains');
   return ovh.requestPromised('GET', '/domain/zone')
   .then(response => {
-    console.log(response)
+    console.log(response, typeof response, Object.prototype.toString.call(Object.values(response)) );
+    return Object.values(response);
   })
   .catch(err => {
-    console.log(err);
+    console.log('readDomains ERR', err);
+    throw err;
+  })
+}
+
+function readRecords(domain) {
+  console.log('readRecords');
+  return ovh.requestPromised('GET', '/domain/zone/' + domain + '/record')
+  .then(response => {
+    return Object.values(response);
+  })
+  .then(recordIds => Promise.map(recordIds,
+    recordId => ovh.requestPromised('GET', '/domain/zone/' + domain + '/record/' + recordId)
+  ))
+  .then(response => {
+    return Object.values(response);
+  })
+  .catch(err => {
+    console.log('readRecords ERR', err);
+    throw err;
   })
 }
+
+
 function readMe() {
   console.log('readMe');
   return ovh.requestPromised('GET', '/me')
@@ -51,9 +79,32 @@ function readMe() {
   })
 }
 
+function passLog(label) {
+  return obj => {
+    console.log(label, obj);
+    return obj;
+  }
+}
+
 if(consumerKey) {
-  readMe();
-  readDomains();
+  let domains;
+  readDomains()
+  .then(passLog('domains'))
+  .then(_domains => {
+    domains = _domains;
+    return Promise.map(_domains, domain => readRecords(domain));
+  })
+  .then(passLog('subdomains'))
+  .then(results => {
+    // console.log('## results for all domains', results);
+    results.forEach((recordsForDomain, index) => {
+      const domain = domains[index];
+      console.log('### DOMAIN: ' + domain);
+      const filtered = recordsForDomain.filter(r => (r.fieldType === 'A' || (r.fieldType === 'CNAME' && r.target === domain + '.')));
+      const lines = filtered.map(r => (r.subDomain + ' => ' + r.fieldType));
+      console.log(lines.join('\n'));
+    })
+  });
   // readCurrentConsumerKey();
 }
 else {

+ 2 - 0
package.json

@@ -1,6 +1,8 @@
 {
   "name": "ovh-dnz-ozne",
   "dependencies": {
+    "bluebird": "^3.5.0",
+    "lodash": "^4.17.4",
     "ovh": "^2.0.1"
   }
 }