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 26, 2021
1 parent 9ecacf5 commit d5e60b6
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 4,778 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ All notable changes to `Tools for Apache Kafka®` are documented in this file.
### Added
- Show cluster state in kafka file. See [#175](https://github.com/jlandersen/vscode-kafka/pull/175).
- Validation for available topics in `.kafka` files. See [#153](https://github.com/jlandersen/vscode-kafka/issues/153).
- Simplify snippets. See [#180](https://github.com/jlandersen/vscode-kafka/pull/180).
- 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).
- Edit cluster configuration. See [#25](https://github.com/jlandersen/vscode-kafka/issues/25).
- Added SSL configuration. See [#86](https://github.com/jlandersen/vscode-kafka/issues/86).
- "Select cluster command" provides the option to create a new cluster. See [#103](https://github.com/jlandersen/vscode-kafka/issues/103).
- `Select Cluster` command provides the option to create a new cluster. See [#103](https://github.com/jlandersen/vscode-kafka/issues/103).
- 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).

### Changed
- Hide internal [strimzi](https://strimzi.io/) topics/consumers by default. See [#176](https://github.com/jlandersen/vscode-kafka/pull/176).
- Changed cluster wizard to use a Webview. See [#88](https://github.com/jlandersen/vscode-kafka/issues/88).
- Hide internal [strimzi](https://strimzi.io/) topics/consumers by default. See [#176](https://github.com/jlandersen/vscode-kafka/pull/176).
- Simplify snippets. See [#180](https://github.com/jlandersen/vscode-kafka/pull/180).
- Allow non-SSL configuration with SASL authentication. See [#200](https://github.com/jlandersen/vscode-kafka/issues/200).

## [0.12.0] - 2021-04-26
Expand Down
4,695 changes: 14 additions & 4,681 deletions package-lock.json

Large diffs are not rendered by default.

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
101 changes: 82 additions & 19 deletions src/commands/cluster.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,58 @@
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 {

public static commandId = "vscode-kafka.api.saveclusters";

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

async execute(clusters: Cluster[]): Promise<void> {
if (!clusters || clusters.length === 0) {
return;
}
let update = false;
try {
// Save collected clusters in settings.
let createdClusterNames = getNames(clusters);
for (const cluster of clusters) {
update = update || !!this.clusterSettings.get(cluster.id);
this.clusterSettings.upsert(cluster);
}
vscode.window.showInformationMessage(`${clusters.length > 1 ? `${clusters.length} clusters` : 'Cluster'} ${createdClusterNames} ${update ? 'updated' : '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
const selectCluster = !update && clusters.length === 1;
setTimeout(() => {
this.explorer.selectClusterByName(clusters[0].name);
if (selectCluster){
this.clusterSettings.selected = clusters[0];
}
}, 1000);
}
catch (error) {
showErrorMessage(`Error while ${update ? 'updating' : 'creating'} cluster${clusters.length > 1 ?'s':''}`, error);
}
}
}

/**
* Adds a new cluster to the collection.
Expand All @@ -18,35 +64,52 @@ export class AddClusterCommandHandler {
constructor(private clusterSettings: ClusterSettings, private clientAccessor: ClientAccessor, private explorer: KafkaExplorer, private context: vscode.ExtensionContext) {
}

async execute(selectCluster = false): Promise<void> {
openClusterWizard(this.clusterSettings, this.clientAccessor, this.explorer, this.context, selectCluster);
async execute(): Promise<void> {
openClusterWizard(this.clusterSettings, this.clientAccessor, this.explorer, this.context);
}
}

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

/**
* Deletes an existing cluster from the collection.
*/
export class DeleteClusterCommandHandler {

public static commandId = 'vscode-kafka.cluster.delete';
public static commandId = 'vscode-kafka.api.deleteclusters';
public static userCommandId = 'vscode-kafka.cluster.delete';

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);
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 @@ -93,7 +156,7 @@ export class EditClusterCommandHandler {
if (!cluster) {
return;
}
openClusterForm(cluster, this.clusterSettings, this.clientAccessor, this.explorer, this.context, false);
openClusterForm(cluster, this.clusterSettings, this.clientAccessor, this.explorer, this.context);
}
}

Expand Down Expand Up @@ -146,4 +209,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 @@ -94,3 +94,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;
}
2 changes: 1 addition & 1 deletion src/explorer/kafkaExplorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export class KafkaExplorer implements KafkaModelProvider, vscode.Disposable, vsc
if (nodes && nodes.length > 0) {
const item = nodes[0];
if (item instanceof ClusterItem) {
vscode.commands.executeCommand(DeleteClusterCommandHandler.commandId, item);
vscode.commands.executeCommand(DeleteClusterCommandHandler.userCommandId, item);
} else if (item instanceof TopicItem) {
vscode.commands.executeCommand(DeleteTopicCommandHandler.commandId, item);
} else if (item instanceof ConsumerGroupItem) {
Expand Down
59 changes: 34 additions & 25 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, addClusterCommandHandler);
const editClusterCommandHandler = new EditClusterCommandHandler(clusterSettings, clientAccessor, explorer, context);
Expand All @@ -98,8 +101,8 @@ export function activate(context: vscode.ExtensionContext): KafkaExtensionPartic
EditClusterCommandHandler.commandId,
handleErrors((clusterItem?: ClusterItem) => editClusterCommandHandler.execute(clusterItem?.cluster.id))));
context.subscriptions.push(vscode.commands.registerCommand(
DeleteClusterCommandHandler.commandId,
handleErrors((clusterItem?: ClusterItem) => deleteClusterCommandHandler.execute(clusterItem?.cluster.id))));
DeleteClusterCommandHandler.userCommandId,
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(
SaveClusterCommandHandler.commandId,
handleErrors((clusters: Cluster[]) => saveClusterCommandHandler.execute(clusters))));
context.subscriptions.push(vscode.commands.registerCommand(
DeleteClusterCommandHandler.commandId,
handleErrors((deleteRequest: DeleteClusterRequest) => deleteClusterCommandHandler.execute(deleteRequest))));

registerVSCodeKafkaDocumentationCommands(context);

// .kafka file related
Expand Down
Loading

0 comments on commit d5e60b6

Please sign in to comment.