From 36b67859bc635aa031e3241a6de9c0edc98d2f83 Mon Sep 17 00:00:00 2001 From: Ben Lewis Date: Fri, 11 Sep 2020 15:45:18 +0100 Subject: [PATCH 1/7] Add the new SNI endpoints to the app api --- heroku3/models/__init__.py | 10 ++++++++++ heroku3/models/app.py | 29 ++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/heroku3/models/__init__.py b/heroku3/models/__init__.py index 218bf4f..a480b01 100644 --- a/heroku3/models/__init__.py +++ b/heroku3/models/__init__.py @@ -154,6 +154,16 @@ def __init__(self): def __repr__(self): return "".format(self.name) +class SNI(BaseResource): + """Heroku SNI.""" + + _strs = ["id", "name", "certificate_chain", "cname"] + _pks = ["name", "id"] + _dates = ["created_at", "updated_at"] + + def __repr__(self): + return "".format(self.name) + class User(BaseResource): """Heroku User.""" diff --git a/heroku3/models/app.py b/heroku3/models/app.py index 1740624..87be8e1 100644 --- a/heroku3/models/app.py +++ b/heroku3/models/app.py @@ -11,7 +11,7 @@ from .buildpack_installation import BuildpackInstallation from .domain import Domain from .region import Region -from ..models import User, Stack, BaseResource, Organization +from ..models import User, Stack, BaseResource, Organization, SNI from .release import Release from .logdrain import LogDrain from .formation import Formation @@ -134,6 +134,33 @@ def remove_addon(self, id): item = self._h._resource_deserialize(r.content.decode("utf-8")) return Addon.new_from_dict(item, h=self._h, app=self) + + def add_sni(self, certificate_chain, private_key): + payload = { 'certificate_chain': certificate_chain, 'private_key': private_key } + + r = self._h._http_resource(method="POST", resource=("apps", self.name, "sni-endpoints"), data=self._h._resource_serialize(payload)) + + r.raise_for_status() + item = self._h._resource_deserialize(r.content.decode("utf-8")) + return SNI.new_from_dict(item, h=self._h, app=self) + + + def remove_sni(self, sni_id_or_name): + r = self._h._http_resource(method="DELETE", resource=("apps", self.name, "sni-endpoints", sni_id_or_name)) + + r.raise_for_status() + item = self._h._resource_deserialize(r.content.decode("utf-8")) + return SNI.new_from_dict(item, h=self._h, app=self) + + + def get_sni(self, sni_id_or_name): + r = self._h._http_resource(method="GET", resource=("apps", self.id, "sni-endpoint", sni_id_or_name)) + + r.raise_for_status() + item = self._h._resource_deserialize(r.content.decode("utf-8")) + return SNI.new_from_dict(item, h=self._h, app=self) + + def collaborators(self, **kwargs): """The collaborators for this app.""" return self._h._get_resources( From 88a88fc9bb478ff643fbc6f29e4f0d5584c14365 Mon Sep 17 00:00:00 2001 From: marcelloromani Date: Mon, 12 Apr 2021 12:13:46 +0100 Subject: [PATCH 2/7] added support for /limits/boot_timeout --- heroku3/models/app.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/heroku3/models/app.py b/heroku3/models/app.py index 87be8e1..ba6c550 100644 --- a/heroku3/models/app.py +++ b/heroku3/models/app.py @@ -1,8 +1,11 @@ +import json import sys from pprint import pprint # NOQA # Third party libraries from heroku3.models.slug import Slug +from requests.exceptions import HTTPError +from requests import codes as http_codes # Project libraries from .dyno import Dyno @@ -332,6 +335,37 @@ def resize_formation_process(self, formation_id_or_name, size): r.raise_for_status() return self._h._process_items(self._h._resource_deserialize(r.content.decode("utf-8")), Formation) + def get_boot_timeout(self): + try: + r = self._h._http_resource( + method="GET", + resource=("apps", self.id, "limits", "boot_timeout") + ) + except HTTPError as e: + if e.response.status_code == http_codes.not_found: + return None + else: + raise + + obj = r.json() + assert obj["name"] == "boot_timeout" + return obj["value"] + + def set_boot_timeout(self, value: int): + r = self._h._http_resource( + method="PUT", + resource=("apps", self.id, "limits", "boot_timeout"), + data=json.dumps({"value": value}) + ) + return r.status_code + + def del_boot_timeout(self): + r = self._h._http_resource( + method="DELETE", + resource=("apps", self.id, "limits", "boot_timeout") + ) + return r.status_code + @property def info(self): """Returns current info for this app.""" From 4f7aa3adb1297665a35911ceff776ab88dafc0bb Mon Sep 17 00:00:00 2001 From: marcelloromani Date: Mon, 12 Apr 2021 16:29:57 +0100 Subject: [PATCH 3/7] fix invalid syntax --- heroku3/models/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/heroku3/models/app.py b/heroku3/models/app.py index ba6c550..4ecba9b 100644 --- a/heroku3/models/app.py +++ b/heroku3/models/app.py @@ -351,7 +351,7 @@ def get_boot_timeout(self): assert obj["name"] == "boot_timeout" return obj["value"] - def set_boot_timeout(self, value: int): + def set_boot_timeout(self, value): r = self._h._http_resource( method="PUT", resource=("apps", self.id, "limits", "boot_timeout"), From 061d36e2ac39e74cb61f11d4dfc3a123f73f3b98 Mon Sep 17 00:00:00 2001 From: marcelloromani Date: Mon, 12 Apr 2021 17:04:25 +0100 Subject: [PATCH 4/7] rename method for consistency --- heroku3/models/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/heroku3/models/app.py b/heroku3/models/app.py index 4ecba9b..9630373 100644 --- a/heroku3/models/app.py +++ b/heroku3/models/app.py @@ -359,7 +359,7 @@ def set_boot_timeout(self, value): ) return r.status_code - def del_boot_timeout(self): + def remove_boot_timeout(self): r = self._h._http_resource( method="DELETE", resource=("apps", self.id, "limits", "boot_timeout") From b47e3baaab8b5e9e2c2d555dc9c94bce209c477d Mon Sep 17 00:00:00 2001 From: marcelloromani Date: Mon, 12 Apr 2021 21:51:03 +0100 Subject: [PATCH 5/7] use _resource_serialise for consistency --- heroku3/models/app.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/heroku3/models/app.py b/heroku3/models/app.py index 9630373..c9c43f2 100644 --- a/heroku3/models/app.py +++ b/heroku3/models/app.py @@ -1,4 +1,3 @@ -import json import sys from pprint import pprint # NOQA @@ -355,7 +354,7 @@ def set_boot_timeout(self, value): r = self._h._http_resource( method="PUT", resource=("apps", self.id, "limits", "boot_timeout"), - data=json.dumps({"value": value}) + data=self._h._resource_serialize({"value": value}) ) return r.status_code From 18030dc518d370d4b8fda80dc390fb9fc241b824 Mon Sep 17 00:00:00 2001 From: marcelloromani Date: Mon, 12 Apr 2021 22:01:23 +0100 Subject: [PATCH 6/7] align return value with other methods --- heroku3/models/app.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/heroku3/models/app.py b/heroku3/models/app.py index c9c43f2..1f7b8d0 100644 --- a/heroku3/models/app.py +++ b/heroku3/models/app.py @@ -356,14 +356,16 @@ def set_boot_timeout(self, value): resource=("apps", self.id, "limits", "boot_timeout"), data=self._h._resource_serialize({"value": value}) ) - return r.status_code + r.raise_for_status() + return r.ok def remove_boot_timeout(self): r = self._h._http_resource( method="DELETE", resource=("apps", self.id, "limits", "boot_timeout") ) - return r.status_code + r.raise_for_status() + return r.ok @property def info(self): From 5f1ea71ae0a3a583e3c4f39572a4f920cabda788 Mon Sep 17 00:00:00 2001 From: marcelloromani Date: Wed, 19 May 2021 16:41:06 +0100 Subject: [PATCH 7/7] raise http exception if unexpected http error --- heroku3/models/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/heroku3/models/app.py b/heroku3/models/app.py index 1f7b8d0..154712a 100644 --- a/heroku3/models/app.py +++ b/heroku3/models/app.py @@ -344,7 +344,7 @@ def get_boot_timeout(self): if e.response.status_code == http_codes.not_found: return None else: - raise + r.raise_for_status() obj = r.json() assert obj["name"] == "boot_timeout"