From 9afde3baefe3cff3421d9f6d02e7096d664e558f Mon Sep 17 00:00:00 2001 From: Justin Cinkelj Date: Tue, 30 Sep 2025 07:56:51 +0200 Subject: [PATCH] Logout when module finishes Signed-off-by: Justin Cinkelj --- plugins/module_utils/client.py | 28 +++++++++++++++++++ plugins/modules/api.py | 8 +++--- plugins/modules/certificate.py | 8 +++--- plugins/modules/cluster_info.py | 8 +++--- plugins/modules/cluster_name.py | 12 ++++---- plugins/modules/cluster_shutdown.py | 8 +++--- plugins/modules/dns_config.py | 8 +++--- plugins/modules/dns_config_info.py | 8 +++--- plugins/modules/email_alert.py | 10 +++---- plugins/modules/email_alert_info.py | 8 +++--- plugins/modules/iso.py | 8 +++--- plugins/modules/iso_info.py | 8 +++--- plugins/modules/node_info.py | 8 +++--- plugins/modules/oidc_config.py | 8 +++--- plugins/modules/oidc_config_info.py | 8 +++--- plugins/modules/registration.py | 8 +++--- plugins/modules/registration_info.py | 8 +++--- plugins/modules/remote_cluster_info.py | 8 +++--- plugins/modules/smtp.py | 8 +++--- plugins/modules/smtp_info.py | 8 +++--- plugins/modules/snapshot_schedule.py | 8 +++--- plugins/modules/snapshot_schedule_info.py | 8 +++--- plugins/modules/support_tunnel.py | 6 ++-- plugins/modules/support_tunnel_info.py | 6 ++-- plugins/modules/syslog_server.py | 8 +++--- plugins/modules/syslog_server_info.py | 8 +++--- plugins/modules/task_wait.py | 8 +++--- plugins/modules/time_server.py | 8 +++--- plugins/modules/time_server_info.py | 8 +++--- plugins/modules/time_zone.py | 8 +++--- plugins/modules/time_zone_info.py | 8 +++--- plugins/modules/user.py | 8 +++--- plugins/modules/user_info.py | 8 +++--- plugins/modules/version_update.py | 8 +++--- plugins/modules/version_update_info.py | 8 +++--- plugins/modules/version_update_status_info.py | 8 +++--- plugins/modules/virtual_disk.py | 12 ++++---- plugins/modules/virtual_disk_attach.py | 12 ++++---- plugins/modules/virtual_disk_info.py | 12 ++++---- plugins/modules/vm.py | 12 ++++---- plugins/modules/vm_boot_devices.py | 8 +++--- plugins/modules/vm_clone.py | 8 +++--- plugins/modules/vm_disk.py | 10 +++---- plugins/modules/vm_export.py | 8 +++--- plugins/modules/vm_import.py | 8 +++--- plugins/modules/vm_info.py | 8 +++--- plugins/modules/vm_nic.py | 18 ++++++------ plugins/modules/vm_nic_info.py | 8 +++--- plugins/modules/vm_node_affinity.py | 8 +++--- plugins/modules/vm_params.py | 8 +++--- plugins/modules/vm_replication.py | 8 +++--- plugins/modules/vm_replication_info.py | 8 +++--- plugins/modules/vm_snapshot.py | 8 +++--- plugins/modules/vm_snapshot_attach_disk.py | 8 +++--- plugins/modules/vm_snapshot_info.py | 8 +++--- 55 files changed, 259 insertions(+), 231 deletions(-) diff --git a/plugins/module_utils/client.py b/plugins/module_utils/client.py index c56806eb..1c9b31d8 100644 --- a/plugins/module_utils/client.py +++ b/plugins/module_utils/client.py @@ -15,6 +15,7 @@ import os import ssl from io import BufferedReader +from types import TracebackType from typing import Any from typing import Optional from typing import Union @@ -140,6 +141,33 @@ def _login_username_password(self) -> dict[str, str]: ) return dict(Cookie=f"sessionID={resp.json['sessionID']}") + def _logout(self) -> None: + if not self._auth_header: + return + headers = { + "Accept": "application/json", + "Content-type": "application/json", + } + self._request( + "POST", + f"{self.host}/rest/v1/logout", + # data=json.dumps({}), + headers=headers, + timeout=self.timeout, + ) + self._auth_header = None + + def __enter__(self) -> Client: + return self + + def __exit__( + self, + exc_type: type[BaseException] | None, + exc_value: BaseException | None, + exc_traceback: TracebackType | None, + ) -> None: + self._logout() + def _request( self, method: str, diff --git a/plugins/modules/api.py b/plugins/modules/api.py index 358745ec..4642c4ed 100644 --- a/plugins/modules/api.py +++ b/plugins/modules/api.py @@ -333,10 +333,10 @@ def main(): ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client) - changed, record = run(module, rest_client) - module.exit_json(changed=changed, record=record) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client) + changed, record = run(module, rest_client) + module.exit_json(changed=changed, record=record) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/certificate.py b/plugins/modules/certificate.py index 3b9b9afe..bb32884b 100644 --- a/plugins/modules/certificate.py +++ b/plugins/modules/certificate.py @@ -148,10 +148,10 @@ def main() -> None: ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client=client) - changed, record, diff = run(module, rest_client) - module.exit_json(changed=changed, record=record, diff=diff) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client=client) + changed, record, diff = run(module, rest_client) + module.exit_json(changed=changed, record=record, diff=diff) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/cluster_info.py b/plugins/modules/cluster_info.py index ed8d8bf6..b76ada73 100644 --- a/plugins/modules/cluster_info.py +++ b/plugins/modules/cluster_info.py @@ -79,10 +79,10 @@ def main() -> None: ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client) - record = run(rest_client) - module.exit_json(changed=False, record=record) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client) + record = run(rest_client) + module.exit_json(changed=False, record=record) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/cluster_name.py b/plugins/modules/cluster_name.py index 1103b50e..4866f14e 100644 --- a/plugins/modules/cluster_name.py +++ b/plugins/modules/cluster_name.py @@ -110,12 +110,12 @@ def main() -> None: ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client) - hcversion = HyperCoreVersion(rest_client) - hcversion.check_version(module, HYPERCORE_VERSION_REQUIREMENTS) - changed, record, diff = run(module, rest_client) - module.exit_json(changed=changed, record=record, diff=diff) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client) + hcversion = HyperCoreVersion(rest_client) + hcversion.check_version(module, HYPERCORE_VERSION_REQUIREMENTS) + changed, record, diff = run(module, rest_client) + module.exit_json(changed=changed, record=record, diff=diff) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/cluster_shutdown.py b/plugins/modules/cluster_shutdown.py index e40905b9..d79af0f9 100644 --- a/plugins/modules/cluster_shutdown.py +++ b/plugins/modules/cluster_shutdown.py @@ -75,10 +75,10 @@ def main() -> None: ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client) - shutdown = run(module, rest_client) - module.exit_json(changed=True, shutdown=shutdown) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client) + shutdown = run(module, rest_client) + module.exit_json(changed=True, shutdown=shutdown) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/dns_config.py b/plugins/modules/dns_config.py index c82bd372..fbacf3fc 100644 --- a/plugins/modules/dns_config.py +++ b/plugins/modules/dns_config.py @@ -277,10 +277,10 @@ def main() -> None: ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client) - changed, new_state, diff = run(module, rest_client) - module.exit_json(changed=changed, new_state=new_state, diff=diff) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client) + changed, new_state, diff = run(module, rest_client) + module.exit_json(changed=changed, new_state=new_state, diff=diff) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/dns_config_info.py b/plugins/modules/dns_config_info.py index f559d470..0f4e341f 100644 --- a/plugins/modules/dns_config_info.py +++ b/plugins/modules/dns_config_info.py @@ -100,10 +100,10 @@ def main(): ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client) - record = run(rest_client) - module.exit_json(changed=False, record=record) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client) + record = run(rest_client) + module.exit_json(changed=False, record=record) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/email_alert.py b/plugins/modules/email_alert.py index 1da221ad..728f840a 100644 --- a/plugins/modules/email_alert.py +++ b/plugins/modules/email_alert.py @@ -278,11 +278,11 @@ def main() -> None: ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client) - validate_params(module) - changed, record, diff = run(module, rest_client) - module.exit_json(changed=changed, record=record, diff=diff) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client) + validate_params(module) + changed, record, diff = run(module, rest_client) + module.exit_json(changed=changed, record=record, diff=diff) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/email_alert_info.py b/plugins/modules/email_alert_info.py index 13adfc2b..92c4f72c 100644 --- a/plugins/modules/email_alert_info.py +++ b/plugins/modules/email_alert_info.py @@ -109,10 +109,10 @@ def main() -> None: ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client) - records = run(rest_client) - module.exit_json(changed=False, records=records) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client) + records = run(rest_client) + module.exit_json(changed=False, records=records) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/iso.py b/plugins/modules/iso.py index 7ce338f9..6660aa4d 100644 --- a/plugins/modules/iso.py +++ b/plugins/modules/iso.py @@ -279,10 +279,10 @@ def main(): ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client) - changed, record, diff = run(module, rest_client) - module.exit_json(changed=changed, record=record, results=[record], diff=diff) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client) + changed, record, diff = run(module, rest_client) + module.exit_json(changed=changed, record=record, results=[record], diff=diff) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/iso_info.py b/plugins/modules/iso_info.py index 68db2dd0..c02b3995 100644 --- a/plugins/modules/iso_info.py +++ b/plugins/modules/iso_info.py @@ -123,10 +123,10 @@ def main(): ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client) - records = run(module, rest_client) - module.exit_json(changed=False, records=records) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client) + records = run(module, rest_client) + module.exit_json(changed=False, records=records) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/node_info.py b/plugins/modules/node_info.py index 0fa3722e..5220aaea 100644 --- a/plugins/modules/node_info.py +++ b/plugins/modules/node_info.py @@ -83,10 +83,10 @@ def main(): ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client) - records = run(rest_client) - module.exit_json(changed=False, records=records) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client) + records = run(rest_client) + module.exit_json(changed=False, records=records) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/oidc_config.py b/plugins/modules/oidc_config.py index 2b9e1857..d1e7592a 100644 --- a/plugins/modules/oidc_config.py +++ b/plugins/modules/oidc_config.py @@ -179,10 +179,10 @@ def main() -> None: ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client=client) - changed, record, diff = run(module, rest_client) - module.exit_json(changed=changed, record=record, diff=diff) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client=client) + changed, record, diff = run(module, rest_client) + module.exit_json(changed=changed, record=record, diff=diff) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/oidc_config_info.py b/plugins/modules/oidc_config_info.py index 69f43d51..7341dc90 100644 --- a/plugins/modules/oidc_config_info.py +++ b/plugins/modules/oidc_config_info.py @@ -83,10 +83,10 @@ def main() -> None: ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = CachedRestClient(client=client) - changed, record = run(module, rest_client) - module.exit_json(changed=changed, record=record) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = CachedRestClient(client=client) + changed, record = run(module, rest_client) + module.exit_json(changed=changed, record=record) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/registration.py b/plugins/modules/registration.py index d369b4cd..2e2d0f3c 100644 --- a/plugins/modules/registration.py +++ b/plugins/modules/registration.py @@ -184,10 +184,10 @@ def main() -> None: ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client=client) - changed, record, diff = run(module, rest_client) - module.exit_json(changed=changed, record=record, diff=diff) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client=client) + changed, record, diff = run(module, rest_client) + module.exit_json(changed=changed, record=record, diff=diff) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/registration_info.py b/plugins/modules/registration_info.py index 806ada7d..3d938404 100644 --- a/plugins/modules/registration_info.py +++ b/plugins/modules/registration_info.py @@ -90,10 +90,10 @@ def main() -> None: ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = CachedRestClient(client) - record = run(module, rest_client) - module.exit_json(changed=False, record=record) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = CachedRestClient(client) + record = run(module, rest_client) + module.exit_json(changed=False, record=record) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/remote_cluster_info.py b/plugins/modules/remote_cluster_info.py index c5d6ee01..577b35bd 100644 --- a/plugins/modules/remote_cluster_info.py +++ b/plugins/modules/remote_cluster_info.py @@ -116,10 +116,10 @@ def main(): ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client) - records = run(module, rest_client) - module.exit_json(changed=False, records=records) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client) + records = run(module, rest_client) + module.exit_json(changed=False, records=records) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/smtp.py b/plugins/modules/smtp.py index 572847d5..45f8dd4b 100644 --- a/plugins/modules/smtp.py +++ b/plugins/modules/smtp.py @@ -327,10 +327,10 @@ def main() -> None: ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client) - changed, record, diff = run(module, rest_client) - module.exit_json(changed=changed, record=record, diff=diff) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client) + changed, record, diff = run(module, rest_client) + module.exit_json(changed=changed, record=record, diff=diff) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/smtp_info.py b/plugins/modules/smtp_info.py index c1fcbdca..ffba75b5 100644 --- a/plugins/modules/smtp_info.py +++ b/plugins/modules/smtp_info.py @@ -129,10 +129,10 @@ def main() -> None: ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client) - record = run(rest_client) - module.exit_json(changed=False, record=record) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client) + record = run(rest_client) + module.exit_json(changed=False, record=record) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/snapshot_schedule.py b/plugins/modules/snapshot_schedule.py index 46d55dac..e0f2d492 100644 --- a/plugins/modules/snapshot_schedule.py +++ b/plugins/modules/snapshot_schedule.py @@ -249,10 +249,10 @@ def main(): ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client) - changed, record, diff = run(module, rest_client) - module.exit_json(changed=changed, record=record, diff=diff) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client) + changed, record, diff = run(module, rest_client) + module.exit_json(changed=changed, record=record, diff=diff) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/snapshot_schedule_info.py b/plugins/modules/snapshot_schedule_info.py index 06830e55..262a182d 100644 --- a/plugins/modules/snapshot_schedule_info.py +++ b/plugins/modules/snapshot_schedule_info.py @@ -108,10 +108,10 @@ def main(): ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client) - records = run(module, rest_client) - module.exit_json(changed=False, records=records) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client) + records = run(module, rest_client) + module.exit_json(changed=False, records=records) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/support_tunnel.py b/plugins/modules/support_tunnel.py index 76bf4ca5..87f9abc1 100644 --- a/plugins/modules/support_tunnel.py +++ b/plugins/modules/support_tunnel.py @@ -145,9 +145,9 @@ def main() -> None: ) try: - client = Client.get_client(module.params["cluster_instance"]) - changed, record, diff = run(module, client) - module.exit_json(changed=changed, record=record, diff=diff) + with Client.get_client(module.params["cluster_instance"]) as client: + changed, record, diff = run(module, client) + module.exit_json(changed=changed, record=record, diff=diff) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/support_tunnel_info.py b/plugins/modules/support_tunnel_info.py index 99f4b586..b5f5da69 100644 --- a/plugins/modules/support_tunnel_info.py +++ b/plugins/modules/support_tunnel_info.py @@ -66,9 +66,9 @@ def main() -> None: ) try: - client = Client.get_client(module.params["cluster_instance"]) - record = run(client) - module.exit_json(record=record) + with Client.get_client(module.params["cluster_instance"]) as client: + record = run(client) + module.exit_json(record=record) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/syslog_server.py b/plugins/modules/syslog_server.py index cd02d60d..0058f0fc 100644 --- a/plugins/modules/syslog_server.py +++ b/plugins/modules/syslog_server.py @@ -445,10 +445,10 @@ def main() -> None: ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client) - changed, record, records, diff = run(module, rest_client) - module.exit_json(changed=changed, record=record, records=records, diff=diff) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client) + changed, record, records, diff = run(module, rest_client) + module.exit_json(changed=changed, record=record, records=records, diff=diff) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/syslog_server_info.py b/plugins/modules/syslog_server_info.py index d57b6ff1..4eb62aa3 100644 --- a/plugins/modules/syslog_server_info.py +++ b/plugins/modules/syslog_server_info.py @@ -119,10 +119,10 @@ def main() -> None: ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client) - records = run(rest_client) - module.exit_json(changed=False, records=records) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client) + records = run(rest_client) + module.exit_json(changed=False, records=records) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/task_wait.py b/plugins/modules/task_wait.py index 9fa2d579..bdb019e7 100644 --- a/plugins/modules/task_wait.py +++ b/plugins/modules/task_wait.py @@ -78,10 +78,10 @@ def main(): ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client) - changed, record, diff = run(module, rest_client) - module.exit_json(changed=changed, record=record, diff=diff) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client) + changed, record, diff = run(module, rest_client) + module.exit_json(changed=changed, record=record, diff=diff) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/time_server.py b/plugins/modules/time_server.py index 4c1a6e1f..7b4797de 100644 --- a/plugins/modules/time_server.py +++ b/plugins/modules/time_server.py @@ -159,10 +159,10 @@ def main() -> None: ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client) - changed, record, diff = run(module, rest_client) - module.exit_json(changed=changed, record=record, diff=diff) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client) + changed, record, diff = run(module, rest_client) + module.exit_json(changed=changed, record=record, diff=diff) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/time_server_info.py b/plugins/modules/time_server_info.py index c7ffe685..ab749374 100644 --- a/plugins/modules/time_server_info.py +++ b/plugins/modules/time_server_info.py @@ -93,10 +93,10 @@ def main(): ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client) - record = run(rest_client) - module.exit_json(changed=False, record=record) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client) + record = run(rest_client) + module.exit_json(changed=False, record=record) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/time_zone.py b/plugins/modules/time_zone.py index 4d064f07..f329ad66 100644 --- a/plugins/modules/time_zone.py +++ b/plugins/modules/time_zone.py @@ -960,10 +960,10 @@ def main() -> None: ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client) - changed, record, diff = run(module, rest_client) - module.exit_json(changed=changed, record=record, diff=diff) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client) + changed, record, diff = run(module, rest_client) + module.exit_json(changed=changed, record=record, diff=diff) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/time_zone_info.py b/plugins/modules/time_zone_info.py index 071098e3..c2c47f71 100644 --- a/plugins/modules/time_zone_info.py +++ b/plugins/modules/time_zone_info.py @@ -94,10 +94,10 @@ def main(): ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client) - record = run(rest_client) - module.exit_json(changed=False, record=record) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client) + record = run(rest_client) + module.exit_json(changed=False, record=record) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/user.py b/plugins/modules/user.py index 74b2a8bd..2cb6ffb8 100644 --- a/plugins/modules/user.py +++ b/plugins/modules/user.py @@ -283,10 +283,10 @@ def main() -> None: ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = CachedRestClient(client) - changed, record, diff = run(module, rest_client) - module.exit_json(changed=changed, record=record, diff=diff) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = CachedRestClient(client) + changed, record, diff = run(module, rest_client) + module.exit_json(changed=changed, record=record, diff=diff) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/user_info.py b/plugins/modules/user_info.py index d99e37d5..819054ce 100644 --- a/plugins/modules/user_info.py +++ b/plugins/modules/user_info.py @@ -108,10 +108,10 @@ def main() -> None: ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client) - records = run(module, rest_client) - module.exit_json(changed=False, records=records) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client) + records = run(module, rest_client) + module.exit_json(changed=False, records=records) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/version_update.py b/plugins/modules/version_update.py index 138e9f85..ae1a6de4 100644 --- a/plugins/modules/version_update.py +++ b/plugins/modules/version_update.py @@ -168,10 +168,10 @@ def main() -> None: ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client) - changed, record, diff = run(module, rest_client) - module.exit_json(changed=changed, record=record, diff=diff) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client) + changed, record, diff = run(module, rest_client) + module.exit_json(changed=changed, record=record, diff=diff) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/version_update_info.py b/plugins/modules/version_update_info.py index bc00b973..d726b2d1 100644 --- a/plugins/modules/version_update_info.py +++ b/plugins/modules/version_update_info.py @@ -193,10 +193,10 @@ def main() -> None: ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client) - records, next_version, latest_version = run(rest_client) - module.exit_json(changed=False, records=records, next=next_version, latest=latest_version) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client) + records, next_version, latest_version = run(rest_client) + module.exit_json(changed=False, records=records, next=next_version, latest=latest_version) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/version_update_status_info.py b/plugins/modules/version_update_status_info.py index 7c2b0745..a7f8d90d 100644 --- a/plugins/modules/version_update_status_info.py +++ b/plugins/modules/version_update_status_info.py @@ -105,10 +105,10 @@ def main() -> None: ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client) - record = run(rest_client) - module.exit_json(record=record) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client) + record = run(rest_client) + module.exit_json(record=record) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/virtual_disk.py b/plugins/modules/virtual_disk.py index c637856e..9ac7b07a 100644 --- a/plugins/modules/virtual_disk.py +++ b/plugins/modules/virtual_disk.py @@ -209,12 +209,12 @@ def main() -> None: ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client) - hcversion = HyperCoreVersion(rest_client) - hcversion.check_version(module, HYPERCORE_VERSION_REQUIREMENTS) - changed, record, diff = run(module, rest_client) - module.exit_json(changed=changed, record=record, diff=diff) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client) + hcversion = HyperCoreVersion(rest_client) + hcversion.check_version(module, HYPERCORE_VERSION_REQUIREMENTS) + changed, record, diff = run(module, rest_client) + module.exit_json(changed=changed, record=record, diff=diff) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/virtual_disk_attach.py b/plugins/modules/virtual_disk_attach.py index 247f6922..91c6d8cf 100644 --- a/plugins/modules/virtual_disk_attach.py +++ b/plugins/modules/virtual_disk_attach.py @@ -269,12 +269,12 @@ def main() -> None: ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client) - hcversion = HyperCoreVersion(rest_client) - hcversion.check_version(module, HYPERCORE_VERSION_REQUIREMENTS) - changed, record, diff = run(module, rest_client) - module.exit_json(changed=changed, record=record, diff=diff) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client) + hcversion = HyperCoreVersion(rest_client) + hcversion.check_version(module, HYPERCORE_VERSION_REQUIREMENTS) + changed, record, diff = run(module, rest_client) + module.exit_json(changed=changed, record=record, diff=diff) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/virtual_disk_info.py b/plugins/modules/virtual_disk_info.py index 6a8e5689..3e87eee3 100644 --- a/plugins/modules/virtual_disk_info.py +++ b/plugins/modules/virtual_disk_info.py @@ -111,12 +111,12 @@ def main() -> None: ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = CachedRestClient(client) - hcversion = HyperCoreVersion(rest_client) - hcversion.check_version(module, HYPERCORE_VERSION_REQUIREMENTS) - records = run(module, rest_client) - module.exit_json(changed=False, records=records) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = CachedRestClient(client) + hcversion = HyperCoreVersion(rest_client) + hcversion.check_version(module, HYPERCORE_VERSION_REQUIREMENTS) + records = run(module, rest_client) + module.exit_json(changed=False, records=records) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/vm.py b/plugins/modules/vm.py index 00b4924b..ff22f698 100644 --- a/plugins/modules/vm.py +++ b/plugins/modules/vm.py @@ -722,12 +722,12 @@ def main(): ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client) - check_params(module, rest_client) - compute_params(module) - changed, record, diff, reboot = run(module, rest_client) - module.exit_json(changed=changed, record=record, diff=diff, vm_rebooted=reboot) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client) + check_params(module, rest_client) + compute_params(module) + changed, record, diff, reboot = run(module, rest_client) + module.exit_json(changed=changed, record=record, diff=diff, vm_rebooted=reboot) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/vm_boot_devices.py b/plugins/modules/vm_boot_devices.py index 7ae8a6e5..9f0a4fce 100644 --- a/plugins/modules/vm_boot_devices.py +++ b/plugins/modules/vm_boot_devices.py @@ -349,10 +349,10 @@ def main(): ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client) - changed, record, diff, reboot = run(module, rest_client) - module.exit_json(changed=changed, record=record, diff=diff, vm_rebooted=reboot) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client) + changed, record, diff, reboot = run(module, rest_client) + module.exit_json(changed=changed, record=record, diff=diff, vm_rebooted=reboot) except ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/vm_clone.py b/plugins/modules/vm_clone.py index 8d518a9e..c063a208 100644 --- a/plugins/modules/vm_clone.py +++ b/plugins/modules/vm_clone.py @@ -213,10 +213,10 @@ def main(): ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client=client) - changed, msg = run(module, rest_client) - module.exit_json(changed=changed, msg=msg) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client=client) + changed, msg = run(module, rest_client) + module.exit_json(changed=changed, msg=msg) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/vm_disk.py b/plugins/modules/vm_disk.py index b8882c97..e69315f6 100644 --- a/plugins/modules/vm_disk.py +++ b/plugins/modules/vm_disk.py @@ -437,11 +437,11 @@ def main(): ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client) - compute_params(module) - changed, record, diff, reboot = run(module, rest_client) - module.exit_json(changed=changed, record=record, diff=diff, vm_rebooted=reboot) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client) + compute_params(module) + changed, record, diff, reboot = run(module, rest_client) + module.exit_json(changed=changed, record=record, diff=diff, vm_rebooted=reboot) except ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/vm_export.py b/plugins/modules/vm_export.py index 62134809..1f5e6386 100644 --- a/plugins/modules/vm_export.py +++ b/plugins/modules/vm_export.py @@ -153,10 +153,10 @@ def main(): ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client=client) - changed, msg = run(module, rest_client) - module.exit_json(changed=changed, msg=msg) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client=client) + changed, msg = run(module, rest_client) + module.exit_json(changed=changed, msg=msg) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/vm_import.py b/plugins/modules/vm_import.py index 18575e0b..4859b753 100644 --- a/plugins/modules/vm_import.py +++ b/plugins/modules/vm_import.py @@ -222,10 +222,10 @@ def main(): ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client=client) - changed, msg = run(module, rest_client) - module.exit_json(changed=changed, msg=msg) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client=client) + changed, msg = run(module, rest_client) + module.exit_json(changed=changed, msg=msg) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/vm_info.py b/plugins/modules/vm_info.py index 886fe2fa..e5ff099a 100644 --- a/plugins/modules/vm_info.py +++ b/plugins/modules/vm_info.py @@ -179,10 +179,10 @@ def main(): ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = CachedRestClient(client) - records = run(module, rest_client) - module.exit_json(changed=False, records=records) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = CachedRestClient(client) + records = run(module, rest_client) + module.exit_json(changed=False, records=records) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/vm_nic.py b/plugins/modules/vm_nic.py index cea730de..ed92cc76 100644 --- a/plugins/modules/vm_nic.py +++ b/plugins/modules/vm_nic.py @@ -302,15 +302,15 @@ def main(): ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client=client) - changed, records, diff, reboot = run(module, rest_client) - module.exit_json( - changed=changed, - records=records, - diff=diff, - vm_rebooted=reboot, - ) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client=client) + changed, records, diff, reboot = run(module, rest_client) + module.exit_json( + changed=changed, + records=records, + diff=diff, + vm_rebooted=reboot, + ) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/vm_nic_info.py b/plugins/modules/vm_nic_info.py index b610469f..d46d2a3b 100644 --- a/plugins/modules/vm_nic_info.py +++ b/plugins/modules/vm_nic_info.py @@ -115,10 +115,10 @@ def main(): ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client) - changed, records = run(module, rest_client) - module.exit_json(changed=changed, records=records) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client) + changed, records = run(module, rest_client) + module.exit_json(changed=changed, records=records) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/vm_node_affinity.py b/plugins/modules/vm_node_affinity.py index 22dec2c7..52a7f8aa 100644 --- a/plugins/modules/vm_node_affinity.py +++ b/plugins/modules/vm_node_affinity.py @@ -278,10 +278,10 @@ def main(): ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client) - changed, msg, diff = run(module, rest_client) - module.exit_json(changed=changed, msg=msg, diff=diff) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client) + changed, msg, diff = run(module, rest_client) + module.exit_json(changed=changed, msg=msg, diff=diff) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/vm_params.py b/plugins/modules/vm_params.py index 407eddfb..a507368f 100644 --- a/plugins/modules/vm_params.py +++ b/plugins/modules/vm_params.py @@ -205,10 +205,10 @@ def main(): ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client) - changed, reboot, diff = run(module, rest_client) - module.exit_json(changed=changed, vm_rebooted=reboot, diff=diff) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client) + changed, reboot, diff = run(module, rest_client) + module.exit_json(changed=changed, vm_rebooted=reboot, diff=diff) except ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/vm_replication.py b/plugins/modules/vm_replication.py index 2580adc3..cf3d437b 100644 --- a/plugins/modules/vm_replication.py +++ b/plugins/modules/vm_replication.py @@ -205,10 +205,10 @@ def main(): ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client=client) - changed, record, diff = run(module, rest_client) - module.exit_json(changed=changed, record=record, diff=diff) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client=client) + changed, record, diff = run(module, rest_client) + module.exit_json(changed=changed, record=record, diff=diff) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/vm_replication_info.py b/plugins/modules/vm_replication_info.py index b7ee6555..ffcb2946 100644 --- a/plugins/modules/vm_replication_info.py +++ b/plugins/modules/vm_replication_info.py @@ -109,10 +109,10 @@ def main(): ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client) - changed, records = run(module, rest_client) - module.exit_json(changed=changed, records=records) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client) + changed, records = run(module, rest_client) + module.exit_json(changed=changed, records=records) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/vm_snapshot.py b/plugins/modules/vm_snapshot.py index 9b39cd62..b6bd2198 100644 --- a/plugins/modules/vm_snapshot.py +++ b/plugins/modules/vm_snapshot.py @@ -284,10 +284,10 @@ def main() -> None: ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client) - changed, record, diff = run(module, rest_client) - module.exit_json(changed=changed, record=record, diff=diff) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client) + changed, record, diff = run(module, rest_client) + module.exit_json(changed=changed, record=record, diff=diff) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/vm_snapshot_attach_disk.py b/plugins/modules/vm_snapshot_attach_disk.py index 56917737..3c24891e 100644 --- a/plugins/modules/vm_snapshot_attach_disk.py +++ b/plugins/modules/vm_snapshot_attach_disk.py @@ -269,10 +269,10 @@ def main() -> None: ), ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client) - changed, record, diff, reboot = run(module, rest_client) - module.exit_json(changed=changed, record=record, diff=diff, vm_rebooted=reboot) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client) + changed, record, diff, reboot = run(module, rest_client) + module.exit_json(changed=changed, record=record, diff=diff, vm_rebooted=reboot) except errors.ScaleComputingError as e: module.fail_json(msg=str(e)) diff --git a/plugins/modules/vm_snapshot_info.py b/plugins/modules/vm_snapshot_info.py index 5095ce21..63150829 100644 --- a/plugins/modules/vm_snapshot_info.py +++ b/plugins/modules/vm_snapshot_info.py @@ -181,10 +181,10 @@ def main() -> None: ) try: - client = Client.get_client(module.params["cluster_instance"]) - rest_client = RestClient(client) - records = run(module, rest_client) - module.exit_json(changed=False, records=records) + with Client.get_client(module.params["cluster_instance"]) as client: + rest_client = RestClient(client) + records = run(module, rest_client) + module.exit_json(changed=False, records=records) except errors.ScaleComputingError as e: module.fail_json(msg=str(e))