forked from openshift/origin-web-console
/
processTemplate.js
307 lines (278 loc) · 10.2 KB
/
processTemplate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
'use strict';
(function() {
angular.module('openshiftConsole').component('processTemplate', {
controller: [
'$filter',
'$q',
'$scope',
'$uibModal',
'DataService',
'Navigate',
'NotificationsService',
'ProcessedTemplateService',
'ProjectsService',
'QuotaService',
'SecurityCheckService',
'TaskList',
'keyValueEditorUtils',
ProcessTemplate
],
controllerAs: '$ctrl',
bindings: {
template: '<',
project: '<',
onProjectSelected: '<',
availableProjects: '<',
prefillParameters: '<',
isDialog: '<'
},
templateUrl: 'views/directives/process-template.html'
});
function ProcessTemplate($filter,
$q,
$scope,
$uibModal,
DataService,
Navigate,
NotificationsService,
ProcessedTemplateService,
ProjectsService,
QuotaService,
SecurityCheckService,
TaskList,
keyValueEditorUtils) {
var ctrl = this;
var context;
var displayName = $filter('displayName');
var humanize = $filter('humanize');
ctrl.noProjectsCantCreate = false;
function getHelpLinks(template) {
var helpLinkName = /^helplink\.(.*)\.title$/;
var helpLinkURL = /^helplink\.(.*)\.url$/;
var helpLinks = {};
for (var attr in template.annotations) {
var match = attr.match(helpLinkName);
var link;
if (match) {
link = helpLinks[match[1]] || {};
link.title = template.annotations[attr];
helpLinks[match[1]] = link;
}
else {
match = attr.match(helpLinkURL);
if (match) {
link = helpLinks[match[1]] || {};
link.url = template.annotations[attr];
helpLinks[match[1]] = link;
}
}
}
return helpLinks;
}
ctrl.$onInit = function() {
ctrl.labels = [];
// Make a copy of the template to avoid modifying the original if it's cached.
ctrl.template = angular.copy(ctrl.template);
ctrl.templateDisplayName = displayName(ctrl.template);
ctrl.selectedProject = ctrl.project;
$scope.$on('no-projects-cannot-create', function() {
ctrl.noProjectsCantCreate = true;
});
setTemplateParams();
};
var processedResources;
var createResources = function() {
var titles = {
started: "Creating " + ctrl.templateDisplayName + " in project " + displayName(ctrl.selectedProject),
success: "Created " + ctrl.templateDisplayName + " in project " + displayName(ctrl.selectedProject),
failure: "Failed to create " + ctrl.templateDisplayName + " in project " + displayName(ctrl.selectedProject)
};
var helpLinks = getHelpLinks(ctrl.template);
TaskList.clear();
TaskList.add(titles, helpLinks, ctrl.selectedProject.metadata.name, function() {
var d = $q.defer();
DataService.batch(processedResources, context).then(
function(result) {
var alerts = [];
var hasErrors = false;
if (result.failure.length > 0) {
hasErrors = true;
result.failure.forEach(
function(failure) {
alerts.push({
type: "error",
message: "Cannot create " + humanize(failure.object.kind).toLowerCase() + " \"" + failure.object.metadata.name + "\". ",
details: failure.data.message
});
}
);
result.success.forEach(
function(success) {
alerts.push({
type: "success",
message: "Created " + humanize(success.kind).toLowerCase() + " \"" + success.metadata.name + "\" successfully. "
});
}
);
} else {
alerts.push({ type: "success", message: "All items in template " + ctrl.templateDisplayName +
" were created successfully."});
}
d.resolve({alerts: alerts, hasErrors: hasErrors});
}
);
return d.promise;
});
if (ctrl.isDialog) {
$scope.$emit('templateInstantiated', {
project: ctrl.selectedProject,
template: ctrl.template
});
} else {
Navigate.toNextSteps(ctrl.templateDisplayName, ctrl.selectedProject.metadata.name);
}
};
var launchConfirmationDialog = function(alerts) {
var modalInstance = $uibModal.open({
animation: true,
templateUrl: 'views/modals/confirm.html',
controller: 'ConfirmModalController',
resolve: {
modalConfig: function() {
return {
alerts: alerts,
message: "We checked your application for potential problems. Please confirm you still want to create this application.",
okButtonText: "Create Anyway",
okButtonClass: "btn-danger",
cancelButtonText: "Cancel"
};
}
}
});
modalInstance.result.then(createResources);
};
var alerts = {};
var hideNotificationErrors = function() {
NotificationsService.hideNotification("process-template-error");
_.each(alerts, function(alert) {
if (alert.id && (alert.type === 'error' || alert.type === 'warning')) {
NotificationsService.hideNotification(alert.id);
}
});
};
var showWarningsOrCreate = function(result) {
// Hide any previous notifications when form is resubmitted.
hideNotificationErrors();
alerts = SecurityCheckService.getSecurityAlerts(processedResources, ctrl.selectedProject.metadata.name);
// Now that all checks are completed, show any Alerts if we need to
var quotaAlerts = result.quotaAlerts || [];
alerts = alerts.concat(quotaAlerts);
var errorAlerts = _.filter(alerts, {type: 'error'});
if (errorAlerts.length) {
ctrl.disableInputs = false;
_.each(alerts, function(alert) {
alert.id = _.uniqueId('process-template-alert-');
NotificationsService.addNotification(alert);
});
}
else if (alerts.length) {
launchConfirmationDialog(alerts);
ctrl.disableInputs = false;
}
else {
createResources();
}
};
var createProjectIfNecessary = function() {
if (_.has(ctrl.selectedProject, 'metadata.uid')) {
return $q.when(ctrl.selectedProject);
}
var newProjName = ctrl.selectedProject.metadata.name;
var newProjDisplayName = ctrl.selectedProject.metadata.annotations['new-display-name'];
var newProjDesc = $filter('description')(ctrl.selectedProject);
return ProjectsService.create(newProjName, newProjDisplayName, newProjDesc);
};
ctrl.createFromTemplate = function() {
ctrl.disableInputs = true;
createProjectIfNecessary().then(function(project) {
ctrl.selectedProject = project;
context = {
namespace: ctrl.selectedProject.metadata.name
};
ctrl.template.labels = keyValueEditorUtils.mapEntries(keyValueEditorUtils.compactEntries(ctrl.labels));
DataService.create("processedtemplates", null, ctrl.template, context).then(
function(config) { // success
// Cache template parameters and message so they can be displayed in the nexSteps page
ProcessedTemplateService.setTemplateData(config.parameters, ctrl.template.parameters, config.message);
processedResources = config.objects;
QuotaService.getLatestQuotaAlerts(processedResources, context).then(showWarningsOrCreate);
},
function(result) { // failure
ctrl.disableInputs = false;
var details;
if (result.data && result.data.message) {
details = result.data.message;
}
NotificationsService.addNotification({
id: "process-template-error",
type: "error",
message: "An error occurred processing the template.",
details: details
});
}
);
}, function(result) {
ctrl.disableInputs = false;
var details;
if (result.data && result.data.message) {
details = result.data.message;
}
NotificationsService.addNotification({
id: "process-template-error",
type: "error",
message: "An error occurred creating the project.",
details: details
});
});
};
// Only called when not in a dialog.
ctrl.cancel = function() {
hideNotificationErrors();
Navigate.toProjectOverview(ctrl.project.metadata.name);
};
// When the process-template component is displayed in a dialog, the create
// button is outside the component since it is in the wizard footer. Listen
// for an event for when the button is clicked.
$scope.$on('instantiateTemplate', ctrl.createFromTemplate);
$scope.$on('$destroy', hideNotificationErrors);
var shouldAddAppLabel = function() {
// If the template defines its own app label, we don't need to add one at all
if (_.get(ctrl.template, 'labels.app')) {
return false;
}
// Otherwise check if an object in the template has an app label defined
return !_.some(ctrl.template.objects, "metadata.labels.app");
};
function setTemplateParams() {
if(ctrl.prefillParameters) {
_.each(ctrl.template.parameters, function(parameter) {
if (ctrl.prefillParameters[parameter.name]) {
parameter.value = ctrl.prefillParameters[parameter.name];
}
});
}
ctrl.labels = _.map(ctrl.template.labels, function(value, key) {
return {
name: key,
value: value
};
});
if (shouldAddAppLabel()) {
ctrl.labels.push({
name: 'app',
value: ctrl.template.metadata.name
});
}
}
}
})();