blob: 4225caccbc54bf9e311c96361ba755d8d26d8348 [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( {
Scott Baker21909342015-01-22 15:21:24 -08008 listFields: ["name", "allocated", "ready"],
Scott Baker342d9b92015-01-11 13:44:30 -08009 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];
Scott Baker21909342015-01-22 15:21:24 -080017 ready = slice.attributes.site_ready[siteName] || 0;
18 tenantSites.push(new XOSTenantSite( { name: siteName, allocated: allocated, ready: ready, id: id} ));
Scott Baker342d9b92015-01-11 13:44:30 -080019 id = id + 1;
20 }
21 for (index in xos.tenantview.models[0].attributes.blessed_site_names) {
22 siteName = xos.tenantview.models[0].attributes.blessed_site_names[index];
23 if (! (siteName in slice.attributes.site_allocation)) {
Scott Baker21909342015-01-22 15:21:24 -080024 tenantSites.push(new XOSTenantSite( { name: siteName, allocated: 0, ready: 0, id: id} ));
Scott Baker342d9b92015-01-11 13:44:30 -080025 id = id + 1;
26 }
27 }
28 this.set(tenantSites);
Scott Baker21909342015-01-22 15:21:24 -080029
30 var that=this;
31 this.listenTo(slice, 'change', function() { that.getReadyFromSlice(slice); })
32 },
33
34 getReadyFromSlice: function(slice) {
35 for (siteName in slice.attributes.site_ready) {
36 ready = slice.attributes.site_ready[siteName];
37 for (index in this.models) {
38 tenantSite = this.models[index];
39 if (tenantSite.attributes.name == siteName) {
40 tenantSite.set("ready", ready);
41 }
42 }
43 }
Scott Baker342d9b92015-01-11 13:44:30 -080044 },
Scott Baker97acad92015-01-12 19:45:40 -080045
46 putToSlice: function(slice) {
47 slice.attributes.site_allocation = {};
48 for (index in this.models) {
Scott Baker435c2c92015-01-14 00:34:45 -080049 var model = this.models[index];
Scott Baker97acad92015-01-12 19:45:40 -080050 slice.attributes.site_allocation[ model.attributes.name ] = model.attributes.allocated;
51 }
52 },
Scott Baker342d9b92015-01-11 13:44:30 -080053});
54
Scott Baker97acad92015-01-12 19:45:40 -080055XOSEditUsersView = Marionette.ItemView.extend({
56 template: "#tenant-edit-users",
57 viewInitializers: [],
58
59 onShow: function() {
60 _.each(this.viewInitializers, function(initializer) {
61 initializer();
62 });
63 },
64
65 templateHelpers: function() { return { detailView: this, model: this.model }; },
66
67 });
68
Scott Bakerf5d97bd2015-01-20 00:04:19 -080069XOSTenantSummaryView = XOSDetailView.extend({
70 events: {"change": "onChange"},
71
72 onChange: function(e) {
73 XOSTenantApp.setDirty(true);
74 },
75
76 saveSuccess: function() {
77 console.log("saveSuccess!");
78 XOSTenantApp.setDirty(false);
79 },
80
81 });
82
Scott Baker97acad92015-01-12 19:45:40 -080083
Scott Baker342d9b92015-01-11 13:44:30 -080084XOSTenantButtonView = Marionette.ItemView.extend({
85 template: "#xos-tenant-buttons-template",
86
87 events: {"click button.btn-tenant-create": "createClicked",
88 "click button.btn-tenant-delete": "deleteClicked",
89 "click button.btn-tenant-add-user": "addUserClicked",
90 "click button.btn-tenant-save": "saveClicked",
Scott Bakerec930102015-01-20 01:02:08 -080091 "click button.btn-tenant-download-ssh": "downloadClicked",
Scott Baker342d9b92015-01-11 13:44:30 -080092 },
93
94 createClicked: function(e) {
Scott Bakercd07a592015-01-12 12:37:38 -080095 XOSTenantApp.addSlice();
Scott Baker342d9b92015-01-11 13:44:30 -080096 },
97
98 deleteClicked: function(e) {
Scott Baker6b145aa2015-01-12 12:56:25 -080099 XOSTenantApp.deleteSlice(this.options.linkedView.model);
Scott Baker342d9b92015-01-11 13:44:30 -0800100 },
101
102 addUserClicked: function(e) {
Scott Baker97acad92015-01-12 19:45:40 -0800103 XOSTenantApp.editUsers(this.options.linkedView.model);
Scott Baker342d9b92015-01-11 13:44:30 -0800104 },
105
Scott Bakerec930102015-01-20 01:02:08 -0800106 downloadClicked: function(e) {
107 XOSTenantApp.downloadSSH(this.options.linkedView.model);
108 },
109
Scott Baker342d9b92015-01-11 13:44:30 -0800110 saveClicked: function(e) {
Scott Baker97acad92015-01-12 19:45:40 -0800111 model = this.options.linkedView.model;
112 model.tenantSiteCollection.putToSlice(model);
Scott Baker435c2c92015-01-14 00:34:45 -0800113 model.attributes.users = model.usersBuffer;
Scott Bakerf5d97bd2015-01-20 00:04:19 -0800114
115 e.preventDefault();
116 this.options.linkedView.save();
117 //this.options.linkedView.submitContinueClicked.call(this.options.linkedView, e);
118 //XOSTenantApp.setDirty(false);
Scott Baker342d9b92015-01-11 13:44:30 -0800119 },
120 });
121
122XOSTenantApp = new XOSApplication({
123 logTableId: "#logTable",
124 statusMsgId: "#statusMsg",
Scott Bakercd07a592015-01-12 12:37:38 -0800125 hideTabsByDefault: true,
Scott Baker5175e8a2015-01-20 00:20:12 -0800126 dirty: false,
Scott Bakercd07a592015-01-12 12:37:38 -0800127 varName: "XOSTenantApp",
Scott Baker342d9b92015-01-11 13:44:30 -0800128});
129
130XOSTenantApp.addRegions({
131 tenantSliceSelector: "#tenantSliceSelector",
132 tenantSummary: "#tenantSummary",
133 tenantSiteList: "#tenantSiteList",
134 tenantButtons: "#tenantButtons",
Scott Bakercd07a592015-01-12 12:37:38 -0800135 tenantAddSliceInterior: "#tenant-addslice-interior",
Scott Baker97acad92015-01-12 19:45:40 -0800136 tenantEditUsersInterior: "#tenant-edit-users-interior",
Scott Bakerec930102015-01-20 01:02:08 -0800137 tenantSSHCommandsInterior: "#tenant-ssh-commands-interior",
Scott Baker342d9b92015-01-11 13:44:30 -0800138});
139
Scott Bakerf5d97bd2015-01-20 00:04:19 -0800140XOSTenantApp.setDirty = function(dirty) {
Scott Baker5175e8a2015-01-20 00:20:12 -0800141 XOSTenantApp.dirty = dirty;
Scott Bakerf5d97bd2015-01-20 00:04:19 -0800142 if (dirty) {
143 $("button.btn-tenant-save").addClass("btn-success");
144 } else {
145 $("button.btn-tenant-save").removeClass("btn-success");
146 }
147};
148
Scott Bakercd07a592015-01-12 12:37:38 -0800149XOSTenantApp.buildViews = function() {
Scott Baker342d9b92015-01-11 13:44:30 -0800150 XOSTenantApp.tenantSites = new XOSTenantSiteCollection();
151
Scott Bakerf5d97bd2015-01-20 00:04:19 -0800152 tenantSummaryClass = XOSTenantSummaryView.extend({template: "#xos-detail-template",
Scott Baker342d9b92015-01-11 13:44:30 -0800153 app: XOSTenantApp,
Scott Baker21909342015-01-22 15:21:24 -0800154 detailFields: ["serviceClass", "default_image", "default_flavor", "network_ports"],
Scott Bakere56d3272015-01-14 00:47:50 -0800155 fieldDisplayNames: {serviceClass: "Service Level", "default_flavor": "Flavor", "default_image": "Image", "mount_data_sets": "Data Sets"},
Scott Bakerc3876fb2015-01-20 12:37:36 -0800156 helpText: {"serviceClass": "Existing slivers will be re-instantiated if changed",
157 "default_image": "Existing slivers will be re-instantiated if changed",
Scott Baker51bacad2015-01-20 12:24:16 -0800158 "default_flavor": "Existing slivers will be re-instantiated if changed"},
Scott Bakere7a90452015-01-14 17:07:30 -0800159
160 onShow: function() {
161 // the slice selector is in a different table, so make every label cell the maximal width
162 make_same_width("#xos-tenant-view-panel", ".xos-label-cell");
163 },
Scott Baker97acad92015-01-12 19:45:40 -0800164 });
Scott Baker342d9b92015-01-11 13:44:30 -0800165
166 XOSTenantApp.tenantSummaryView = tenantSummaryClass;
167
Scott Bakercd07a592015-01-12 12:37:38 -0800168 tenantAddClass = XOSDetailView.extend({template: "#xos-detail-template",
169 app: XOSTenantApp,
170 detailFields: ["name", "description"]});
171
172 XOSTenantApp.tenantAddView = tenantAddClass;
173
Scott Baker342d9b92015-01-11 13:44:30 -0800174 tenantSiteItemClass = XOSItemView.extend({template: "#xos-listitem-template",
175 app: XOSTenantApp});
176
177 XOSTenantApp.tenantSiteItemView = tenantSiteItemClass;
178
179 tenantSiteListClass = XOSDataTableView.extend({template: "#xos-list-template",
180 app: XOSTenantApp,
181 childView: tenantSiteItemClass,
182 collection: XOSTenantApp.tenantSites,
183 title: "sites",
184 inputType: {allocated: "spinner"},
185 noDeleteColumn: true,
Scott Bakere56d3272015-01-14 00:47:50 -0800186 disablePaginate: true,
187 disableFilter: true,
Scott Bakere7a90452015-01-14 17:07:30 -0800188 fieldDisplayNames: {"name": "Site"},
Scott Baker342d9b92015-01-11 13:44:30 -0800189 });
190
191 XOSTenantApp.tenantSiteListView = tenantSiteListClass;
192
193 XOSTenantApp.tenantSliceSelectorView = SliceSelectorView.extend( {
194 sliceChanged: function(id) {
Scott Bakercd07a592015-01-12 12:37:38 -0800195 XOSTenantApp.navToSlice(id);
Scott Baker342d9b92015-01-11 13:44:30 -0800196 },
Scott Bakere0e407a2015-01-20 17:02:53 -0800197 filter: function(slice) {
198 return slice.attributes.current_user_can_see;
199 },
Scott Baker342d9b92015-01-11 13:44:30 -0800200 });
201
202 xos.sites.fetch();
203 xos.slicesPlus.fetch();
204 xos.tenantview.fetch();
205};
206
207make_choices = function(list_of_names, list_of_values) {
208 result = [];
209 if (!list_of_values) {
210 for (index in list_of_names) {
211 displayName = list_of_names[index];
212 result.push( [displayName, displayName] );
213 }
Scott Bakeraba91832015-01-12 13:37:31 -0800214 } else {
215 for (index in list_of_names) {
216 displayName = list_of_names[index];
217 id = list_of_values[index];
218 result.push( [displayName, id] );
219 }
Scott Baker342d9b92015-01-11 13:44:30 -0800220 }
221 return result;
222};
223
Scott Bakercd07a592015-01-12 12:37:38 -0800224XOSTenantApp.navToSlice = function(id) {
225 XOSTenantApp.viewSlice(xos.slicesPlus.get(id));
226};
227
228XOSTenantApp.adjustCollectionField = function(collectionName, id, fieldName, amount) {
229 model = XOSTenantApp[collectionName].get(id);
230 model.set(fieldName, Math.max(model.get(fieldName) + amount, 0));
Scott Bakerf5d97bd2015-01-20 00:04:19 -0800231 XOSTenantApp.setDirty(true);
Scott Bakercd07a592015-01-12 12:37:38 -0800232};
233
234XOSTenantApp.addSlice = function() {
235 var app=this;
Scott Bakerce0dfb82015-01-14 09:54:19 -0800236
237 if (!xos.tenant().current_user_can_create_slice) {
238 window.alert("You do not have sufficient rights to create a slice on your site");
239 return;
240 }
241
Scott Baker97acad92015-01-12 19:45:40 -0800242 model = new xos.slicesPlus.model({site: xos.tenant().current_user_site_id,
Scott Bakerc8984372015-01-19 08:56:16 -0800243 name: xos.tenant().current_user_login_base + "_",
244 creator: xos.tenant().current_user_id});
Scott Bakercd07a592015-01-12 12:37:38 -0800245 console.log(model);
Scott Baker97acad92015-01-12 19:45:40 -0800246 var detailView = new XOSTenantApp.tenantAddView({model: model,
Scott Bakerc8984372015-01-19 08:56:16 -0800247 collection: xos.slicesPlus,
248 noSubmitButton: true,
Scott Baker97acad92015-01-12 19:45:40 -0800249 });
250 detailView.dialog = $("#tenant-addslice-dialog");
Scott Bakercd07a592015-01-12 12:37:38 -0800251 app.tenantAddSliceInterior.show(detailView);
252 $("#tenant-addslice-dialog").dialog({
253 autoOpen: false,
254 modal: true,
255 width: 640,
256 buttons : {
Scott Baker435c2c92015-01-14 00:34:45 -0800257 "Create Slice" : function() {
Scott Bakercd07a592015-01-12 12:37:38 -0800258 var addDialog = this;
Scott Baker97acad92015-01-12 19:45:40 -0800259 console.log("SAVE!!!");
Scott Bakercd07a592015-01-12 12:37:38 -0800260 detailView.synchronous = true;
261 detailView.afterSave = function() { $(addDialog).dialog("close"); XOSTenantApp.navToSlice(detailView.model.id); }
262 detailView.save();
263 },
264 "Cancel" : function() {
265 $(this).dialog("close");
266 }
267 }
268 });
269 $("#tenant-addslice-dialog").dialog("open");
270};
271
Scott Baker97acad92015-01-12 19:45:40 -0800272XOSTenantApp.editUsers = function(model) {
273 var app=this;
274 var detailView = new XOSEditUsersView({model: model, collection: xos.slicesPlus});
275 detailView.dialog = $("#tenant-edit-users-dialog");
276 app.tenantEditUsersInterior.show(detailView);
277 $("#tenant-edit-users-dialog").dialog({
278 autoOpen: false,
279 modal: true,
280 width: 640,
281 buttons : {
Scott Baker435c2c92015-01-14 00:34:45 -0800282 "Ok" : function() {
Scott Baker97acad92015-01-12 19:45:40 -0800283 var editDialog = this;
284 user_ids = all_options($("#tenant-edit-users-dialog").find(".select-picker-to"));
285 user_ids = user_ids.map( function(x) { return parseInt(x,10); } );
Scott Baker5175e8a2015-01-20 00:20:12 -0800286 if (!array_same_elements(user_ids, model.usersBuffer)) {
287 XOSTenantApp.setDirty(true);
288 }
Scott Baker435c2c92015-01-14 00:34:45 -0800289 model.usersBuffer = user_ids;
Scott Baker97acad92015-01-12 19:45:40 -0800290 $(editDialog).dialog("close");
291 },
292 "Cancel" : function() {
293 $(this).dialog("close");
294 }
295 }
296 });
297 $("#tenant-edit-users-dialog").dialog("open");
298};
299
Scott Bakerec930102015-01-20 01:02:08 -0800300XOSTenantApp.downloadSSH = function(model) {
Scott Baker9e0ea232015-01-25 17:04:00 -0800301 var sshCommands = "";
Scott Bakerec930102015-01-20 01:02:08 -0800302 for (index in model.attributes.sliceInfo.sshCommands) {
303 sshCommand = model.attributes.sliceInfo.sshCommands[index];
304 sshCommands = sshCommands + sshCommand + "\n";
305 }
306
307 if (sshCommands.length == 0) {
308 alert("this slice has no instantiated slivers yet");
309 return;
310 }
311
Scott Baker61b5bfc2015-01-20 01:19:12 -0800312 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 -0800313 XOSTenantApp.tenantSSHCommandsInterior.show(htmlView);
314
315 $("#tenant-ssh-commands-dialog").dialog({
316 autoOpen: false,
317 modal: true,
318 width: 640,
319 buttons : {
Scott Baker9e0ea232015-01-25 17:04:00 -0800320 "Download": function() {
321 var dlLink = document.createElement('a');
322 dlLink.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(sshCommands));
323 dlLink.setAttribute('download', 'sshcommands.txt');
324 dlLink.click();
325
326 //window.open('data:text/text,' + encodeURIComponent(sshCommands));
327 },
328 "Close" : function() {
Scott Bakerec930102015-01-20 01:02:08 -0800329 $(this).dialog("close");
330 },
331 }
332 });
333 $("#tenant-ssh-commands-dialog").dialog("open");
334};
335
Scott Baker6b145aa2015-01-12 12:56:25 -0800336XOSTenantApp.deleteSlice = function(model) {
337 var app=this;
338 app.deleteDialog(model, function() { console.log("afterDelete"); app.viewSlice(undefined); });
339};
340
Scott Baker342d9b92015-01-11 13:44:30 -0800341XOSTenantApp.viewSlice = function(model) {
Scott Baker5175e8a2015-01-20 00:20:12 -0800342 if (XOSTenantApp.dirty) {
343 if (!confirm("The current sliver has unsaved data -- view new sliver anyway ?")) {
344 $("#tenantSliceSelector select").val(XOSTenantApp.currentSlice.id);
345 return;
346 }
347 }
348
349 XOSTenantApp.setDirty(false);
350
Scott Baker342d9b92015-01-11 13:44:30 -0800351 if (!model && xos.slicesPlus.models.length > 0) {
352 model = xos.slicesPlus.models[0];
353 }
354
Scott Bakerb52f7af2015-01-13 14:41:41 -0800355 if (model) {
356 sliceSelector = new XOSTenantApp.tenantSliceSelectorView({collection: xos.slicesPlus,
357 selectedID: model ? model.id : null,
358 } );
359 XOSTenantApp.sliceSelector = sliceSelector;
360 XOSTenantApp.tenantSliceSelector.show(sliceSelector);
Scott Baker342d9b92015-01-11 13:44:30 -0800361
Scott Bakerb52f7af2015-01-13 14:41:41 -0800362 tenantSummary = new XOSTenantApp.tenantSummaryView({model: model,
363 choices: {mount_data_sets: make_choices(xos.tenant().public_volume_names, null),
364 serviceClass: make_choices(xos.tenant().blessed_service_class_names, xos.tenant().blessed_service_classes),
365 default_image: make_choices(xos.tenant().blessed_image_names, xos.tenant().blessed_images),
366 default_flavor: make_choices(xos.tenant().blessed_flavor_names, xos.tenant().blessed_flavors),},
367 });
368 XOSTenantApp.tenantSummary.show(tenantSummary);
Scott Baker342d9b92015-01-11 13:44:30 -0800369
Scott Bakerb52f7af2015-01-13 14:41:41 -0800370 tenantSites = new XOSTenantSiteCollection();
371 tenantSites.getFromSlice(model);
Scott Baker435c2c92015-01-14 00:34:45 -0800372 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 -0800373 model.usersOrig = model.attributes.users; /* save an immutable copy that we'll use for username lookups */
374 model.user_namesOrig = model.attributes.user_names;
Scott Bakerb52f7af2015-01-13 14:41:41 -0800375 model.tenantSiteCollection = tenantSites;
376 XOSTenantApp.tenantSites = tenantSites;
Scott Baker342d9b92015-01-11 13:44:30 -0800377
Scott Bakerb52f7af2015-01-13 14:41:41 -0800378 tenantSiteList = new XOSTenantApp.tenantSiteListView({collection: tenantSites });
379 XOSTenantApp.tenantSiteList.show(tenantSiteList);
380 // on xos.slicePlus.sort, need to update xostenantapp.tenantSites
Scott Baker342d9b92015-01-11 13:44:30 -0800381
Scott Bakerb52f7af2015-01-13 14:41:41 -0800382 XOSTenantApp.tenantButtons.show( new XOSTenantButtonView( { app: XOSTenantApp,
383 linkedView: tenantSummary } ) );
Scott Baker5175e8a2015-01-20 00:20:12 -0800384
385 XOSTenantApp.currentSlice = model;
Scott Bakerb52f7af2015-01-13 14:41:41 -0800386 } else {
387 XOSTenantApp.tenantSliceSelector.show(new HTMLView({html: ""}));
388 XOSTenantApp.tenantSummary.show(new HTMLView({html: "You have no slices"}));
389 XOSTenantApp.tenantSiteList.show(new HTMLView({html: ""}));
390 XOSTenantApp.tenantButtons.show( new XOSTenantButtonView( { template: "#xos-tenant-buttons-noslice-template",
391 app: XOSTenantApp,
392 linkedView: tenantSummary } ) );
393 }
Scott Baker342d9b92015-01-11 13:44:30 -0800394};
395
Scott Bakeraba91832015-01-12 13:37:31 -0800396XOSTenantApp.sanityCheck = function() {
397 errors = [];
398 if (xos.tenant().blessed_service_classes.length == 0) {
399 errors.push("no blessed service classes");
400 }
401 if (xos.tenant().blessed_flavors.length == 0) {
402 errors.push("no blessed flavors");
403 }
404 if (xos.tenant().blessed_images.length == 0) {
405 errors.push("no blessed images");
406 }
407 if (xos.tenant().blessed_sites.length == 0) {
408 errors.push("no blessed sites");
409 }
Scott Baker9eebcb62015-01-18 17:04:35 -0800410 if (xos.tenant().current_user_site_id == null) {
411 errors.push("current user does not have a site");
412 }
Scott Bakeraba91832015-01-12 13:37:31 -0800413
414 if (errors.length > 0) {
415 $("#tenantSummary").html("Tenant view sanity check failed<br>" + errors.join("<br>"));
416 return false;
417 }
418
419 return true;
Scott Baker342d9b92015-01-11 13:44:30 -0800420}
421
422XOSTenantApp.collectionLoadChange = function() {
423 stats = xos.getCollectionStatus();
424
425 if (!XOSTenantApp.navigationStarted) {
426 if (stats["isLoaded"] + stats["failedLoad"] >= stats["startedLoad"]) {
Scott Bakeraba91832015-01-12 13:37:31 -0800427 if (XOSTenantApp.sanityCheck()) {
428 XOSTenantApp.viewSlice(undefined);
429 }
Scott Baker342d9b92015-01-11 13:44:30 -0800430 } else {
431 $("#tenantSummary").html("<h3>Loading...</h3><div id='xos-startup-progress'></div>");
432 $("#xos-startup-progress").progressbar({value: stats["completedLoad"], max: stats["startedLoad"]});
433 }
434 }
435};
436
437XOSTenantApp.on("start", function() {
438 XOSTenantApp.buildViews();
439
Scott Baker342d9b92015-01-11 13:44:30 -0800440 // fire it once to initially show the progress bar
441 XOSTenantApp.collectionLoadChange();
442
443 // fire it each time the collection load status is updated
444 Backbone.on("xoslib:collectionLoadChange", XOSTenantApp.collectionLoadChange);
445});
446
447$(document).ready(function(){
448 XOSTenantApp.start();
449});
450