diff --git a/automon/integrations/swimlaneWrapper/__init__.py b/automon/integrations/swimlaneWrapper/__init__.py index e69de29b..2d1ec20f 100644 --- a/automon/integrations/swimlaneWrapper/__init__.py +++ b/automon/integrations/swimlaneWrapper/__init__.py @@ -0,0 +1 @@ +from .client import SwimlaneClient, SwimlaneClientRest, SwimlaneConfig diff --git a/automon/integrations/swimlaneWrapper/api/__init__.py b/automon/integrations/swimlaneWrapper/api/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/automon/integrations/swimlaneWrapper/api/v1.py b/automon/integrations/swimlaneWrapper/api/v1.py new file mode 100644 index 00000000..ef91b09c --- /dev/null +++ b/automon/integrations/swimlaneWrapper/api/v1.py @@ -0,0 +1,20 @@ +class Api(object): + api = f'api' + + +class User(object): + user = f'{Api.api}/user' + login = f'{user}/login' + + +class Auth(object): + + def __init__(self, userId: str): + self.auth = f'{Api.api}/auth' + + self.token = f'{self.auth}/token' + self.create = f'{self.token}/create' + + self.user = f'{self.auth}/user' + self.userId = userId + self.token = f'{userId}/token' diff --git a/automon/integrations/swimlaneWrapper/api/v2.py b/automon/integrations/swimlaneWrapper/api/v2.py new file mode 100644 index 00000000..e69de29b diff --git a/automon/integrations/swimlaneWrapper/client.py b/automon/integrations/swimlaneWrapper/client.py index b599b157..c0f80e06 100644 --- a/automon/integrations/swimlaneWrapper/client.py +++ b/automon/integrations/swimlaneWrapper/client.py @@ -1,6 +1,57 @@ +from automon import log +from automon.integrations.requestsWrapper import RequestsClient + +from .config import SwimlaneConfig +from .api.v1 import Auth, User + +logger = log.logging.getLogger(__name__) +logger.setLevel(log.DEBUG) + + class SwimlaneClient(object): pass class SwimlaneClientRest(object): - pass + + def __init__(self): + self.config = SwimlaneConfig() + + self.requests = RequestsClient() + + async def is_ready(self): + if await self.config.is_ready(): + return True + + async def test_connection(self): + return + + async def login(self): + """Login with username and password""" + url = f'{self.host}/{User.login}' + + r = await self.requests.post( + url=url, + data=self.config.credentials, + ) + + return + + async def auth_create_token(self): + """Creates a new access token for the user making the request""" + url = f'{self.host}/{Auth(userId=self.userId).create}' + + r = await self.requests.post( + url=url, + headers=self.config.headers, + ) + + return + + @property + def host(self): + return self.config.host + + @property + def userId(self): + return self.config.userName diff --git a/automon/integrations/swimlaneWrapper/config.py b/automon/integrations/swimlaneWrapper/config.py index 3502449d..b13d88f1 100644 --- a/automon/integrations/swimlaneWrapper/config.py +++ b/automon/integrations/swimlaneWrapper/config.py @@ -1,2 +1,60 @@ +from automon import log +from automon.helpers import environ + +logger = log.logging.getLogger(__name__) +logger.setLevel(log.DEBUG) + + class SwimlaneConfig(object): - pass + + def __init__( + self, + host: str = None, + userName: str = None, + password: str = None, + apiKey: str = None, + jwt_token: str = None, + ): + self.host = host or environ('SWIMLANE_HOST') + self.userName = userName or environ('SWIMLANE_USERNAME') + self.password = password or environ('SWIMLANE_PASSWORD') + self.apiKey = apiKey or environ('SWIMLANE_APIKEY') + self.jwt_token = jwt_token or environ('SWIMLANE_JWT_TOKEN', 'missing SWIMLANE_JWT_TOKEN') + + creds = {'userName': 'USERNAME', 'password': 'PASSWORD'} + + @property + def credentials(self): + return { + 'userName': self.userName, + 'password': self.password, + } + + @property + def token(self): + return self.jwt_token or self.apiKey + + @property + def headers(self): + if self.token: + return { + 'Authorization': f'Bearer {self.jwt_token}' + } + + async def is_ready(self) -> bool: + if self.host: + if self.userName and self.password: + return True + if self.apiKey: + return True + if self.jwt_token: + return True + + logger.error(str(dict( + host=self.host, + userName=self.userName, + password=self.password, + apiKey=self.apiKey, + jwt_token=self.jwt_token, + ))) + return False diff --git a/automon/integrations/swimlaneWrapper/tests/__init__.py b/automon/integrations/swimlaneWrapper/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/automon/integrations/swimlaneWrapper/tests/test_auth.py b/automon/integrations/swimlaneWrapper/tests/test_auth.py new file mode 100644 index 00000000..ec9c1595 --- /dev/null +++ b/automon/integrations/swimlaneWrapper/tests/test_auth.py @@ -0,0 +1,18 @@ +import unittest +import asyncio + +from automon.integrations.swimlaneWrapper.client import SwimlaneClientRest + +client = SwimlaneClientRest() + + +class MyTestCase(unittest.TestCase): + def test_something(self): + if asyncio.run(client.is_ready()): + self.assertTrue( + asyncio.run(client.login()) + ) + + +if __name__ == '__main__': + unittest.main()