Skip to content

Commit f98774e

Browse files
committed
Logout when module finishes
Signed-off-by: Justin Cinkelj <[email protected]>
1 parent 6ea2c66 commit f98774e

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

+269
-231
lines changed

plugins/module_utils/client.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import os
1313
import ssl
1414
from typing import Any, Optional, Union
15+
from types import TracebackType
1516
from io import BufferedReader
1617
import enum
1718

@@ -142,6 +143,33 @@ def _login_username_password(self) -> dict[str, str]:
142143
)
143144
return dict(Cookie=f"sessionID={resp.json['sessionID']}")
144145

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

plugins/modules/api.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,10 +329,10 @@ def main():
329329
)
330330

331331
try:
332-
client = Client.get_client(module.params["cluster_instance"])
333-
rest_client = RestClient(client)
334-
changed, record = run(module, rest_client)
335-
module.exit_json(changed=changed, record=record)
332+
with Client.get_client(module.params["cluster_instance"]) as client:
333+
rest_client = RestClient(client)
334+
changed, record = run(module, rest_client)
335+
module.exit_json(changed=changed, record=record)
336336
except errors.ScaleComputingError as e:
337337
module.fail_json(msg=str(e))
338338

plugins/modules/certificate.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,10 @@ def main() -> None:
158158
)
159159

160160
try:
161-
client = Client.get_client(module.params["cluster_instance"])
162-
rest_client = RestClient(client=client)
163-
changed, record, diff = run(module, rest_client)
164-
module.exit_json(changed=changed, record=record, diff=diff)
161+
with Client.get_client(module.params["cluster_instance"]) as client:
162+
rest_client = RestClient(client=client)
163+
changed, record, diff = run(module, rest_client)
164+
module.exit_json(changed=changed, record=record, diff=diff)
165165
except errors.ScaleComputingError as e:
166166
module.fail_json(msg=str(e))
167167

plugins/modules/cluster_info.py

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

7878
try:
79-
client = Client.get_client(module.params["cluster_instance"])
80-
rest_client = RestClient(client)
81-
record = run(rest_client)
82-
module.exit_json(changed=False, record=record)
79+
with Client.get_client(module.params["cluster_instance"]) as client:
80+
rest_client = RestClient(client)
81+
record = run(rest_client)
82+
module.exit_json(changed=False, record=record)
8383

8484
except errors.ScaleComputingError as e:
8585
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
@@ -109,12 +109,12 @@ def main() -> None:
109109
)
110110

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

plugins/modules/cluster_shutdown.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ def main() -> None:
7272
)
7373

7474
try:
75-
client = Client.get_client(module.params["cluster_instance"])
76-
rest_client = RestClient(client)
77-
shutdown = run(module, rest_client)
78-
module.exit_json(changed=True, shutdown=shutdown)
75+
with Client.get_client(module.params["cluster_instance"]) as client:
76+
rest_client = RestClient(client)
77+
shutdown = run(module, rest_client)
78+
module.exit_json(changed=True, shutdown=shutdown)
7979
except errors.ScaleComputingError as e:
8080
module.fail_json(msg=str(e))
8181

plugins/modules/dns_config.py

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

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

plugins/modules/dns_config_info.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ def main():
9797
)
9898

9999
try:
100-
client = Client.get_client(module.params["cluster_instance"])
101-
rest_client = RestClient(client)
102-
record = run(rest_client)
103-
module.exit_json(changed=False, record=record)
100+
with Client.get_client(module.params["cluster_instance"]) as client:
101+
rest_client = RestClient(client)
102+
record = run(rest_client)
103+
module.exit_json(changed=False, record=record)
104104
except errors.ScaleComputingError as e:
105105
module.fail_json(msg=str(e))
106106

plugins/modules/email_alert.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,11 @@ def main() -> None:
291291
)
292292

293293
try:
294-
client = Client.get_client(module.params["cluster_instance"])
295-
rest_client = RestClient(client)
296-
validate_params(module)
297-
changed, record, diff = run(module, rest_client)
298-
module.exit_json(changed=changed, record=record, diff=diff)
294+
with Client.get_client(module.params["cluster_instance"]) as client:
295+
rest_client = RestClient(client)
296+
validate_params(module)
297+
changed, record, diff = run(module, rest_client)
298+
module.exit_json(changed=changed, record=record, diff=diff)
299299
except errors.ScaleComputingError as e:
300300
module.fail_json(msg=str(e))
301301

plugins/modules/email_alert_info.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ def main() -> None:
104104
)
105105

106106
try:
107-
client = Client.get_client(module.params["cluster_instance"])
108-
rest_client = RestClient(client)
109-
records = run(rest_client)
110-
module.exit_json(changed=False, records=records)
107+
with Client.get_client(module.params["cluster_instance"]) as client:
108+
rest_client = RestClient(client)
109+
records = run(rest_client)
110+
module.exit_json(changed=False, records=records)
111111
except errors.ScaleComputingError as e:
112112
module.fail_json(msg=str(e))
113113

0 commit comments

Comments
 (0)