Skip to content

Commit

Permalink
Add 'vscode-kafka.api.saveclusters' and 'vscode-kafka.api.deleteclust…
Browse files Browse the repository at this point in the history
…ers' command

Signed-off-by: Fred Bricon <[email protected]>
  • Loading branch information
fbricon committed Jul 15, 2021
1 parent 889a606 commit cd5b51e
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 49 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ All notable changes to `Tools for Apache Kafka®` are documented in this file.
- Hover support in `.kafka` files. See [#149](https://github.com/jlandersen/vscode-kafka/issues/149).
- String encoding serialization support. See [#181](https://github.com/jlandersen/vscode-kafka/issues/181).
- Refresh Cluster Provider API when extensions are installed/uninstalled. See [#137](https://github.com/jlandersen/vscode-kafka/issues/137).
- Expose new internal commands (`vscode-kafka.api.saveclusters` and `vscode-kafka.api.deleteclusters`) to programmatically add/delete clusters (from 3rd party extensions). See [#182](https://github.com/jlandersen/vscode-kafka/issues/182).

## [0.12.0] - 2021-04-26
### Added
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
"activationEvents": [
"onCommand:vscode-kafka.open.docs.home",
"onCommand:vscode-kafka.open.docs.page",
"onCommand:vscode-kafka.api.saveclusters",
"onCommand:vscode-kafka.api.deleteclusters",
"onCommand:vscode-kafka.explorer.addcluster",
"onCommand:vscode-kafka.explorer.selectcluster",
"onCommand:vscode-kafka.explorer.editcluster",
Expand Down
88 changes: 72 additions & 16 deletions src/commands/cluster.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,52 @@
import * as vscode from "vscode";
import { dump } from "js-yaml";
import { Broker, ClientAccessor } from "../client";
import * as vscode from "vscode";
import { Broker, ClientAccessor, Cluster } from "../client";
import { KafkaExplorer } from "../explorer";
import { BrokerItem } from "../explorer/models/brokers";
import { OutputChannelProvider } from "../providers";
import { pickBroker, pickClient, pickCluster } from "./common";
import { ClusterSettings } from "../settings";
import { KafkaExplorer } from "../explorer";
import { openClusterForm, openClusterWizard } from "../wizards/clusters";
import { showErrorMessage } from "../wizards/multiStepInput";
import { pickBroker, pickClient, pickCluster, getNames } from "./common";



export class SaveClusterCommandHandler {

constructor(protected clusterSettings: ClusterSettings, protected explorer: KafkaExplorer) {
}

async execute(clusters: Cluster[]): Promise<void> {
if (clusters.length === 0) {
return;
}
try {
// Save collected clusters in settings.
let createdClusterNames = getNames(clusters);
for (const cluster of clusters) {
this.clusterSettings.upsert(cluster);
}
vscode.window.showInformationMessage(`${clusters.length > 1 ? `${clusters.length} clusters` : 'Cluster'} ${createdClusterNames} created successfully`);

// Refresh the explorer
this.explorer.refresh();

// Selecting the created cluster is done with TreeView#reveal
// 1. Show the treeview of the explorer (otherwise reveal will not work)
this.explorer.show();
// 2. the reveal() call must occur within a timeout(),
// while waiting for a fix in https://github.com/microsoft/vscode/issues/114149
setTimeout(() => {
if (clusters) {
this.explorer.selectClusterByName(clusters[0].name);
}
}, 1000);
}
catch (error) {
showErrorMessage(`Error while creating cluster`, error);
}
}
}

/**
* Adds a new cluster to the collection.
Expand All @@ -18,7 +58,11 @@ export class AddClusterCommandHandler {
async execute(): Promise<void> {
openClusterWizard(this.clusterSettings, this.clientAccessor, this.explorer, this.context);
}
}

export interface DeleteClusterRequest {
clusterIds : string | string[] | undefined
confirm: boolean
}

/**
Expand All @@ -31,20 +75,32 @@ export class DeleteClusterCommandHandler {
constructor(private clusterSettings: ClusterSettings, private clientAccessor: ClientAccessor, private explorer: KafkaExplorer) {
}

async execute(clusterId?: string): Promise<void> {
const cluster = clusterId ? this.clusterSettings.get(clusterId) : await pickCluster(this.clusterSettings);
if (!cluster) {
return;
async execute(deleteRequest: DeleteClusterRequest): Promise<void> {
const clusterIds = deleteRequest.clusterIds;
let clusters:(Cluster|undefined)[] = [];
if (Array.isArray(clusterIds)) {
clusters = clusterIds.map(id => this.clusterSettings.get(id)).filter(c=> c !== undefined);
} else {
const cluster = clusterIds ? this.clusterSettings.get(clusterIds) : await pickCluster(this.clusterSettings);
if (cluster) {
clusters.push(cluster);
}
}

const deleteConfirmation = await vscode.window.showWarningMessage(`Are you sure you want to delete cluster '${cluster.name}'?`, 'Cancel', 'Delete');
if (deleteConfirmation !== 'Delete') {
if (clusters.length === 0) {
return;
}

this.clusterSettings.remove(cluster.id);
this.clientAccessor.remove(cluster.id);
this.explorer.refresh();
if (deleteRequest.confirm) {
const clusterNames = getNames(clusters);
const deleteConfirmation = await vscode.window.showWarningMessage(`Are you sure you want to delete cluster${clusters.length === 1?'':'s'} ${clusterNames}?`, 'Cancel', 'Delete');
if (deleteConfirmation !== 'Delete') {
return;
}
}
clusters.forEach(cluster => {
this.clusterSettings.remove(cluster!.id);
this.clientAccessor.remove(cluster!.id);
this.explorer.refresh();
});
}
}

Expand Down Expand Up @@ -143,4 +199,4 @@ export class DumpClusterMetadataCommandHandler {
channel.append(dump(data));
channel.show();
}
}
}
14 changes: 14 additions & 0 deletions src/commands/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,17 @@ export async function pickBroker(clientAccessor: ClientAccessor): Promise<Broker
const pickedBroker = await vscode.window.showQuickPick(brokerQuickPickItems);
return pickedBroker?.broker;
}

export function getNames(clusters: (Cluster|undefined)[]): string {
let names = '';
for (const cluster of clusters) {
if (names !== '') {
names += '\', \'';
}
names += cluster!.name;
}
if (names !== '') {
names = '\''+names+'\'';
}
return names;
}
57 changes: 33 additions & 24 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,45 @@
import * as path from 'path';
import * as vscode from "vscode";

import { getClientAccessor, ConsumerCollection } from "./client";
import { Cluster, ConsumerCollection, getClientAccessor } from "./client";
import { ProducerCollection } from "./client/producer";
import {
AddClusterCommandHandler,
ClearConsumerViewCommandHandler,
CreateTopicCommandHandler,
DeleteClusterCommandHandler,
DeleteClusterRequest,
DeleteConsumerGroupCommand,
DeleteConsumerGroupCommandHandler,
DeleteTopicCommandHandler,
DumpBrokerMetadataCommandHandler,
DumpClusterMetadataCommandHandler,
DumpTopicMetadataCommandHandler,
EditClusterCommandHandler,
handleErrors,
LaunchConsumerCommand,
ListConsumersCommandHandler,
ProduceRecordCommand,
ProduceRecordCommandHandler,
SaveClusterCommandHandler,
SelectClusterCommandHandler,
StartConsumerCommandHandler,
StopConsumerCommandHandler,
ToggleConsumerCommandHandler,
AddClusterCommandHandler,
DeleteClusterCommandHandler,
SelectClusterCommandHandler,
handleErrors,
ClearConsumerViewCommandHandler,
DeleteConsumerGroupCommandHandler,
DeleteConsumerGroupCommand,
LaunchConsumerCommand,
ProduceRecordCommand,
EditClusterCommandHandler
ToggleConsumerCommandHandler
} from "./commands";
import { Context } from "./context";
import { markdownPreviewProvider } from "./docs/markdownPreviewProvider";
import { BrokerItem, KafkaExplorer, TopicItem } from "./explorer";
import { ConsumerVirtualTextDocumentProvider, OutputChannelProvider } from "./providers";
import { getClusterSettings, getWorkspaceSettings } from "./settings";
import { ClusterItem } from "./explorer/models/cluster";
import { TopicGroupItem } from "./explorer/models/topics";
import { ConsumerStatusBarItem } from "./views/consumerStatusBarItem";
import { SelectedClusterStatusBarItem } from "./views/selectedClusterStatusBarItem";
import { NodeBase } from "./explorer/models/nodeBase";
import * as path from 'path';
import { markdownPreviewProvider } from "./docs/markdownPreviewProvider";
import { getDefaultKafkaExtensionParticipant, refreshClusterProviderDefinitions } from "./kafka-extensions/registry";
import { TopicGroupItem } from "./explorer/models/topics";
import { KafkaExtensionParticipant } from "./kafka-extensions/api";
import { ProducerCollection } from "./client/producer";
import { getDefaultKafkaExtensionParticipant, refreshClusterProviderDefinitions } from "./kafka-extensions/registry";
import { startLanguageClient } from "./kafka-file/kafkaFileClient";
import { ConsumerVirtualTextDocumentProvider, OutputChannelProvider } from "./providers";
import { getClusterSettings, getWorkspaceSettings } from "./settings";
import { ConsumerStatusBarItem } from "./views/consumerStatusBarItem";
import { SelectedClusterStatusBarItem } from "./views/selectedClusterStatusBarItem";


export function activate(context: vscode.ExtensionContext): KafkaExtensionParticipant {
Context.register(context);
Expand Down Expand Up @@ -75,6 +77,7 @@ export function activate(context: vscode.ExtensionContext): KafkaExtensionPartic
const clearConsumerViewCommandHandler = new ClearConsumerViewCommandHandler(consumerVirtualTextDocumentProvider);
const deleteConsumerGroupCommandHandler = new DeleteConsumerGroupCommandHandler(clientAccessor, explorer);
const addClusterCommandHandler = new AddClusterCommandHandler(clusterSettings, clientAccessor, explorer, context);
const saveClusterCommandHandler = new SaveClusterCommandHandler(clusterSettings, explorer);
const deleteClusterCommandHandler = new DeleteClusterCommandHandler(clusterSettings, clientAccessor, explorer);
const selectClusterCommandHandler = new SelectClusterCommandHandler(clusterSettings);
const editClusterCommandHandler = new EditClusterCommandHandler(clusterSettings, clientAccessor, explorer, context);
Expand All @@ -99,7 +102,7 @@ export function activate(context: vscode.ExtensionContext): KafkaExtensionPartic
handleErrors((clusterItem?: ClusterItem) => editClusterCommandHandler.execute(clusterItem?.cluster.id))));
context.subscriptions.push(vscode.commands.registerCommand(
DeleteClusterCommandHandler.commandId,
handleErrors((clusterItem?: ClusterItem) => deleteClusterCommandHandler.execute(clusterItem?.cluster.id))));
handleErrors((clusterItem?: ClusterItem) => deleteClusterCommandHandler.execute({ clusterIds: clusterItem?.cluster.id, confirm: true}))));
context.subscriptions.push(vscode.commands.registerCommand(
"vscode-kafka.explorer.dumptopicmetadata",
(topic?: TopicItem) => dumpTopicMetadataCommandHandler.execute(topic)));
Expand Down Expand Up @@ -143,7 +146,13 @@ export function activate(context: vscode.ExtensionContext): KafkaExtensionPartic
return vscode.commands.executeCommand("workbench.extensions.search", "@tag:kafka-provider");
}
));

context.subscriptions.push(vscode.commands.registerCommand(
"vscode-kafka.api.saveclusters",
handleErrors((clusters: Cluster[]) => saveClusterCommandHandler.execute(clusters))));
context.subscriptions.push(vscode.commands.registerCommand(
"vscode-kafka.api.deleteclusters",
handleErrors((deleteRequest: DeleteClusterRequest) => deleteClusterCommandHandler.execute(deleteRequest))));

registerVSCodeKafkaDocumentationCommands(context);

// .kafka file related
Expand Down
11 changes: 2 additions & 9 deletions src/wizards/clusters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { validateAuthentificationUserName, validateBroker, validateClusterName,
import { KafkaExplorer } from "../explorer";
import { ClusterProvider, defaultClusterProviderId, getClusterProviders } from "../kafka-extensions/registry";
import { showErrorMessage } from "./multiStepInput";
import { getNames } from "../commands/common";

export function openClusterWizard(clusterSettings: ClusterSettings, clientAccessor: ClientAccessor, explorer: KafkaExplorer, context: vscode.ExtensionContext) {
const providers = getClusterProviders();
Expand Down Expand Up @@ -419,15 +420,7 @@ function saveCluster(update: boolean, data: any, cluster: Cluster, clusterSettin
function saveClusters(update: boolean, clusters: Cluster[], clusterSettings: ClusterSettings, clientAccessor: ClientAccessor, explorer: KafkaExplorer) {
try {
// Save collected clusters in settings.
let createdClusterNames = '';
for (const cluster of clusters) {
clusterSettings.upsert(cluster);
clientAccessor.remove(cluster.id);
if (createdClusterNames !== '') {
createdClusterNames += '\', \'';
}
createdClusterNames += cluster.name;
}
let createdClusterNames = getNames(clusters);
vscode.window.showInformationMessage(`${clusters.length > 1 ? `${clusters.length} clusters` : 'Cluster'} '${createdClusterNames}' ${update ? 'updated' : 'created'} successfully`);

// Refresh the explorer
Expand Down

0 comments on commit cd5b51e

Please sign in to comment.