Skip to content

Commit

Permalink
various tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
silverwind committed Feb 15, 2019
1 parent 0680f5b commit f7903f5
Showing 1 changed file with 23 additions and 47 deletions.
70 changes: 23 additions & 47 deletions aix.js
Original file line number Diff line number Diff line change
@@ -1,92 +1,68 @@
"use strict";

const getSqlStatement = family => {
let sql =
"select NEXT_HOP, LOCAL_BINDING_INTERFACE from QSYS2.NETSTAT_ROUTE_INFO where ROUTE_TYPE='DFTROUTE' and NEXT_HOP!='*DIRECT'";

if (family === "v4") {
sql += " and CONNECTION_TYPE='IPV4'";
} else {
sql += " and CONNECTION_TYPE='IPV6'";
}

return sql;
const queries = {
v4: "select NEXT_HOP, LOCAL_BINDING_INTERFACE from QSYS2.NETSTAT_ROUTE_INFO where ROUTE_TYPE='DFTROUTE' and NEXT_HOP!='*DIRECT' and CONNECTION_TYPE='IPV4'",
v6: "select NEXT_HOP, LOCAL_BINDING_INTERFACE from QSYS2.NETSTAT_ROUTE_INFO where ROUTE_TYPE='DFTROUTE' and NEXT_HOP!='*DIRECT' and CONNECTION_TYPE='IPV6'",
};

const getGatewayInformationAsync = async family => {
const get = family => {
return new Promise((resolve, reject) => {
try {
const idbConnector = require("idb-connector");

const dbconn = new idbConnector.dbconn();
dbconn.conn("*LOCAL");

const sql = getSqlStatement(family);
const stmt = new idbConnector.dbstmt(dbconn);

stmt.exec(sql, async results => {
const dbstmt = new idbConnector.dbstmt(dbconn);
dbstmt.exec(queries[family], results => {
try {
stmt.close();
dbstmt.close();
dbconn.disconn();
dbconn.close();
} catch (err) {
reject(new Error("Unable to determine default gateway"));
return;
return reject(err);
}

if (results && results[0] && results[0].NEXT_HOP) {
resolve({
gateway: results[0].NEXT_HOP,
interface: results[0].LOCAL_BINDING_INTERFACE || ""
interface: results[0].LOCAL_BINDING_INTERFACE || null,
});
} else {
reject(new Error("Unable to determine default gateway"));
return reject(new Error("Unable to determine default gateway"));
}
});
} catch (err) {
reject(new Error("Unable to determine default gateway"));
return;
return reject(err);
}
});
};

const getGatewayInformationSync = family => {
let results;
try {
const idbConnector = require("idb-connector");
const getSync = family => {
const idbConnector = require("idb-connector");

const dbconn = new idbConnector.dbconn();
dbconn.conn("*LOCAL");
const dbconn = new idbConnector.dbconn();
dbconn.conn("*LOCAL");

const sql = getSqlStatement(family);
const stmt = new idbConnector.dbstmt(dbconn);
const dbstmt = new idbConnector.dbstmt(dbconn);
const results = dbstmt.execSync(queries[family]);

results = stmt.execSync(sql);

stmt.close();
dbconn.disconn();
dbconn.close();
} catch (err) {
throw new Error("Unable to determine default gateway");
}
dbstmt.close();
dbconn.disconn();
dbconn.close();

if (results && results[0] && results[0].NEXT_HOP) {
return {
gateway: results[0].NEXT_HOP,
interface: results[0].LOCAL_BINDING_INTERFACE || ""
interface: results[0].LOCAL_BINDING_INTERFACE || null,
};
} else {
throw new Error("Unable to determine default gateway");
}
};

const promise = family => {
return getGatewayInformationAsync(family);
};

const sync = family => {
return getGatewayInformationSync(family);
};
const promise = family => get(family);
const sync = family => getSync(family);

module.exports.v4 = () => promise("v4");
module.exports.v6 = () => promise("v6");
Expand Down

0 comments on commit f7903f5

Please sign in to comment.