Add connect endpoint for computes

Param to connect to compute after creation
Report compute unauthorized HTTP errors to client
This commit is contained in:
grossmj
2021-12-24 13:05:39 +10:30
parent 36cf43475d
commit 10fdd8fcf4
10 changed files with 55 additions and 36 deletions

View File

@ -154,6 +154,7 @@ class Compute:
return self._interfaces_cache
async def update(self, **kwargs):
for kw in kwargs:
if kw not in ("user", "password"):
setattr(self, kw, kwargs[kw])
@ -373,7 +374,7 @@ class Compute:
pass
@locking
async def connect(self):
async def connect(self, report_failed_connection=False):
"""
Check if remote server is accessible
"""
@ -383,6 +384,8 @@ class Compute:
log.info(f"Connecting to compute '{self._id}'")
response = await self._run_http_query("GET", "/capabilities")
except ComputeError as e:
if report_failed_connection:
raise
log.warning(f"Cannot connect to compute '{self._id}': {e}")
# Try to reconnect after 5 seconds if server unavailable only if not during tests (otherwise we create a ressource usage bomb)
if not hasattr(sys, "_called_from_test") or not sys._called_from_test:
@ -491,7 +494,7 @@ class Compute:
# Try to reconnect after 1 second if server unavailable only if not during tests (otherwise we create a ressources usage bomb)
from gns3server.api.server import app
if not app.state.exiting and not hasattr(sys, "_called_from_test"):
log.info(f"Reconnecting to to compute '{self._id}' WebSocket '{ws_url}'")
log.info(f"Reconnecting to compute '{self._id}' WebSocket '{ws_url}'")
asyncio.get_event_loop().call_later(1, lambda: asyncio.ensure_future(self.connect()))
self._cpu_usage_percent = None
@ -572,7 +575,7 @@ class Compute:
msg = ""
if response.status == 401:
raise ControllerUnauthorizedError(f"Invalid authentication for compute {self.id}")
raise ControllerUnauthorizedError(f"Invalid authentication for compute '{self.name}' [{self.id}]")
elif response.status == 403:
raise ControllerForbiddenError(msg)
elif response.status == 404: