From 42cf96b602eeab290019217694c87f3e864a4709 Mon Sep 17 00:00:00 2001 From: William Gibson Date: Tue, 22 Oct 2019 20:35:34 +0100 Subject: [PATCH] Add support for initial Portal assign during REST create request --- src/controllers/internal.controller.ts | 16 ---------------- src/drivers/portals.driver.ts | 5 ++++- src/models/room/defs.ts | 12 +++++------- src/models/room/index.ts | 6 +++--- src/server/websocket/index.ts | 3 --- src/utils/helpers.utils.ts | 2 +- 6 files changed, 13 insertions(+), 31 deletions(-) diff --git a/src/controllers/internal.controller.ts b/src/controllers/internal.controller.ts index e05f1a0..709ba29 100644 --- a/src/controllers/internal.controller.ts +++ b/src/controllers/internal.controller.ts @@ -12,22 +12,6 @@ import authenticate from '../server/middleware/authenticate.internal.middleware' const app = express() -/** - * Assign New Portal ID to Room - */ -app.post('/portal', authenticate, async (req, res) => { - const { id, roomId } = req.body as { id: string, roomId: string } - - try { - const room = await new Room().load(roomId) - await room.setPortalId(id) - - res.sendStatus(200) - } catch(error) { - handleError(error, res) - } -}) - /** * Existing Portal Status Update */ diff --git a/src/drivers/portals.driver.ts b/src/drivers/portals.driver.ts index 423bf7b..659b345 100644 --- a/src/drivers/portals.driver.ts +++ b/src/drivers/portals.driver.ts @@ -19,7 +19,10 @@ export const createPortal = (room: Room) => new Promise(async (resolve, reject) const headers = await generateHeaders(room) log(`Sending request to ${url}create with room id: ${room.id}`, [ { content: 'portals', color: 'MAGENTA' }]) - await axios.post(`${url}create`, { roomId: room.id }, { headers }) + const { data: portal } = await axios.post(`${url}create`, { roomId: room.id }, { headers }), + { id, status } = portal + + await room.setPortalId(id, status) resolve() } catch(error) { diff --git a/src/models/room/defs.ts b/src/models/room/defs.ts index e1f6bd1..223da7b 100644 --- a/src/models/room/defs.ts +++ b/src/models/room/defs.ts @@ -1,18 +1,16 @@ import { Document } from 'mongoose' /** - * open - The portal is open - * starting - The portal has been created and is now starting - * creating - The portal is currently being created by the microservice - * in-queue - The portal is in a queue to be created by the microservice - * requested - A portal has been requested and is being allocated by the microservice - * waiting - The room is waiting until the right conditions are met for the microservice to be contacted + * open - The portal is connected to a server + * starting - The portal is being connected to a server + * in-queue - The room is waiting to be connected with a VM + * waiting - The room is waiting for the right conditions to be met to request a VM * * error - An error occured * closed - The portal was closed for one reason or another */ -export type PortalAllocationStatus = 'waiting' | 'requested' | 'in-queue' | 'creating' | 'starting' | 'open' | 'closed' | 'error' +export type PortalAllocationStatus = 'open' | 'starting' | 'in-queue' | 'waiting' | 'closed' | 'error' export interface PortalAllocation { id?: string diff --git a/src/models/room/index.ts b/src/models/room/index.ts index d408fb3..582b5d7 100644 --- a/src/models/room/index.ts +++ b/src/models/room/index.ts @@ -6,7 +6,7 @@ import StoredUser from '../../schemas/user.schema' import StoredInvite from '../../schemas/invite.schema' import StoredMessage from '../../schemas/message.schema' -import IRoom, { PortalAllocation } from './defs' +import IRoom, { PortalAllocation, PortalAllocationStatus } from './defs' import StoredRoom from '../../schemas/room.schema' import { destroyPortal, createPortal } from '../../drivers/portals.driver' @@ -246,11 +246,11 @@ export default class Room { } }) - setPortalId = (id: string) => new Promise(async (resolve, reject) => { + setPortalId = (id: string, status: PortalAllocationStatus) => new Promise(async (resolve, reject) => { try { const allocation: PortalAllocation = { id, - status: 'creating', + status, lastUpdatedAt: Date.now() } diff --git a/src/server/websocket/index.ts b/src/server/websocket/index.ts index 48c0fde..71e7136 100644 --- a/src/server/websocket/index.ts +++ b/src/server/websocket/index.ts @@ -1,7 +1,5 @@ import WebSocket, { Server } from 'ws' -import Room from '../../models/room' - import WSEvent from './models/event' import WSSocket from './models/socket' import WSMessage from './models/message' @@ -10,7 +8,6 @@ import log from '../../utils/log.utils' import client, { createPubSubClient } from '../../config/redis.config' import handleMessage from './handlers/message' -import { PortalEvent } from '../../models/portal/defs' import handleInternalMessage, { WSInternalEvent } from './handlers/internal' import { extractUserId, extractRoomId, UNALLOCATED_PORTALS_KEYS } from '../../utils/helpers.utils' diff --git a/src/utils/helpers.utils.ts b/src/utils/helpers.utils.ts index 1fdb42a..edea81a 100644 --- a/src/utils/helpers.utils.ts +++ b/src/utils/helpers.utils.ts @@ -6,7 +6,7 @@ import { InviteResolvable } from '../models/invite' import { TargetResolvable } from '../models/invite/defs' import { PortalAllocationStatus } from '../models/room/defs' -export const UNALLOCATED_PORTALS_KEYS: PortalAllocationStatus[] = ['waiting', 'requested', 'error', 'closed'] +export const UNALLOCATED_PORTALS_KEYS: PortalAllocationStatus[] = ['waiting', 'closed', 'error'] export const extractUserId = (user: UserResolvable) => user ? (typeof user === 'string' ? user : user.id) : null export const extractTargetId = (target: TargetResolvable) => target ? (typeof target === 'string' ? target : target.id) : null