Skip to content

Commit

Permalink
fix: create a new workspace flow
Browse files Browse the repository at this point in the history
Signed-off-by: Oleksii Orel <[email protected]>
  • Loading branch information
olexii4 committed Feb 19, 2025
1 parent b7096ea commit 75c4cb9
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
import {
DEVWORKSPACE_CONFIG_ATTR,
DEVWORKSPACE_CONTAINER_BUILD_ATTR,
DEVWORKSPACE_STORAGE_TYPE_ATTR,
} from '@/services/devfileApi/devWorkspace/spec/template';
import { delay } from '@/services/helpers/delay';
import { isWebTerminal } from '@/services/helpers/devworkspace';
Expand Down Expand Up @@ -209,14 +210,22 @@ export class DevWorkspaceClient {
return DwtApi.createTemplate(devWorkspaceTemplateResource);
}

async updateDevWorkspace(
devWorkspace: devfileApi.DevWorkspace,
/**
* Update the storage-type of a devworkspace
* @param namespace
* @param name
* @param storageType
*/
async updateDevWorkspaceStorageType(
namespace,
name,
storageType,
): Promise<{ headers: DwApi.Headers; devWorkspace: devfileApi.DevWorkspace }> {
return await DwApi.patchWorkspace(devWorkspace.metadata.namespace, devWorkspace.metadata.name, [
return await DwApi.patchWorkspace(namespace, name, [
{
op: 'replace',
path: '/spec/template/components',
value: devWorkspace.spec.template.components || [],
path: '/spec/template/attributes/' + DEVWORKSPACE_STORAGE_TYPE_ATTR.replace(/\//g, '~1'),
value: storageType,
},
]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,25 @@ jest.mock('@/store/InfrastructureNamespaces');
jest.mock('@/store/ClusterInfo');

describe('devWorkspaces, actions', () => {
let mockWorkspace: devfileApi.DevWorkspace;

beforeEach(() => {
mockWorkspace = {
metadata: {
namespace: 'test-namespace',
name: 'test-workspace',
uid: '1',
},
spec: {
template: {
attributes: {
'controller.devfile.io/storage-type': 'per-user',
},
},
},
} as devfileApi.DevWorkspace;
});

afterEach(() => {
jest.clearAllMocks();
});
Expand All @@ -48,20 +67,12 @@ describe('devWorkspaces, actions', () => {
let store: ReturnType<typeof createMockStore>;
const mockCreateDevWorkspace = jest.fn();
const mockCreateDevWorkspaceTemplate = jest.fn();
const mockUpdateDevWorkspace = jest.fn();
const mockUpdateDevWorkspaceStorageType = jest.fn();
const mockVerifyAuthorized = verifyAuthorized as jest.MockedFunction<typeof verifyAuthorized>;
const mockUpdateDevWorkspaceTemplate = updateDevWorkspaceTemplate as jest.MockedFunction<
typeof updateDevWorkspaceTemplate
>;

const mockWorkspace = {
metadata: {
namespace: 'test-namespace',
name: 'test-workspace',
uid: '1',
},
} as devfileApi.DevWorkspace;

const mockWorkspaceTemplate = {} as devfileApi.DevWorkspaceTemplate;

const mockFactoryParams = {} as Partial<FactoryParams>;
Expand Down Expand Up @@ -92,15 +103,15 @@ describe('devWorkspaces, actions', () => {
(getDevWorkspaceClient as jest.Mock).mockReturnValue({
createDevWorkspace: mockCreateDevWorkspace,
createDevWorkspaceTemplate: mockCreateDevWorkspaceTemplate,
updateDevWorkspace: mockUpdateDevWorkspace,
updateDevWorkspaceStorageType: mockUpdateDevWorkspaceStorageType,
});

mockCreateDevWorkspace.mockResolvedValue({
devWorkspace: mockWorkspace,
headers: {},
});

mockUpdateDevWorkspace.mockResolvedValue({
mockUpdateDevWorkspaceStorageType.mockResolvedValue({
devWorkspace: mockWorkspace,
headers: {},
});
Expand Down Expand Up @@ -132,7 +143,11 @@ describe('devWorkspaces, actions', () => {

expect(mockCreateDevWorkspaceTemplate).toHaveBeenCalled();

expect(mockUpdateDevWorkspace).toHaveBeenCalledWith(mockWorkspace);
expect(mockUpdateDevWorkspaceStorageType).toHaveBeenCalledWith(
'test-namespace',
'test-workspace',
'per-user',
);
});

it('should handle warnings from createDevWorkspace and dispatch warning action', async () => {
Expand All @@ -159,8 +174,8 @@ describe('devWorkspaces, actions', () => {
expect(actions[2]).toEqual(devWorkspacesAddAction(mockWorkspace));
});

it('should handle warnings from updateDevWorkspace and dispatch warning action', async () => {
mockUpdateDevWorkspace.mockResolvedValueOnce({
it('should handle warnings from updateDevWorkspaceStorageType and dispatch warning action', async () => {
mockUpdateDevWorkspaceStorageType.mockResolvedValueOnce({
devWorkspace: mockWorkspace,
headers: {
warning: '299 - Another warning message',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import common, { ApplicationId } from '@eclipse-che/common';

import devfileApi from '@/services/devfileApi';
import { DEVWORKSPACE_STORAGE_TYPE_ATTR } from '@/services/devfileApi/devWorkspace/spec/template';
import { FactoryParams } from '@/services/helpers/factoryFlow/buildFactoryParams';
import { AppThunk } from '@/store';
import { selectApplications } from '@/store/ClusterInfo';
Expand Down Expand Up @@ -54,6 +55,10 @@ export const createWorkspaceFromResources =

dispatch(devWorkspacesRequestAction());

const storageType = devWorkspace.spec?.template?.attributes?.[DEVWORKSPACE_STORAGE_TYPE_ATTR];
if (storageType && storageType !== 'ephemeral') {
devWorkspace.spec.template.attributes[DEVWORKSPACE_STORAGE_TYPE_ATTR] = 'ephemeral';
}
/* create a new DevWorkspace */
const createResp = await getDevWorkspaceClient().createDevWorkspace(
defaultNamespace,
Expand Down Expand Up @@ -90,18 +95,27 @@ export const createWorkspaceFromResources =

/* update the DevWorkspace */

const updateResp = await getDevWorkspaceClient().updateDevWorkspace(createResp.devWorkspace);

if (updateResp.headers.warning) {
dispatch(
devWorkspaceWarningUpdateAction({
warning: cleanupMessage(updateResp.headers.warning),
workspace: updateResp.devWorkspace,
}),
if (storageType && storageType !== 'ephemeral') {
const { namespace, name } = devWorkspace.metadata;
const updateResp = await getDevWorkspaceClient().updateDevWorkspaceStorageType(
namespace,
name,
storageType,
);
}

dispatch(devWorkspacesAddAction(updateResp.devWorkspace));
if (updateResp.headers.warning) {
dispatch(
devWorkspaceWarningUpdateAction({
warning: cleanupMessage(updateResp.headers.warning),
workspace: updateResp.devWorkspace,
}),
);
}

dispatch(devWorkspacesAddAction(updateResp.devWorkspace));
} else {
dispatch(devWorkspacesAddAction(createResp.devWorkspace));
}
} catch (e) {
const errorMessage =
'Failed to create a new workspace, reason: ' + common.helpers.errors.getMessage(e);
Expand Down

0 comments on commit 75c4cb9

Please sign in to comment.