|
3 | 3 | from packet.Vlan import Vlan
|
4 | 4 | from .baseapi import BaseAPI
|
5 | 5 | from .baseapi import Error as PacketError
|
| 6 | +from .baseapi import ResponseError |
6 | 7 | from .Batch import Batch
|
7 | 8 | from .Plan import Plan
|
8 | 9 | from .Device import Device
|
@@ -197,11 +198,46 @@ def create_ssh_key(self, label, public_key):
|
197 | 198 | return SSHKey(data, self)
|
198 | 199 |
|
199 | 200 | 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 |
205 | 241 |
|
206 | 242 | def list_volumes(self, project_id, params={}):
|
207 | 243 | params["include"] = "facility,attachments.device"
|
|
0 commit comments