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

login reference count with context manager #14

Closed
hudecof opened this issue Dec 27, 2019 · 6 comments
Closed

login reference count with context manager #14

hudecof opened this issue Dec 27, 2019 · 6 comments

Comments

@hudecof
Copy link

hudecof commented Dec 27, 2019

Hi,

Would like to have some kind of reference counter for login/logout.

For simplicity I provide code that not work, but in my case it's a little bit more complex

with fmg as fmg_instance1:
   with fmg as fmg_instance2:
      <do something with fmg_instance2>
   <do something with fmg_instance1, but there is not session, you are logout>

@p4r4n0y1ng
Copy link
Owner

Can you explain why you need this a little better. Maybe I can fix it so that it works for what you need if I understand the issue better.

@hudecof
Copy link
Author

hudecof commented Feb 28, 2020

Hi @p4r4n0y1ng ,

my problem is following.
I'm using one framework for API /fastapi/ and fortimanager is one ofthe backend I'm trying connect to. The problem than the instance of the class is created on server startup, not found the lazy method /on first call/

On each call, where do I need the fmg instance I do login/using context manager. There are some functions which could be called directly or as result of another call. If the function is called directly, there is no problem. if is called as nested, the context manager will do the second time login.

I could not create session on server start, since if the fortimanager is not available, the server failed to start. Or the session could expiry and there is not automatic renewal. I have similar issue with the IPAM client library.

I do not understand the magic with _lock_ctx, otherwise I would do it myself. It's something like nested transaction is sql.

regards
Peter

@p4r4n0y1ng
Copy link
Owner

I still don't see this as an issue. Please show me the code or a piece of real code that is causing issues so I can better determine if this is something that can be fixed or if it's best just to not use the context and control the login and logout yourself.

@hudecof
Copy link
Author

hudecof commented Apr 14, 2020

the code is in the issue description. This is very simple example, my code is more complex, but it illustrates the usage.

I need to login on first function call, so oin each function I use contex manager for this purpose.
The problem is I can't use nested contex manager calls.

    fmg = FortiManager(os.getenv('FMG_IP'), os.getenv('FMG_USER'), os.getenv('FMG_PASS'), debug=False, timeout=20)
    with fmg as instance1:
        with fmg as instance2:
            device_info(instance1)
            device_info(instance2)
        device_info(instance1)

or

    fmg = FortiManager(os.getenv('FMG_IP'), os.getenv('FMG_USER'), os.getenv('FMG_PASS'), debug=False, timeout=20)
    with fmg as instance1:
        with fmg as instance2:
            device_info(instance1)
            device_info(instance2)
        device_info(instance1)

result is

OK
OK
/Users/hudecof/devel/cnc/edunet/poc-fortimanager/env/lib/python3.7/site-packages/urllib3/connectionpool.py:1004: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
  InsecureRequestWarning,
Traceback (most recent call last):
  File "./test.py", line 225, in <module>
    main(args)
  File "./test.py", line 212, in main
    device_info(instance1)
  File "./test.py", line 93, in device_info
    result = fmg.get('/dvmdb/device', params)
  File "/Users/hudecof/devel/cnc/edunet/poc-fortimanager/env/lib/python3.7/site-packages/pyFMG/fortimgr.py", line 517, in get
    return self._post_request("get", self.common_datagram_params("get", url, *args, **kwargs))
  File "/Users/hudecof/devel/cnc/edunet/poc-fortimanager/env/lib/python3.7/site-packages/pyFMG/fortimgr.py", line 371, in _post_request
    raise FMGValidSessionException(method, params)
pyFMG.fortimgr.FMGValidSessionException: A call using the get method was requested to /dvmdb/device on a FortiManager instance that had no valid session or was not connected. Paramaters were:
[{'url': '/dvmdb/device', 'fields': ['sn', 'name', 'hostname', 'ip', 'platform_str', 'hw_rev_major', 'hw_rev_minor']}]

@p4r4n0y1ng
Copy link
Owner

Again, why...? Just login and send the reference around. why do you need the multiple context calls and multiple logins. If you want to check it for session livelihood, you can (str is overridden for that if you want) and if it's not logged in, call the login function again...I don't see the issue....your code is there certainly but you're not explaining why you have to do nested contexts. Put pyFMG in a class and make your own singleton won't work?

@hudecof
Copy link
Author

hudecof commented May 5, 2020

@p4r4n0y1ng OK, I rewrite the code, no need this any more

@hudecof hudecof closed this as completed May 5, 2020
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

No branches or pull requests

2 participants