Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Components Metadata #161 #186

Merged
merged 5 commits into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, private readonly token?: vscode.CancellationToken | undefined) {
sk593 marked this conversation as resolved.
Show resolved Hide resolved
}

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[]> {
philliphoff marked this conversation as resolved.
Show resolved Hide resolved
const label = localize('views.applications.daprComponentsNode.noComponents', 'There are no components in use.');
const responseData = await this.daprClient.getMetadata(this.application, this.token);
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 {
philliphoff marked this conversation as resolved.
Show resolved Hide resolved
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[]>;
}