diff --git a/README.md b/README.md index 388965e..35edf0c 100644 --- a/README.md +++ b/README.md @@ -105,3 +105,19 @@ endpoint_map_file=aesolo.json "ingest" : "http://127.0.0.1:9000" } ``` + +Alternatively `global_endpoint` configuration option or `ALERTLOGIC_ENDPOINT` value might be set to the url value: +``` +[aesolo] +access_key_id=skip +secret_key=skip +global_endpoint=http://api.aesolo.com +... +global_endpoint=http://api.aesolo.com:3001 +``` + +``` +export ALERTLOGIC_ENDPOINT="http://api.aesolo.com" +... +export ALERTLOGIC_ENDPOINT="http://api.aesolo.com:3001" +``` \ No newline at end of file diff --git a/almdrlib/config.py b/almdrlib/config.py index 61e583b..af1b829 100644 --- a/almdrlib/config.py +++ b/almdrlib/config.py @@ -28,7 +28,7 @@ class Config(): If not specified [default] section is used ALERTLOGIC_ACCESS_KEY_ID - Access Key Id ALERTLOGIC_SECRET_KEY - Secret Key - ALERTLOGIC_ENDPOINT - production, integration, or map are supported values + ALERTLOGIC_ENDPOINT - endpoint id: production | integration, a map, or a url are the supported values ALERTLOGIC_ENDPOINT_MAP - (full or relative to config directory) path of a json file mapping services to endpoints ALERTLOGIC_ACCOUNT_ID - Account Id to perform operations against. ALERTLOGIC_RESIDENCY - Data Residency when creating new deployments diff --git a/almdrlib/session.py b/almdrlib/session.py index 63bc9fb..a03f25a 100644 --- a/almdrlib/session.py +++ b/almdrlib/session.py @@ -12,6 +12,7 @@ import requests from requests.adapters import HTTPAdapter from requests.packages.urllib3.util.retry import Retry +import re from almdrlib.config import Config from almdrlib.region import Region @@ -120,8 +121,7 @@ def _init_session(self, *args, **kwargs): self._residency = self._config.residency self._global_endpoint = self._config.global_endpoint self._endpoint_map = self._config.endpoint_map - self._global_endpoint_url = Region.get_global_endpoint( - self._global_endpoint) + self._global_endpoint_url = Region.get_global_endpoint(self._global_endpoint) self._raise_for_status = kwargs.get('raise_for_status') if aims_token: @@ -252,6 +252,8 @@ def __init__(self, def get_url(self, service_name, account_id=None): if self._global_endpoint == "map": return self.get_mapped_url(service_name, account_id) + elif re.match(r'^(http|https)://.*$', self._global_endpoint): + return self._global_endpoint try: response = self.request( 'get', diff --git a/tests/test_config.py b/tests/test_config.py new file mode 100644 index 0000000..f70380d --- /dev/null +++ b/tests/test_config.py @@ -0,0 +1,80 @@ +import unittest +from almdrlib.session import Session +import re + +MOCK_AUTH = { + "authentication": { + "user": { + "id": "589B64BB-AE91-4FA9-A6D8-37AC6759BB5D", + "account_id": "2", + "created": { + "at": 1443713420, + "by": "693BA145-78C0-4C77-AC1A-5385461839CD" + }, + "modified": { + "at": 1610707251, + "by": "system" + } + }, + "account": { + "id": "2", + "name": "Alert Logic, Inc." + }, + "token": "123", + } +} + + +class NameSpace: + def __init__(self, **kwargs): + self.__dict__ = kwargs + + +class MockResponse(): + elapsed = NameSpace(total_seconds=lambda: 123) + + def __init__(self, code_body): + (self.code, self.body) = code_body + + def json(self): + return self.body + + def status_code(self): + return self.code + + def raise_for_status(self): + return None + + +class MockSession(): + def __init__(self, map): + self.map = map + + def post(self, url): + return MockResponse(self.get_status_body(url)) + + def request(self, method, url, **kwargs): + print("URL", url) + return MockResponse(self.get_status_body(url)) + + def get_status_body(self, url): + for k, v in self.map.items(): + if re.match(k, url): + return v + return 200, {} + + +class TestConf(unittest.TestCase): + def test_globalep(self): + session = Session(global_endpoint="http://api.aesolo.com:8100") + assert session.get_url("aetuner", "1234567") == "http://api.aesolo.com:8100" + session = Session(global_endpoint="production") + session._session = MockSession({r".*aims/v1/authenticate": + (200, MOCK_AUTH), + r".*residency/default/services/aetuner/endpoint": + (200, {"aetuner":"api.alertlogic.com"})}) + assert session.get_url("aetuner", "1234567") == "https://api.alertlogic.com" + + +if __name__ == '__main__': + unittest.main()