Skip to content

Commit

Permalink
Create workspace from devfile
Browse files Browse the repository at this point in the history
Signed-off-by: Anna Shumilova <ashumilo@redhat.com>
  • Loading branch information
ashumilova committed Jun 3, 2019
1 parent 64129a8 commit 799c246
Show file tree
Hide file tree
Showing 14 changed files with 406 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
ICheButtonDropdownMainAction,
ICheButtonDropdownOtherAction
} from '../../../components/widget/button-dropdown/che-button-dropdown.directive';
import {DevfileRegistry} from '../../../components/api/devfile-registry.factory';

/**
* This class is handling the controller for workspace creation.
Expand All @@ -31,8 +32,8 @@ import {
*/
export class CreateWorkspaceController {

static $inject = ['$mdDialog', '$timeout', 'cheEnvironmentRegistry', 'createWorkspaceSvc', 'namespaceSelectorSvc', 'stackSelectorSvc',
'randomSvc', '$log', 'cheNotification'];
static $inject = ['$mdDialog', '$timeout', 'cheEnvironmentRegistry', 'createWorkspaceSvc', 'namespaceSelectorSvc',
'randomSvc', '$log', 'cheNotification', 'devfileRegistry'];

/**
* Dropdown button config.
Expand All @@ -58,10 +59,6 @@ export class CreateWorkspaceController {
* Namespace selector service.
*/
private namespaceSelectorSvc: NamespaceSelectorSvc;
/**
* Stack selector service.
*/
private stackSelectorSvc: StackSelectorSvc;
/**
* Generator for random strings.
*/
Expand All @@ -75,29 +72,21 @@ export class CreateWorkspaceController {
*/
private cheNotification: CheNotification;
/**
* The environment manager.
* Devfile registry.
*/
private environmentManager: EnvironmentManager;
private devfileRegistry: DevfileRegistry;
/**
* The selected stack.
* The environment manager.
*/
private stack: che.IStack;
private environmentManager: EnvironmentManager;
/**
* The workspace config of the current stack.
* The selected devfile.
*/
private workspaceConfig: che.IWorkspaceConfig;
private selectedDevfile: che.IWorkspaceDevfile;
/**
* The selected namespace ID.
*/
private namespaceId: string;
/**
* The list of machines of selected stack.
*/
private stackMachines: Array<IEnvironmentManagerMachine>;
/**
* Desired memory limit by machine name.
*/
private memoryByMachine: {[name: string]: number};
/**
* The map of forms.
*/
Expand All @@ -122,23 +111,21 @@ export class CreateWorkspaceController {
cheEnvironmentRegistry: CheEnvironmentRegistry,
createWorkspaceSvc: CreateWorkspaceSvc,
namespaceSelectorSvc: NamespaceSelectorSvc,
stackSelectorSvc: StackSelectorSvc,
randomSvc: RandomSvc,
$log: ng.ILogService,
cheNotification: CheNotification) {
cheNotification: CheNotification,
devfileRegistry: DevfileRegistry) {
this.$mdDialog = $mdDialog;
this.$timeout = $timeout;
this.cheEnvironmentRegistry = cheEnvironmentRegistry;
this.createWorkspaceSvc = createWorkspaceSvc;
this.namespaceSelectorSvc = namespaceSelectorSvc;
this.stackSelectorSvc = stackSelectorSvc;
this.randomSvc = randomSvc;
this.$log = $log;
this.cheNotification = cheNotification;
this.devfileRegistry = devfileRegistry;

this.usedNamesList = [];
this.stackMachines = [];
this.memoryByMachine = {};
this.forms = new Map();

this.namespaceId = this.namespaceSelectorSvc.getNamespaceId();
Expand All @@ -150,7 +137,8 @@ export class CreateWorkspaceController {
// loader should be hidden and page content shown
// when stacks selector is rendered
// and default stack is selected
this.hideLoader = false;
//TODO
this.hideLoader = true;

// header toolbar
// dropdown button config
Expand Down Expand Up @@ -182,43 +170,12 @@ export class CreateWorkspaceController {
*
* @param {string} stackId the stack ID
*/
onStackSelected(stackId: string): void {
onDevfileSelected(devfile: che.IWorkspaceDevfile): void {
// tiny timeout for templates selector to be rendered
this.$timeout(() => {
this.hideLoader = true;
}, 10);

this.stack = this.stackSelectorSvc.getStackById(stackId);
this.workspaceConfig = angular.copy(this.stack.workspaceConfig);

if (!this.stack.workspaceConfig || !this.stack.workspaceConfig.defaultEnv) {
this.memoryByMachine = {};
this.stackMachines = [];
return;
}

const environmentName = this.stack.workspaceConfig.defaultEnv;
const environment = this.stack.workspaceConfig.environments[environmentName];
const recipeType = environment.recipe.type;
this.environmentManager = this.cheEnvironmentRegistry.getEnvironmentManager(recipeType);
if (!this.environmentManager) {
const errorMessage = `Unsupported recipe type '${recipeType}'`;
this.$log.error(errorMessage);
this.cheNotification.showError(errorMessage);
return;
}
this.memoryByMachine = {};
this.stackMachines = this.environmentManager.getMachines(environment);
}

/**
* Callback which is called when machine's memory limit is changes.
*
* @param {string} name a machine name
* @param {number} memoryLimitBytes a machine's memory limit in bytes
*/
onRamChanged(name: string, memoryLimitBytes: number): void {
this.memoryByMachine[name] = memoryLimitBytes;
this.selectedDevfile = devfile
}

/**
Expand Down Expand Up @@ -267,7 +224,7 @@ export class CreateWorkspaceController {
* @return {boolean}
*/
isCreateButtonDisabled(): boolean {
if (!this.namespaceId || (this.stack && !this.stack.workspaceConfig)) {
if (!this.namespaceId || !this.selectedDevfile) {
return true;
}

Expand Down Expand Up @@ -312,7 +269,7 @@ export class CreateWorkspaceController {
this.usedNamesList = workspaces.filter((workspace: che.IWorkspace) => {
return workspace.namespace === this.namespaceId;
}).map((workspace: che.IWorkspace) => {
return workspace.config.name;
return this.createWorkspaceSvc.getWorkspaceName(workspace);
});
});
}
Expand Down Expand Up @@ -342,23 +299,8 @@ export class CreateWorkspaceController {
*/
createWorkspace(): ng.IPromise<che.IWorkspace> {
// update workspace name
this.workspaceConfig.name = this.workspaceName;

// update memory limits of machines
if (Object.keys(this.memoryByMachine).length !== 0) {
this.stackMachines.forEach((machine: IEnvironmentManagerMachine) => {
if (this.memoryByMachine[machine.name]) {
this.environmentManager.setMemoryLimit(machine, this.memoryByMachine[machine.name]);
}
});
const environmentName = this.workspaceConfig.defaultEnv;
const environment = this.workspaceConfig.environments[environmentName];
const newEnvironment = this.environmentManager.getEnvironment(environment, this.stackMachines);
this.workspaceConfig.environments[environmentName] = newEnvironment;
}
let attributes = {stackId: this.stack.id};

return this.createWorkspaceSvc.createWorkspaceFromConfig(this.workspaceConfig, attributes);
this.selectedDevfile.name = this.workspaceName;
return this.createWorkspaceSvc.createWorkspaceFromDevfile(this.selectedDevfile, null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,11 @@
on-namespace-change="createWorkspaceController.onNamespaceChanged(namespaceId)"></namespace-selector>
</che-label-container>

<!-- Select Stack -->
<che-label-container che-label-name="Select Stack"
che-label-description="Choose your workspace runtime environment used to build and run your projects.">
<stack-selector on-stack-select="createWorkspaceController.onStackSelected(stackId)"></stack-selector>
</che-label-container>

<!-- RAM Settings-->
<che-label-container che-label-name="Ram"
che-label-description="Allocate the workspace machine(s) memory.">
<ng-form name="ramSettingsForm">
<ram-settings ng-init="createWorkspaceController.registerForm('ramSettings', ramSettingsForm)"
machines="createWorkspaceController.stackMachines"
environment-manager="createWorkspaceController.environmentManager"
on-ram-change="createWorkspaceController.onRamChanged(name, memoryLimitBytes)"></ram-settings>
</ng-form>
</che-label-container>
<devfile-selector on-devfile-select="createWorkspaceController.onDevfileSelected(devfile)">
</devfile-selector>
</che-label-container-->

<!-- Project source selector -->
<che-label-container che-label-name="Projects">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export class CreateWorkspaceSvc {
});
}

/*createWorkspaceFromDevfile(workspaceDevfile: che.IWorkspaceDevfile, attributes: any): ng.IPromise<che.IWorkspace> {
createWorkspaceFromDevfile(workspaceDevfile: che.IWorkspaceDevfile, attributes: any): ng.IPromise<che.IWorkspace> {
const namespaceId = this.namespaceSelectorSvc.getNamespaceId(),
projectTemplates = this.projectSourceSelectorService.getProjectTemplates();

Expand Down Expand Up @@ -220,7 +220,7 @@ export class CreateWorkspaceSvc {
return this.$q.reject(error);
});
});
}*/
}

/**
* Show confirmation dialog when project editing is not completed.
Expand All @@ -244,7 +244,8 @@ export class CreateWorkspaceSvc {
* @param {che.IWorkspace} workspace the workspace to open in IDE
*/
redirectToIDE(workspace: che.IWorkspace): void {
const path = `/ide/${workspace.namespace}/${workspace.config.name}`;
let name = this.cheWorkspace.getWorkspaceDataManager().getName(workspace);
const path = `/ide/${workspace.namespace}/${name}`;
this.$location.path(path);
}

Expand All @@ -254,7 +255,8 @@ export class CreateWorkspaceSvc {
* @param {che.IWorkspace} workspace the workspace to open in IDE
*/
redirectToDetails(workspace: che.IWorkspace): void {
const path = `/workspace/${workspace.namespace}/${workspace.config.name}`;
let name = this.cheWorkspace.getWorkspaceDataManager().getName(workspace);
const path = `/workspace/${workspace.namespace}/${name}`;
this.$location.path(path);
}

Expand All @@ -273,4 +275,13 @@ export class CreateWorkspaceSvc {
});
});
}

/**
* Returns name of the pointed workspace.
*
* @param workspace workspace
*/
getWorkspaceName(workspace: che.IWorkspace): string {
return this.cheWorkspace.getWorkspaceDataManager().getName(workspace);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2015-2018 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
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
'use strict';
import {CheWorkspace} from '../../../../components/api/workspace/che-workspace.factory';
import {DevfileRegistry, IDevfileMetaData} from '../../../../components/api/devfile-registry.factory';

/**
* @description This class is handling the controller of devfile selector.
* @author Ann Shumilova
*/
export class DevfileSelectorController {

static $inject = ['devfileRegistry', 'cheWorkspace'];

private devfileRegistry: DevfileRegistry;
private cheWorkspace: CheWorkspace;
private devfiles: Array<IDevfileMetaData>;
onDevfileSelect: Function;
selectedDevfile: any;

/**
* Default constructor that is using resource injection
*/
constructor(devfileRegistry: DevfileRegistry, cheWorkspace: CheWorkspace) {
this.devfileRegistry = devfileRegistry;
this.cheWorkspace = cheWorkspace;
this.loadDevfiles();
}

loadDevfiles(): void {
let location = this.cheWorkspace.getWorkspaceSettings().devfileRegistry;
this.devfileRegistry.fetchDevfiles(location).then((data: Array<IDevfileMetaData>) => {
this.devfiles = data;

if (this.devfiles && this.devfiles.length > 0) {
this.devfileOnClick(this.devfiles[0]);
}
});
}

devfileOnClick(devfile: any): void {
this.selectedDevfile = devfile;

let location = this.cheWorkspace.getWorkspaceSettings().devfileRegistry;

let devfileContent = this.devfileRegistry.getDevfile(location, devfile.links.self);
if (devfileContent) {
this.onDevfileSelect({devfile: devfileContent});
} else {
this.devfileRegistry.fetchDevfile(location, devfile.links.self).then((devfileContent: che.IWorkspaceDevfile) => {
this.onDevfileSelect({devfile: devfileContent});
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2018-2018 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
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
'use strict';

/**
* Defines a directive for displaying devfile selector widget.
*
* @author Ann Shumilova
*/
export class DevfileSelector implements ng.IDirective {
restrict: string = 'E';
templateUrl: string = 'app/workspaces/create-workspace/devfile-selector/devfile-selector.html';
replace: boolean = true;

controller: string = 'DevfileSelectorController';
controllerAs: string = 'devfileSelectorController';

bindToController: boolean = true;

scope: {
[propName: string]: string
};

/**
* Default constructor that is using resource
*/
constructor() {
this.scope = {
devfileIdSelected: '=',
onDevfileSelect: '&'
};
}
}
Loading

0 comments on commit 799c246

Please sign in to comment.