Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try no ids #858

Draft
wants to merge 2 commits into
base: fortuna-cloud
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/server_manager/model/digitalocean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>;
// Returns the status of the account.
Expand Down
3 changes: 0 additions & 3 deletions src/server_manager/model/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
77 changes: 30 additions & 47 deletions src/server_manager/web_app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ export class App {
private digitalOceanAccount: digitalocean.Account;
private gcpAccount: gcp.Account;
private selectedServer: server.Server;
private idServerMap = new Map<string, server.Server>();

constructor(
private appRoot: AppRoot, private readonly version: string,
Expand Down Expand Up @@ -162,11 +161,11 @@ export class App {
});

appRoot.addEventListener('DeleteServerRequested', (event: CustomEvent) => {
this.deleteServer(event.detail.serverId);
this.deleteServer(event.detail.server);
});

appRoot.addEventListener('ForgetServerRequested', (event: CustomEvent) => {
this.forgetServer(event.detail.serverId);
this.forgetServer(event.detail.server);
});

appRoot.addEventListener('AddAccessKeyRequested', (event: CustomEvent) => {
Expand Down Expand Up @@ -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`);
}
});

Expand Down Expand Up @@ -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) {
Expand All @@ -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 {
Expand All @@ -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()) {
Expand Down Expand Up @@ -416,23 +408,18 @@ 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);
}
}

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.
Expand Down Expand Up @@ -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 = '';
Expand Down Expand Up @@ -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);
Expand All @@ -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();
}
Expand All @@ -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();
Expand Down Expand Up @@ -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<boolean> {
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});
Expand All @@ -978,9 +964,8 @@ export class App {
}
}

private async removePerKeyDataLimit(serverId: string, keyId: string): Promise<boolean> {
private async removePerKeyDataLimit(server: server.Server, keyId: string): Promise<boolean> {
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);
Expand Down Expand Up @@ -1077,8 +1062,7 @@ export class App {
});
}

private deleteServer(serverId: string) {
const serverToDelete = this.getServerById(serverId);
private deleteServer(serverToDelete: server.Server) {
if (!isManagedServer(serverToDelete)) {
const msg = 'cannot delete non-ManagedServer';
console.error(msg);
Expand All @@ -1094,7 +1078,7 @@ export class App {
})
.then(
() => {
this.removeServer(serverId);
this.removeServer(serverToDelete);
this.showIntro();
this.appRoot.showNotification(
this.appRoot.localize('notification-server-destroyed'));
Expand All @@ -1109,8 +1093,7 @@ export class App {
});
}

private forgetServer(serverId: string) {
const serverToForget = this.getServerById(serverId);
private forgetServer(serverToForget: server.Server) {
if (!isManualServer(serverToForget)) {
const msg = 'cannot forget non-ManualServer';
console.error(msg);
Expand All @@ -1122,7 +1105,7 @@ export class App {
const confirmationButton = this.appRoot.localize('remove');
this.appRoot.getConfirmation(confirmationTitle, confirmationText, confirmationButton, () => {
serverToForget.forget();
this.removeServer(serverId);
this.removeServer(serverToForget);
this.showIntro();
this.appRoot.showNotification(this.appRoot.localize('notification-server-removed'));
});
Expand Down Expand Up @@ -1167,7 +1150,7 @@ export class App {
throw new Error(msg);
}
serverToCancel.getHost().delete().then(() => {
this.removeServer(serverToCancel.getId());
this.removeServer(serverToCancel);
this.showIntro();
});
}
Expand Down
3 changes: 1 addition & 2 deletions src/server_manager/web_app/digitalocean_account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
5 changes: 2 additions & 3 deletions src/server_manager/web_app/digitalocean_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 1 addition & 5 deletions src/server_manager/web_app/shadowbox_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<server.AccessKey[]> {
console.info('Listing access keys');
Expand Down
Loading