Skip to content

Commit

Permalink
Switch Blade*Connection* classes to be directly context manageable
Browse files Browse the repository at this point in the history
  • Loading branch information
erl-hpe committed Jul 2, 2024
1 parent a1b18ff commit e4d1bcb
Show file tree
Hide file tree
Showing 2 changed files with 187 additions and 62 deletions.
102 changes: 97 additions & 5 deletions vtds_provider_gcp/api_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
and operations in the provider layer.
"""
from contextlib import contextmanager
from abc import (
ABCMeta,
abstractmethod
Expand Down Expand Up @@ -92,7 +91,6 @@ def blade_ssh_key_paths(self, blade_type):
"""

@abstractmethod
@contextmanager
def connect_blade(self, blade_type, instance, remote_port):
"""Establish an external connection to the specified remote
port on the specified instance of the named Virtual Blade
Expand All @@ -101,9 +99,13 @@ def connect_blade(self, blade_type, instance, remote_port):
connection. Upon leaving the 'with' context, the connection in
the APIBladeConnection is closed.
This can also be used to connect to a host with an IP address
reachable by the blade if 'host' is specified. In this case,
the port will be tunneled through the blade to the specified
host instead of the blade itself.
"""

@contextmanager
@abstractmethod
def connect_blades(self, remote_port, blade_types=None):
"""Establish external connections to the specified remote port
Expand All @@ -118,7 +120,6 @@ def connect_blades(self, remote_port, blade_types=None):
"""

@abstractmethod
@contextmanager
def ssh_connect_blade(self, blade_type, instance, remote_port):
"""Establish an external connection to the SSH server
port on the specified instance of the named Virtual Blade
Expand All @@ -129,7 +130,6 @@ def ssh_connect_blade(self, blade_type, instance, remote_port):
"""

@contextmanager
@abstractmethod
def ssh_connect_blades(self, blade_types=None, remote_port=22):
"""Establish external connections to the specified remote port
Expand Down Expand Up @@ -202,6 +202,29 @@ def remote_port(self):
"""

@abstractmethod
def __enter__(self):
"""Context entry handler to make BladeConnection objects
usable with the 'with ... as' construct. This returns the
BladeConnection for use in a context.
"""

@abstractmethod
def __exit__(
self,
exception_type=None,
exception_value=None,
traceback=None
):
"""Context departure handler to make BladeConnection
objects usable with the 'with ... as' construct. This cleans
up all resources associated with the BladeConnection on
exit from the 'with' block context. Not called if the object
is used outside a 'with' context.
"""


class BladeConnectionSet(metaclass=ABCMeta):
"""A class that contains multiple active BladeConnections to
Expand All @@ -227,6 +250,29 @@ def get_connection(self, hostname):
"""

@abstractmethod
def __enter__(self):
"""Context entry handler to make BladeConnectionSet objects
usable with the 'with ... as' construct. This returns the
BladeConnectionSet for use in a context.
"""

@abstractmethod
def __exit__(
self,
exception_type=None,
exception_value=None,
traceback=None
):
"""Context departure handler to make BladeConnectionSet
objects usable with the 'with ... as' construct. This cleans
up all resources associated with the BladeConnectionSet on
exit from the 'with' block context. Not called if the object
is used outside a 'with' context.
"""


class BladeSSHConnection(BladeConnection, metaclass=ABCMeta):
"""Specifically a connection to the SSH server on a blade (remote
Expand Down Expand Up @@ -335,6 +381,29 @@ def run_command(self, cmd, blocking=True, logfiles=None, **kwargs):
"""

@abstractmethod
def __enter__(self):
"""Context entry handler to make BladeSSHConnection objects
usable with the 'with ... as' construct. This returns the
BladeSSHConnection for use in a context.
"""

@abstractmethod
def __exit__(
self,
exception_type=None,
exception_value=None,
traceback=None
):
"""Context departure handler to make BladeSSHConnection objects
usable with the 'with ... as' construct. This cleans up all
resources associated with the BladeSSHConnection on exit from
the 'with' block context. Not called if the object is used
outside a 'with' context.
"""


class BladeSSHConnectionSet(BladeConnectionSet, metaclass=ABCMeta):
"""A class to wrap multiple BladeSSHConnections and provide
Expand Down Expand Up @@ -396,6 +465,29 @@ def run_command(self, cmd, logname=None, blade_type=None):
"""

@abstractmethod
def __enter__(self):
"""Context entry handler to make BladeSSHConnectionSet objects
usable with the 'with ... as' construct. This returns the
BladeSSHConnectionSet for use in a context.
"""

@abstractmethod
def __exit__(
self,
exception_type=None,
exception_value=None,
traceback=None
):
"""Context exit handler to make BladeSSHConnectionSet objects
usable with the 'with ... as' construct. This cleans up all
resources associated with the BladeSSHConnectionSet on exit
from the 'with' block context. Not called if the object is
used outside a 'with' context.
"""


class Secrets:
"""Provider Layers Secrets API object. Provides ways to populate
Expand Down
Loading

0 comments on commit e4d1bcb

Please sign in to comment.