import Docker from 'dockerode';
import { DockerClient } from '../docker/client.js';
export interface NetworkCreateOptions {
Name: string;
Driver?: string;
IPAM?: Docker.IPAM;
Options?: Record<string, string>;
Labels?: Record<string, string>;
CheckDuplicate?: boolean;
Internal?: boolean;
Attachable?: boolean;
Ingress?: boolean;
EnableIPv6?: boolean;
}
export class NetworkManager {
constructor(private dockerClient: DockerClient) {}
private getDocker(): Docker {
return this.dockerClient.getDocker();
}
async listNetworks(options: { filters?: Record<string, string[]> } = {}): Promise<any[]> {
const docker = this.getDocker();
return await docker.listNetworks({
filters: options.filters ? JSON.stringify(options.filters) : undefined,
});
}
async createNetwork(options: NetworkCreateOptions): Promise<Docker.Network> {
const docker = this.getDocker();
return await docker.createNetwork(options);
}
async getNetwork(idOrName: string): Promise<Docker.Network> {
const docker = this.getDocker();
return docker.getNetwork(idOrName);
}
async removeNetwork(idOrName: string): Promise<void> {
const network = await this.getNetwork(idOrName);
await network.remove();
}
async inspectNetwork(idOrName: string): Promise<Docker.NetworkInspectInfo> {
const network = await this.getNetwork(idOrName);
return await network.inspect();
}
async connectContainer(networkIdOrName: string, containerId: string, options?: Docker.NetworkConnectOptions): Promise<void> {
const network = await this.getNetwork(networkIdOrName);
await network.connect({
Container: containerId,
...options,
});
}
async disconnectContainer(networkIdOrName: string, containerId: string, force?: boolean): Promise<void> {
const network = await this.getNetwork(networkIdOrName);
await network.disconnect({
Container: containerId,
Force: force ?? false,
});
}
}