Skip to content

Commit fbaf181

Browse files
committed
ssh keys: retry on 404
DEBUG:packet.baseapi:POST https://api.packet.net/projects/86d5d066-b891-4608-af55-a481aa2c0094/ssh-keys {'key': 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIL/w0nE8ezh9UX88pT3BLtn9Sx2dFZcDSR1+tv4oBjSj', 'label': 'grahamc-test'} {'X-Auth-Token': 'TOKEN', 'X-Consumer-Token': None, 'Content-Type': 'application/json'} DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.packet.net:443 DEBUG:urllib3.connectionpool:https://api.packet.net:443 "POST /projects/86d5d066-b891-4608-af55-a481aa2c0094/ssh-keys HTTP/1.1" 404 24 DEBUG:packet.baseapi:POST https://api.packet.net/projects/86d5d066-b891-4608-af55-a481aa2c0094/ssh-keys {'key': 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIL/w0nE8ezh9UX88pT3BLtn9Sx2dFZcDSR1+tv4oBjSj', 'label': 'grahamc-test'} {'X-Auth-Token': 'TOKEN', 'X-Consumer-Token': None, 'Content-Type': 'application/json'} DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.packet.net:443 DEBUG:urllib3.connectionpool:https://api.packet.net:443 "POST /projects/86d5d066-b891-4608-af55-a481aa2c0094/ssh-keys HTTP/1.1" 422 33 DEBUG:packet.baseapi:GET https://api.packet.net/ssh-keys {} {'X-Auth-Token': 'TOKEN', 'X-Consumer-Token': None, 'Content-Type': 'application/json'} DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.packet.net:443 DEBUG:urllib3.connectionpool:https://api.packet.net:443 "GET /ssh-keys HTTP/1.1" 200 None SSHKey: 3088ab9e-af4d-4f5a-b301-94d90384b2f9
1 parent 0fc6aa2 commit fbaf181

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

packet/Manager.py

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from packet.Vlan import Vlan
44
from .baseapi import BaseAPI
55
from .baseapi import Error as PacketError
6+
from .baseapi import ResponseError
67
from .Batch import Batch
78
from .Plan import Plan
89
from .Device import Device
@@ -197,11 +198,46 @@ def create_ssh_key(self, label, public_key):
197198
return SSHKey(data, self)
198199

199200
def create_project_ssh_key(self, project_id, label, public_key):
200-
params = {"key": public_key, "label": label}
201-
data = self.call_api(
202-
"projects/%s/ssh-keys" % project_id, type="POST", params=params
203-
)
204-
return SSHKey(data, self)
201+
"""
202+
Successfully creating an SSH key with a Project API Token results
203+
in a 404 from the API. If we get a 404, we try the request again.
204+
205+
If the request actually failed with a 404, we will get another 404
206+
which we raise.
207+
208+
If the request actually succeeded, we will get a 422. In this case,
209+
we will try to list all the keys and find the SSHKey we just
210+
received.
211+
212+
Customer Report Reference: TUVD-0107-UIKB
213+
"""
214+
215+
def issue_req():
216+
try:
217+
params = {"key": public_key, "label": label}
218+
data = self.call_api(
219+
"projects/%s/ssh-keys" % project_id, type="POST", params=params
220+
)
221+
return SSHKey(data, self)
222+
except ResponseError as e:
223+
if e.response.status_code == 422:
224+
# Try to pluck the SSH key from the listing API
225+
keys = [
226+
key
227+
for key in self.list_ssh_keys()
228+
if key.key.strip() == public_key.strip()
229+
]
230+
if len(keys) == 1:
231+
return keys.pop()
232+
raise
233+
234+
try:
235+
return issue_req()
236+
except ResponseError as e:
237+
if e.response.status_code == 404:
238+
return issue_req()
239+
else:
240+
raise
205241

206242
def list_volumes(self, project_id, params={}):
207243
params["include"] = "facility,attachments.device"

packet/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@
2626
from .Organization import Organization # noqa
2727
from .Provider import Provider # noqa
2828
from .baseapi import Error # noqa
29+
from .baseapi import ResponseError # noqa

0 commit comments

Comments
 (0)