blob: c0d9ab9beb570e32df2965226ca7f9fb6bab642d [file] [log] [blame]
Scott Baker342d9b92015-01-11 13:44:30 -08001XOSTenantSite = XOSModel.extend( {
2 listFields: ["name", "allocated"],
3 modelName: "tenantSite",
4 collectionName: "tenantSites"
5});
6
7XOSTenantSiteCollection = XOSCollection.extend( {
8 listFields: ["name", "allocated"],
9 modelName: "tenantSite",
10 collectionName: "tenantSites",
11
Scott Baker97acad92015-01-12 19:45:40 -080012 getFromSlice: function(slice) {
Scott Baker342d9b92015-01-11 13:44:30 -080013 var tenantSites = [];
14 var id = 0;
15 for (siteName in slice.attributes.site_allocation) {
16 allocated = slice.attributes.site_allocation[siteName];
17 tenantSites.push(new XOSTenantSite( { name: siteName, allocated: allocated, id: id} ));
18 id = id + 1;
19 }
20 for (index in xos.tenantview.models[0].attributes.blessed_site_names) {
21 siteName = xos.tenantview.models[0].attributes.blessed_site_names[index];
22 if (! (siteName in slice.attributes.site_allocation)) {
23 tenantSites.push(new XOSTenantSite( { name: siteName, allocated: 0, id: id} ));
24 id = id + 1;
25 }
26 }
27 this.set(tenantSites);
28 },
Scott Baker97acad92015-01-12 19:45:40 -080029
30 putToSlice: function(slice) {
31 slice.attributes.site_allocation = {};
32 for (index in this.models) {
Scott Baker435c2c92015-01-14 00:34:45 -080033 var model = this.models[index];
Scott Baker97acad92015-01-12 19:45:40 -080034 slice.attributes.site_allocation[ model.attributes.name ] = model.attributes.allocated;
35 }
36 },
Scott Baker342d9b92015-01-11 13:44:30 -080037});
38
Scott Baker97acad92015-01-12 19:45:40 -080039XOSEditUsersView = Marionette.ItemView.extend({
40 template: "#tenant-edit-users",
41 viewInitializers: [],
42
43 onShow: function() {
44 _.each(this.viewInitializers, function(initializer) {
45 initializer();
46 });
47 },
48
49 templateHelpers: function() { return { detailView: this, model: this.model }; },
50
51 });
52
Scott Bakerf5d97bd2015-01-20 00:04:19 -080053XOSTenantSummaryView = XOSDetailView.extend({
54 events: {"change": "onChange"},
55
56 onChange: function(e) {
57 XOSTenantApp.setDirty(true);
58 },
59
60 saveSuccess: function() {
61 console.log("saveSuccess!");
62 XOSTenantApp.setDirty(false);
63 },
64
65 });
66
Scott Baker97acad92015-01-12 19:45:40 -080067
Scott Baker342d9b92015-01-11 13:44:30 -080068XOSTenantButtonView = Marionette.ItemView.extend({
69 template: "#xos-tenant-buttons-template",
70
71 events: {"click button.btn-tenant-create": "createClicked",
72 "click button.btn-tenant-delete": "deleteClicked",
73 "click button.btn-tenant-add-user": "addUserClicked",
74 "click button.btn-tenant-save": "saveClicked",
Scott Bakerec930102015-01-20 01:02:08 -080075 "click button.btn-tenant-download-ssh": "downloadClicked",
Scott Baker342d9b92015-01-11 13:44:30 -080076 },
77
78 createClicked: function(e) {
Scott Bakercd07a592015-01-12 12:37:38 -080079 XOSTenantApp.addSlice();
Scott Baker342d9b92015-01-11 13:44:30 -080080 },
81
82 deleteClicked: function(e) {
Scott Baker6b145aa2015-01-12 12:56:25 -080083 XOSTenantApp.deleteSlice(this.options.linkedView.model);
Scott Baker342d9b92015-01-11 13:44:30 -080084 },
85
86 addUserClicked: function(e) {
Scott Baker97acad92015-01-12 19:45:40 -080087 XOSTenantApp.editUsers(this.options.linkedView.model);
Scott Baker342d9b92015-01-11 13:44:30 -080088 },
89
Scott Bakerec930102015-01-20 01:02:08 -080090 downloadClicked: function(e) {
91 XOSTenantApp.downloadSSH(this.options.linkedView.model);
92 },
93
Scott Baker342d9b92015-01-11 13:44:30 -080094 saveClicked: function(e) {
Scott Baker97acad92015-01-12 19:45:40 -080095 model = this.options.linkedView.model;
96 model.tenantSiteCollection.putToSlice(model);
Scott Baker435c2c92015-01-14 00:34:45 -080097 model.attributes.users = model.usersBuffer;
Scott Bakerf5d97bd2015-01-20 00:04:19 -080098
99 e.preventDefault();
100 this.options.linkedView.save();
101 //this.options.linkedView.submitContinueClicked.call(this.options.linkedView, e);
102 //XOSTenantApp.setDirty(false);
Scott Baker342d9b92015-01-11 13:44:30 -0800103 },
104 });
105
106XOSTenantApp = new XOSApplication({
107 logTableId: "#logTable",
108 statusMsgId: "#statusMsg",
Scott Bakercd07a592015-01-12 12:37:38 -0800109 hideTabsByDefault: true,
Scott Baker5175e8a2015-01-20 00:20:12 -0800110 dirty: false,
Scott Bakercd07a592015-01-12 12:37:38 -0800111 varName: "XOSTenantApp",
Scott Baker342d9b92015-01-11 13:44:30 -0800112});
113
114XOSTenantApp.addRegions({
115 tenantSliceSelector: "#tenantSliceSelector",
116 tenantSummary: "#tenantSummary",
117 tenantSiteList: "#tenantSiteList",
118 tenantButtons: "#tenantButtons",
Scott Bakercd07a592015-01-12 12:37:38 -0800119 tenantAddSliceInterior: "#tenant-addslice-interior",
Scott Baker97acad92015-01-12 19:45:40 -0800120 tenantEditUsersInterior: "#tenant-edit-users-interior",
Scott Bakerec930102015-01-20 01:02:08 -0800121 tenantSSHCommandsInterior: "#tenant-ssh-commands-interior",
Scott Baker342d9b92015-01-11 13:44:30 -0800122});
123
Scott Bakerf5d97bd2015-01-20 00:04:19 -0800124XOSTenantApp.setDirty = function(dirty) {
Scott Baker5175e8a2015-01-20 00:20:12 -0800125 XOSTenantApp.dirty = dirty;
Scott Bakerf5d97bd2015-01-20 00:04:19 -0800126 if (dirty) {
127 $("button.btn-tenant-save").addClass("btn-success");
128 } else {
129 $("button.btn-tenant-save").removeClass("btn-success");
130 }
131};
132
Scott Bakercd07a592015-01-12 12:37:38 -0800133XOSTenantApp.buildViews = function() {
Scott Baker342d9b92015-01-11 13:44:30 -0800134 XOSTenantApp.tenantSites = new XOSTenantSiteCollection();
135
Scott Bakerf5d97bd2015-01-20 00:04:19 -0800136 tenantSummaryClass = XOSTenantSummaryView.extend({template: "#xos-detail-template",
Scott Baker342d9b92015-01-11 13:44:30 -0800137 app: XOSTenantApp,
Scott Baker97acad92015-01-12 19:45:40 -0800138 detailFields: ["serviceClass", "default_image", "default_flavor", "network_ports", "mount_data_sets"],
Scott Bakere56d3272015-01-14 00:47:50 -0800139 fieldDisplayNames: {serviceClass: "Service Level", "default_flavor": "Flavor", "default_image": "Image", "mount_data_sets": "Data Sets"},
Scott Baker51bacad2015-01-20 12:24:16 -0800140 helpText: {"default_image": "Existing slivers will be re-instantiated if changed",
141 "default_flavor": "Existing slivers will be re-instantiated if changed"},
Scott Bakere7a90452015-01-14 17:07:30 -0800142
143 onShow: function() {
144 // the slice selector is in a different table, so make every label cell the maximal width
145 make_same_width("#xos-tenant-view-panel", ".xos-label-cell");
146 },
Scott Baker97acad92015-01-12 19:45:40 -0800147 });
Scott Baker342d9b92015-01-11 13:44:30 -0800148
149 XOSTenantApp.tenantSummaryView = tenantSummaryClass;
150
Scott Bakercd07a592015-01-12 12:37:38 -0800151 tenantAddClass = XOSDetailView.extend({template: "#xos-detail-template",
152 app: XOSTenantApp,
153 detailFields: ["name", "description"]});
154
155 XOSTenantApp.tenantAddView = tenantAddClass;
156
Scott Baker342d9b92015-01-11 13:44:30 -0800157 tenantSiteItemClass = XOSItemView.extend({template: "#xos-listitem-template",
158 app: XOSTenantApp});
159
160 XOSTenantApp.tenantSiteItemView = tenantSiteItemClass;
161
162 tenantSiteListClass = XOSDataTableView.extend({template: "#xos-list-template",
163 app: XOSTenantApp,
164 childView: tenantSiteItemClass,
165 collection: XOSTenantApp.tenantSites,
166 title: "sites",
167 inputType: {allocated: "spinner"},
168 noDeleteColumn: true,
Scott Bakere56d3272015-01-14 00:47:50 -0800169 disablePaginate: true,
170 disableFilter: true,
Scott Bakere7a90452015-01-14 17:07:30 -0800171 fieldDisplayNames: {"name": "Site"},
Scott Baker342d9b92015-01-11 13:44:30 -0800172 });
173
174 XOSTenantApp.tenantSiteListView = tenantSiteListClass;
175
176 XOSTenantApp.tenantSliceSelectorView = SliceSelectorView.extend( {
177 sliceChanged: function(id) {
Scott Bakercd07a592015-01-12 12:37:38 -0800178 XOSTenantApp.navToSlice(id);
Scott Baker342d9b92015-01-11 13:44:30 -0800179 },
180 });
181
182 xos.sites.fetch();
183 xos.slicesPlus.fetch();
184 xos.tenantview.fetch();
185};
186
187make_choices = function(list_of_names, list_of_values) {
188 result = [];
189 if (!list_of_values) {
190 for (index in list_of_names) {
191 displayName = list_of_names[index];
192 result.push( [displayName, displayName] );
193 }
Scott Bakeraba91832015-01-12 13:37:31 -0800194 } else {
195 for (index in list_of_names) {
196 displayName = list_of_names[index];
197 id = list_of_values[index];
198 result.push( [displayName, id] );
199 }
Scott Baker342d9b92015-01-11 13:44:30 -0800200 }
201 return result;
202};
203
Scott Bakercd07a592015-01-12 12:37:38 -0800204XOSTenantApp.navToSlice = function(id) {
205 XOSTenantApp.viewSlice(xos.slicesPlus.get(id));
206};
207
208XOSTenantApp.adjustCollectionField = function(collectionName, id, fieldName, amount) {
209 model = XOSTenantApp[collectionName].get(id);
210 model.set(fieldName, Math.max(model.get(fieldName) + amount, 0));
Scott Bakerf5d97bd2015-01-20 00:04:19 -0800211 XOSTenantApp.setDirty(true);
Scott Bakercd07a592015-01-12 12:37:38 -0800212};
213
214XOSTenantApp.addSlice = function() {
215 var app=this;
Scott Bakerce0dfb82015-01-14 09:54:19 -0800216
217 if (!xos.tenant().current_user_can_create_slice) {
218 window.alert("You do not have sufficient rights to create a slice on your site");
219 return;
220 }
221
Scott Baker97acad92015-01-12 19:45:40 -0800222 model = new xos.slicesPlus.model({site: xos.tenant().current_user_site_id,
Scott Bakerc8984372015-01-19 08:56:16 -0800223 name: xos.tenant().current_user_login_base + "_",
224 creator: xos.tenant().current_user_id});
Scott Bakercd07a592015-01-12 12:37:38 -0800225 console.log(model);
Scott Baker97acad92015-01-12 19:45:40 -0800226 var detailView = new XOSTenantApp.tenantAddView({model: model,
Scott Bakerc8984372015-01-19 08:56:16 -0800227 collection: xos.slicesPlus,
228 noSubmitButton: true,
Scott Baker97acad92015-01-12 19:45:40 -0800229 });
230 detailView.dialog = $("#tenant-addslice-dialog");
Scott Bakercd07a592015-01-12 12:37:38 -0800231 app.tenantAddSliceInterior.show(detailView);
232 $("#tenant-addslice-dialog").dialog({
233 autoOpen: false,
234 modal: true,
235 width: 640,
236 buttons : {
Scott Baker435c2c92015-01-14 00:34:45 -0800237 "Create Slice" : function() {
Scott Bakercd07a592015-01-12 12:37:38 -0800238 var addDialog = this;
Scott Baker97acad92015-01-12 19:45:40 -0800239 console.log("SAVE!!!");
Scott Bakercd07a592015-01-12 12:37:38 -0800240 detailView.synchronous = true;
241 detailView.afterSave = function() { $(addDialog).dialog("close"); XOSTenantApp.navToSlice(detailView.model.id); }
242 detailView.save();
243 },
244 "Cancel" : function() {
245 $(this).dialog("close");
246 }
247 }
248 });
249 $("#tenant-addslice-dialog").dialog("open");
250};
251
Scott Baker97acad92015-01-12 19:45:40 -0800252XOSTenantApp.editUsers = function(model) {
253 var app=this;
254 var detailView = new XOSEditUsersView({model: model, collection: xos.slicesPlus});
255 detailView.dialog = $("#tenant-edit-users-dialog");
256 app.tenantEditUsersInterior.show(detailView);
257 $("#tenant-edit-users-dialog").dialog({
258 autoOpen: false,
259 modal: true,
260 width: 640,
261 buttons : {
Scott Baker435c2c92015-01-14 00:34:45 -0800262 "Ok" : function() {
Scott Baker97acad92015-01-12 19:45:40 -0800263 var editDialog = this;
264 user_ids = all_options($("#tenant-edit-users-dialog").find(".select-picker-to"));
265 user_ids = user_ids.map( function(x) { return parseInt(x,10); } );
Scott Baker5175e8a2015-01-20 00:20:12 -0800266 if (!array_same_elements(user_ids, model.usersBuffer)) {
267 XOSTenantApp.setDirty(true);
268 }
Scott Baker435c2c92015-01-14 00:34:45 -0800269 model.usersBuffer = user_ids;
Scott Baker97acad92015-01-12 19:45:40 -0800270 $(editDialog).dialog("close");
271 },
272 "Cancel" : function() {
273 $(this).dialog("close");
274 }
275 }
276 });
277 $("#tenant-edit-users-dialog").dialog("open");
278};
279
Scott Bakerec930102015-01-20 01:02:08 -0800280XOSTenantApp.downloadSSHOld = function(model) {
281 sshCommands = "";
282 for (index in model.attributes.sliceInfo.sshCommands) {
283 sshCommand = model.attributes.sliceInfo.sshCommands[index];
284 sshCommands = sshCommands + sshCommand + "\n";
285 }
286
287 if (sshCommands.length == 0) {
288 alert("this slice has no instantiated slivers yet");
289 return;
290 }
291
292 var myWindow = window.open("", "ssh_command_list",""); // "width=640, height=480");
293 myWindow.document.write("<html><head><title>SSH Commands</title></head><body><pre>" + sshCommands + "</pre></body></html>");
294 myWindow.document.close();
295};
296
297XOSTenantApp.downloadSSH = function(model) {
298 sshCommands = "";
299 for (index in model.attributes.sliceInfo.sshCommands) {
300 sshCommand = model.attributes.sliceInfo.sshCommands[index];
301 sshCommands = sshCommands + sshCommand + "\n";
302 }
303
304 if (sshCommands.length == 0) {
305 alert("this slice has no instantiated slivers yet");
306 return;
307 }
308
Scott Baker61b5bfc2015-01-20 01:19:12 -0800309 var htmlView = new HTMLView({html: '<pre style="overflow: auto; word-wrap: normal; white-space: pre; word-wrap: normal;">' + sshCommands + '</pre>'});
Scott Bakerec930102015-01-20 01:02:08 -0800310 XOSTenantApp.tenantSSHCommandsInterior.show(htmlView);
311
312 $("#tenant-ssh-commands-dialog").dialog({
313 autoOpen: false,
314 modal: true,
315 width: 640,
316 buttons : {
317 "Ok" : function() {
318 $(this).dialog("close");
319 },
320 }
321 });
322 $("#tenant-ssh-commands-dialog").dialog("open");
323};
324
Scott Baker6b145aa2015-01-12 12:56:25 -0800325XOSTenantApp.deleteSlice = function(model) {
326 var app=this;
327 app.deleteDialog(model, function() { console.log("afterDelete"); app.viewSlice(undefined); });
328};
329
Scott Baker342d9b92015-01-11 13:44:30 -0800330XOSTenantApp.viewSlice = function(model) {
Scott Baker5175e8a2015-01-20 00:20:12 -0800331 if (XOSTenantApp.dirty) {
332 if (!confirm("The current sliver has unsaved data -- view new sliver anyway ?")) {
333 $("#tenantSliceSelector select").val(XOSTenantApp.currentSlice.id);
334 return;
335 }
336 }
337
338 XOSTenantApp.setDirty(false);
339
Scott Baker342d9b92015-01-11 13:44:30 -0800340 if (!model && xos.slicesPlus.models.length > 0) {
341 model = xos.slicesPlus.models[0];
342 }
343
Scott Bakerb52f7af2015-01-13 14:41:41 -0800344 if (model) {
345 sliceSelector = new XOSTenantApp.tenantSliceSelectorView({collection: xos.slicesPlus,
346 selectedID: model ? model.id : null,
347 } );
348 XOSTenantApp.sliceSelector = sliceSelector;
349 XOSTenantApp.tenantSliceSelector.show(sliceSelector);
Scott Baker342d9b92015-01-11 13:44:30 -0800350
Scott Bakerb52f7af2015-01-13 14:41:41 -0800351 tenantSummary = new XOSTenantApp.tenantSummaryView({model: model,
352 choices: {mount_data_sets: make_choices(xos.tenant().public_volume_names, null),
353 serviceClass: make_choices(xos.tenant().blessed_service_class_names, xos.tenant().blessed_service_classes),
354 default_image: make_choices(xos.tenant().blessed_image_names, xos.tenant().blessed_images),
355 default_flavor: make_choices(xos.tenant().blessed_flavor_names, xos.tenant().blessed_flavors),},
356 });
357 XOSTenantApp.tenantSummary.show(tenantSummary);
Scott Baker342d9b92015-01-11 13:44:30 -0800358
Scott Bakerb52f7af2015-01-13 14:41:41 -0800359 tenantSites = new XOSTenantSiteCollection();
360 tenantSites.getFromSlice(model);
Scott Baker435c2c92015-01-14 00:34:45 -0800361 model.usersBuffer = model.attributes.users; /* save a copy of 'users' that we can edit. This prevents another view (developer) from overwriting our copy with a fetch from the server */
Scott Baker12154242015-01-16 19:26:54 -0800362 model.usersOrig = model.attributes.users; /* save an immutable copy that we'll use for username lookups */
363 model.user_namesOrig = model.attributes.user_names;
Scott Bakerb52f7af2015-01-13 14:41:41 -0800364 model.tenantSiteCollection = tenantSites;
365 XOSTenantApp.tenantSites = tenantSites;
Scott Baker342d9b92015-01-11 13:44:30 -0800366
Scott Bakerb52f7af2015-01-13 14:41:41 -0800367 tenantSiteList = new XOSTenantApp.tenantSiteListView({collection: tenantSites });
368 XOSTenantApp.tenantSiteList.show(tenantSiteList);
369 // on xos.slicePlus.sort, need to update xostenantapp.tenantSites
Scott Baker342d9b92015-01-11 13:44:30 -0800370
Scott Bakerb52f7af2015-01-13 14:41:41 -0800371 XOSTenantApp.tenantButtons.show( new XOSTenantButtonView( { app: XOSTenantApp,
372 linkedView: tenantSummary } ) );
Scott Baker5175e8a2015-01-20 00:20:12 -0800373
374 XOSTenantApp.currentSlice = model;
Scott Bakerb52f7af2015-01-13 14:41:41 -0800375 } else {
376 XOSTenantApp.tenantSliceSelector.show(new HTMLView({html: ""}));
377 XOSTenantApp.tenantSummary.show(new HTMLView({html: "You have no slices"}));
378 XOSTenantApp.tenantSiteList.show(new HTMLView({html: ""}));
379 XOSTenantApp.tenantButtons.show( new XOSTenantButtonView( { template: "#xos-tenant-buttons-noslice-template",
380 app: XOSTenantApp,
381 linkedView: tenantSummary } ) );
382 }
Scott Baker342d9b92015-01-11 13:44:30 -0800383};
384
Scott Bakeraba91832015-01-12 13:37:31 -0800385XOSTenantApp.sanityCheck = function() {
386 errors = [];
387 if (xos.tenant().blessed_service_classes.length == 0) {
388 errors.push("no blessed service classes");
389 }
390 if (xos.tenant().blessed_flavors.length == 0) {
391 errors.push("no blessed flavors");
392 }
393 if (xos.tenant().blessed_images.length == 0) {
394 errors.push("no blessed images");
395 }
396 if (xos.tenant().blessed_sites.length == 0) {
397 errors.push("no blessed sites");
398 }
Scott Baker9eebcb62015-01-18 17:04:35 -0800399 if (xos.tenant().current_user_site_id == null) {
400 errors.push("current user does not have a site");
401 }
Scott Bakeraba91832015-01-12 13:37:31 -0800402
403 if (errors.length > 0) {
404 $("#tenantSummary").html("Tenant view sanity check failed<br>" + errors.join("<br>"));
405 return false;
406 }
407
408 return true;
Scott Baker342d9b92015-01-11 13:44:30 -0800409}
410
411XOSTenantApp.collectionLoadChange = function() {
412 stats = xos.getCollectionStatus();
413
414 if (!XOSTenantApp.navigationStarted) {
415 if (stats["isLoaded"] + stats["failedLoad"] >= stats["startedLoad"]) {
Scott Bakeraba91832015-01-12 13:37:31 -0800416 if (XOSTenantApp.sanityCheck()) {
417 XOSTenantApp.viewSlice(undefined);
418 }
Scott Baker342d9b92015-01-11 13:44:30 -0800419 } else {
420 $("#tenantSummary").html("<h3>Loading...</h3><div id='xos-startup-progress'></div>");
421 $("#xos-startup-progress").progressbar({value: stats["completedLoad"], max: stats["startedLoad"]});
422 }
423 }
424};
425
426XOSTenantApp.on("start", function() {
427 XOSTenantApp.buildViews();
428
Scott Baker342d9b92015-01-11 13:44:30 -0800429 // fire it once to initially show the progress bar
430 XOSTenantApp.collectionLoadChange();
431
432 // fire it each time the collection load status is updated
433 Backbone.on("xoslib:collectionLoadChange", XOSTenantApp.collectionLoadChange);
434});
435
436$(document).ready(function(){
437 XOSTenantApp.start();
438});
439