Community discussions

MUM Europe 2020
 
lukoramu
just joined
Topic Author
Posts: 18
Joined: Mon Jan 07, 2013 11:11 am

Java API: filtering in {set,remove} commands

Fri Mar 31, 2017 9:32 am

Hello Java coders out there! Can you condense this to one execute() statement?:
List<Map<String,String>> vlan_res = api.execute("/interface/ethernet/switch/vlan/print where vlan-id=" + vid.toString());

for (Map<String,String> vlan_item : vlan_res)
    api.execute("/interface/ethernet/switch/vlan/remove .id=" + vlan_item.get(".id"));
Legrange's API does some smart parsing (that's why we're able to use "where" in "print" commands), so I thought this could work:
api.execute("/interface/ethernet/switch/vlan/remove where vlan-id=" + vid.toString());
... Nope.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Java API: filtering in {set,remove} commands

Fri Mar 31, 2017 9:50 am

In some menus (typically, wherever there is "name"), you can use the name instead of the ID, meaning you don't need the "print" in those cases.

For VLANs in particular though, that doesn't seem to be the case. So sorry, you can't do that. It's a protocol limitation. It has already been suggested to MikroTik that they enable API queries in commands other than "print", and have the items matched by the query be affected (which is what you were doing and expected to happen)... But I don't remember MikroTik ever replying one way or another if they would ever do it.
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
legrang
just joined
Posts: 22
Joined: Wed Nov 03, 2010 4:05 pm
Location: South Africa
Contact:

Re: Java API: filtering in {set,remove} commands

Thu Apr 20, 2017 2:22 pm

The limitation as explained by @boen_robot is correct.

If your reason for condensing the code is efficiency (i.e. making fewer API calls), then you're out of luck. But I don't think that loop should be particularly slow or inefficient, removing VLANs should not be regular an occurrence anyway :)

If you're concerned about code brevity on the other hand, you can do something like
api.execute("/interface/ethernet/switch/vlan/print where vlan-id=" + vid.toString())
  .forEach((Map<String, String> vlan -> api.execute("/interface/ethernet/switch/vlan/remove .id=" + vlan_item.get(".id"));
Gideon le Grange
RouterOS Java API: https://github.com/GideonLeGrange/mikrotik-java

Who is online

Users browsing this forum: No registered users and 3 guests