Skip to content

Commit 9afde3b

Browse files
committed
Logout when module finishes
Signed-off-by: Justin Cinkelj <[email protected]>
1 parent d38769e commit 9afde3b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+259
-231
lines changed

plugins/module_utils/client.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import os
1616
import ssl
1717
from io import BufferedReader
18+
from types import TracebackType
1819
from typing import Any
1920
from typing import Optional
2021
from typing import Union
@@ -140,6 +141,33 @@ def _login_username_password(self) -> dict[str, str]:
140141
)
141142
return dict(Cookie=f"sessionID={resp.json['sessionID']}")
142143

144+
def _logout(self) -> None:
145+
if not self._auth_header:
146+
return
147+
headers = {
148+
"Accept": "application/json",
149+
"Content-type": "application/json",
150+
}
151+
self._request(
152+
"POST",
153+
f"{self.host}/rest/v1/logout",
154+
# data=json.dumps({}),
155+
headers=headers,
156+
timeout=self.timeout,
157+
)
158+
self._auth_header = None
159+
160+
def __enter__(self) -> Client:
161+
return self
162+
163+
def __exit__(
164+
self,
165+
exc_type: type[BaseException] | None,
166+
exc_value: BaseException | None,
167+
exc_traceback: TracebackType | None,
168+
) -> None:
169+
self._logout()
170+
143171
def _request(
144172
self,
145173
method: str,

plugins/modules/api.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,10 @@ def main():
333333
)
334334

335335
try:
336-
client = Client.get_client(module.params["cluster_instance"])
337-
rest_client = RestClient(client)
338-
changed, record = run(module, rest_client)
339-
module.exit_json(changed=changed, record=record)
336+
with Client.get_client(module.params["cluster_instance"]) as client:
337+
rest_client = RestClient(client)
338+
changed, record = run(module, rest_client)
339+
module.exit_json(changed=changed, record=record)
340340
except errors.ScaleComputingError as e:
341341
module.fail_json(msg=str(e))
342342

plugins/modules/certificate.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,10 @@ def main() -> None:
148148
)
149149

150150
try:
151-
client = Client.get_client(module.params["cluster_instance"])
152-
rest_client = RestClient(client=client)
153-
changed, record, diff = run(module, rest_client)
154-
module.exit_json(changed=changed, record=record, diff=diff)
151+
with Client.get_client(module.params["cluster_instance"]) as client:
152+
rest_client = RestClient(client=client)
153+
changed, record, diff = run(module, rest_client)
154+
module.exit_json(changed=changed, record=record, diff=diff)
155155
except errors.ScaleComputingError as e:
156156
module.fail_json(msg=str(e))
157157

plugins/modules/cluster_info.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ def main() -> None:
7979
)
8080

8181
try:
82-
client = Client.get_client(module.params["cluster_instance"])
83-
rest_client = RestClient(client)
84-
record = run(rest_client)
85-
module.exit_json(changed=False, record=record)
82+
with Client.get_client(module.params["cluster_instance"]) as client:
83+
rest_client = RestClient(client)
84+
record = run(rest_client)
85+
module.exit_json(changed=False, record=record)
8686

8787
except errors.ScaleComputingError as e:
8888
module.fail_json(msg=str(e))

plugins/modules/cluster_name.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,12 @@ def main() -> None:
110110
)
111111

112112
try:
113-
client = Client.get_client(module.params["cluster_instance"])
114-
rest_client = RestClient(client)
115-
hcversion = HyperCoreVersion(rest_client)
116-
hcversion.check_version(module, HYPERCORE_VERSION_REQUIREMENTS)
117-
changed, record, diff = run(module, rest_client)
118-
module.exit_json(changed=changed, record=record, diff=diff)
113+
with Client.get_client(module.params["cluster_instance"]) as client:
114+
rest_client = RestClient(client)
115+
hcversion = HyperCoreVersion(rest_client)
116+
hcversion.check_version(module, HYPERCORE_VERSION_REQUIREMENTS)
117+
changed, record, diff = run(module, rest_client)
118+
module.exit_json(changed=changed, record=record, diff=diff)
119119
except errors.ScaleComputingError as e:
120120
module.fail_json(msg=str(e))
121121

plugins/modules/cluster_shutdown.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ def main() -> None:
7575
)
7676

7777
try:
78-
client = Client.get_client(module.params["cluster_instance"])
79-
rest_client = RestClient(client)
80-
shutdown = run(module, rest_client)
81-
module.exit_json(changed=True, shutdown=shutdown)
78+
with Client.get_client(module.params["cluster_instance"]) as client:
79+
rest_client = RestClient(client)
80+
shutdown = run(module, rest_client)
81+
module.exit_json(changed=True, shutdown=shutdown)
8282
except errors.ScaleComputingError as e:
8383
module.fail_json(msg=str(e))
8484

plugins/modules/dns_config.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,10 @@ def main() -> None:
277277
)
278278

279279
try:
280-
client = Client.get_client(module.params["cluster_instance"])
281-
rest_client = RestClient(client)
282-
changed, new_state, diff = run(module, rest_client)
283-
module.exit_json(changed=changed, new_state=new_state, diff=diff)
280+
with Client.get_client(module.params["cluster_instance"]) as client:
281+
rest_client = RestClient(client)
282+
changed, new_state, diff = run(module, rest_client)
283+
module.exit_json(changed=changed, new_state=new_state, diff=diff)
284284
except errors.ScaleComputingError as e:
285285
module.fail_json(msg=str(e))
286286

plugins/modules/dns_config_info.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ def main():
100100
)
101101

102102
try:
103-
client = Client.get_client(module.params["cluster_instance"])
104-
rest_client = RestClient(client)
105-
record = run(rest_client)
106-
module.exit_json(changed=False, record=record)
103+
with Client.get_client(module.params["cluster_instance"]) as client:
104+
rest_client = RestClient(client)
105+
record = run(rest_client)
106+
module.exit_json(changed=False, record=record)
107107
except errors.ScaleComputingError as e:
108108
module.fail_json(msg=str(e))
109109

plugins/modules/email_alert.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,11 @@ def main() -> None:
278278
)
279279

280280
try:
281-
client = Client.get_client(module.params["cluster_instance"])
282-
rest_client = RestClient(client)
283-
validate_params(module)
284-
changed, record, diff = run(module, rest_client)
285-
module.exit_json(changed=changed, record=record, diff=diff)
281+
with Client.get_client(module.params["cluster_instance"]) as client:
282+
rest_client = RestClient(client)
283+
validate_params(module)
284+
changed, record, diff = run(module, rest_client)
285+
module.exit_json(changed=changed, record=record, diff=diff)
286286
except errors.ScaleComputingError as e:
287287
module.fail_json(msg=str(e))
288288

plugins/modules/email_alert_info.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ def main() -> None:
109109
)
110110

111111
try:
112-
client = Client.get_client(module.params["cluster_instance"])
113-
rest_client = RestClient(client)
114-
records = run(rest_client)
115-
module.exit_json(changed=False, records=records)
112+
with Client.get_client(module.params["cluster_instance"]) as client:
113+
rest_client = RestClient(client)
114+
records = run(rest_client)
115+
module.exit_json(changed=False, records=records)
116116
except errors.ScaleComputingError as e:
117117
module.fail_json(msg=str(e))
118118

0 commit comments

Comments
 (0)