Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions nextmv/nextmv/cloud/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from collections.abc import Generator
from dataclasses import dataclass, field
from typing import IO, Any
from urllib.parse import urljoin
from urllib.parse import urljoin, urlparse

import requests
from requests.adapters import HTTPAdapter, Retry
Expand All @@ -40,6 +40,7 @@
from nextmv.config import (
CLIENT_ID_KEY,
CONFIG_FILE,
DEFAULT_ENDPOINT,
OIDC_DISCOVERY_URL_KEY,
AuthType,
get_auth_session,
Expand All @@ -51,6 +52,9 @@
load_sessions,
)

_DEFAULT_CONSOLE_URL: str = "https://cloud.nextmv.io"
"""str: URL of the Nextmv Cloud console."""

_MAX_LAMBDA_PAYLOAD_SIZE: int = 500 * 1024 * 1024
"""int: Maximum size of the payload handled by the Nextmv Cloud API.

Expand Down Expand Up @@ -252,7 +256,7 @@ class Client:
6. If all of the above lookups fail, the hardcoded default URL of
`https://api.cloud.nextmv.io` is used.
"""
console_url: str = "https://cloud.nextmv.io"
console_url: str = ""
"""URL of the Nextmv Cloud console."""
profile: str | None = None
"""
Expand Down Expand Up @@ -298,6 +302,9 @@ def __post_init__(self):
profile = self.__resolve_profile()
self.url = self.__resolve_endpoint(profile)

if not self.console_url and urlparse(self.url).hostname == DEFAULT_ENDPOINT:
self.console_url = _DEFAULT_CONSOLE_URL

# Resolve system_certs: explicit constructor arg > env var > config file.
if self.system_certs:
apply_system_certs()
Expand Down
74 changes: 73 additions & 1 deletion nextmv/tests/cloud/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from unittest.mock import patch

import requests
from nextmv.cloud import Client
from nextmv.cloud import Application, Client


def _make_id_token(aud: str = "test-client-id") -> str:
Expand Down Expand Up @@ -200,6 +200,78 @@ def test_profile_takes_precedence_over_default_config_endpoint(self):
self.assertEqual(client.url, "https://api.profile.io")


class TestConsoleUrl(unittest.TestCase):
"""Tests for the Client.console_url default and the run console link."""

PRODUCTION_CONSOLE = "https://cloud.nextmv.io"

def _env(self):
env_patch = patch.dict(os.environ, {"NEXTMV_API_KEY": "k"})
env = env_patch.start()
self.addCleanup(env_patch.stop)
env.pop("NEXTMV_PROFILE", None)
env.pop("NEXTMV_ENDPOINT", None)

def test_production_endpoint_sets_production_console(self):
"""The production API host gets the production console URL."""
self._env()
client = Client(url="https://api.cloud.nextmv.io")
self.assertEqual(client.console_url, self.PRODUCTION_CONSOLE)

def test_production_host_without_scheme_sets_production_console(self):
"""The production API host is detected when given without a scheme."""
self._env()
client = Client(url="api.cloud.nextmv.io")
self.assertEqual(client.console_url, self.PRODUCTION_CONSOLE)

def test_default_endpoint_sets_production_console(self):
"""The hardcoded default endpoint is the production host."""
self._env()
with patch("nextmv.cloud.client.load_config", return_value={"apikey": "k"}):
client = Client()
self.assertEqual(client.url, "https://api.cloud.nextmv.io")
self.assertEqual(client.console_url, self.PRODUCTION_CONSOLE)

def test_non_production_endpoint_leaves_console_empty(self):
"""A non-production API host does not get the production console URL."""
self._env()
client = Client(url="https://api.example.com")
self.assertEqual(client.console_url, "")

def test_lookalike_host_leaves_console_empty(self):
"""A host that only contains the production host name is not production."""
self._env()
client = Client(url="https://api.cloud.nextmv.io.example.com")
self.assertEqual(client.console_url, "")

def test_explicit_console_url_is_honored_for_non_production(self):
"""An explicit console_url is kept for a non-production endpoint."""
self._env()
client = Client(url="https://api.example.com", console_url="https://console.example.com")
self.assertEqual(client.console_url, "https://console.example.com")

def test_explicit_console_url_is_honored_for_production(self):
"""An explicit console_url is kept for the production endpoint."""
self._env()
client = Client(url="https://api.cloud.nextmv.io", console_url="https://console.example.com")
self.assertEqual(client.console_url, "https://console.example.com")

def test_run_console_url_production(self):
"""Run links start with the production console URL on production."""
self._env()
app = Application(client=Client(url="https://api.cloud.nextmv.io"), id="app-1")
link = app._ApplicationRunMixin__console_url("run-1")
self.assertEqual(link, f"{self.PRODUCTION_CONSOLE}/app/app-1/run/run-1?view=details")

def test_run_console_url_non_production_is_path_only(self):
"""Run links are the path only on a non-production endpoint."""
self._env()
app = Application(client=Client(url="https://api.example.com"), id="app-1")
link = app._ApplicationRunMixin__console_url("run-1")
self.assertEqual(link, "/app/app-1/run/run-1?view=details")
self.assertNotIn("nextmv.io", link)


class TestResolveApiKey(unittest.TestCase):
"""Tests for Client.__resolve_api_key (exercised via __post_init__)."""

Expand Down
Loading