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

Adom Lock with Contextmanager #30

Merged
merged 13 commits into from
Sep 7, 2023
Merged

Conversation

icovada
Copy link
Contributor

@icovada icovada commented May 23, 2023

This PR adds a couple extras like removing unused imports and a dangerous default "{}" in a function call

Breaking changes:

  • login() and logout() return None, not a Session()
  • login() raises ValueError if login fails

It looks enormous because of the formatting I've run on it.

@icovada icovada marked this pull request as draft May 23, 2023 13:37
@icovada
Copy link
Contributor Author

icovada commented May 23, 2023

Added commits 946394 and e108e3a with ContextManagers.

The goal is to avoid leaving open sessions and/or stale ADM locks should your code have an issue between open and close, you can call this library like so:

with FortiManager([data]) as fm:
    with Lock([data]) as admlock:
        fm.do_stuff()

instead of

fm = FortiManager()
fm.login()
fm.lock_adm("whatever")
fm.do_stuff()
fm.unlock_adm("whatever")
fm.logout()

@icovada
Copy link
Contributor Author

icovada commented May 23, 2023

Done.
Final code should be:

with FortiManager([data]) as fm:
    adm_lock = fm.get_lock()  # can pass an ADOM name to get a lock for that ADOM
    with adm_lock:
        do_stuff()

@icovada icovada marked this pull request as ready for review May 23, 2023 14:44
@icovada icovada changed the title Use a single Session throughout the library Adom Lock with Contextmanager May 25, 2023
@akshaymane920 akshaymane920 merged commit f8d89c3 into akshaymane920:master Sep 7, 2023
@moustic999
Copy link

Hello, It seems there is a missing "return self" in the enter method of FortiManager. This make the with statement giving None Object !

@icovada
Copy link
Contributor Author

icovada commented Sep 20, 2023

Oops... well this PR is already merged, I'd need a new one, can't you fix this yourself?

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.

None yet

3 participants