Skip to content

Commit

Permalink
feat: register custom plugins (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
sophia-bq authored Nov 1, 2024
1 parent 2dafe42 commit 393a5fb
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
2 changes: 1 addition & 1 deletion common/lib/connection_plugin_chain_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type PluginFactoryInfo = {
weight: number;
};

type FactoryClass = typeof IamAuthenticationPluginFactory | typeof FailoverPluginFactory;
export type FactoryClass = typeof IamAuthenticationPluginFactory | typeof FailoverPluginFactory;

export class ConnectionPluginChainBuilder {
static readonly DEFAULT_PLUGINS = "failover,efm";
Expand Down
9 changes: 8 additions & 1 deletion common/lib/plugin_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { ConnectionPlugin } from "./connection_plugin";
import { HostInfo } from "./host_info";
import { ConnectionPluginChainBuilder } from "./connection_plugin_chain_builder";
import { ConnectionPluginChainBuilder, FactoryClass } from "./connection_plugin_chain_builder";
import { AwsWrapperError } from "./utils/errors";
import { Messages } from "./utils/messages";
import { PluginServiceManagerContainer } from "./plugin_service_manager_container";
Expand Down Expand Up @@ -326,4 +326,11 @@ export class PluginManager {
}
throw new AwsWrapperError(Messages.get("PluginManager.unableToRetrievePlugin"));
}

static registerPlugin(pluginCode: string, pluginFactory: FactoryClass) {
ConnectionPluginChainBuilder.PLUGIN_FACTORIES.set(pluginCode, {
factory: pluginFactory,
weight: ConnectionPluginChainBuilder.WEIGHT_RELATIVE_TO_PRIOR_PLUGIN
});
}
}
41 changes: 41 additions & 0 deletions tests/unit/connection_plugin_chain_builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ import { ConnectTimePlugin } from "../../common/lib/plugins/connect_time_plugin"
import { StaleDnsPlugin } from "../../common/lib/plugins/stale_dns/stale_dns_plugin";
import { ConnectionProviderManager } from "../../common/lib/connection_provider_manager";
import { NullTelemetryFactory } from "../../common/lib/utils/telemetry/null_telemetry_factory";
import { AbstractConnectionPlugin } from "../../common/lib/abstract_connection_plugin";
import { ConnectionPluginFactory } from "../../common/lib/plugin_factory";
import { PluginManager } from "../../common/lib/plugin_manager";

const mockPluginService: PluginService = mock(PluginService);
const mockPluginServiceInstance: PluginService = instance(mockPluginService);
Expand Down Expand Up @@ -106,4 +109,42 @@ describe("testConnectionPluginChainBuilder", () => {
expect(result[3]).toBeInstanceOf(ConnectTimePlugin);
expect(result[4]).toBeInstanceOf(DefaultPlugin);
});

it("register plugin", async () => {
PluginManager.registerPlugin("test", TestPluginFactory);

const props = new Map();
props.set(WrapperProperties.PLUGINS.name, "test");

const result = await ConnectionPluginChainBuilder.getPlugins(
mockPluginServiceInstance,
props,
new ConnectionProviderManager(mockDefaultConnProvider, mockEffectiveConnProvider)
);

expect(result.length).toBe(2);
expect(result[0]).toBeInstanceOf(TestPlugin);
expect(result[1]).toBeInstanceOf(DefaultPlugin);
});
});

class TestPluginFactory implements ConnectionPluginFactory {
async getInstance(pluginService: PluginService, properties: Map<string, any>): Promise<TestPlugin> {
return new TestPlugin(pluginService, properties);
}
}

class TestPlugin extends AbstractConnectionPlugin {
pluginService: PluginService;
properties: Map<string, any>;

constructor(pluginService: PluginService, properties: Map<string, any>) {
super();
this.pluginService = pluginService;
this.properties = properties;
}

override getSubscribedMethods(): Set<string> {
return new Set<string>(["*"]);
}
}

0 comments on commit 393a5fb

Please sign in to comment.