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

fix(api): fixed a bug when splitting the argument string #45

Merged
merged 8 commits into from
Sep 29, 2021

Conversation

akimrx
Copy link
Contributor

@akimrx akimrx commented Aug 23, 2021

SUMMARY

Closes #44

ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

routeros_api

@akimrx
Copy link
Contributor Author

akimrx commented Aug 23, 2021

All tests passed in my repo, but you can check again

@codecov
Copy link

codecov bot commented Aug 23, 2021

Codecov Report

Merging #45 (0f8ce99) into main (48c7920) will decrease coverage by 0.06%.
The diff coverage is 80.45%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main      #45      +/-   ##
==========================================
- Coverage   80.13%   80.06%   -0.07%     
==========================================
  Files          11       11              
  Lines        1223     1304      +81     
  Branches      161      181      +20     
==========================================
+ Hits          980     1044      +64     
- Misses        184      193       +9     
- Partials       59       67       +8     
Impacted Files Coverage Δ
plugins/modules/api.py 71.87% <77.02%> (+1.13%) ⬆️
tests/unit/plugins/modules/test_api.py 98.61% <100.00%> (+0.08%) ⬆️

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 48c7920...0f8ce99. Read the comment docs.

@felixfontein
Copy link
Collaborator

Related PR: #47

Copy link
Collaborator

@heuels heuels left a comment

Choose a reason for hiding this comment

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

Looks good to me!

Although I haven't touched Python in a while now, @felixfontein may be a better judge here 🙂

@NikolayDachev
Copy link
Collaborator

@akimrx Thanks a lot for the fix

Initial testing look ok, however I want to do more deep testing with arbitary cmd, query etc..

@felixfontein
Copy link
Collaborator

Before discussing on whether this should we merged, we should really discuss the underlying questions in #44 which need to be answered before we can decide whether this PR actually provides a correct fix.

@NikolayDachev
Copy link
Collaborator

@akimrx can you please merge after #47 is committed.

@felixfontein
Copy link
Collaborator

@akimrx I've rebased your branch.

@felixfontein
Copy link
Collaborator

I've added #44 (comment) + a changelog fragment to this PR in 3bc446c.

this is correct thanks !

Co-authored-by: Felix Fontein <[email protected]>
@NikolayDachev
Copy link
Collaborator

Thanks again for the work here,

note: We should update module documentation about strings with '""' for users which define it as variables , no sure how to define it but

ros_ip_addr_comment: '"my long string"' -> correct
ros_ip_addr_comment: "'my long string'" -> wrong
ros_ip_addr_comment: "my long string" -> wrong
ros_ip_addr_comment: 'my long string' -> wrong

@felixfontein
Copy link
Collaborator

It's probably a good idea to simplify this in some ways:

  1. Provide Jinja2 filters to quote and unquote parameters.
  2. Allow to pass in parameters as a dictionary to the api module, so you don't need to encode everything as a string first.

@NikolayDachev
Copy link
Collaborator

NikolayDachev commented Sep 27, 2021

Ideally we should be able to use | as this example without escape symbols

{{ ros_backup_script_withes }} - work (tested with current code pull/45/head )
{{ ros_backup_script_noes }} - we want to work as well (if is possible ! )

I'm not sure how correctly to utilize

ros_backup_script_noes: |
  {%raw%} 
  .. 
  {%endraw%}
  hosts: ros_testing
  gather_facts: no
  connection: local

  tasks:
  - name: add script
    vars:
      ros_backup_script_withes: '"
        :local host value=[/system identity get name];
        :local date value=[/system clock get date];
        :local day [ :pick $date 4 6 ];
        :local month [ :pick $date 0 3 ];
        :local year [ :pick $date 7 11 ];
        :local name value=($host.\"-\".$day.\"-\".$month.\"-\".$year);
        /system backup save name=$name;
        /export file=$name;
        /tool fetch address=192.168.1.1 user=ros password=\"PASSWORD\" mode=ftp dst-path=(\"/mikrotik/rsc/\".$name.\".rsc\") src-path=($name.\".rsc\") upload=yes;
        /tool fetch address=192.168.1.1 user=ros password=\"PASSWORD\" mode=ftp dst-path=(\"/mikrotik/backup/\".$name.\".backup\") src-path=($name.\".backup\") upload=yes;
        "'
      
      ros_backup_script_noes: |
        :local host value=[/system identity get name];
        :local date value=[/system clock get date];
        :local day [ :pick $date 4 6 ];
        :local month [ :pick $date 0 3 ];
        :local year [ :pick $date 7 11 ];
        :local name value=($host."-".$day."-".$month."-".$year);
        /system backup save name=$name;
        /export file=$name;
        /tool fetch address=192.168.1.1 user=ros password="PASSWORD" mode=ftp dst-path=("/mikrotik/rsc/".$name.".rsc") src-path=($name.".rsc") upload=yes;
        /tool fetch address=192.168.1.1 user=ros password="PASSWORD" mode=ftp dst-path=("/mikrotik/backup/".$name.".backup") src-path=($name.".backup") upload=yes;

    community.routeros.api:
      hostname: "{{ inventory_hostname }}"
      ssl: "false"
      password: "{{ vault_common_pwd }}"
      username: "admin"
      path: "system script"
      add:
        name="test"
        source={{ ros_backup_script_withes }}
    register: scriptout
    delegate_to: localhost
  
  - name: result ip address
    debug:
      msg: '{{ scriptout }}'

@NikolayDachev
Copy link
Collaborator

NikolayDachev commented Sep 27, 2021

2. Allow to pass in parameters as a dictionary to the api module, so you don't need to encode everything as a string first.

well the idea is to be close as ros cmd, but both cases are good idea as well !

@NikolayDachev
Copy link
Collaborator

I believe we can submit the code (if there is no other objections )

For dictionary arguments will solve some problems which we have, however will open others, so I suggest to open separate pr for it.

@NikolayDachev NikolayDachev self-requested a review September 27, 2021 21:36
@felixfontein
Copy link
Collaborator

I agree, let's merge this and then work on improvements in further PRs.

@felixfontein felixfontein merged commit 5042905 into ansible-collections:main Sep 29, 2021
@felixfontein
Copy link
Collaborator

@akimrx thanks for your contribution!
@heuels @NikolayDachev thanks for reviewing!

@felixfontein
Copy link
Collaborator

@NikolayDachev with #53, you can do

  - community.routeros.api:
      hostname: "{{ inventory_hostname }}"
      ssl: "false"
      password: "{{ vault_common_pwd }}"
      username: "admin"
      path: "system script"
      add:
        name="test"
        source={{ ros_backup_script_noes | community.routeros.quote_argument_value }}
    register: scriptout
    delegate_to: localhost

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.

[API] The comment breaks the work of the module
4 participants