| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import logging |
| 4 |
import os |
| 5 |
import typing as t |
| 6 |
|
| 7 |
from .env_vars import get_environment_name |
| 8 |
|
| 9 |
log = logging.getLogger(__name__) |
| 10 |
# the format string for a service URL pulled out of the environment |
| 11 |
# these are handled with uppercased service names, e.g. |
| 12 |
# `GLOBUS_SDK_SERVICE_URL_SEARCH=...` |
| 13 |
_SERVICE_URL_VAR_FORMAT = "GLOBUS_SDK_SERVICE_URL_{}" |
| 14 |
|
| 15 |
|
| 16 |
class EnvConfig: |
| 17 |
envname: str |
| 18 |
domain: str |
| 19 |
no_dotapi: list[str] = ["app", "auth"] |
| 20 |
automate_services: list[str] = ["actions", "flows", "timer"] |
| 21 |
|
| 22 |
# this same dict is inherited (and therefore shared!) by all subclasses |
| 23 |
_registry: dict[str, type[EnvConfig]] = {} |
| 24 |
|
| 25 |
# this is an easier hook to use than metaclass definition -- register every subclass |
| 26 |
# in this dict automatically |
| 27 |
# |
| 28 |
# as a result, anyone can define |
| 29 |
# |
| 30 |
# class BetaEnv(EnvConfig): |
| 31 |
# domain = "beta.foo.bar.example.com" |
| 32 |
# envname = "beta" |
| 33 |
# |
| 34 |
# and retrieve it with get_config_by_name("beta") |
| 35 |
def __init_subclass__(cls, **kwargs: t.Any): |
| 36 |
super().__init_subclass__(**kwargs) |
| 37 |
cls._registry[cls.envname] = cls |
| 38 |
|
| 39 |
@classmethod |
| 40 |
def get_service_url(cls, service: str) -> str: |
| 41 |
# you can override any name with a config attribute |
| 42 |
service_url_attr = f"{service}_url" |
| 43 |
if hasattr(cls, service_url_attr): |
| 44 |
return t.cast(str, getattr(cls, service_url_attr)) |
| 45 |
|
| 46 |
# the typical pattern for a service hostname is X.api.Y |
| 47 |
# X=transfer, Y=preview.globus.org => transfer.api.preview.globus.org |
| 48 |
# check `no_dotapi` for services which don't have `.api` in their names |
| 49 |
if service in cls.no_dotapi: |
| 50 |
return f"https://{service}.{cls.domain}/" |
| 51 |
if service in cls.automate_services: |
| 52 |
return f"https://{cls.envname}.{service}.automate.globus.org/" |
| 53 |
return f"https://{service}.api.{cls.domain}/" |
| 54 |
|
| 55 |
@classmethod |
| 56 |
def get_by_name(cls, env: str) -> type[EnvConfig] | None: |
| 57 |
return cls._registry.get(env) |
| 58 |
|
| 59 |
|
| 60 |
def get_service_url(service: str, environment: str | None = None) -> str: |
| 61 |
""" |
| 62 |
Return the base URL for the given service in this environment. For example: |
| 63 |
|
| 64 |
>>> from globus_sdk.config import get_service_url |
| 65 |
>>> get_service_url("auth", environment="preview") |
| 66 |
'https://auth.preview.globus.org/' |
| 67 |
>>> get_service_url("search", environment="production") |
| 68 |
'https://search.api.globus.org/' |
| 69 |
|
| 70 |
|
| 71 |
:param service: The short name of the service to get the URL for |
| 72 |
:param environment: The name of the environment to use. If unspecified, this will |
| 73 |
use the ``GLOBUS_SDK_ENVIRONMENT`` environment variable. |
| 74 |
""" |
| 75 |
log.debug(f'Service URL Lookup for "{service}" under env "{environment}"') |
| 76 |
environment = environment or get_environment_name() |
| 77 |
# check for an environment variable of the form |
| 78 |
# GLOBUS_SDK_SERVICE_URL_* |
| 79 |
# and use it ahead of any env config if set |
| 80 |
varname = _SERVICE_URL_VAR_FORMAT.format(service.upper()) |
| 81 |
from_env = os.getenv(varname) |
| 82 |
if from_env: |
| 83 |
log.debug(f"Got URL from env var, {varname}={from_env}") |
| 84 |
return from_env |
| 85 |
conf = EnvConfig.get_by_name(environment) |
| 86 |
if not conf: |
| 87 |
raise ValueError(f'Unrecognized environment "{environment}"') |
| 88 |
url = conf.get_service_url(service) |
| 89 |
log.debug(f'Service URL Lookup Result: "{service}" is at "{url}"') |
| 90 |
return url |
| 91 |
|
| 92 |
|
| 93 |
def get_webapp_url(environment: str | None = None) -> str: |
| 94 |
""" |
| 95 |
Return the URL to access the Globus web app in the given environment. For example: |
| 96 |
|
| 97 |
>>> get_webapp_url("preview") |
| 98 |
'https://app.preview.globus.org/' |
| 99 |
|
| 100 |
:param environment: The name of the environment to use. If unspecified, this will |
| 101 |
use the ``GLOBUS_SDK_ENVIRONMENT`` environment variable. |
| 102 |
""" |
| 103 |
environment = environment or get_environment_name() |
| 104 |
return get_service_url("app", environment=environment) |
| 105 |
|
| 106 |
|
| 107 |
# |
| 108 |
# public environments |
| 109 |
# |
| 110 |
|
| 111 |
|
| 112 |
class ProductionEnvConfig(EnvConfig): |
| 113 |
envname = "production" |
| 114 |
domain = "globus.org" |
| 115 |
nexus_url = "https://nexus.api.globusonline.org/" |
| 116 |
timer_url = "https://timer.automate.globus.org/" |
| 117 |
flows_url = "https://flows.automate.globus.org/" |
| 118 |
actions_url = "https://actions.automate.globus.org/" |
| 119 |
|
| 120 |
|
| 121 |
class PreviewEnvConfig(EnvConfig): |
| 122 |
envname = "preview" |
| 123 |
domain = "preview.globus.org" |
| 124 |
|
| 125 |
|
| 126 |
# |
| 127 |
# environments for internal use only |
| 128 |
# |
| 129 |
for envname in ["sandbox", "integration", "test", "staging"]: |
| 130 |
# use `type()` rather than the `class` syntax to control classnames |
| 131 |
type( |
| 132 |
f"{envname.title()}EnvConfig", |
| 133 |
(EnvConfig,), |
| 134 |
{ |
| 135 |
"envname": envname, |
| 136 |
"domain": f"{envname}.globuscs.info", |
| 137 |
}, |
| 138 |
) |