Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
aluisiora committed Mar 8, 2018
2 parents 84605d7 + 76fcd36 commit 9aa9268
Show file tree
Hide file tree
Showing 3 changed files with 492 additions and 358 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "routeros-client",
"version": "0.9.0",
"version": "0.10.0",
"description": "Abstraction layer over the node-routeros API",
"main": "./dist/index",
"types": "./dist/index",
Expand Down
169 changes: 99 additions & 70 deletions src/RosApiCrud.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RouterOSAPI, RosException } from "node-routeros";
import { flatten } from "lodash";
import { flatten, reduce } from "lodash";
import * as utils from "./utils";
import * as Types from "./Types";

Expand Down Expand Up @@ -51,7 +51,7 @@ export abstract class RouterOSAPICrud {
public add(data: object): Types.SocPromise {
return this.exec("add", data).then((results: any) => {
if (results.length === 0) return Promise.resolve(null);
return this.recoverDataFromIdsOfChangedItems(null, results[0].ret);
return this.recoverDataFromChangedItems(results.shift().ret);
});
}

Expand All @@ -74,9 +74,12 @@ export abstract class RouterOSAPICrud {
ids = this.stringfySearchQuery(ids);
this.queryVal.push("=numbers=" + ids);
}
const disabledIds = utils.lookForIdParameterAndReturnItsValue(this.queryVal);
return this.exec("disable").then((response: any[]) => {
return this.recoverDataFromIdsOfChangedItems(response, disabledIds);
let disabledIds = utils.lookForIdParameterAndReturnItsValue(this.queryVal);
return this.queryForIdsIfNeeded(disabledIds).then((ids: string) => {
disabledIds = ids;
return this.exec("disable");
}).then((response: any[]) => {
return this.recoverDataFromChangedItems(disabledIds);
});
}

Expand All @@ -99,9 +102,12 @@ export abstract class RouterOSAPICrud {
ids = this.stringfySearchQuery(ids);
this.queryVal.push("=numbers=" + ids);
}
const enabledIds = utils.lookForIdParameterAndReturnItsValue(this.queryVal);
return this.exec("enable").then((response: any[]) => {
return this.recoverDataFromIdsOfChangedItems(response, enabledIds);
let enabledIds = utils.lookForIdParameterAndReturnItsValue(this.queryVal);
return this.queryForIdsIfNeeded(enabledIds).then((ids: string) => {
enabledIds = ids;
return this.exec("enable");
}).then((response: any[]) => {
return this.recoverDataFromChangedItems(enabledIds);
});
}

Expand Down Expand Up @@ -138,8 +144,24 @@ export abstract class RouterOSAPICrud {
this.queryVal.push("=destination=" + to);
}
const movedIds = utils.lookForIdParameterAndReturnItsValue(this.queryVal);
return this.exec("move").then((response: any[]) => {
return this.recoverDataFromIdsOfChangedItems(response, movedIds);
return this.exec("move").then(() => {
return this.recoverDataFromChangedItems(movedIds);
});
}

/**
* Move a queried rule above another.
*
* @param to where to move the queried rule
*/
public moveAbove(to?: string): Types.SocPromise {
let movedIds = utils.lookForIdParameterAndReturnItsValue(this.queryVal);
return this.queryForIdsIfNeeded(movedIds).then((ids: string) => {
movedIds = ids;
if (to) this.queryVal.push("=destination=" + to);
return this.exec("move");
}).then(() => {
return this.recoverDataFromChangedItems(movedIds);
});
}

Expand All @@ -154,10 +176,15 @@ export abstract class RouterOSAPICrud {
ids = this.stringfySearchQuery(ids);
this.queryVal.push("=numbers=" + ids);
}
this.makeQuery(data);
const updatedIds = utils.lookForIdParameterAndReturnItsValue(this.queryVal);
return this.exec("set").then((response: any[]) => {
return this.recoverDataFromIdsOfChangedItems(response, updatedIds);

let updatedIds = utils.lookForIdParameterAndReturnItsValue(this.queryVal);

return this.queryForIdsIfNeeded(updatedIds).then((ids: string) => {
updatedIds = ids;
this.makeQuery(data);
return this.exec("set");
}).then((response: any[]) => {
return this.recoverDataFromChangedItems(updatedIds);
});
}

Expand All @@ -172,42 +199,30 @@ export abstract class RouterOSAPICrud {
ids = this.stringfySearchQuery(ids);
this.queryVal.push("=numbers=" + ids);
}
if (typeof properties === "string") properties = [properties];
const $q: Types.SocPromise[] = [];

// Saving current queryVal for reuse, since running exec will reset it
const curQueryVal = this.queryVal.slice();
const updatedIds = utils.lookForIdParameterAndReturnItsValue(this.queryVal);

// Cleaning current queryVal to prevent duplication
this.queryVal = [];
properties.forEach((property) => {
// Putting back queryVal after a cleanup
this.queryVal = curQueryVal.slice();
this.queryVal.push("=value-name=" + utils.camelCaseOrSnakeCaseToDashedCase(property));
$q.push(this.exec("unset"));
});
return Promise.all($q).then((data: any[]) => {
data = flatten(data);
return Promise.resolve(data);
}).then((response: any[]) => {
if (!response || !updatedIds) return Promise.resolve(response);
const promises = [];
const ids = updatedIds.split(",");
for (const id of ids) {
const promise = this.rosApi.write([
this.pathVal + "/print",
"?.id=" + id
]);
promises.push(promise);
}
return Promise.all(promises);
}).then((data) => {
if (!data) return Promise.resolve(data);
data = flatten(data);
data = this.treatMikrotikProperties(data);
if (!updatedIds.includes(",")) return Promise.resolve(data[0]);
return Promise.resolve(data);
let updatedIds = utils.lookForIdParameterAndReturnItsValue(this.queryVal);

return this.queryForIdsIfNeeded(updatedIds).then((ids: string) => {
updatedIds = ids;

if (typeof properties === "string") properties = [properties];

const $q: Types.SocPromise[] = [];

// Saving current queryVal for reuse, since running exec will reset it
const curQueryVal = this.queryVal.slice();

// Cleaning current queryVal to prevent duplication
this.queryVal = [];
properties.forEach((property) => {
// Putting back queryVal after a cleanup
this.queryVal = curQueryVal.slice();
this.queryVal.push("=value-name=" + utils.camelCaseOrSnakeCaseToDashedCase(property));
$q.push(this.exec("unset"));
});
return Promise.all($q);
}).then(() => {
return this.recoverDataFromChangedItems(updatedIds);
});
}

Expand All @@ -221,27 +236,14 @@ export abstract class RouterOSAPICrud {
ids = this.stringfySearchQuery(ids);
this.queryVal.push("=numbers=" + ids);
}

const idsForRemoval = utils.lookForIdParameterAndReturnItsValue(this.queryVal);
if (!idsForRemoval) return this.exec("remove");

const promises = [];
const foundIds = idsForRemoval.split(",");
for (const id of foundIds) {
const promise = this.rosApi.write([
this.pathVal + "/print",
"?.id=" + id
]);
promises.push(promise);
}
let responseData;
return Promise.all(promises).then((data: any) => {
if (!data) return Promise.resolve(data);
data = flatten(data);
data = this.treatMikrotikProperties(data);
if (!idsForRemoval.includes(",")) return Promise.resolve(data[0]);
return Promise.resolve(data);
}).then((data: any) => {
responseData = data;
return this.queryForIdsIfNeeded(idsForRemoval).then((ids: string) => {
return this.recoverDataFromChangedItems(ids);
}).then((response: any) => {
responseData = response;
return this.exec("remove");
}).then(() => {
return Promise.resolve(responseData);
Expand Down Expand Up @@ -501,8 +503,12 @@ export abstract class RouterOSAPICrud {
* @param data
* @param ids
*/
private recoverDataFromIdsOfChangedItems(data: any[], ids?: string): Promise<any | any[]> {
if (!ids) return Promise.resolve(data);
private recoverDataFromChangedItems(ids?: string): Promise<any | any[]> {
if (!ids) {
return this.rosApi.write([this.pathVal + "/print"])
.then((data) => Promise.resolve(this.treatMikrotikProperties(data).shift()));
}

const promises = [];
const splittedIds = ids.split(",");
for (const id of splittedIds) {
Expand All @@ -516,9 +522,32 @@ export abstract class RouterOSAPICrud {
if (!data) return Promise.resolve(data);
data = flatten(data);
data = this.treatMikrotikProperties(data);
if (!ids.includes(",")) return Promise.resolve(data[0]);
if (!ids.includes(",")) return Promise.resolve(data.shift());
return Promise.resolve(data);
});
}

/**
* If trying do any action without providing any id, just
* a query. Find all their ids and return them
*
* @param ids
*/
private queryForIdsIfNeeded(ids?: string): Promise<string> {
if (ids) return Promise.resolve(ids);

this.queryVal.push("=.proplist=.id");
const query = this.fullQuery("/print");
let queriedIds;
return this.write(query).then((data: any[]) => {
data = reduce(data, (result, value, key) => {
result.push(value.id);
return result;
}, []);
queriedIds = data + "";
if (queriedIds) this.queryVal.push("=numbers=" + queriedIds);
return Promise.resolve(queriedIds);
});
}

}
Loading

0 comments on commit 9aa9268

Please sign in to comment.