Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Commit

Permalink
feat(task-plugin): bump to devfile 2.0 API
Browse files Browse the repository at this point in the history
Change-Id: Ib64a9a1b8e4f15ffba869e79bfe7aa1aa0a87b00
Signed-off-by: Florent Benoit <fbenoit@redhat.com>
  • Loading branch information
benoitf committed Mar 11, 2021
1 parent b8c5727 commit d96a869
Show file tree
Hide file tree
Showing 14 changed files with 271 additions and 194 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import { injectable, interfaces } from 'inversify';

import { CheDevfileMainImpl } from './che-devfile-main';
import { CheEndpointMainImpl } from './che-endpoint-main';
import { CheGithubMainImpl } from './che-github-main';
import { CheK8SMainImpl } from './che-k8s-main';
import { CheLanguagesTestAPIImpl } from './che-languages-test-api';
Expand All @@ -34,6 +35,7 @@ export class CheApiProvider implements MainPluginApiProvider {
initialize(rpc: RPCProtocol, container: interfaces.Container): void {
rpc.set(PLUGIN_RPC_CONTEXT.CHE_WORKSPACE_MAIN, new CheWorkspaceMainImpl(container));
rpc.set(PLUGIN_RPC_CONTEXT.CHE_DEVFILE_MAIN, new CheDevfileMainImpl(container));
rpc.set(PLUGIN_RPC_CONTEXT.CHE_ENDPOINT_MAIN, new CheEndpointMainImpl(container));
rpc.set(PLUGIN_RPC_CONTEXT.CHE_TELEMETRY_MAIN, new CheTelemetryMainImpl(container, rpc));
rpc.set(PLUGIN_RPC_CONTEXT.CHE_VARIABLES_MAIN, new CheVariablesMainImpl(container, rpc));
rpc.set(PLUGIN_RPC_CONTEXT.CHE_TASK_MAIN, new CheTaskMainImpl(container, rpc));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**********************************************************************
* Copyright (c) 2018-2021 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
***********************************************************************/

import { ComponentExposedEndpoint, ExposedEndpoint } from '@eclipse-che/theia-remote-api/lib/common/endpoint-service';

import { CheEndpointMain } from '../common/che-protocol';
import { EndpointService } from '@eclipse-che/theia-remote-api/lib/common/endpoint-service';
import { interfaces } from 'inversify';

export class CheEndpointMainImpl implements CheEndpointMain {
private readonly endpointService: EndpointService;

constructor(container: interfaces.Container) {
this.endpointService = container.get(EndpointService);
}

async $getEndpoints(): Promise<ComponentExposedEndpoint[]> {
return this.endpointService.getEndpoints();
}
async $getEndpointsByName(...names: string[]): Promise<ExposedEndpoint[]> {
return this.endpointService.getEndpointsByName(...names);
}
async $getEndpointsByType(type: string): Promise<ExposedEndpoint[]> {
return this.$getEndpointsByType(type);
}
}
11 changes: 11 additions & 0 deletions extensions/eclipse-che-theia-plugin-ext/src/common/che-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ export interface CheDevfileMain {
$update(updatedDevfile: che.devfile.Devfile): Promise<void>;
}

export interface CheEndpoint {}

export interface CheEndpointMain {
$getEndpoints(): Promise<che.endpoint.ComponentExposedEndpoint[]>;
$getEndpointsByName(...names: string[]): Promise<che.endpoint.ExposedEndpoint[]>;
$getEndpointsByType(type: string): Promise<che.endpoint.ExposedEndpoint[]>;
}

export interface CheSsh {}

export interface CheSshMain {
Expand Down Expand Up @@ -416,6 +424,9 @@ export const PLUGIN_RPC_CONTEXT = {
CHE_DEVFILE: createProxyIdentifier<CheDevfile>('CheDevfile'),
CHE_DEVFILE_MAIN: createProxyIdentifier<CheDevfileMain>('CheDevfileMain'),

CHE_ENDPOINT: createProxyIdentifier<CheEndpoint>('CheEndpoint'),
CHE_ENDPOINT_MAIN: createProxyIdentifier<CheEndpointMain>('CheEndpointMain'),

CHE_TELEMETRY: createProxyIdentifier<CheTelemetry>('CheTelemetry'),
CHE_TELEMETRY_MAIN: createProxyIdentifier<CheTelemetryMain>('CheTelemetryMain'),

Expand Down
15 changes: 15 additions & 0 deletions extensions/eclipse-che-theia-plugin-ext/src/plugin/che-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as theia from '@theia/plugin';
import { CheTaskImpl, TaskStatus, TaskTerminallKind } from './che-task-impl';

import { CheDevfileImpl } from './che-devfile';
import { CheEndpointImpl } from './che-endpoint';
import { CheGithubImpl } from './che-github';
import { CheK8SImpl } from './che-k8s';
import { CheOauthImpl } from './che-oauth';
Expand Down Expand Up @@ -41,6 +42,7 @@ export interface CheApiFactory {
export function createAPIFactory(rpc: RPCProtocol): CheApiFactory {
const cheWorkspaceImpl = rpc.set(PLUGIN_RPC_CONTEXT.CHE_WORKSPACE, new CheWorkspaceImpl(rpc));
const cheDevfileImpl = rpc.set(PLUGIN_RPC_CONTEXT.CHE_DEVFILE, new CheDevfileImpl(rpc));
const cheEndpointImpl = rpc.set(PLUGIN_RPC_CONTEXT.CHE_DEVFILE, new CheEndpointImpl(rpc));
const cheVariablesImpl = rpc.set(PLUGIN_RPC_CONTEXT.CHE_VARIABLES, new CheVariablesImpl(rpc));
const cheTaskImpl = rpc.set(PLUGIN_RPC_CONTEXT.CHE_TASK, new CheTaskImpl(rpc));
const cheSshImpl = rpc.set(PLUGIN_RPC_CONTEXT.CHE_SSH, new CheSshImpl(rpc));
Expand Down Expand Up @@ -118,6 +120,18 @@ export function createAPIFactory(rpc: RPCProtocol): CheApiFactory {
},
};

const endpoint: typeof che.endpoint = {
getEndpoints(): Promise<che.endpoint.ComponentExposedEndpoint[]> {
return cheEndpointImpl.getEndpoints();
},
getEndpointsByName(...names: string[]): Promise<che.endpoint.ExposedEndpoint[]> {
return cheEndpointImpl.getEndpointsByName(...names);
},
getEndpointsByType(type: string): Promise<che.endpoint.ExposedEndpoint[]> {
return cheEndpointImpl.getEndpointsByType(type);
},
};

const telemetry: typeof che.telemetry = {
event(id: string, ownerId: string, properties: [string, string][]): Promise<void> {
return cheTelemetryImpl.event(id, ownerId, properties);
Expand Down Expand Up @@ -424,6 +438,7 @@ export function createAPIFactory(rpc: RPCProtocol): CheApiFactory {
return <typeof che>{
workspace,
devfile,
endpoint,
variables,
task,
ssh,
Expand Down
33 changes: 33 additions & 0 deletions extensions/eclipse-che-theia-plugin-ext/src/plugin/che-endpoint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**********************************************************************
* Copyright (c) 2018-2021 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
***********************************************************************/

import { CheEndpoint, CheEndpointMain, PLUGIN_RPC_CONTEXT } from '../common/che-protocol';

import { RPCProtocol } from '@theia/plugin-ext/lib/common/rpc-protocol';
import { endpoint } from '@eclipse-che/plugin';

export class CheEndpointImpl implements CheEndpoint {
private readonly endpointMain: CheEndpointMain;

constructor(rpc: RPCProtocol) {
this.endpointMain = rpc.getProxy(PLUGIN_RPC_CONTEXT.CHE_ENDPOINT_MAIN);
}

async getEndpoints(): Promise<endpoint.ComponentExposedEndpoint[]> {
return this.endpointMain.$getEndpoints();
}
async getEndpointsByName(...names: string[]): Promise<endpoint.ExposedEndpoint[]> {
return this.endpointMain.$getEndpointsByName(...names);
}

async getEndpointsByType(type: string): Promise<endpoint.ExposedEndpoint[]> {
return this.endpointMain.$getEndpointsByType(type);
}
}
19 changes: 19 additions & 0 deletions extensions/eclipse-che-theia-plugin/src/che-proposed.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,25 @@ declare module '@eclipse-che/plugin' {
export function createWorkspace(devfilePath: string): Promise<void>;
}

export namespace endpoint {

export interface ExposedEndpoint {
attributes?: { [key: string]: string };
url?: string;
name: string;
component: string;
}

export interface ComponentExposedEndpoint {
name: string;
endpoints: ExposedEndpoint[];
}

export function getEndpoints(): Promise<ComponentExposedEndpoint[]>;
export function getEndpointsByName(...names: string[]): Promise<ExposedEndpoint[]>;
export function getEndpointsByType(type: string): Promise<ExposedEndpoint[]>;
}

export interface GithubUser {
login: string,
id: number,
Expand Down
22 changes: 22 additions & 0 deletions plugins/task-plugin/__mocks__/@eclipse-che/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**********************************************************************
* Copyright (c) 2021 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
***********************************************************************/

/* eslint-disable @typescript-eslint/no-explicit-any */
const chePlugin: any = {};
chePlugin.devfile = {
get: jest.fn(),
getComponentStatuses: jest.fn(),
};

chePlugin.endpoint = {
getEndpointsByType: jest.fn(),
};

module.exports = chePlugin;
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
* SPDX-License-Identifier: EPL-2.0
***********************************************************************/

describe('no-op', function () {
it('no-op', function () {});
});
/* eslint-disable @typescript-eslint/no-explicit-any */
const theiaPlugin: any = {};

theiaPlugin.window = {
createOutputChannel: jest.fn()
};
module.exports = theiaPlugin;
103 changes: 19 additions & 84 deletions plugins/task-plugin/src/che-workspace-client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**********************************************************************
* Copyright (c) 2019-2020 Red Hat, Inc.
* Copyright (c) 2019-2021 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand All @@ -9,18 +9,14 @@
***********************************************************************/

import * as che from '@eclipse-che/plugin';
import * as theia from '@theia/plugin';

import { che as cheApi } from '@eclipse-che/api';
import { injectable } from 'inversify';

const TERMINAL_SERVER_TYPE = 'terminal';

export const RECIPE_CONTAINER_SOURCE = 'recipe';
export const CONTAINER_SOURCE_ATTRIBUTE = 'source';

export interface WorkspaceContainer extends cheApi.workspace.Machine {
name: string;
}
export interface WorkspaceContainer extends che.devfile.DevfileComponentStatus {}

@injectable()
export class CheWorkspaceClient {
Expand All @@ -30,57 +26,8 @@ export class CheWorkspaceClient {
return workspace.links;
}

/** Returns array of containers' names for the current workspace. */
async getContainersNames(): Promise<string[]> {
const containerNames: string[] = [];

try {
const containers = await this.getMachines();
for (const containerName in containers) {
if (containers.hasOwnProperty(containerName)) {
containerNames.push(containerName);
}
}
} catch (error) {
} finally {
return containerNames;
}
}

async getMachines(): Promise<{ [attrName: string]: cheApi.workspace.Machine }> {
const workspace = await this.getCurrentWorkspace();
const runtime = workspace.runtime;
if (!runtime) {
throw new Error('Workspace is not running.');
}

const machines = runtime.machines;
if (!machines) {
throw new Error('No machines for current workspace is found.');
}
return machines;
}

async getContainers(): Promise<WorkspaceContainer[]> {
const containers: WorkspaceContainer[] = [];
try {
const workspace = await this.getCurrentWorkspace();

if (workspace.runtime && workspace.runtime.machines) {
const machines = workspace.runtime.machines;
for (const machineName in machines) {
if (!machines.hasOwnProperty(machineName)) {
continue;
}
const container: WorkspaceContainer = { name: machineName, ...machines[machineName] };
containers.push(container);
}
}
} catch (e) {
throw new Error('Unable to get list workspace containers. Cause: ' + e);
}

return containers;
async getComponentStatuses(): Promise<che.devfile.DevfileComponentStatus[]> {
return che.devfile.getComponentStatuses();
}

async getCommands(): Promise<cheApi.workspace.Command[]> {
Expand All @@ -105,34 +52,22 @@ export class CheWorkspaceClient {
}

async getMachineExecServerURL(): Promise<string> {
const machineExecServer = await this.getMachineExecServer();
if (!machineExecServer) {
throw new Error(`No server with type ${TERMINAL_SERVER_TYPE} found.`);
}
if (!machineExecServer.attributes!.port) {
throw new Error('No machine-exec-server attributes.port found');
}
return `ws://127.0.0.1:${machineExecServer.attributes!.port}`;
const machineExecEndpoint = await this.getMachineExecEndpoint();
const outputChannelTaskPlugin = theia.window.createOutputChannel('task-plugin-ref');
const port = machineExecEndpoint.attributes?.port || machineExecEndpoint.attributes?.targetPort;
outputChannelTaskPlugin.appendLine(
`port is ${port} and machineExecEndpoint is ${JSON.stringify(machineExecEndpoint, undefined, 2)}`
);
return `ws://127.0.0.1:${port}`;
}

protected async getMachineExecServer(): Promise<cheApi.workspace.Server | undefined> {
const machines = await this.getMachines();
for (const machineName in machines) {
if (!machines.hasOwnProperty(machineName)) {
continue;
}
const servers = machines[machineName].servers!;
for (const serverName in servers) {
if (!servers.hasOwnProperty(serverName)) {
continue;
}

const serverAttributes = servers[serverName].attributes;
if (serverAttributes && serverAttributes['type'] === TERMINAL_SERVER_TYPE) {
return servers[serverName];
}
}
protected async getMachineExecEndpoint(): Promise<che.endpoint.ExposedEndpoint> {
const terminalEndpoints = await che.endpoint.getEndpointsByType(TERMINAL_SERVER_TYPE);
const outputChannel = theia.window.createOutputChannel('task-plugin');
outputChannel.appendLine(`terminalEndpoints are ${JSON.stringify(terminalEndpoints, undefined, 2)}`);
if (terminalEndpoints.length === 1) {
return terminalEndpoints[0];
}
return undefined;
throw new Error(`Unable to find terminal with type ${TERMINAL_SERVER_TYPE}: Found: ${terminalEndpoints}`);
}
}
Loading

0 comments on commit d96a869

Please sign in to comment.