Skip to content

Commit

Permalink
Add devfile support in UD
Browse files Browse the repository at this point in the history
Signed-off-by: Anna Shumilova <ashumilo@redhat.com>
  • Loading branch information
ashumilova committed May 21, 2019
1 parent 6bc1d6e commit be3a56c
Show file tree
Hide file tree
Showing 29 changed files with 703 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ export class NavbarRecentWorkspacesController {
*/
getWorkspaceName(workspaceId: string): string {
let workspace = this.cheWorkspace.getWorkspaceById(workspaceId);
return workspace ? workspace.config.name : 'unknown';
return workspace ? this.cheWorkspace.getWorkspaceDataManager().getName(workspace) : 'unknown';
}

/**
Expand Down Expand Up @@ -256,7 +256,7 @@ export class NavbarRecentWorkspacesController {
* @returns {string}
*/
getIdeLink(workspace: che.IWorkspace): string {
return '#/ide/' + (workspace ? (workspace.namespace + '/' + workspace.config.name) : 'unknown');
return '#/ide/' + (workspace ? (workspace.namespace + '/' + this.cheWorkspace.getWorkspaceDataManager().getName(workspace)) : 'unknown');
}

/**
Expand All @@ -265,7 +265,7 @@ export class NavbarRecentWorkspacesController {
* @returns {string}
*/
getWorkspaceDetailsLink(workspace: che.IWorkspace): string {
return '#/workspace/' + workspace.namespace + '/' + workspace.config.name;
return '#/workspace/' + workspace.namespace + '/' + this.cheWorkspace.getWorkspaceDataManager().getName(workspace);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<span title="{{navbarRecentWorkspacesController.getWorkspaceName(workspace.id)}}"
class="recent-workspace-name-container"
ng-class="{'recent-workspaces-stopped-workspace': navbarRecentWorkspacesController.getWorkspaceStatus(workspace.id) === 'STOPPED'}">
<span che-clip-the-middle>{{workspace.config.name}}</span>
<span che-clip-the-middle>{{navbarRecentWorkspacesController.getWorkspaceName(workspace.id)}}</span>
</span>
</div>
</md-button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ export class CreateWorkspaceController {
}
let attributes = {stackId: this.stack.id};

return this.createWorkspaceSvc.createWorkspace(this.workspaceConfig, attributes);
return this.createWorkspaceSvc.createWorkspaceFromConfig(this.workspaceConfig, attributes);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,20 +156,13 @@ export class CreateWorkspaceSvc {
return defer.promise;
}

/**
* Creates a workspace from config.
*
* @param {che.IWorkspaceConfig} workspaceConfig the config of workspace which will be created
* @param {any} attributes the attributes of the workspace
* @returns {ng.IPromise<che.IWorkspace>}
*/
createWorkspace(workspaceConfig: che.IWorkspaceConfig, attributes: any): ng.IPromise<che.IWorkspace> {
createWorkspaceFromConfig(workspaceConfig: che.IWorkspaceConfig, attributes: any): ng.IPromise<che.IWorkspace> {
const namespaceId = this.namespaceSelectorSvc.getNamespaceId(),
projectTemplates = this.projectSourceSelectorService.getProjectTemplates();

return this.checkEditingProgress().then(() => {
workspaceConfig.projects = projectTemplates;
this.addProjectCommands(workspaceConfig, projectTemplates);
this.addProjectCommands({config: workspaceConfig}, projectTemplates);
return this.cheWorkspace.createWorkspaceFromConfig(namespaceId, workspaceConfig, attributes).then((workspace: che.IWorkspace) => {
return this.cheWorkspace.fetchWorkspaces().then(() => this.cheWorkspace.getWorkspaceById(workspace.id));
})
Expand All @@ -196,6 +189,39 @@ export class CreateWorkspaceSvc {
});
}

/*createWorkspaceFromDevfile(workspaceDevfile: che.IWorkspaceDevfile, attributes: any): ng.IPromise<che.IWorkspace> {
const namespaceId = this.namespaceSelectorSvc.getNamespaceId(),
projectTemplates = this.projectSourceSelectorService.getProjectTemplates();
return this.checkEditingProgress().then(() => {
workspaceDevfile.projects = projectTemplates;
this.addProjectCommands({devfile: workspaceDevfile}, projectTemplates);
return this.cheWorkspace.createWorkspaceFromDevfile(namespaceId, workspaceDevfile, attributes).then((workspace: che.IWorkspace) => {
return this.cheWorkspace.fetchWorkspaces().then(() => this.cheWorkspace.getWorkspaceById(workspace.id));
})
.then((workspace: che.IWorkspace) => {
this.projectSourceSelectorService.clearTemplatesList();
const workspaces = this.cheWorkspace.getWorkspaces();
if (workspaces.findIndex((_workspace: che.IWorkspace) => {
return _workspace.id === workspace.id;
}) === -1) {
workspaces.push(workspace);
}
this.cheWorkspace.startUpdateWorkspaceStatus(workspace.id);
return workspace;
}, (error: any) => {
let errorMessage = 'Creation workspace failed.';
if (error && error.data && error.data.message) {
errorMessage = error.data.message;
}
this.cheNotification.showError(errorMessage);
return this.$q.reject(error);
});
});
}*/

/**
* Show confirmation dialog when project editing is not completed.
*
Expand Down Expand Up @@ -233,19 +259,17 @@ export class CreateWorkspaceSvc {
}

/**
* Adds commands from the bunch of project templates to provided workspace config.
* Adds commands from the bunch of project templates to provided workspace.
*
* @param {che.IWorkspaceConfig} workspaceConfig workspace config
* @param {che.IWorkspace} workspace
* @param {Array<che.IProjectTemplate>} projectTemplates the list of project templates
*/
addProjectCommands(workspaceConfig: che.IWorkspaceConfig, projectTemplates: Array<che.IProjectTemplate>): void {
workspaceConfig.commands = workspaceConfig.commands || [];

addProjectCommands(workspace: che.IWorkspace, projectTemplates: Array<che.IProjectTemplate>): void {
projectTemplates.forEach((template: che.IProjectTemplate) => {
let projectName = template.name;
template.commands.forEach((command: any) => {
command.name = projectName + ':' + command.name;
workspaceConfig.commands.push(command);
this.cheWorkspace.getWorkspaceDataManager().addCommand(workspace, command);
});
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ export class ListWorkspacesCtrl {
return this.$q.when();
}
let message = error.data && error.data.message ? ' Reason: ' + error.data.message : '';
this.cheNotification.showError('Failed to retrieve workspace ' + workspace.config.name + ' data.' + message) ;
let workspaceName = this.cheWorkspace.getWorkspaceDataManager().getName(workspace);
this.cheNotification.showError('Failed to retrieve workspace ' + workspaceName + ' data.' + message) ;
return this.$q.reject(error);
})
.then(() => {
Expand Down Expand Up @@ -249,7 +250,7 @@ export class ListWorkspacesCtrl {
if (!workspace) {
return;
}
workspaceName = workspace.config.name;
workspaceName = this.cheWorkspace.getWorkspaceDataManager().getName(workspace);
let stoppedStatusPromise = this.cheWorkspace.fetchStatusChange(workspaceId, 'STOPPED');

// stop workspace if it's status is RUNNING
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class WorkspaceItemCtrl {
workspacesService: WorkspacesService;

workspace: che.IWorkspace;
workspaceName: string;

/**
* Default constructor that is using resource
Expand All @@ -41,6 +42,7 @@ export class WorkspaceItemCtrl {
this.lodash = lodash;
this.cheWorkspace = cheWorkspace;
this.workspacesService = workspacesService;
this.workspaceName = this.cheWorkspace.getWorkspaceDataManager().getName(this.workspace);
}

/**
Expand All @@ -57,7 +59,7 @@ export class WorkspaceItemCtrl {
* @param tab {string}
*/
redirectToWorkspaceDetails(tab?: string): void {
this.$location.path('/workspace/' + this.workspace.namespace + '/' + this.workspace.config.name).search({tab: tab ? tab : 'Overview'});
this.$location.path('/workspace/' + this.workspace.namespace + '/' + this.workspaceName).search({tab: tab ? tab : 'Overview'});
}

getDefaultEnvironment(workspace: che.IWorkspace): che.IWorkspaceEnvironment {
Expand All @@ -68,6 +70,10 @@ export class WorkspaceItemCtrl {
}

getMemoryLimit(workspace: che.IWorkspace): string {
if (!workspace.config && workspace.devfile) {
return '-';
}

let environment = this.getDefaultEnvironment(workspace);
if (environment) {
let limits = this.lodash.pluck(environment.machines, 'attributes.memoryLimitBytes');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
-->
<che-list-item flex ng-mouseover="hover=true" ng-mouseout="hover=false">
<div flex="100"
id="ws-name-{{workspaceItemCtrl.workspace.config.name}}"
id="ws-name-{{workspaceItemCtrl.workspaceName}}"
data-ws-status="{{workspaceItemCtrl.getWorkspaceStatus()}}"
layout="row"
layout-align="start stretch"
Expand All @@ -24,7 +24,7 @@
ng-if="workspaceItemCtrl.isSelectable === true">
<che-list-item-checked ng-model="workspaceItemCtrl.isSelect"
ng-show="('RUNNING' === workspaceItemCtrl.getWorkspaceStatus() || 'STOPPED' === workspaceItemCtrl.getWorkspaceStatus())"
che-aria-label-checkbox="Workspace {{workspaceItemCtrl.workspace.config.name}}"
che-aria-label-checkbox="Workspace {{workspaceItemCtrl.workspaceName}}"
ng-click="workspaceItemCtrl.onCheckboxClick()"></che-list-item-checked>
</div>
<div flex
Expand All @@ -41,10 +41,10 @@
<span class="che-xs-header noselect" hide-gt-xs>Name</span>
<div layout="row" flex>
<workspace-status-indicator flex="none" che-status="workspaceItemCtrl.getWorkspaceStatus()"></workspace-status-indicator>
<div class="workspace-name-clip" id="ws-full-name-{{workspaceItemCtrl.workspace.namespace}}/{{workspaceItemCtrl.workspace.config.name}}">
<div class="workspace-name-clip" id="ws-full-name-{{workspaceItemCtrl.workspace.namespace}}/{{workspaceItemCtrl.workspaceName}}">
<span uib-tooltip="{{'RUNNING' === workspaceItemCtrl.getWorkspaceStatus() ? 'Running' : 'Last modified: ' + (workspaceItemCtrl.workspace.attributes.updated | amTimeAgo)}}"
class="che-hover">
<che-clip-the-middle>{{workspaceItemCtrl.workspace.namespace}}/{{workspaceItemCtrl.workspace.config.name}}</che-clip-the-middle>
<che-clip-the-middle>{{workspaceItemCtrl.workspace.namespace}}/{{workspaceItemCtrl.workspaceName}}</che-clip-the-middle>
</span>
</div>
</div>
Expand Down Expand Up @@ -86,7 +86,7 @@
<che-workspace-status workspace-id="workspaceItemCtrl.workspace.id"
name="workspace-stop-start-button"
is-request-pending="workspaceItemCtrl.isRequestPending"></che-workspace-status>
<a href="#/workspace/{{workspaceItemCtrl.workspace.namespace}}/{{workspaceItemCtrl.workspace.config.name}}?tab=Config"
<a href="#/workspace/{{workspaceItemCtrl.workspace.namespace}}/{{workspaceItemCtrl.workspaceName}}?tab=Config"
name="configure-workspace-button"
uib-tooltip="Configure workspace">
<span class="fa fa-cog"></span>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* 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';
/**
* @ngdoc controller
* @name workspaces.devfile-editor.controller:WorkspaceDevfileEditorController
* @description This class is handling the controller for the workspace devfile editor widget
* @author Anna Shumilova
*/
export class WorkspaceDevfileEditorController {

static $inject = ['$log', '$scope', '$timeout'];

$log: ng.ILogService;
$scope: ng.IScope;
$timeout: ng.ITimeoutService;

editorOptions: {
lineWrapping: boolean,
lineNumbers: boolean,
matchBrackets: boolean,
mode: string,
onLoad: Function
};
devfileValidationMessages: string[] = [];
isActive: boolean;
workspaceDevfile: che.IWorkspaceDevfile;
devfileYaml: string;
newWorkspaceDevfile: che.IWorkspaceDevfile;
workspaceDevfileOnChange: Function;
private saveTimeoutPromise: ng.IPromise<any>;
private isSaving: boolean;


/**
* Default constructor that is using resource
*/
constructor($log: ng.ILogService, $scope: ng.IScope, $timeout: ng.ITimeoutService) {
this.$log = $log;
this.$scope = $scope;
this.$timeout = $timeout;
this.isSaving = false;
this.devfileYaml = jsyaml.dump(this.workspaceDevfile);

$scope.$watch(() => {
return this.workspaceDevfile;
}, () => {
let editedWorkspaceDevfile;
try {
editedWorkspaceDevfile = jsyaml.load(this.devfileYaml);
angular.extend(editedWorkspaceDevfile, this.workspaceDevfile);
} catch (e) {
editedWorkspaceDevfile = this.workspaceDevfile;
}
this.devfileYaml = jsyaml.dump(this.workspaceDevfile);
const validateOnly = true;
this.onChange(validateOnly);
}, true);

}

/**
* Callback when editor content is changed.
*/
onChange(validateOnly?: boolean): void {
this.devfileValidationMessages = [];
if (!this.devfileYaml) {
return;
}

let devfile;
try {
devfile = jsyaml.load(this.devfileYaml);
} catch (e) {
if (e.name === 'YAMLException') {
this.devfileValidationMessages = [e.message];
}
if (this.devfileValidationMessages.length === 0) {
this.devfileValidationMessages = ['Devfile is invalid.'];
}
this.$log.error(e);
}

if (validateOnly || !this.isActive) {
return;
}
this.isSaving = (this.devfileValidationMessages.length === 0) && !angular.equals(devfile, this.workspaceDevfile);

if (this.saveTimeoutPromise) {
this.$timeout.cancel(this.saveTimeoutPromise);
}

this.saveTimeoutPromise = this.$timeout(() => {
// immediately apply config on IU
this.newWorkspaceDevfile = angular.copy(devfile);
this.isSaving = false;
this.applyChanges();
}, 2000);
}

/**
* Callback when user applies new config.
*/
applyChanges(): void {
this.workspaceDevfileOnChange({devfile: this.newWorkspaceDevfile});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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';

/**
* Defines a directive for displaying devfile editor widget.
* @author Anna Shumilova
*/
export class WorkspaceDevfileEditor {
restrict: string = 'E';
templateUrl: string = 'app/workspaces/workspace-details/devfile/workspace-devfile-editor.html';
replace: boolean = false;

controller: string = 'WorkspaceDevfileEditorController';
controllerAs: string = 'workspaceDevfileEditorController';

bindToController: boolean = true;

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

/**
* Default constructor that is using resource
*/
constructor() {
// scope values
this.scope = {
isActive: '=',
workspaceDevfile: '=',
workspaceDevfileOnChange: '&'
};
}

}
Loading

0 comments on commit be3a56c

Please sign in to comment.