From 92099cd2a29440b7d6ef58cbbd22f2e7473f18d1 Mon Sep 17 00:00:00 2001 From: Vinicius Fortuna Date: Wed, 24 Mar 2021 19:22:38 -0400 Subject: [PATCH] Try no ids --- src/server_manager/model/digitalocean.ts | 2 - src/server_manager/model/server.ts | 3 - src/server_manager/web_app/app.ts | 73 +++++++------------ .../web_app/digitalocean_account.ts | 3 +- .../web_app/digitalocean_server.ts | 5 +- .../web_app/shadowbox_server.ts | 6 +- .../web_app/ui_components/app-root.js | 52 ++++++------- .../ui_components/outline-server-view.js | 8 +- 8 files changed, 60 insertions(+), 92 deletions(-) diff --git a/src/server_manager/model/digitalocean.ts b/src/server_manager/model/digitalocean.ts index 650c12881..79fc3cde4 100644 --- a/src/server_manager/model/digitalocean.ts +++ b/src/server_manager/model/digitalocean.ts @@ -26,8 +26,6 @@ export enum Status { } export interface Account { - // Returns the Account's unique id. - getId(): string; // Returns a user-friendly name (email address) associated with the account. getName(): Promise; // Returns the status of the account. diff --git a/src/server_manager/model/server.ts b/src/server_manager/model/server.ts index c183593b4..d70290db1 100644 --- a/src/server_manager/model/server.ts +++ b/src/server_manager/model/server.ts @@ -13,9 +13,6 @@ // limitations under the License. export interface Server { - // Gets the server ID. - getId(): string; - // Gets the server's name for display. getName(): string; diff --git a/src/server_manager/web_app/app.ts b/src/server_manager/web_app/app.ts index d2387ded1..a1e395685 100644 --- a/src/server_manager/web_app/app.ts +++ b/src/server_manager/web_app/app.ts @@ -126,7 +126,6 @@ export class App { private digitalOceanAccount: digitalocean.Account; private gcpAccount: gcp.Account; private selectedServer: server.Server; - private idServerMap = new Map(); constructor( private appRoot: AppRoot, private readonly version: string, @@ -292,13 +291,12 @@ export class App { }); appRoot.addEventListener('ShowServerRequested', (event: CustomEvent) => { - const server = this.getServerById(event.detail.displayServerId); + const server = event.detail.server as server.Server; if (server) { this.showServer(server); } else { // This should never happen if we are managine the list correctly. - console.error( - `Could not find server for display server ID ${event.detail.displayServerId}`); + console.error(`ShowServerRequested did not pass a server`); } }); @@ -347,7 +345,7 @@ export class App { } const servers = await this.digitalOceanAccount.listServers(); for (const server of servers) { - this.addServer(this.digitalOceanAccount.getId(), server); + this.addServer(this.digitalOceanAccount, server); } return servers; } catch (error) { @@ -364,13 +362,8 @@ export class App { } } - private makeServerListEntry(accountId: string, server: server.Server): ServerListEntry { - return { - id: server.getId(), - accountId, - name: this.makeDisplayName(server), - isSynced: !!server.getName(), - }; + private makeServerListEntry(account: unknown, server: server.Server): ServerListEntry { + return {name: this.makeDisplayName(server), isSynced: !!server.getName(), account, server}; } private makeDisplayName(server: server.Server): string { @@ -384,10 +377,9 @@ export class App { return name; } - private addServer(accountId: string, server: server.Server): void { + private addServer(account: digitalocean.Account, server: server.Server): void { console.log('Loading server', server); - this.idServerMap.set(server.getId(), server); - const serverEntry = this.makeServerListEntry(accountId, server); + const serverEntry = this.makeServerListEntry(account, server); this.appRoot.serverList = this.appRoot.serverList.concat([serverEntry]); if (isManagedServer(server) && !server.isInstallCompleted()) { @@ -416,11 +408,10 @@ export class App { }, 0); } - private removeServer(serverId: string): void { - this.idServerMap.delete(serverId); - this.appRoot.serverList = this.appRoot.serverList.filter((ds) => ds.id !== serverId); - if (this.appRoot.selectedServerId === serverId) { - this.appRoot.selectedServerId = ''; + private removeServer(server: server.Server): void { + this.appRoot.serverList = this.appRoot.serverList.filter((e) => e.server !== server); + if (this.appRoot.selectedServer === server) { + this.appRoot.selectedServer = null; this.selectedServer = null; localStorage.removeItem(LAST_DISPLAYED_SERVER_STORAGE_KEY); } @@ -428,11 +419,7 @@ export class App { private updateServerEntry(server: server.Server): void { this.appRoot.serverList = this.appRoot.serverList.map( - (ds) => ds.id === server.getId() ? this.makeServerListEntry(ds.accountId, server) : ds); - } - - private getServerById(serverId: string): server.Server { - return this.idServerMap.get(serverId); + (e) => e.server === server ? this.makeServerListEntry(e.account, server) : e); } // Returns a promise that resolves when the account is active. @@ -611,12 +598,12 @@ export class App { // Not connected. return; } - const accountId = this.digitalOceanAccount.getId(); + const account = this.digitalOceanAccount; this.cloudAccounts.disconnectDigitalOceanAccount(); this.digitalOceanAccount = null; for (const serverEntry of this.appRoot.serverList) { - if (serverEntry.accountId === accountId) { - this.removeServer(serverEntry.id); + if (serverEntry.account === account) { + this.removeServer(serverEntry.server as server.Server); } } this.appRoot.digitalOceanAccountName = ''; @@ -670,7 +657,7 @@ export class App { const server = await this.digitalOceanRetry(() => { return this.digitalOceanAccount.createServer(regionId, serverName); }); - this.addServer(this.digitalOceanAccount.getId(), server); + this.addServer(this.digitalOceanAccount, server); this.showServer(server); } catch (error) { console.error('Error from createDigitalOceanServer', error); @@ -690,7 +677,7 @@ export class App { public showServer(server: server.Server): void { this.selectedServer = server; - this.appRoot.selectedServerId = server.getId(); + this.appRoot.selectedServer = server; localStorage.setItem(LAST_DISPLAYED_SERVER_STORAGE_KEY, server.getId()); this.appRoot.showServerView(); } @@ -709,7 +696,7 @@ export class App { const view = this.appRoot.getServerView(server.getId()); const version = server.getVersion(); view.selectedPage = 'managementView'; - view.serverId = server.getId(); + view.server = server; view.metricsId = server.getMetricsId(); view.serverName = server.getName(); view.serverHostname = server.getHostnameForAccessKeys(); @@ -950,21 +937,20 @@ export class App { keyId: string, keyDataLimitBytes: number|undefined, keyName: string, - serverId: string, + server: server.Server, defaultDataLimitBytes: number|undefined }>) { const detail = event.detail; - const onDataLimitSet = this.savePerKeyDataLimit.bind(this, detail.serverId, detail.keyId); - const onDataLimitRemoved = this.removePerKeyDataLimit.bind(this, detail.serverId, detail.keyId); + const onDataLimitSet = this.savePerKeyDataLimit.bind(this, detail.server, detail.keyId); + const onDataLimitRemoved = this.removePerKeyDataLimit.bind(this, detail.server, detail.keyId); const activeDataLimitBytes = detail.keyDataLimitBytes ?? detail.defaultDataLimitBytes; this.appRoot.openPerKeyDataLimitDialog( detail.keyName, activeDataLimitBytes, onDataLimitSet, onDataLimitRemoved); } - private async savePerKeyDataLimit(serverId: string, keyId: string, dataLimitBytes: number): + private async savePerKeyDataLimit(server: server.Server, keyId: string, dataLimitBytes: number): Promise { this.appRoot.showNotification(this.appRoot.localize('saving')); - const server = this.idServerMap.get(serverId); const serverView = this.appRoot.getServerView(server.getId()); try { await server.setAccessKeyDataLimit(keyId, {bytes: dataLimitBytes}); @@ -978,9 +964,8 @@ export class App { } } - private async removePerKeyDataLimit(serverId: string, keyId: string): Promise { + private async removePerKeyDataLimit(server: server.Server, keyId: string): Promise { this.appRoot.showNotification(this.appRoot.localize('saving')); - const server = this.idServerMap.get(serverId); const serverView = this.appRoot.getServerView(server.getId()); try { await server.removeAccessKeyDataLimit(keyId); @@ -1079,7 +1064,6 @@ export class App { private deleteSelectedServer() { const serverToDelete = this.selectedServer; - const serverId = serverToDelete.getId(); if (!isManagedServer(serverToDelete)) { const msg = 'cannot delete non-ManagedServer'; console.error(msg); @@ -1095,9 +1079,7 @@ export class App { }) .then( () => { - this.removeServer(serverId); - this.appRoot.selectedServer = null; - this.selectedServer = null; + this.removeServer(serverToDelete); this.showIntro(); this.appRoot.showNotification( this.appRoot.localize('notification-server-destroyed')); @@ -1114,7 +1096,6 @@ export class App { private forgetSelectedServer() { const serverToForget = this.selectedServer; - const serverId = serverToForget.getId(); if (!isManualServer(serverToForget)) { const msg = 'cannot forget non-ManualServer'; console.error(msg); @@ -1126,9 +1107,7 @@ export class App { const confirmationButton = this.appRoot.localize('remove'); this.appRoot.getConfirmation(confirmationTitle, confirmationText, confirmationButton, () => { serverToForget.forget(); - this.removeServer(serverId); - this.appRoot.selectedServerId = ''; - this.selectedServer = null; + this.removeServer(serverToForget); this.showIntro(); this.appRoot.showNotification(this.appRoot.localize('notification-server-removed')); }); @@ -1173,7 +1152,7 @@ export class App { throw new Error(msg); } serverToCancel.getHost().delete().then(() => { - this.removeServer(serverToCancel.getId()); + this.removeServer(serverToCancel); this.showIntro(); }); } diff --git a/src/server_manager/web_app/digitalocean_account.ts b/src/server_manager/web_app/digitalocean_account.ts index 8670a2d22..016d13219 100644 --- a/src/server_manager/web_app/digitalocean_account.ts +++ b/src/server_manager/web_app/digitalocean_account.ts @@ -125,8 +125,7 @@ export class DigitalOceanAccount implements digitalocean.Account { // Creates a DigitalOceanServer object and adds it to the in-memory server list. private createDigitalOceanServer(digitalOcean: DigitalOceanSession, dropletInfo: DropletInfo) { - const server = - new DigitalOceanServer(`${this.id}:${dropletInfo.id}`, digitalOcean, dropletInfo); + const server = new DigitalOceanServer(digitalOcean, dropletInfo); this.servers.push(server); return server; } diff --git a/src/server_manager/web_app/digitalocean_server.ts b/src/server_manager/web_app/digitalocean_server.ts index ba1eb6998..4806f6ce3 100644 --- a/src/server_manager/web_app/digitalocean_server.ts +++ b/src/server_manager/web_app/digitalocean_server.ts @@ -60,11 +60,10 @@ export class DigitalOceanServer extends ShadowboxServer implements server.Manage private eventQueue = new EventEmitter(); private installState: InstallState = InstallState.UNKNOWN; - constructor( - id: string, private digitalOcean: DigitalOceanSession, private dropletInfo: DropletInfo) { + constructor(private digitalOcean: DigitalOceanSession, private dropletInfo: DropletInfo) { // Consider passing a RestEndpoint object to the parent constructor, // to better encapsulate the management api address logic. - super(id); + super(); console.info('DigitalOceanServer created'); this.eventQueue.once('server-active', () => console.timeEnd('activeServer')); this.pollInstallState(); diff --git a/src/server_manager/web_app/shadowbox_server.ts b/src/server_manager/web_app/shadowbox_server.ts index 64c042520..75b0d74ea 100644 --- a/src/server_manager/web_app/shadowbox_server.ts +++ b/src/server_manager/web_app/shadowbox_server.ts @@ -58,11 +58,7 @@ export class ShadowboxServer implements server.Server { private managementApiAddress: string; private serverConfig: ServerConfigJson; - constructor(private readonly id: string) {} - - getId(): string { - return this.id; - } + constructor() {} listAccessKeys(): Promise { console.info('Listing access keys'); diff --git a/src/server_manager/web_app/ui_components/app-root.js b/src/server_manager/web_app/ui_components/app-root.js index deca223eb..35162e0f0 100644 --- a/src/server_manager/web_app/ui_components/app-root.js +++ b/src/server_manager/web_app/ui_components/app-root.js @@ -54,10 +54,10 @@ const TOS_ACK_LOCAL_STORAGE_KEY = 'tos-ack'; /** * An access key to be displayed * @typedef {Object} ServerListEntry - * @prop {string} id - * @prop {string|null} accountId * @prop {string} name * @prop {boolean} isSynced + * @prop {unknown} server + * @prop {unknown} account */ export class AppRoot extends mixinBehaviors @@ -388,7 +388,7 @@ export class AppRoot extends mixinBehaviors
@@ -462,8 +462,8 @@ export class AppRoot extends mixinBehaviors
@@ -476,8 +476,8 @@ export class AppRoot extends mixinBehaviors
@@ -492,8 +492,8 @@ export class AppRoot extends mixinBehaviors @@ -501,8 +501,8 @@ export class AppRoot extends mixinBehaviors @@ -521,7 +521,7 @@ export class AppRoot extends mixinBehaviors supportedLanguages: {type: Array, readonly: true}, useKeyIfMissing: {type: Boolean}, serverList: {type: Array}, - selectedServerId: {type: String}, + selectedServer: {type: Object}, digitalOceanAccountName: String, gcpAccountName: String, outlineVersion: String, @@ -545,8 +545,8 @@ export class AppRoot extends mixinBehaviors constructor() { super(); - /** @type {string} */ - this.selectedServerId = ''; + /** @type {unknown} */ + this.selectedServer = null; this.language = ''; this.supportedLanguages = []; this.useKeyIfMissing = true; @@ -613,7 +613,7 @@ export class AppRoot extends mixinBehaviors showIntro() { this.maybeCloseDrawer(); - this.selectedServerId = ''; + this.selectedServer = null; this.currentPage = 'intro'; } @@ -769,7 +769,7 @@ export class AppRoot extends mixinBehaviors } _hasManualServers(serverList) { - return serverList.filter(server => !server.accountId).length > 0; + return serverList.filter(server => !server.account).length > 0; } _userAcceptedTosChanged(userAcceptedTos) { @@ -905,11 +905,11 @@ export class AppRoot extends mixinBehaviors } _isServerManaged(server) { - return !!server.accountId; + return !!server.account; } _isServerManual(server) { - return !server.accountId; + return !server.account; } _sortServersByName(a, b) { @@ -923,9 +923,9 @@ export class AppRoot extends mixinBehaviors return 0; } - _computeServerClasses(selectedServerId, server) { + _computeServerClasses(selectedServer, server) { let serverClasses = []; - if (this._isServerSelected(selectedServerId, server)) { + if (this._isServerSelected(selectedServer, server)) { serverClasses.push('selected'); } if (!server.isSynced) { @@ -934,20 +934,20 @@ export class AppRoot extends mixinBehaviors return serverClasses.join(' '); } - _computeServerImage(selectedServerId, server) { - if (this._isServerSelected(selectedServerId, server)) { + _computeServerImage(selectedServer, server) { + if (this._isServerSelected(selectedServer, server)) { return 'server-icon-selected.png'; } return 'server-icon.png'; } - _isServerSelected(selectedServerId, server) { - return !!selectedServerId && selectedServerId === server.id; + _isServerSelected(selectedServer, server) { + return !!selectedServer && selectedServer === server.id; } _showServer(event) { - const server = event.model.server; - this.fire('ShowServerRequested', {displayServerId: server.id}); + const serverEntry = event.model.server; + this.fire('ShowServerRequested', {server: serverEntry.server}); this.maybeCloseDrawer(); } diff --git a/src/server_manager/web_app/ui_components/outline-server-view.js b/src/server_manager/web_app/ui_components/outline-server-view.js index bf71b34a3..f7aa678b6 100644 --- a/src/server_manager/web_app/ui_components/outline-server-view.js +++ b/src/server_manager/web_app/ui_components/outline-server-view.js @@ -619,7 +619,7 @@ export class ServerView extends DirMixin(PolymerElement) { static get properties() { return { metricsId: String, - serverId: String, + server: Object, serverName: String, serverHostname: String, serverVersion: String, @@ -667,7 +667,8 @@ export class ServerView extends DirMixin(PolymerElement) { constructor() { super(); - this.serverId = ''; + /** @type {unknown} */ + this.server = null; this.metricsId = ''; this.serverName = ''; this.serverHostname = ''; @@ -863,10 +864,9 @@ export class ServerView extends DirMixin(PolymerElement) { accessKey.name || accessKey.placeholderName; const defaultDataLimitBytes = this.isDefaultDataLimitEnabled ? this.defaultDataLimitBytes : undefined; - const serverId = this.serverId; this.dispatchEvent(makePublicEvent( 'OpenPerKeyDataLimitDialogRequested', - {keyId, keyDataLimitBytes, keyName, serverId, defaultDataLimitBytes})); + {keyId, keyDataLimitBytes, keyName, server: this.server, defaultDataLimitBytes})); } _handleRenameAccessKeyPressed(event) {