blob: 286537db1138a953573bb93393594dd64af662fd [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",
75 },
76
77 createClicked: function(e) {
Scott Bakercd07a592015-01-12 12:37:38 -080078 XOSTenantApp.addSlice();
Scott Baker342d9b92015-01-11 13:44:30 -080079 },
80
81 deleteClicked: function(e) {
Scott Baker6b145aa2015-01-12 12:56:25 -080082 XOSTenantApp.deleteSlice(this.options.linkedView.model);
Scott Baker342d9b92015-01-11 13:44:30 -080083 },
84
85 addUserClicked: function(e) {
Scott Baker97acad92015-01-12 19:45:40 -080086 XOSTenantApp.editUsers(this.options.linkedView.model);
Scott Baker342d9b92015-01-11 13:44:30 -080087 },
88
89 saveClicked: function(e) {
Scott Baker97acad92015-01-12 19:45:40 -080090 model = this.options.linkedView.model;
91 model.tenantSiteCollection.putToSlice(model);
Scott Baker435c2c92015-01-14 00:34:45 -080092 model.attributes.users = model.usersBuffer;
Scott Bakerf5d97bd2015-01-20 00:04:19 -080093
94 e.preventDefault();
95 this.options.linkedView.save();
96 //this.options.linkedView.submitContinueClicked.call(this.options.linkedView, e);
97 //XOSTenantApp.setDirty(false);
Scott Baker342d9b92015-01-11 13:44:30 -080098 },
99 });
100
101XOSTenantApp = new XOSApplication({
102 logTableId: "#logTable",
103 statusMsgId: "#statusMsg",
Scott Bakercd07a592015-01-12 12:37:38 -0800104 hideTabsByDefault: true,
Scott Baker5175e8a2015-01-20 00:20:12 -0800105 dirty: false,
Scott Bakercd07a592015-01-12 12:37:38 -0800106 varName: "XOSTenantApp",
Scott Baker342d9b92015-01-11 13:44:30 -0800107});
108
109XOSTenantApp.addRegions({
110 tenantSliceSelector: "#tenantSliceSelector",
111 tenantSummary: "#tenantSummary",
112 tenantSiteList: "#tenantSiteList",
113 tenantButtons: "#tenantButtons",
Scott Bakercd07a592015-01-12 12:37:38 -0800114 tenantAddSliceInterior: "#tenant-addslice-interior",
Scott Baker97acad92015-01-12 19:45:40 -0800115 tenantEditUsersInterior: "#tenant-edit-users-interior",
Scott Baker342d9b92015-01-11 13:44:30 -0800116});
117
Scott Bakerf5d97bd2015-01-20 00:04:19 -0800118XOSTenantApp.setDirty = function(dirty) {
Scott Baker5175e8a2015-01-20 00:20:12 -0800119 XOSTenantApp.dirty = dirty;
Scott Bakerf5d97bd2015-01-20 00:04:19 -0800120 if (dirty) {
121 $("button.btn-tenant-save").addClass("btn-success");
122 } else {
123 $("button.btn-tenant-save").removeClass("btn-success");
124 }
125};
126
Scott Bakercd07a592015-01-12 12:37:38 -0800127XOSTenantApp.buildViews = function() {
Scott Baker342d9b92015-01-11 13:44:30 -0800128 XOSTenantApp.tenantSites = new XOSTenantSiteCollection();
129
Scott Bakerf5d97bd2015-01-20 00:04:19 -0800130 tenantSummaryClass = XOSTenantSummaryView.extend({template: "#xos-detail-template",
Scott Baker342d9b92015-01-11 13:44:30 -0800131 app: XOSTenantApp,
Scott Baker97acad92015-01-12 19:45:40 -0800132 detailFields: ["serviceClass", "default_image", "default_flavor", "network_ports", "mount_data_sets"],
Scott Bakere56d3272015-01-14 00:47:50 -0800133 fieldDisplayNames: {serviceClass: "Service Level", "default_flavor": "Flavor", "default_image": "Image", "mount_data_sets": "Data Sets"},
Scott Bakere7a90452015-01-14 17:07:30 -0800134
135 onShow: function() {
136 // the slice selector is in a different table, so make every label cell the maximal width
137 make_same_width("#xos-tenant-view-panel", ".xos-label-cell");
138 },
Scott Baker97acad92015-01-12 19:45:40 -0800139 });
Scott Baker342d9b92015-01-11 13:44:30 -0800140
141 XOSTenantApp.tenantSummaryView = tenantSummaryClass;
142
Scott Bakercd07a592015-01-12 12:37:38 -0800143 tenantAddClass = XOSDetailView.extend({template: "#xos-detail-template",
144 app: XOSTenantApp,
145 detailFields: ["name", "description"]});
146
147 XOSTenantApp.tenantAddView = tenantAddClass;
148
Scott Baker342d9b92015-01-11 13:44:30 -0800149 tenantSiteItemClass = XOSItemView.extend({template: "#xos-listitem-template",
150 app: XOSTenantApp});
151
152 XOSTenantApp.tenantSiteItemView = tenantSiteItemClass;
153
154 tenantSiteListClass = XOSDataTableView.extend({template: "#xos-list-template",
155 app: XOSTenantApp,
156 childView: tenantSiteItemClass,
157 collection: XOSTenantApp.tenantSites,
158 title: "sites",
159 inputType: {allocated: "spinner"},
160 noDeleteColumn: true,
Scott Bakere56d3272015-01-14 00:47:50 -0800161 disablePaginate: true,
162 disableFilter: true,
Scott Bakere7a90452015-01-14 17:07:30 -0800163 fieldDisplayNames: {"name": "Site"},
Scott Baker342d9b92015-01-11 13:44:30 -0800164 });
165
166 XOSTenantApp.tenantSiteListView = tenantSiteListClass;
167
168 XOSTenantApp.tenantSliceSelectorView = SliceSelectorView.extend( {
169 sliceChanged: function(id) {
Scott Bakercd07a592015-01-12 12:37:38 -0800170 XOSTenantApp.navToSlice(id);
Scott Baker342d9b92015-01-11 13:44:30 -0800171 },
172 });
173
174 xos.sites.fetch();
175 xos.slicesPlus.fetch();
176 xos.tenantview.fetch();
177};
178
179make_choices = function(list_of_names, list_of_values) {
180 result = [];
181 if (!list_of_values) {
182 for (index in list_of_names) {
183 displayName = list_of_names[index];
184 result.push( [displayName, displayName] );
185 }
Scott Bakeraba91832015-01-12 13:37:31 -0800186 } else {
187 for (index in list_of_names) {
188 displayName = list_of_names[index];
189 id = list_of_values[index];
190 result.push( [displayName, id] );
191 }
Scott Baker342d9b92015-01-11 13:44:30 -0800192 }
193 return result;
194};
195
Scott Bakercd07a592015-01-12 12:37:38 -0800196XOSTenantApp.navToSlice = function(id) {
197 XOSTenantApp.viewSlice(xos.slicesPlus.get(id));
198};
199
200XOSTenantApp.adjustCollectionField = function(collectionName, id, fieldName, amount) {
201 model = XOSTenantApp[collectionName].get(id);
202 model.set(fieldName, Math.max(model.get(fieldName) + amount, 0));
Scott Bakerf5d97bd2015-01-20 00:04:19 -0800203 XOSTenantApp.setDirty(true);
Scott Bakercd07a592015-01-12 12:37:38 -0800204};
205
206XOSTenantApp.addSlice = function() {
207 var app=this;
Scott Bakerce0dfb82015-01-14 09:54:19 -0800208
209 if (!xos.tenant().current_user_can_create_slice) {
210 window.alert("You do not have sufficient rights to create a slice on your site");
211 return;
212 }
213
Scott Baker97acad92015-01-12 19:45:40 -0800214 model = new xos.slicesPlus.model({site: xos.tenant().current_user_site_id,
Scott Bakerc8984372015-01-19 08:56:16 -0800215 name: xos.tenant().current_user_login_base + "_",
216 creator: xos.tenant().current_user_id});
Scott Bakercd07a592015-01-12 12:37:38 -0800217 console.log(model);
Scott Baker97acad92015-01-12 19:45:40 -0800218 var detailView = new XOSTenantApp.tenantAddView({model: model,
Scott Bakerc8984372015-01-19 08:56:16 -0800219 collection: xos.slicesPlus,
220 noSubmitButton: true,
Scott Baker97acad92015-01-12 19:45:40 -0800221 });
222 detailView.dialog = $("#tenant-addslice-dialog");
Scott Bakercd07a592015-01-12 12:37:38 -0800223 app.tenantAddSliceInterior.show(detailView);
224 $("#tenant-addslice-dialog").dialog({
225 autoOpen: false,
226 modal: true,
227 width: 640,
228 buttons : {
Scott Baker435c2c92015-01-14 00:34:45 -0800229 "Create Slice" : function() {
Scott Bakercd07a592015-01-12 12:37:38 -0800230 var addDialog = this;
Scott Baker97acad92015-01-12 19:45:40 -0800231 console.log("SAVE!!!");
Scott Bakercd07a592015-01-12 12:37:38 -0800232 detailView.synchronous = true;
233 detailView.afterSave = function() { $(addDialog).dialog("close"); XOSTenantApp.navToSlice(detailView.model.id); }
234 detailView.save();
235 },
236 "Cancel" : function() {
237 $(this).dialog("close");
238 }
239 }
240 });
241 $("#tenant-addslice-dialog").dialog("open");
242};
243
Scott Baker97acad92015-01-12 19:45:40 -0800244XOSTenantApp.editUsers = function(model) {
245 var app=this;
246 var detailView = new XOSEditUsersView({model: model, collection: xos.slicesPlus});
247 detailView.dialog = $("#tenant-edit-users-dialog");
248 app.tenantEditUsersInterior.show(detailView);
249 $("#tenant-edit-users-dialog").dialog({
250 autoOpen: false,
251 modal: true,
252 width: 640,
253 buttons : {
Scott Baker435c2c92015-01-14 00:34:45 -0800254 "Ok" : function() {
Scott Baker97acad92015-01-12 19:45:40 -0800255 var editDialog = this;
256 user_ids = all_options($("#tenant-edit-users-dialog").find(".select-picker-to"));
257 user_ids = user_ids.map( function(x) { return parseInt(x,10); } );
Scott Baker5175e8a2015-01-20 00:20:12 -0800258 if (!array_same_elements(user_ids, model.usersBuffer)) {
259 XOSTenantApp.setDirty(true);
260 }
Scott Baker435c2c92015-01-14 00:34:45 -0800261 model.usersBuffer = user_ids;
Scott Baker97acad92015-01-12 19:45:40 -0800262 $(editDialog).dialog("close");
263 },
264 "Cancel" : function() {
265 $(this).dialog("close");
266 }
267 }
268 });
269 $("#tenant-edit-users-dialog").dialog("open");
270};
271
Scott Baker6b145aa2015-01-12 12:56:25 -0800272XOSTenantApp.deleteSlice = function(model) {
273 var app=this;
274 app.deleteDialog(model, function() { console.log("afterDelete"); app.viewSlice(undefined); });
275};
276
Scott Baker342d9b92015-01-11 13:44:30 -0800277XOSTenantApp.viewSlice = function(model) {
Scott Baker5175e8a2015-01-20 00:20:12 -0800278 if (XOSTenantApp.dirty) {
279 if (!confirm("The current sliver has unsaved data -- view new sliver anyway ?")) {
280 $("#tenantSliceSelector select").val(XOSTenantApp.currentSlice.id);
281 return;
282 }
283 }
284
285 XOSTenantApp.setDirty(false);
286
Scott Baker342d9b92015-01-11 13:44:30 -0800287 if (!model && xos.slicesPlus.models.length > 0) {
288 model = xos.slicesPlus.models[0];
289 }
290
Scott Bakerb52f7af2015-01-13 14:41:41 -0800291 if (model) {
292 sliceSelector = new XOSTenantApp.tenantSliceSelectorView({collection: xos.slicesPlus,
293 selectedID: model ? model.id : null,
294 } );
295 XOSTenantApp.sliceSelector = sliceSelector;
296 XOSTenantApp.tenantSliceSelector.show(sliceSelector);
Scott Baker342d9b92015-01-11 13:44:30 -0800297
Scott Bakerb52f7af2015-01-13 14:41:41 -0800298 tenantSummary = new XOSTenantApp.tenantSummaryView({model: model,
299 choices: {mount_data_sets: make_choices(xos.tenant().public_volume_names, null),
300 serviceClass: make_choices(xos.tenant().blessed_service_class_names, xos.tenant().blessed_service_classes),
301 default_image: make_choices(xos.tenant().blessed_image_names, xos.tenant().blessed_images),
302 default_flavor: make_choices(xos.tenant().blessed_flavor_names, xos.tenant().blessed_flavors),},
303 });
304 XOSTenantApp.tenantSummary.show(tenantSummary);
Scott Baker342d9b92015-01-11 13:44:30 -0800305
Scott Bakerb52f7af2015-01-13 14:41:41 -0800306 tenantSites = new XOSTenantSiteCollection();
307 tenantSites.getFromSlice(model);
Scott Baker435c2c92015-01-14 00:34:45 -0800308 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 -0800309 model.usersOrig = model.attributes.users; /* save an immutable copy that we'll use for username lookups */
310 model.user_namesOrig = model.attributes.user_names;
Scott Bakerb52f7af2015-01-13 14:41:41 -0800311 model.tenantSiteCollection = tenantSites;
312 XOSTenantApp.tenantSites = tenantSites;
Scott Baker342d9b92015-01-11 13:44:30 -0800313
Scott Bakerb52f7af2015-01-13 14:41:41 -0800314 tenantSiteList = new XOSTenantApp.tenantSiteListView({collection: tenantSites });
315 XOSTenantApp.tenantSiteList.show(tenantSiteList);
316 // on xos.slicePlus.sort, need to update xostenantapp.tenantSites
Scott Baker342d9b92015-01-11 13:44:30 -0800317
Scott Bakerb52f7af2015-01-13 14:41:41 -0800318 XOSTenantApp.tenantButtons.show( new XOSTenantButtonView( { app: XOSTenantApp,
319 linkedView: tenantSummary } ) );
Scott Baker5175e8a2015-01-20 00:20:12 -0800320
321 XOSTenantApp.currentSlice = model;
Scott Bakerb52f7af2015-01-13 14:41:41 -0800322 } else {
323 XOSTenantApp.tenantSliceSelector.show(new HTMLView({html: ""}));
324 XOSTenantApp.tenantSummary.show(new HTMLView({html: "You have no slices"}));
325 XOSTenantApp.tenantSiteList.show(new HTMLView({html: ""}));
326 XOSTenantApp.tenantButtons.show( new XOSTenantButtonView( { template: "#xos-tenant-buttons-noslice-template",
327 app: XOSTenantApp,
328 linkedView: tenantSummary } ) );
329 }
Scott Baker342d9b92015-01-11 13:44:30 -0800330};
331
Scott Bakeraba91832015-01-12 13:37:31 -0800332XOSTenantApp.sanityCheck = function() {
333 errors = [];
334 if (xos.tenant().blessed_service_classes.length == 0) {
335 errors.push("no blessed service classes");
336 }
337 if (xos.tenant().blessed_flavors.length == 0) {
338 errors.push("no blessed flavors");
339 }
340 if (xos.tenant().blessed_images.length == 0) {
341 errors.push("no blessed images");
342 }
343 if (xos.tenant().blessed_sites.length == 0) {
344 errors.push("no blessed sites");
345 }
Scott Baker9eebcb62015-01-18 17:04:35 -0800346 if (xos.tenant().current_user_site_id == null) {
347 errors.push("current user does not have a site");
348 }
Scott Bakeraba91832015-01-12 13:37:31 -0800349
350 if (errors.length > 0) {
351 $("#tenantSummary").html("Tenant view sanity check failed<br>" + errors.join("<br>"));
352 return false;
353 }
354
355 return true;
Scott Baker342d9b92015-01-11 13:44:30 -0800356}
357
358XOSTenantApp.collectionLoadChange = function() {
359 stats = xos.getCollectionStatus();
360
361 if (!XOSTenantApp.navigationStarted) {
362 if (stats["isLoaded"] + stats["failedLoad"] >= stats["startedLoad"]) {
Scott Bakeraba91832015-01-12 13:37:31 -0800363 if (XOSTenantApp.sanityCheck()) {
364 XOSTenantApp.viewSlice(undefined);
365 }
Scott Baker342d9b92015-01-11 13:44:30 -0800366 } else {
367 $("#tenantSummary").html("<h3>Loading...</h3><div id='xos-startup-progress'></div>");
368 $("#xos-startup-progress").progressbar({value: stats["completedLoad"], max: stats["startedLoad"]});
369 }
370 }
371};
372
373XOSTenantApp.on("start", function() {
374 XOSTenantApp.buildViews();
375
Scott Baker342d9b92015-01-11 13:44:30 -0800376 // fire it once to initially show the progress bar
377 XOSTenantApp.collectionLoadChange();
378
379 // fire it each time the collection load status is updated
380 Backbone.on("xoslib:collectionLoadChange", XOSTenantApp.collectionLoadChange);
381});
382
383$(document).ready(function(){
384 XOSTenantApp.start();
385});
386