Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add api_info and api_modify modules #91

Merged

Conversation

felixfontein
Copy link
Collaborator

SUMMARY

This adds two new modules, api_info and api_modify, which support querying and modifying certain paths.

The modules provide a high-level interface and only support some paths. The API information included is based on what I gathered from RouterOS 6.49. I'd like to have a better way to gather this data, but haven't been able to look into that yet.

The api_modify module should be able to at least solve some of the problems mentioned in #33.

ISSUE TYPE
  • New Module Pull Request
COMPONENT NAME

api_info
api_modify

@codecov
Copy link

codecov bot commented May 14, 2022

Codecov Report

Merging #91 (f783df2) into main (7973f58) will increase coverage by 2.70%.
The diff coverage is 90.79%.

❗ Current head f783df2 differs from pull request most recent head 6353f21. Consider uploading reports for the commit 6353f21 to get more accurate results

@@            Coverage Diff             @@
##             main      #91      +/-   ##
==========================================
+ Coverage   83.78%   86.48%   +2.70%     
==========================================
  Files          25       29       +4     
  Lines        2491     3507    +1016     
  Branches      432      656     +224     
==========================================
+ Hits         2087     3033     +946     
- Misses        315      348      +33     
- Partials       89      126      +37     
Impacted Files Coverage Δ
plugins/modules/api.py 77.82% <ø> (ø)
plugins/modules/api_facts.py 77.52% <ø> (ø)
plugins/modules/api_find_and_modify.py 91.42% <ø> (ø)
plugins/modules/api_modify.py 79.57% <79.57%> (ø)
plugins/modules/api_info.py 88.88% <88.88%> (ø)
plugins/module_utils/_api_data.py 100.00% <100.00%> (+4.08%) ⬆️
tests/unit/plugins/modules/fake_api.py 81.92% <100.00%> (+13.63%) ⬆️
tests/unit/plugins/modules/test_api_info.py 100.00% <100.00%> (ø)
tests/unit/plugins/modules/test_api_modify.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7973f58...6353f21. Read the comment docs.

@felixfontein
Copy link
Collaborator Author

Example:

- community.routeros.api_modify:
    path: ip dns static
    data:
      - address: 192.168.88.1
        name: router
      - address: 192.168.88.2
        name: server1
      - address: 192.168.88.3
        name: server2
    handle_absent_entries: remove
    handle_entries_content: remove
    ensure_order: true

This will ensure that /ip dns static has extractly three entries in the specified order of exactly the specified format.

@felixfontein felixfontein force-pushed the api_info-api_modify branch 4 times, most recently from edf4b84 to 59b7b9f Compare May 15, 2022 13:48
@felixfontein felixfontein force-pushed the api_info-api_modify branch 2 times, most recently from 67b6e07 to 067003f Compare May 22, 2022 09:59
@NikolayDachev
Copy link
Collaborator

I'm not sure why choices: list must be in docs ? Is there anything specifics for those paths ? what I miss ?

@NikolayDachev
Copy link
Collaborator

As general I absolutely like it :D

More questions

This NAT to WAN example .. make no sense to me, Also I get error for "msg": "Unknown key \"name\" at index 1." which is ok since I'm not sure if ip firewall nat .. have attribute name ?!

The example from this PR make more sense (about DNS static entries )

From code prospective I think is ok

@felixfontein
Copy link
Collaborator Author

felixfontein commented May 23, 2022

It should have been comment, not name in that example... I've updated it and added another example.

choices: is kind of needed since the modules only support some pathes, namely the ones they "know how they work" and for which they have information in fields that exist, default values, etc.

I have a one note here.

Please test it with RouterOS 7 ! Some paths are changed

Also if this affect " /interface/wireless" for example with routeros pkg wifiwave2 (if is installed) this path is changed to '/interface/wifiwave2'

It is not a big issue just for information

Overall I think we can merge this request !

Copy link
Collaborator

@NikolayDachev NikolayDachev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look ok to be merged in main

@NikolayDachev
Copy link
Collaborator

note: Probably rebase will be needed today I merge "extended-query"

@felixfontein felixfontein force-pushed the api_info-api_modify branch from 0fa1438 to 4c2ef41 Compare May 23, 2022 15:54
@felixfontein
Copy link
Collaborator Author

felixfontein commented May 23, 2022

Things this PR is still missing:

  • A way to get hold of the API information. Right now it's manually compiling everything, but that's not really efficient and will likely cause problems already with RouterOS 7. Also there are quite some paths that aren't fully implemented yet due to me not knowing how to correctly support them, how exactly they work, etc.
  • Having better data, like some options only work when other options have certain values, etc. Might also be related to the above point.
  • Ordering for paths with stratified keys seems to have problems (maybe also for paths with primary keys?). For some firewall rules I had to run the module several times until they finally had the correct order. I'll need to debug this more to figure out what causes this and how to fix it.
  • Not related directly to this PR, but a common problem for all api* modules: using non-ASCII characters. For example I had an ü in a comment for my home router, and the module crashed when trying to send that comment to RouterOS since it didn't like that letter. I was able to set it in the web UI though.

@felixfontein felixfontein force-pushed the api_info-api_modify branch 3 times, most recently from 2443a48 to cf7876b Compare May 24, 2022 20:20
@NikolayDachev
Copy link
Collaborator

NikolayDachev commented May 24, 2022

For firewall order (not directly related) from ros prospective

Let say we have

 0    chain=forward action=accept log=no log-prefix="" 
 1    chain=forward action=accept log=no log-prefix="" 
 2    chain=forward action=accept log=no log-prefix="" 

If you want to change rule number 1 to action=drop set can be used

 /ip/firewall/filter/set action=drop numbers=1

result

 0    chain=forward action=accept log=no log-prefix="" 
 1    chain=forward action=drop log=no log-prefix="" 
 2    chain=forward action=accept log=no log-prefix="" 

@felixfontein Not sure if this example can help, probably if you provide me more info ... ?

or with api module cmd

    - name: add ip address test
      community.routeros.api:
        hostname: "10.20.36.253"
        ssl: "false"
        password: "SUPERSECRETPWD"
        username: "admin"
        encoding: "utf-8"
        path: "ip firewall filter"
        cmd: "set action=drop numbers=1"

@NikolayDachev
Copy link
Collaborator

For " Having better data, like some options only work when other options have certain values, etc. Might also be related to the above point."

I guess you know some routeros paths haves sub path, so is normal some attributes to depend on other (specially with sub paths) .. if I understand correctly the problem ?

@felixfontein
Copy link
Collaborator Author

No, I meant more that for some values in one path some attributes only make sense in some case, like in /ip service, certificate and tls-version make sense for www-ssl and api-ssl, but not for www, api, ftp, ... There are more cases like this in other paths.

@NikolayDachev
Copy link
Collaborator

"Also there are quite some paths that aren't fully implemented yet due to me not knowing how to correctly support them, how exactly they work, etc."

Let's clear the problems her first on ROS level then we can see what we can do with API, let me know if you have a issues with something specific, probably I can help

Yes ROS7 don't have long term support version yet, we cannot predict every change so simple "disclaimer" in doc's will do the job for now , for the rest we can relay on module users feedback !

@NikolayDachev
Copy link
Collaborator

NikolayDachev commented May 24, 2022

No, I meant more that for some values in one path some attributes only make sense in some case, like in /ip service, certificate and tls-version make sense for www-ssl and api-ssl, but not for www, api, ftp, ... There are more cases like this in other paths.

I think I understand the problem, unfortunately no clear solution,

  1. "hardcoded check per case"
  2. somehow routeros trap errors to be tracked .. probably
  3. no idea really

note: I think rebase is needed ..

@felixfontein felixfontein force-pushed the api_info-api_modify branch from cf7876b to 84d9a48 Compare May 25, 2022 04:48
@NikolayDachev
Copy link
Collaborator

@felixfontein let me know if you need a help with something here

@felixfontein felixfontein force-pushed the api_info-api_modify branch from 33f232a to cc296e4 Compare June 12, 2022 15:22
@felixfontein
Copy link
Collaborator Author

@felixfontein let me know if you need a help with something here

I was looking at how to automatically export information on the API structure from RouterOS recently (like which fields are supported for which path etc.), but I couldn't really find anything.

Some information seems to be available from the HTTP interface, like https://192.168.88.1/webfig/roteros.jg (base system) or https://192.168.88.1/webfig/wlan6.jg (there seems to be one such file per package that can be installed). This is unfortunately not JSON, but something a JavaScript interpreter understands, and I suspect it contains some information needed to render the web UI. But it doesn't seem to be everything, it probably works together with other internal data.

That's probably the most important thing left...

@NikolayDachev
Copy link
Collaborator

Ros7 have REST api support, if you need something specific probably can look it with postman, I guess rest and routeros api are similar .

Not sure if api structure can be dump in both cases

@felixfontein felixfontein force-pushed the api_info-api_modify branch from 9f8ff6e to 9ca249d Compare July 18, 2022 21:18
@github-actions
Copy link

github-actions bot commented Jul 18, 2022

Docs Build 📝

Thank you for contribution!✨

This PR has been merged and your docs changes will be incorporated when they are next published.

@felixfontein
Copy link
Collaborator Author

@NikolayDachev I haven't been able to really work on this, but I also don't see how to actually solve the remaining problems. What do you think of simply declaring this as "finished" for now and merging it and releasing it, so folks can start trying it out and maybe even extending/improving/... it for the use cases they have?

@NikolayDachev
Copy link
Collaborator

No problems, just we should mark somehow what is working and what not in doc.

@felixfontein
Copy link
Collaborator Author

I'll update the docs and mark this as ready once I've pushed that. Might not happen today, but very soon :)

@felixfontein
Copy link
Collaborator Author

@NikolayDachev what do you think of the formulation I pushed?

@NikolayDachev
Copy link
Collaborator

I will try to find a time during the weekend

@NikolayDachev
Copy link
Collaborator

Looking good, just check my suggestion in code review .. I don't think is something really special.
Depend on your decision .. overall we can merge if no future changes are needed.

@felixfontein felixfontein changed the title [WIP] Add api_info and api_modify modules Add api_info and api_modify modules Jul 31, 2022
@felixfontein
Copy link
Collaborator Author

@NikolayDachev in case you had a suggestion, you didn't add it to the review :)

@NikolayDachev
Copy link
Collaborator

@NikolayDachev in case you had a suggestion, you didn't add it to the review :)

sorry was in pending state

@felixfontein felixfontein force-pushed the api_info-api_modify branch from f783df2 to 6353f21 Compare July 31, 2022 10:49
@felixfontein
Copy link
Collaborator Author

Ok, ready for another round of review :)

Copy link
Collaborator

@NikolayDachev NikolayDachev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

look good to go

@felixfontein felixfontein merged commit 2911710 into ansible-collections:main Jul 31, 2022
@felixfontein felixfontein deleted the api_info-api_modify branch July 31, 2022 20:06
@felixfontein
Copy link
Collaborator Author

Then let's do this! Thanks a lot for reviewing and testing this, @NikolayDachev!
I'll create a new release probably tomorrow, so folks can more easily try this out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants