Skip to content

Commit

Permalink
Added Components Metadata #161 (#186)
Browse files Browse the repository at this point in the history
* added components tree view and made minor changes to namings + return types

* added localize() usage and reworked usage of dapr client

* minor refactoring

* removed token

Co-authored-by: Phillip Hoff <phillip@orst.edu>
  • Loading branch information
sk593 and philliphoff authored Jul 1, 2021
1 parent 01f5fda commit 6aea135
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export function activate(context: vscode.ExtensionContext): Promise<void> {
registerDisposable(
vscode.window.registerTreeDataProvider(
'vscode-dapr.views.applications',
registerDisposable(new DaprApplicationTreeDataProvider(daprApplicationProvider, new LocalDaprInstallationManager()))));
registerDisposable(new DaprApplicationTreeDataProvider(daprApplicationProvider, new LocalDaprInstallationManager(), daprClient))));

registerDisposable(
vscode.window.registerTreeDataProvider(
Expand Down
21 changes: 20 additions & 1 deletion src/services/daprClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface DaprClient {
invokeGet(application: DaprApplication, method: string, token?: vscode.CancellationToken): Promise<unknown>;
invokePost(application: DaprApplication, method: string, payload?: unknown, token?: vscode.CancellationToken): Promise<unknown>;
publishMessage(application: DaprApplication, pubSubName: string, topic: string, payload?: unknown, token?: vscode.CancellationToken): Promise<void>;
getMetadata(application: DaprApplication, token?: vscode.CancellationToken): Promise<DaprMetadata>;
}

function manageResponse(response: HttpResponse): unknown {
Expand Down Expand Up @@ -60,4 +61,22 @@ export default class HttpDaprClient implements DaprClient {

await this.httpClient.post(url, payload, { json: true }, token);
}
}

async getMetadata(application: DaprApplication, token?: vscode.CancellationToken | undefined): Promise<DaprMetadata> {
const originalUrl = `http://localhost:${application.httpPort}/v1.0/metadata`;

const response = await this.httpClient.get(originalUrl, { allowRedirects: false }, token);

return manageResponse(response) as DaprMetadata;
}
}

export interface DaprMetadata {
components: DaprComponentMetadata[];
}

export interface DaprComponentMetadata {
name: string;
type: string;
version: string;
}
10 changes: 8 additions & 2 deletions src/views/applications/daprApplicationNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@
import * as vscode from 'vscode';
import TreeNode from "../treeNode";
import { DaprApplication } from '../../services/daprApplicationProvider';
import DaprComponentsNode from "./daprComponentsNode";
import { DaprClient } from '../../services/daprClient';

export default class DaprApplicationNode implements TreeNode {
constructor(public readonly application: DaprApplication) {
constructor(public readonly application: DaprApplication, public readonly daprClient: DaprClient) {
}

getTreeItem(): Promise<vscode.TreeItem> {
const item = new vscode.TreeItem(this.application.appId);
const item = new vscode.TreeItem(this.application.appId, vscode.TreeItemCollapsibleState.Collapsed);

item.contextValue = 'application';

item.iconPath = new vscode.ThemeIcon('globe');

return Promise.resolve(item);
}

getChildren(): TreeNode[] {
return [new DaprComponentsNode(this.application, this.daprClient)];
}
}
23 changes: 16 additions & 7 deletions src/views/applications/daprApplicationTreeDataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import TreeNode from '../treeNode';
import DaprApplicationNode from './daprApplicationNode';
import NoApplicationsRunningNode from './noApplicationsRunningNode';
import { DaprInstallationManager } from '../../services/daprInstallationManager';
import { DaprClient } from '../../services/daprClient';

export default class DaprApplicationTreeDataProvider extends vscode.Disposable implements vscode.TreeDataProvider<TreeNode> {
private readonly onDidChangeTreeDataEmitter = new vscode.EventEmitter<TreeNode | null | undefined>();
private readonly applicationProviderListener: vscode.Disposable;

constructor(
private readonly applicationProvider: DaprApplicationProvider,
private readonly installationManager: DaprInstallationManager) {
private readonly installationManager: DaprInstallationManager,
private readonly daprClient: DaprClient) {
super(() => {
this.applicationProviderListener.dispose();
this.onDidChangeTreeDataEmitter.dispose();
Expand All @@ -34,13 +36,20 @@ export default class DaprApplicationTreeDataProvider extends vscode.Disposable i
return element.getTreeItem();
}

async getChildren(): Promise<TreeNode[]> {
const applications = await this.applicationProvider.getApplications();

if (applications.length > 0) {
return applications.map(application => new DaprApplicationNode(application));
async getChildren(element?: TreeNode): Promise<TreeNode[]> {
if (element) {
return element.getChildren?.() ?? [];
} else {
return [ new NoApplicationsRunningNode(this.installationManager) ];
const applications = await this.applicationProvider.getApplications();
const appNodeList = applications.map(application => new DaprApplicationNode(application, this.daprClient));


if (appNodeList.length > 0) {
return appNodeList;
} else {
return [ new NoApplicationsRunningNode(this.installationManager) ];
}
}

}
}
41 changes: 41 additions & 0 deletions src/views/applications/daprComponentsNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import * as vscode from 'vscode';
import * as nls from 'vscode-nls';
import TreeNode from "../treeNode";
import { DaprApplication } from '../../services/daprApplicationProvider';
import DaprMetadataNode from './daprMetadataNode';
import { DaprClient } from '../../services/daprClient';
import { getLocalizationPathForFile } from '../../util/localization';

const localize = nls.loadMessageBundle(getLocalizationPathForFile(__filename));


export default class DaprComponentsNode implements TreeNode {
constructor(private readonly application: DaprApplication, private readonly daprClient: DaprClient) {
}

getTreeItem(): Promise<vscode.TreeItem> {
const label = localize('views.applications.daprComponentsNode.componentNode', 'Components');

const item = new vscode.TreeItem(label, vscode.TreeItemCollapsibleState.Collapsed);

item.contextValue = 'components';

item.iconPath = new vscode.ThemeIcon('archive');

return Promise.resolve(item);
}

async getChildren(): Promise<TreeNode[]> {
const label = localize('views.applications.daprComponentsNode.noComponents', 'There are no components in use.');
const responseData = await this.daprClient.getMetadata(this.application);
const components = responseData.components;
if(components.length > 0) {
return components.map(comp => new DaprMetadataNode(comp.name, 'database'));
}
return [new DaprMetadataNode(label, 'warning')];
}
}

22 changes: 22 additions & 0 deletions src/views/applications/daprMetadataNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import * as vscode from 'vscode';
import TreeNode from "../treeNode";

export default class DaprMetadataNode implements TreeNode {
constructor(private readonly metadata: string, private readonly themeIconId: string) {
}

getTreeItem(): Promise<vscode.TreeItem> {
const item = new vscode.TreeItem(this.metadata);

item.contextValue = 'metadata';

item.iconPath = new vscode.ThemeIcon(this.themeIconId);

return Promise.resolve(item);
}


}
1 change: 1 addition & 0 deletions src/views/treeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ import * as vscode from 'vscode';

export default interface TreeNode {
getTreeItem(): Promise<vscode.TreeItem>;
getChildren?: () => TreeNode[] | Promise<TreeNode[]>;
}

0 comments on commit 6aea135

Please sign in to comment.