blob: 10e70e62907f3c7403f41ddd2ca331f64c6ec951 [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,
105 varName: "XOSTenantApp",
Scott Baker342d9b92015-01-11 13:44:30 -0800106});
107
108XOSTenantApp.addRegions({
109 tenantSliceSelector: "#tenantSliceSelector",
110 tenantSummary: "#tenantSummary",
111 tenantSiteList: "#tenantSiteList",
112 tenantButtons: "#tenantButtons",
Scott Bakercd07a592015-01-12 12:37:38 -0800113 tenantAddSliceInterior: "#tenant-addslice-interior",
Scott Baker97acad92015-01-12 19:45:40 -0800114 tenantEditUsersInterior: "#tenant-edit-users-interior",
Scott Baker342d9b92015-01-11 13:44:30 -0800115});
116
Scott Bakerf5d97bd2015-01-20 00:04:19 -0800117XOSTenantApp.setDirty = function(dirty) {
118 if (dirty) {
119 $("button.btn-tenant-save").addClass("btn-success");
120 } else {
121 $("button.btn-tenant-save").removeClass("btn-success");
122 }
123};
124
Scott Bakercd07a592015-01-12 12:37:38 -0800125XOSTenantApp.buildViews = function() {
Scott Baker342d9b92015-01-11 13:44:30 -0800126 XOSTenantApp.tenantSites = new XOSTenantSiteCollection();
127
Scott Bakerf5d97bd2015-01-20 00:04:19 -0800128 tenantSummaryClass = XOSTenantSummaryView.extend({template: "#xos-detail-template",
Scott Baker342d9b92015-01-11 13:44:30 -0800129 app: XOSTenantApp,
Scott Baker97acad92015-01-12 19:45:40 -0800130 detailFields: ["serviceClass", "default_image", "default_flavor", "network_ports", "mount_data_sets"],
Scott Bakere56d3272015-01-14 00:47:50 -0800131 fieldDisplayNames: {serviceClass: "Service Level", "default_flavor": "Flavor", "default_image": "Image", "mount_data_sets": "Data Sets"},
Scott Bakere7a90452015-01-14 17:07:30 -0800132
133 onShow: function() {
134 // the slice selector is in a different table, so make every label cell the maximal width
135 make_same_width("#xos-tenant-view-panel", ".xos-label-cell");
136 },
Scott Baker97acad92015-01-12 19:45:40 -0800137 });
Scott Baker342d9b92015-01-11 13:44:30 -0800138
139 XOSTenantApp.tenantSummaryView = tenantSummaryClass;
140
Scott Bakercd07a592015-01-12 12:37:38 -0800141 tenantAddClass = XOSDetailView.extend({template: "#xos-detail-template",
142 app: XOSTenantApp,
143 detailFields: ["name", "description"]});
144
145 XOSTenantApp.tenantAddView = tenantAddClass;
146
Scott Baker342d9b92015-01-11 13:44:30 -0800147 tenantSiteItemClass = XOSItemView.extend({template: "#xos-listitem-template",
148 app: XOSTenantApp});
149
150 XOSTenantApp.tenantSiteItemView = tenantSiteItemClass;
151
152 tenantSiteListClass = XOSDataTableView.extend({template: "#xos-list-template",
153 app: XOSTenantApp,
154 childView: tenantSiteItemClass,
155 collection: XOSTenantApp.tenantSites,
156 title: "sites",
157 inputType: {allocated: "spinner"},
158 noDeleteColumn: true,
Scott Bakere56d3272015-01-14 00:47:50 -0800159 disablePaginate: true,
160 disableFilter: true,
Scott Bakere7a90452015-01-14 17:07:30 -0800161 fieldDisplayNames: {"name": "Site"},
Scott Baker342d9b92015-01-11 13:44:30 -0800162 });
163
164 XOSTenantApp.tenantSiteListView = tenantSiteListClass;
165
166 XOSTenantApp.tenantSliceSelectorView = SliceSelectorView.extend( {
167 sliceChanged: function(id) {
Scott Bakercd07a592015-01-12 12:37:38 -0800168 XOSTenantApp.navToSlice(id);
Scott Baker342d9b92015-01-11 13:44:30 -0800169 },
170 });
171
172 xos.sites.fetch();
173 xos.slicesPlus.fetch();
174 xos.tenantview.fetch();
175};
176
177make_choices = function(list_of_names, list_of_values) {
178 result = [];
179 if (!list_of_values) {
180 for (index in list_of_names) {
181 displayName = list_of_names[index];
182 result.push( [displayName, displayName] );
183 }
Scott Bakeraba91832015-01-12 13:37:31 -0800184 } else {
185 for (index in list_of_names) {
186 displayName = list_of_names[index];
187 id = list_of_values[index];
188 result.push( [displayName, id] );
189 }
Scott Baker342d9b92015-01-11 13:44:30 -0800190 }
191 return result;
192};
193
Scott Bakercd07a592015-01-12 12:37:38 -0800194XOSTenantApp.navToSlice = function(id) {
195 XOSTenantApp.viewSlice(xos.slicesPlus.get(id));
196};
197
198XOSTenantApp.adjustCollectionField = function(collectionName, id, fieldName, amount) {
199 model = XOSTenantApp[collectionName].get(id);
200 model.set(fieldName, Math.max(model.get(fieldName) + amount, 0));
Scott Bakerf5d97bd2015-01-20 00:04:19 -0800201 XOSTenantApp.setDirty(true);
Scott Bakercd07a592015-01-12 12:37:38 -0800202};
203
204XOSTenantApp.addSlice = function() {
205 var app=this;
Scott Bakerce0dfb82015-01-14 09:54:19 -0800206
207 if (!xos.tenant().current_user_can_create_slice) {
208 window.alert("You do not have sufficient rights to create a slice on your site");
209 return;
210 }
211
Scott Baker97acad92015-01-12 19:45:40 -0800212 model = new xos.slicesPlus.model({site: xos.tenant().current_user_site_id,
Scott Bakerc8984372015-01-19 08:56:16 -0800213 name: xos.tenant().current_user_login_base + "_",
214 creator: xos.tenant().current_user_id});
Scott Bakercd07a592015-01-12 12:37:38 -0800215 console.log(model);
Scott Baker97acad92015-01-12 19:45:40 -0800216 var detailView = new XOSTenantApp.tenantAddView({model: model,
Scott Bakerc8984372015-01-19 08:56:16 -0800217 collection: xos.slicesPlus,
218 noSubmitButton: true,
Scott Baker97acad92015-01-12 19:45:40 -0800219 });
220 detailView.dialog = $("#tenant-addslice-dialog");
Scott Bakercd07a592015-01-12 12:37:38 -0800221 app.tenantAddSliceInterior.show(detailView);
222 $("#tenant-addslice-dialog").dialog({
223 autoOpen: false,
224 modal: true,
225 width: 640,
226 buttons : {
Scott Baker435c2c92015-01-14 00:34:45 -0800227 "Create Slice" : function() {
Scott Bakercd07a592015-01-12 12:37:38 -0800228 var addDialog = this;
Scott Baker97acad92015-01-12 19:45:40 -0800229 console.log("SAVE!!!");
Scott Bakercd07a592015-01-12 12:37:38 -0800230 detailView.synchronous = true;
231 detailView.afterSave = function() { $(addDialog).dialog("close"); XOSTenantApp.navToSlice(detailView.model.id); }
232 detailView.save();
233 },
234 "Cancel" : function() {
235 $(this).dialog("close");
236 }
237 }
238 });
239 $("#tenant-addslice-dialog").dialog("open");
240};
241
Scott Baker97acad92015-01-12 19:45:40 -0800242XOSTenantApp.editUsers = function(model) {
243 var app=this;
244 var detailView = new XOSEditUsersView({model: model, collection: xos.slicesPlus});
245 detailView.dialog = $("#tenant-edit-users-dialog");
246 app.tenantEditUsersInterior.show(detailView);
247 $("#tenant-edit-users-dialog").dialog({
248 autoOpen: false,
249 modal: true,
250 width: 640,
251 buttons : {
Scott Baker435c2c92015-01-14 00:34:45 -0800252 "Ok" : function() {
Scott Baker97acad92015-01-12 19:45:40 -0800253 var editDialog = this;
254 user_ids = all_options($("#tenant-edit-users-dialog").find(".select-picker-to"));
255 user_ids = user_ids.map( function(x) { return parseInt(x,10); } );
Scott Baker435c2c92015-01-14 00:34:45 -0800256 model.usersBuffer = user_ids;
Scott Baker97acad92015-01-12 19:45:40 -0800257 $(editDialog).dialog("close");
258 },
259 "Cancel" : function() {
260 $(this).dialog("close");
261 }
262 }
263 });
264 $("#tenant-edit-users-dialog").dialog("open");
265};
266
Scott Baker6b145aa2015-01-12 12:56:25 -0800267XOSTenantApp.deleteSlice = function(model) {
268 var app=this;
269 app.deleteDialog(model, function() { console.log("afterDelete"); app.viewSlice(undefined); });
270};
271
Scott Baker342d9b92015-01-11 13:44:30 -0800272XOSTenantApp.viewSlice = function(model) {
273 if (!model && xos.slicesPlus.models.length > 0) {
274 model = xos.slicesPlus.models[0];
275 }
276
Scott Bakerb52f7af2015-01-13 14:41:41 -0800277 if (model) {
278 sliceSelector = new XOSTenantApp.tenantSliceSelectorView({collection: xos.slicesPlus,
279 selectedID: model ? model.id : null,
280 } );
281 XOSTenantApp.sliceSelector = sliceSelector;
282 XOSTenantApp.tenantSliceSelector.show(sliceSelector);
Scott Baker342d9b92015-01-11 13:44:30 -0800283
Scott Bakerb52f7af2015-01-13 14:41:41 -0800284 tenantSummary = new XOSTenantApp.tenantSummaryView({model: model,
285 choices: {mount_data_sets: make_choices(xos.tenant().public_volume_names, null),
286 serviceClass: make_choices(xos.tenant().blessed_service_class_names, xos.tenant().blessed_service_classes),
287 default_image: make_choices(xos.tenant().blessed_image_names, xos.tenant().blessed_images),
288 default_flavor: make_choices(xos.tenant().blessed_flavor_names, xos.tenant().blessed_flavors),},
289 });
290 XOSTenantApp.tenantSummary.show(tenantSummary);
Scott Baker342d9b92015-01-11 13:44:30 -0800291
Scott Bakerb52f7af2015-01-13 14:41:41 -0800292 tenantSites = new XOSTenantSiteCollection();
293 tenantSites.getFromSlice(model);
Scott Baker435c2c92015-01-14 00:34:45 -0800294 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 -0800295 model.usersOrig = model.attributes.users; /* save an immutable copy that we'll use for username lookups */
296 model.user_namesOrig = model.attributes.user_names;
Scott Bakerb52f7af2015-01-13 14:41:41 -0800297 model.tenantSiteCollection = tenantSites;
298 XOSTenantApp.tenantSites = tenantSites;
Scott Baker342d9b92015-01-11 13:44:30 -0800299
Scott Bakerb52f7af2015-01-13 14:41:41 -0800300 tenantSiteList = new XOSTenantApp.tenantSiteListView({collection: tenantSites });
301 XOSTenantApp.tenantSiteList.show(tenantSiteList);
302 // on xos.slicePlus.sort, need to update xostenantapp.tenantSites
Scott Baker342d9b92015-01-11 13:44:30 -0800303
Scott Bakerb52f7af2015-01-13 14:41:41 -0800304 XOSTenantApp.tenantButtons.show( new XOSTenantButtonView( { app: XOSTenantApp,
305 linkedView: tenantSummary } ) );
306 } else {
307 XOSTenantApp.tenantSliceSelector.show(new HTMLView({html: ""}));
308 XOSTenantApp.tenantSummary.show(new HTMLView({html: "You have no slices"}));
309 XOSTenantApp.tenantSiteList.show(new HTMLView({html: ""}));
310 XOSTenantApp.tenantButtons.show( new XOSTenantButtonView( { template: "#xos-tenant-buttons-noslice-template",
311 app: XOSTenantApp,
312 linkedView: tenantSummary } ) );
313 }
Scott Baker342d9b92015-01-11 13:44:30 -0800314};
315
Scott Bakeraba91832015-01-12 13:37:31 -0800316XOSTenantApp.sanityCheck = function() {
317 errors = [];
318 if (xos.tenant().blessed_service_classes.length == 0) {
319 errors.push("no blessed service classes");
320 }
321 if (xos.tenant().blessed_flavors.length == 0) {
322 errors.push("no blessed flavors");
323 }
324 if (xos.tenant().blessed_images.length == 0) {
325 errors.push("no blessed images");
326 }
327 if (xos.tenant().blessed_sites.length == 0) {
328 errors.push("no blessed sites");
329 }
Scott Baker9eebcb62015-01-18 17:04:35 -0800330 if (xos.tenant().current_user_site_id == null) {
331 errors.push("current user does not have a site");
332 }
Scott Bakeraba91832015-01-12 13:37:31 -0800333
334 if (errors.length > 0) {
335 $("#tenantSummary").html("Tenant view sanity check failed<br>" + errors.join("<br>"));
336 return false;
337 }
338
339 return true;
Scott Baker342d9b92015-01-11 13:44:30 -0800340}
341
342XOSTenantApp.collectionLoadChange = function() {
343 stats = xos.getCollectionStatus();
344
345 if (!XOSTenantApp.navigationStarted) {
346 if (stats["isLoaded"] + stats["failedLoad"] >= stats["startedLoad"]) {
Scott Bakeraba91832015-01-12 13:37:31 -0800347 if (XOSTenantApp.sanityCheck()) {
348 XOSTenantApp.viewSlice(undefined);
349 }
Scott Baker342d9b92015-01-11 13:44:30 -0800350 } else {
351 $("#tenantSummary").html("<h3>Loading...</h3><div id='xos-startup-progress'></div>");
352 $("#xos-startup-progress").progressbar({value: stats["completedLoad"], max: stats["startedLoad"]});
353 }
354 }
355};
356
357XOSTenantApp.on("start", function() {
358 XOSTenantApp.buildViews();
359
Scott Baker342d9b92015-01-11 13:44:30 -0800360 // fire it once to initially show the progress bar
361 XOSTenantApp.collectionLoadChange();
362
363 // fire it each time the collection load status is updated
364 Backbone.on("xoslib:collectionLoadChange", XOSTenantApp.collectionLoadChange);
365});
366
367$(document).ready(function(){
368 XOSTenantApp.start();
369});
370