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

Entry Model

Aluisio Amaral edited this page Nov 28, 2017 · 4 revisions

Summary

So, what does this Model do? If you ever used the Laravel's Eloquent you might know what this is about. The Model is a kind of ORM that wraps the item so we can fire commands on it without having to pass a complete query, keep reading for a better understanding.

Retrieving with data

When we are retrieving data from a menu, we can explicitly retrieve it already wrapped with the Model, so it returns a read to use model scheme. Example:

const addressMenu = client.menu("/ip address");
addressMenu.getModel().then((addresses) => {

    // Supose we want to edit the second
    // address coment
    addresses[1].update({
        comment: "Updated comment through the model"
    }).then((updatedAddress) => {
        // UPDATED!!!
        console.log(addresses[1] === updatedAddress); // true
    }).catch((err) => {
        // Error updating address
        console.log(err);
    });

}).catch((err) => {
    // Error getting data
    console.log(err);
});

Note: this can be less performant since it iterates over the array of items returned from the routerOS and then creates a new Model in every item. Also, in each item, iterates their properties to store the values.

Creating from an existing entry

Instead of retrieving a whole list menu with the items already wrapped, you can take a single item, or an array of items, and create a model from it using the model() function from the client object. Example:

model(item: any): RosApiModel | RosApiModel[]

routeros.connect().then((client) => {
    // Connected successfully

    const addressMenu = client.menu("/ip address");
    addressMenu.get().then((addresses) => {

        // Lets transform the first element
        // into a model
        var firstAddress = client.model(addresses[0]);
        firstAddress.update({
            comment: "Updated comment through the model"
        }).then(() => {
            // UPDATED!
            console.log(firstAddress.comment); // Updated comment through the model
        }).catch((err) => {
            // Error updating
            console.log(err);
        });


    }).catch((err) => {
        // Error getting data
        console.log(err);
    });

}).catch((err) => {
    // Error when trying to connect
    console.log(err);
});

Using the Model

After we have our item wrapped into a model, most of the commands we use in the menus are available here. Commands like add(), update(), enable(), disable(), remove(), move() and their aliases we've seen before are available to use directly from the model. Some considerations:

  • Currently, the add() method acts exactly the same when not using models.
  • The update() method always return a promise that returns the same item but with the updated properties.
  • The item properties are "dissolved" in the model, so you can use it normally like with non-model items. The real item is stored in the originalItem property of the model.

So with that in mind, we can use it like:

const accessMenu = client.menu("/ip proxy access");
accessMenu.getModel().then((acls) => {

    let firstAcl = acls[0];
    let secondAcl = acls[1];

    console.log(firstAcl.id);
    console.log(firstAcl.dstHost);
    console.log(firstAcl.action);
    console.log(firstAcl.comment);

    firstAcl.disable().then((firstAclReturned) => {
        return firstAcl.enable();
    }).then((firstAclReturned) => {
        return firstAclReturned.update({comment:"WAN"});
    }).then((firstAclReturned) => {
        // Move second rule above the first
        return secondAcl.move(firstAcl.id);
    }).then((secondAclReturned) => {
        return acls[2].remove();
    }).catch((err) => {
        console.log(err);
    });

}).catch((err) => {
    // Error getting data
    console.log(err);
});

Can be hard to understand this code, but what was done here? (Nothing usefull, but nvm =])

  • Navigated to /ip proxy acess.
  • Printed all acls wrapped in model.
  • Create a reference for first and second acl, firstAcl and secondAcl respectively.
  • Just console logged the properties to see their value.
  • Disabled the first acl (firstAcl).
  • Enabled the first acl using our created reference (firstAcl).
  • Updated the first acl using the returned reference of the acl (firstAclReturned), which is the same object we referenced with firstAcl var.
  • Moved secondRule above firstRule using it's id.
  • Removed third acl acls[2].

Next: Extras