Skip to content

Commit 808bc75

Browse files
authored
Merge pull request ManageIQ#536 from chalettu/edit-orchestration-templates
Added ability to edit a template
2 parents f17f28e + 3506e77 commit 808bc75

File tree

7 files changed

+174
-21
lines changed

7 files changed

+174
-21
lines changed

client/app/states/templates/editor/editor.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
</ol>
77
</span>
88
</div>
9-
<template-editor template="vm.template">
9+
<template-editor existing-template="vm.template">
1010
</template-editor>

client/app/templates/template-editor.component.js

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const TemplateEditorComponent = {
77
controllerAs: 'vm',
88
templateUrl,
99
bindings: {
10-
template: "<",
10+
existingTemplate: "<?",
1111
},
1212
};
1313

@@ -36,35 +36,58 @@ function ComponentController( $state, TemplatesService, EventNotifications, loda
3636
"value": "OrchestrationTemplateVnfd",
3737
},
3838
];
39+
const defaultTemplate = {
40+
name: '',
41+
type: '',
42+
description: '',
43+
content: '',
44+
orderable: true,
45+
draft: true,
46+
};
3947

4048
angular.extend(vm, {
4149
saveTemplate: saveTemplate,
4250
templateTypes: templateTypes,
4351
templateTypeValue: '',
4452
cancelChanges: cancelChanges,
4553
});
46-
if (!vm.template) {
47-
vm.template = {
48-
name: '',
49-
type: '',
50-
description: '',
51-
content: '',
52-
orderable: true,
53-
draft: true,
54+
vm.template = setupTemplate(templateTypes, defaultTemplate);
55+
}
56+
57+
function setupTemplate(templateTypes, defaultTemplate) {
58+
if (vm.existingTemplate) {
59+
vm.templateTypeValue = lodash.find(templateTypes, { value: vm.existingTemplate.type });
60+
61+
return {
62+
name: vm.existingTemplate.name,
63+
type: vm.existingTemplate.type,
64+
description: vm.existingTemplate.description,
65+
content: vm.existingTemplate.content,
66+
orderable: vm.existingTemplate.orderable,
67+
draft: vm.existingTemplate.draft,
68+
id: vm.existingTemplate.id,
5469
};
55-
} else {
56-
vm.templateTypeValue = lodash.find(templateTypes, { value: vm.template.type });
5770
}
58-
}
5971

72+
return defaultTemplate;
73+
}
6074
function saveTemplate() {
61-
if (!vm.template.id) {
62-
vm.template.type = vm.templateTypeValue.value;
63-
TemplatesService.createTemplate(vm.template).then(createSuccess, createFailure);
75+
vm.template.type = vm.templateTypeValue.value;
76+
77+
if (vm.existingTemplate) {
78+
TemplatesService.updateTemplate(vm.template).then(changesSuccessful, changesFailed);
79+
} else {
80+
TemplatesService.createTemplate(vm.template).then(createSuccessful, createFailure);
6481
}
65-
function createSuccess(_data) {
82+
function createSuccessful(_data) {
6683
$state.go('templates.explorer');
6784
}
85+
function changesSuccessful(_data) {
86+
EventNotifications.success(__("Templated updated"));
87+
}
88+
function changesFailed(_data) {
89+
EventNotifications.error(__("There was an error updating the template"));
90+
}
6891
function createFailure(_data) {
6992
EventNotifications.error(__("There was an error creating the template"));
7093
}
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
describe('Component: templateEditor', function () {
22
beforeEach(function () {
33
browser.sleep(10000);
4-
browser.get('/templates/edit/');
4+
browser.get('/templates/edit/10000000000124');
55
});
66
it('should have correct breadcrumb', function () {
77
var breadcrumb = element.all(by.css('.breadcrumb > .active'));
8-
expect(breadcrumb.get(0).getText()).toBe("Add Template");
8+
expect(breadcrumb.get(0).getText()).toBe("Edit Template");
9+
});
10+
it('should have a name', function () {
11+
var name = element.all(by.id('template-editor-name')).first();
12+
expect(name.getAttribute('value')).toBe("101-vm-simple-rhel");
913
});
1014
});

client/app/templates/template-explorer.component.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ function ComponentController(ListView, TemplatesService, EventNotifications, $st
3333
menuActions: getMenuActions(),
3434
toolbarConfig: getToolbarConfig(),
3535
listConfig: getListConfig(),
36+
listActionDisable: listActionDisable,
3637
sortConfig: getSortConfig(),
3738
offset: 0,
3839
pollingInterval: 10000,
@@ -56,6 +57,13 @@ function ComponentController(ListView, TemplatesService, EventNotifications, $st
5657
actionFn: addTemplate,
5758
isDisabled: false,
5859
},
60+
{
61+
name: __('Edit'),
62+
actionName: 'edit',
63+
title: __('Edit template'),
64+
actionFn: editTemplate,
65+
isDisabled: false,
66+
},
5967
],
6068
}];
6169

@@ -119,11 +127,25 @@ function ComponentController(ListView, TemplatesService, EventNotifications, $st
119127
}
120128

121129
function getMenuActions() {
122-
const menuActions = [];
130+
const menuActions = [{
131+
name: __('Edit'),
132+
title: __('Edit Template'),
133+
actionFn: handleEdit,
134+
}];
123135

124136
return menuActions;
125137
}
138+
function listActionDisable(config, items) {
139+
const menuCreate = 0;
140+
const menuEdit = 1;
126141

142+
switch (config.actionName) {
143+
case 'configuration':
144+
config.actions[menuCreate].isDisabled = items.length > 0;
145+
config.actions[menuEdit].isDisabled = items.length !== 1;
146+
break;
147+
}
148+
}
127149
function sortChange(sortId, direction) {
128150
vm.sortConfig.field = sortId.id;
129151
vm.sortConfig.direction = direction === true ? 'asc' : 'desc';
@@ -221,4 +243,15 @@ function ComponentController(ListView, TemplatesService, EventNotifications, $st
221243
function addTemplate() {
222244
$state.go("templates.editor");
223245
}
246+
247+
function handleEdit(_action, template) {
248+
editTemplate(template);
249+
}
250+
251+
function editTemplate(template) {
252+
if (angular.isUndefined(template.id)) {
253+
template = vm.selectedItemsList[0];
254+
}
255+
$state.go('templates.editor', {templateId: template.id});
256+
}
224257
}

client/app/templates/templates.service.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ export function TemplatesServiceFactory(CollectionsApi) {
66
const service = {
77
getMinimal: getMinimal,
88
getTemplates: getTemplates,
9+
getTemplate: getTemplate,
910
createTemplate: createTemplate,
11+
updateTemplate: updateTemplate,
1012
};
1113

1214
return service;
@@ -38,7 +40,13 @@ export function TemplatesServiceFactory(CollectionsApi) {
3840
return CollectionsApi.query(collection, options);
3941
}
4042

41-
// Private
43+
function getTemplate(templateId) {
44+
const options = {
45+
expand: ['resources'],
46+
};
47+
48+
return CollectionsApi.get(collection, templateId, options);
49+
}
4250

4351
function getQueryFilters(filters) {
4452
const queryFilters = [];
@@ -77,4 +85,13 @@ export function TemplatesServiceFactory(CollectionsApi) {
7785
function createTemplate(template) {
7886
return CollectionsApi.post(collection, null, {}, template);
7987
}
88+
89+
function updateTemplate(template) {
90+
const editObj = {
91+
"action": "edit",
92+
"resource": template,
93+
};
94+
95+
return CollectionsApi.post(collection, template.id, {}, editObj);
96+
}
8097
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"url": "orchestration_templates/10000000000124?expand=resources",
3+
"get": {
4+
"href": "http://localhost:3001/api/orchestration_templates/10000000000124",
5+
"id": 10000000000124,
6+
"name": "101-vm-simple-rhel",
7+
"type": "OrchestrationTemplateAzure",
8+
"description": "This template allows deploying a Red Hat Enterprise Linux VM (RHEL 7.2 or RHEL 6.7), using the latest image for the selected RHEL version. This will deploy a Standard D1 VM in the location of your chosen resource group with an additional 100 GiB data disk attached to the VM.\n\nSee https://github.com/Azure/azure-quickstart-templates/tree/master/101-vm-simple-rhel",
9+
"content": "{}",
10+
"md5": "ee4cd9c87a97e0dc9bd119f7eb47edef",
11+
"created_at": "2016-10-05T17:50:38Z",
12+
"updated_at": "2016-10-05T17:50:38Z",
13+
"draft": false,
14+
"orderable": true,
15+
"actions": [
16+
{
17+
"name": "edit",
18+
"method": "post",
19+
"href": "http://localhost:3001/api/orchestration_templates/10000000000124"
20+
},
21+
{
22+
"name": "delete",
23+
"method": "post",
24+
"href": "http://localhost:3001/api/orchestration_templates/10000000000124"
25+
},
26+
{
27+
"name": "copy",
28+
"method": "post",
29+
"href": "http://localhost:3001/api/orchestration_templates/10000000000124"
30+
},
31+
{
32+
"name": "delete",
33+
"method": "delete",
34+
"href": "http://localhost:3001/api/orchestration_templates/10000000000124"
35+
}
36+
]
37+
}
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"url": "orchestration_templates/10000000000125?expand=resources",
3+
"get": {
4+
"href": "http://localhost:3001/api/orchestration_templates/10000000000125",
5+
"id": 10000000000125,
6+
"name": "Demo Template",
7+
"type": "OrchestrationTemplateAzure",
8+
"description": "This template allows deploying a Red Hat Enterprise Linux VM (RHEL 7.2 or RHEL 6.7), using the latest image for the selected RHEL version. This will deploy a Standard D1 VM in the location of your chosen resource group with an additional 100 GiB data disk attached to the VM.\n\nSee https://github.com/Azure/azure-quickstart-templates/tree/master/101-vm-simple-rhel",
9+
"content": "{}",
10+
"md5": "ee4cd9c87a97e0dc9bd119f7eb47edef",
11+
"created_at": "2016-10-05T17:50:38Z",
12+
"updated_at": "2016-10-05T17:50:38Z",
13+
"draft": false,
14+
"orderable": true,
15+
"actions": [
16+
{
17+
"name": "edit",
18+
"method": "post",
19+
"href": "http://localhost:3001/api/orchestration_templates/10000000000125"
20+
},
21+
{
22+
"name": "delete",
23+
"method": "post",
24+
"href": "http://localhost:3001/api/orchestration_templates/10000000000125"
25+
},
26+
{
27+
"name": "copy",
28+
"method": "post",
29+
"href": "http://localhost:3001/api/orchestration_templates/10000000000125"
30+
},
31+
{
32+
"name": "delete",
33+
"method": "delete",
34+
"href": "http://localhost:3001/api/orchestration_templates/10000000000125"
35+
}
36+
]
37+
}
38+
}

0 commit comments

Comments
 (0)