Skip to content

Commit

Permalink
Finish implementation to provision default v3 config, if requested, a…
Browse files Browse the repository at this point in the history
…nd test. Remaining missing implementation is to interpret "endpoints" tunnel definition.
  • Loading branch information
alexdlaird committed Dec 31, 2024
1 parent b1ddc62 commit 67352c9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
3 changes: 2 additions & 1 deletion pyngrok/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def __init__(self,
start_new_session: bool = False,
ngrok_version: str = "v3",
api_key: Optional[str] = None,
config_version: int = "2") -> None:
config_version: int = 2) -> None:
#: The path to the ``ngrok`` binary, defaults to being placed in the same directory as
#: `ngrok's configs <https://ngrok.com/docs/agent/config/v2>`_.
self.ngrok_path: str = DEFAULT_NGROK_PATH if ngrok_path is None else ngrok_path
Expand Down Expand Up @@ -89,6 +89,7 @@ def __init__(self,
self.ngrok_version: str = ngrok_version
#: A ``ngrok`` API key.
self.api_key: Optional[str] = api_key
#: The ``ngrok`` config version.
self.config_version = config_version


Expand Down
17 changes: 11 additions & 6 deletions pyngrok/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,14 @@ def _install_ngrok_zip(ngrok_path: str,
def get_ngrok_config(config_path: str,
use_cache: bool = True,
ngrok_version: Optional[str] = "v3",
config_version: Optional[int] = "2") -> Dict[str, Any]:
config_version: Optional[int] = 2) -> Dict[str, Any]:
"""
Get the ``ngrok`` config from the given path.
:param config_path: The ``ngrok`` config path to read.
:param use_cache: Use the cached version of the config (if populated).
:param ngrok_version: The major version of ``ngrok`` installed.
:param config_version: The ``ngrok`` config version.
:return: The ``ngrok`` config.
"""
if config_path not in _config_cache or not use_cache:
Expand All @@ -170,9 +171,9 @@ def get_ngrok_config(config_path: str,


def get_default_config(ngrok_version: Optional[str],
config_version: Optional[str]) -> Dict[str, Any]:
config_version: Optional[int]) -> Dict[str, Any]:
"""
Get the default config params for the given major version of ``ngrok``.
Get the default config params for the given major version of ``ngrok`` and config version.
:param ngrok_version: The major version of ``ngrok`` installed.
:param config_version: The ``ngrok`` config version.
Expand All @@ -183,7 +184,7 @@ def get_default_config(ngrok_version: Optional[str],
return {}
elif ngrok_version == "v3":
config = {"version": config_version}
if str(config_version) == "2":
if int(config_version) == 2:
config["region"] = "us"
return config
else:
Expand All @@ -193,14 +194,15 @@ def get_default_config(ngrok_version: Optional[str],
def install_default_config(config_path: str,
data: Optional[Dict[str, Any]] = None,
ngrok_version: Optional[str] = "v3",
config_version: Optional[str] = "2") -> None:
config_version: Optional[int] = 2) -> None:
"""
Install the given data to the ``ngrok`` config. If a config is not already present for the given path, create one.
Before saving new data to the default config, validate that they are compatible with ``pyngrok``.
:param config_path: The path to where the ``ngrok`` config should be installed.
:param data: A dictionary of things to add to the default config.
:param ngrok_version: The major version of ``ngrok`` installed.
:param config_version: The ``ngrok`` config version.
"""
if data is None:
data = {}
Expand All @@ -215,7 +217,10 @@ def install_default_config(config_path: str,
if not os.path.exists(config_path):
open(config_path, "w").close()

config = get_ngrok_config(config_path, use_cache=False, ngrok_version=ngrok_version)
config = get_ngrok_config(config_path,
use_cache=False,
ngrok_version=ngrok_version,
config_version=config_version)

config.update(data)

Expand Down
25 changes: 22 additions & 3 deletions tests/test_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,35 @@ def test_config_provisioned(self):
# THEN
self.assertTrue(os.path.exists(self.pyngrok_config_v3.config_path))

def test_get_default_config(self):
def test_get_default_v2_config(self):
# GIVEN
installer.install_default_config(self.pyngrok_config_v3.config_path, {}, self.pyngrok_config_v3.ngrok_version)
installer.install_default_config(self.pyngrok_config_v3.config_path,
{},
self.pyngrok_config_v3.ngrok_version,
2)

# WHEN
ngrok_config = installer.get_ngrok_config(self.pyngrok_config_v3.config_path)

# THEN
self.assertEqual(2, len(ngrok_config))
self.assertEqual("2", ngrok_config["version"])
self.assertEqual(2, ngrok_config["version"])
self.assertEqual("us", ngrok_config["region"])
self.assertTrue(os.path.exists(self.pyngrok_config_v3.config_path))

def test_get_default_v3_config(self):
# GIVEN
installer.install_default_config(self.pyngrok_config_v3.config_path,
{},
self.pyngrok_config_v3.ngrok_version,
3)

# WHEN
ngrok_config = installer.get_ngrok_config(self.pyngrok_config_v3.config_path)

# THEN
self.assertEqual(1, len(ngrok_config))
self.assertEqual(3, ngrok_config["version"])
self.assertTrue(os.path.exists(self.pyngrok_config_v3.config_path))

################################################################################
Expand Down

0 comments on commit 67352c9

Please sign in to comment.