Skip to content

Commit 6e4127c

Browse files
committed
add _initialize_ssh_and_sftp_connections for connection setup
1 parent 7bac2c8 commit 6e4127c

2 files changed

Lines changed: 13 additions & 7 deletions

File tree

cmem_plugin_ssh/list.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,12 @@ def __init__( # noqa: PLR0913
182182
self.input_ports = FixedNumberOfInputs([])
183183
self.output_port = FixedSchemaPort(schema=generate_list_schema())
184184

185-
def close_connections(self) -> None:
185+
def cleanup_ssh_connections(self) -> None:
186186
"""Close connection from sftp and ssh"""
187187
self.sftp.close()
188188
self.ssh_client.close()
189189

190-
def connect_ssh_client(self) -> None:
190+
def establish_ssh_connection(self) -> None:
191191
"""Connect to the ssh client with the selected authentication method"""
192192
if self.authentication_method == "key":
193193
self.ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
@@ -211,6 +211,7 @@ def connect_ssh_client(self) -> None:
211211

212212
def preview_results(self) -> str:
213213
"""Preview the results of an execution"""
214+
self._initialize_ssh_and_sftp_connections()
214215
return preview_results(
215216
ssh_client=self.ssh_client,
216217
no_subfolder=self.no_subfolder,
@@ -220,6 +221,12 @@ def preview_results(self) -> str:
220221
max_workers=self.max_workers,
221222
)
222223

224+
def _initialize_ssh_and_sftp_connections(self) -> None:
225+
self.ssh_client = paramiko.SSHClient()
226+
self.establish_ssh_connection()
227+
self.sftp = self.ssh_client.open_sftp()
228+
229+
223230
def execute(self, inputs: Sequence[Entities], context: ExecutionContext) -> Entities:
224231
"""Execute the workflow task"""
225232
_ = inputs
@@ -228,9 +235,7 @@ def execute(self, inputs: Sequence[Entities], context: ExecutionContext) -> Enti
228235
)
229236
entities = []
230237

231-
self.ssh_client = paramiko.SSHClient()
232-
self.connect_ssh_client()
233-
self.sftp = self.ssh_client.open_sftp()
238+
self._initialize_ssh_and_sftp_connections()
234239

235240
retrieval = SSHRetrieval(
236241
ssh_client=self.ssh_client,
@@ -320,7 +325,7 @@ def execute(self, inputs: Sequence[Entities], context: ExecutionContext) -> Enti
320325
)
321326
)
322327

323-
self.close_connections()
328+
self.cleanup_ssh_connections()
324329

325330
return Entities(
326331
entities=iter(entities),

tests/test_list.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def test_execution_warning_error_handling(testing_environment: TestingEnvironmen
219219
plugin = testing_environment.list_plugin
220220
plugin.error_handling = "warning"
221221
plugin.path = "/etc"
222-
222+
plugin._initialize_ssh_and_sftp_connections()
223223
retrieval = SSHRetrieval(
224224
ssh_client=plugin.ssh_client,
225225
no_subfolder=plugin.no_subfolder,
@@ -249,6 +249,7 @@ def test_execution_ignore_error_handling(testing_environment: TestingEnvironment
249249
plugin = testing_environment.list_plugin
250250
plugin.error_handling = "ignore"
251251
plugin.path = "/etc"
252+
plugin._initialize_ssh_and_sftp_connections()
252253

253254
retrieval = SSHRetrieval(
254255
ssh_client=plugin.ssh_client,

0 commit comments

Comments
 (0)