Skip to content

Commit

Permalink
Add not implemented apis
Browse files Browse the repository at this point in the history
  • Loading branch information
mustard-mh committed Nov 9, 2023
1 parent 1248cc7 commit 8d13c5c
Show file tree
Hide file tree
Showing 14 changed files with 2,247 additions and 1,148 deletions.
299 changes: 290 additions & 9 deletions components/dashboard/src/service/json-rpc-workspace-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,69 @@ import {
GetWorkspaceResponse,
WatchWorkspaceStatusRequest,
WatchWorkspaceStatusResponse,
ListWorkspacesRequest,
ListWorkspacesResponse,
WorkspaceScope,
ListWorkspacesRequest_OrderByField,
CreateAndStartWorkspaceRequest,
CreateAndStartWorkspaceResponse,
CreateWorkspaceSnapshotRequest,
CreateWorkspaceSnapshotResponse,
DeleteWorkspaceRequest,
DeleteWorkspaceResponse,
GetDefaultWorkspaceImageRequest,
GetDefaultWorkspaceImageResponse,
GetWorkspaceEditorCredentialsRequest,
GetWorkspaceEditorCredentialsResponse,
GetWorkspaceOwnerTokenRequest,
GetWorkspaceOwnerTokenResponse,
ListSupportedWorkspaceClassesRequest,
ListSupportedWorkspaceClassesResponse,
ListWorkspaceInstancesRequest,
ListWorkspaceInstancesResponse,
ListWorkspacePortsRequest,
ListWorkspacePortsResponse,
ListWorkspaceSnapshotsRequest,
ListWorkspaceSnapshotsResponse,
RestoreWorkspaceRequest,
RestoreWorkspaceResponse,
SendHeartBeatRequest,
SendHeartBeatResponse,
StartWorkspaceRequest,
StartWorkspaceResponse,
StopWorkspaceRequest,
StopWorkspaceResponse,
UpdateWorkspacePortRequest,
UpdateWorkspacePortResponse,
UpdateWorkspaceRequest,
UpdateWorkspaceResponse,
WaitWorkspaceSnapshotRequest,
WaitWorkspaceSnapshotResponse,
WatchWorkspaceImageBuildLogsRequest,
WatchWorkspaceImageBuildLogsResponse,
} from "@gitpod/public-api/lib/gitpod/v1/workspace_pb";
import { converter } from "./public-api";
import { getGitpodService } from "./service";
import { PaginationResponse } from "@gitpod/public-api/lib/gitpod/v1/pagination_pb";
import { generateAsyncGenerator } from "@gitpod/gitpod-protocol/lib/generate-async-generator";
import { WorkspaceInstance } from "@gitpod/gitpod-protocol";
import {
AdminGetWorkspacesRequest,
WorkspaceImageBuild,
WorkspaceInfo,
WorkspaceInstance,
} from "@gitpod/gitpod-protocol";
import { parseOrderBy, parsePagination } from "@gitpod/gitpod-protocol/lib/public-api-utils";
import { isWorkspaceRegion } from "@gitpod/gitpod-protocol/lib/workspace-cluster";

export class JsonRpcWorkspaceClient implements PromiseClient<typeof WorkspaceService> {
async getWorkspace(request: PartialMessage<GetWorkspaceRequest>): Promise<GetWorkspaceResponse> {
if (!request.id) {
throw new ConnectError("id is required", Code.InvalidArgument);
if (!request.workspaceId) {
throw new ConnectError("workspace_id is required", Code.InvalidArgument);
}
const info = await getGitpodService().server.getWorkspace(request.id);
const info = await getGitpodService().server.getWorkspace(request.workspaceId);
const workspace = converter.toWorkspace(info);
const result = new GetWorkspaceResponse();
result.item = workspace;
result.workspace = workspace;
return result;
}

Expand All @@ -38,11 +86,11 @@ export class JsonRpcWorkspaceClient implements PromiseClient<typeof WorkspaceSer
throw new ConnectError("signal is required", Code.InvalidArgument);
}
if (request.workspaceId) {
const resp = await this.getWorkspace({ id: request.workspaceId });
if (resp.item?.status) {
const resp = await this.getWorkspace({ workspaceId: request.workspaceId });
if (resp.workspace?.status) {
const response = new WatchWorkspaceStatusResponse();
response.workspaceId = resp.item.id;
response.status = resp.item.status;
response.workspaceId = resp.workspace.id;
response.status = resp.workspace.status;
yield response;
}
}
Expand Down Expand Up @@ -80,4 +128,237 @@ export class JsonRpcWorkspaceClient implements PromiseClient<typeof WorkspaceSer
yield response;
}
}

async listWorkspaces(
request: PartialMessage<ListWorkspacesRequest>,
_options?: CallOptions,
): Promise<ListWorkspacesResponse> {
const { offset, limit } = parsePagination(request.pagination, 50);

let resultTotal = 0;
let results: WorkspaceInfo[] = [];

request.scope = request.scope || WorkspaceScope.MY_WORKSPACES_IN_ORGANIZATION;
switch (request.scope) {
case WorkspaceScope.MY_WORKSPACES_IN_ORGANIZATION: {
results = await getGitpodService().server.getWorkspaces({
limit,
// TODO: request.page
pinnedOnly: request.pinned,
searchString: request.searchTerm,
organizationId: request.organizationId,
});
resultTotal = results.length;
break;
}
case WorkspaceScope.ALL_WORKSPACES_IN_INSTALLATION: {
const mappingFields: Record<string, AdminGetWorkspacesRequest["orderBy"]> = {
[ListWorkspacesRequest_OrderByField.UNSPECIFIED]: "instanceCreationTime",
[ListWorkspacesRequest_OrderByField.CREATION_TIME]: "instanceCreationTime",
};
const parsedOrderBy = parseOrderBy(request.orderBy, ListWorkspacesRequest_OrderByField, mappingFields);

const opts: AdminGetWorkspacesRequest = {
limit,
offset,
ownerId: request.ownerId,
orderBy: "instanceCreationTime",
orderDir: "desc",
workspaceId: request.workspaceId,
};
if (parsedOrderBy.length > 0) {
opts.orderBy = parsedOrderBy[0].field;
opts.orderDir = parsedOrderBy[0].order;
}
const data = await getGitpodService().server.adminGetWorkspaces(opts);
resultTotal = data.total;
break;
}
case WorkspaceScope.ALL_WORKSPACES_IN_ORGANIZATION: {
// TODO: implement
throw new ConnectError("not implemented", Code.Unimplemented);
}
}
const response = new ListWorkspacesResponse();
response.workspaces = results.map((info) => converter.toWorkspace(info));
response.pagination = new PaginationResponse();
response.pagination.total = resultTotal;
return response;
}

async createAndStartWorkspace(
request: PartialMessage<CreateAndStartWorkspaceRequest>,
_options?: CallOptions | undefined,
): Promise<CreateAndStartWorkspaceResponse> {
if (!request.organizationId) {
throw new ConnectError("organization_id is required", Code.InvalidArgument);
}
if (!request.source?.contextUrl) {
throw new ConnectError("source.context_url is required", Code.InvalidArgument);
}
const region = request.spec?.region ?? "";
const result = await getGitpodService().server.createWorkspace({
workspaceClass: request.spec?.workspaceClass,
ideSettings: {
defaultIde: request.spec?.editor?.name,
useLatestVersion: request.spec?.editor?.version === "latest",
},
contextUrl: request.source?.contextUrl,
organizationId: request.organizationId,
projectId: request.source?.repositoryId,

ignoreRunningWorkspaceOnSameCommit: request.ignoreRunningWorkspaceOnSameCommit,
forceDefaultConfig: request.forceDefaultConfig,
region: isWorkspaceRegion(region) ? region : undefined,
});
if (!result.createdWorkspaceId) {
throw new ConnectError("failed to create workspace", Code.Internal);
}
const response = new CreateAndStartWorkspaceResponse();
response.workspaceId = result.createdWorkspaceId;
return response;
}

async startWorkspace(
request: PartialMessage<StartWorkspaceRequest>,
options?: CallOptions | undefined,
): Promise<StartWorkspaceResponse> {
throw new ConnectError("not implemented", Code.Unimplemented);
}

async stopWorkspace(
request: PartialMessage<StopWorkspaceRequest>,
options?: CallOptions | undefined,
): Promise<StopWorkspaceResponse> {
throw new ConnectError("not implemented", Code.Unimplemented);
}

async deleteWorkspace(
request: PartialMessage<DeleteWorkspaceRequest>,
options?: CallOptions | undefined,
): Promise<DeleteWorkspaceResponse> {
throw new ConnectError("not implemented", Code.Unimplemented);
}

async updateWorkspace(
request: PartialMessage<UpdateWorkspaceRequest>,
options?: CallOptions | undefined,
): Promise<UpdateWorkspaceResponse> {
throw new ConnectError("not implemented", Code.Unimplemented);
}

async listSupportedWorkspaceClasses(
request: PartialMessage<ListSupportedWorkspaceClassesRequest>,
options?: CallOptions | undefined,
): Promise<ListSupportedWorkspaceClassesResponse> {
throw new ConnectError("not implemented", Code.Unimplemented);
}

async getDefaultWorkspaceImage(
request: PartialMessage<GetDefaultWorkspaceImageRequest>,
options?: CallOptions | undefined,
): Promise<GetDefaultWorkspaceImageResponse> {
throw new ConnectError("not implemented", Code.Unimplemented);
}

async sendHeartBeat(
request: PartialMessage<SendHeartBeatRequest>,
options?: CallOptions | undefined,
): Promise<SendHeartBeatResponse> {
throw new ConnectError("not implemented", Code.Unimplemented);
}

async getWorkspaceOwnerToken(
request: PartialMessage<GetWorkspaceOwnerTokenRequest>,
options?: CallOptions | undefined,
): Promise<GetWorkspaceOwnerTokenResponse> {
throw new ConnectError("not implemented", Code.Unimplemented);
}
async getWorkspaceEditorCredentials(
request: PartialMessage<GetWorkspaceEditorCredentialsRequest>,
options?: CallOptions | undefined,
): Promise<GetWorkspaceEditorCredentialsResponse> {
throw new ConnectError("not implemented", Code.Unimplemented);
}

async *watchWorkspaceImageBuildLogs(
request: PartialMessage<WatchWorkspaceImageBuildLogsRequest>,
options?: CallOptions | undefined,
): AsyncIterable<WatchWorkspaceImageBuildLogsResponse> {
if (!options?.signal) {
throw new ConnectError("signal is required", Code.InvalidArgument);
}
if (!request.workspaceId) {
throw new ConnectError("workspace_id is required", Code.InvalidArgument);
}
await getGitpodService().server.watchWorkspaceImageBuildLogs(request.workspaceId);
// TODO: Make it a global watcher and broadcast / emit events
const it = generateAsyncGenerator<{
state: WorkspaceImageBuild.StateInfo;
log?: WorkspaceImageBuild.LogContent;
}>(
(queue) => {
getGitpodService().registerClient({
onWorkspaceImageBuildLogs: (state, log) => {
queue.push({ state, log });
},
});
},
{ signal: options?.signal },
);
for await (const log of it) {
const response = new WatchWorkspaceImageBuildLogsResponse();
response.workspaceImageBuild = converter.toWorkspaceImageBuild(log.state, log.log);
yield response;
}
}

async listWorkspacePorts(
request: PartialMessage<ListWorkspacePortsRequest>,
options?: CallOptions | undefined,
): Promise<ListWorkspacePortsResponse> {
throw new ConnectError("not implemented", Code.Unimplemented);
}

async updateWorkspacePort(
request: PartialMessage<UpdateWorkspacePortRequest>,
options?: CallOptions | undefined,
): Promise<UpdateWorkspacePortResponse> {
throw new ConnectError("not implemented", Code.Unimplemented);
}

async createWorkspaceSnapshot(
request: PartialMessage<CreateWorkspaceSnapshotRequest>,
options?: CallOptions | undefined,
): Promise<CreateWorkspaceSnapshotResponse> {
throw new ConnectError("not implemented", Code.Unimplemented);
}

async waitWorkspaceSnapshot(
request: PartialMessage<WaitWorkspaceSnapshotRequest>,
options?: CallOptions | undefined,
): Promise<WaitWorkspaceSnapshotResponse> {
throw new ConnectError("not implemented", Code.Unimplemented);
}

async listWorkspaceSnapshots(
request: PartialMessage<ListWorkspaceSnapshotsRequest>,
options?: CallOptions | undefined,
): Promise<ListWorkspaceSnapshotsResponse> {
throw new ConnectError("not implemented", Code.Unimplemented);
}

async listWorkspaceInstances(
request: PartialMessage<ListWorkspaceInstancesRequest>,
options?: CallOptions | undefined,
): Promise<ListWorkspaceInstancesResponse> {
throw new ConnectError("not implemented", Code.Unimplemented);
}

async restoreWorkspace(
request: PartialMessage<RestoreWorkspaceRequest>,
options?: CallOptions | undefined,
): Promise<RestoreWorkspaceResponse> {
throw new ConnectError("not implemented", Code.Unimplemented);
}
}
33 changes: 33 additions & 0 deletions components/gitpod-protocol/src/public-api-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import {
WorkspaceConditions,
WorkspaceEnvironmentVariable,
WorkspaceGitStatus,
WorkspaceImageBuild,
WorkspaceImageBuild_Log,
WorkspaceImageBuild_Phase,
WorkspacePhase,
WorkspacePhase_Phase,
WorkspacePort,
Expand All @@ -35,6 +38,7 @@ import {
WorkspaceContext,
WorkspaceInfo,
Workspace as ProtocolWorkspace,
WorkspaceImageBuild as ProtocolWorkspaceImageBuild,
} from "./protocol";
import {
ConfigurationIdeConfig,
Expand Down Expand Up @@ -376,4 +380,33 @@ export class PublicAPIConverter {
result.defaultWorkspaceImage = settings.defaultWorkspaceImage || undefined;
return result;
}

toWorkspaceImageBuild(
state: ProtocolWorkspaceImageBuild.StateInfo,
log?: ProtocolWorkspaceImageBuild.LogContent,
): WorkspaceImageBuild {
const result = new WorkspaceImageBuild();
result.phase = this.toWorkspaceImageBuildPhase(state.phase);
const logItem = new WorkspaceImageBuild_Log();
if (log) {
logItem.message = log.text;
}
result.logs = [logItem];
return result;
}

toWorkspaceImageBuildPhase(phase: ProtocolWorkspaceImageBuild.Phase): WorkspaceImageBuild_Phase {
switch (phase) {
case "BaseImage":
return WorkspaceImageBuild_Phase.BASE_IMAGE;
case "GitpodLayer":
return WorkspaceImageBuild_Phase.GITPOD_LAYER;
case "Error":
return WorkspaceImageBuild_Phase.ERROR;
case "Done":
return WorkspaceImageBuild_Phase.DONE;
default:
return WorkspaceImageBuild_Phase.UNSPECIFIED;
}
}
}
Loading

0 comments on commit 8d13c5c

Please sign in to comment.