From b2f446255d244b79bf8f8f15270a8f8c1dad1a1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Daher?= Date: Fri, 28 Jun 2019 18:26:18 -0300 Subject: [PATCH] Add support to Unit resource --- tsuru/__init__.py | 3 +++ tsuru/models/__init__.py | 4 +++ tsuru/models/unit.py | 53 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 tsuru/models/unit.py diff --git a/tsuru/__init__.py b/tsuru/__init__.py index dbddffc..832b840 100644 --- a/tsuru/__init__.py +++ b/tsuru/__init__.py @@ -7,6 +7,7 @@ Plan, Platform, Team, + Unit, ) from tsuru.exceptions import ( DoesNotExist, @@ -27,6 +28,8 @@ 'Plan', 'Platform', 'Team', + 'Unit', + 'DoesNotExist', 'UnexpectedDataFormat', 'UnsupportedModelException', diff --git a/tsuru/models/__init__.py b/tsuru/models/__init__.py index 01ce0e2..7d107df 100644 --- a/tsuru/models/__init__.py +++ b/tsuru/models/__init__.py @@ -6,6 +6,9 @@ from tsuru.models.plan import Plan from tsuru.models.platform import Platform from tsuru.models.team import Team +from tsuru.models.unit import Unit + + __all__ = ( 'App', 'Deploy', @@ -15,4 +18,5 @@ 'Plan', 'Platform', 'Team', + 'Unit', ) diff --git a/tsuru/models/unit.py b/tsuru/models/unit.py new file mode 100644 index 0000000..db9f014 --- /dev/null +++ b/tsuru/models/unit.py @@ -0,0 +1,53 @@ +from tsuru.models.base import BaseModel + + +class Unit(BaseModel): + _RESOURCE_NAME = 'units' + + @property + def name(self): + return self._get('Name') + + @property + def process_name(self): + return self._get('ProcessName') + + @property + def app_name(self): + return self._get('AppName') + + @property + def type(self): + return self._get('Type') + + @property + def ip(self): + return self._get('IP') + + @property + def status(self): + return self._get('Status') + + @property + def address(self): + data = self._get('Address') + scheme = data['Scheme'] + user = data['User'] + host = data['Host'] + path = data['Path'] + raw_query = data['RawQuery'] + + auth = f'{user}@' if user else '' + query = f'?{raw_query}' if raw_query else '' + return f"{scheme}://{auth}{host}{path}{query}" + + @property + def host_(self): + address = self._get('HostAddr') + port = self._get('HostPort') + return f"{address}:{port}" + + def app(self): + from tsuru import App + + return App(data={'name': self.app_name})