blob: 929552c95e5d6f85c67b4d3742b0d782ebb88722 [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
53
Scott Baker342d9b92015-01-11 13:44:30 -080054XOSTenantButtonView = Marionette.ItemView.extend({
55 template: "#xos-tenant-buttons-template",
56
57 events: {"click button.btn-tenant-create": "createClicked",
58 "click button.btn-tenant-delete": "deleteClicked",
59 "click button.btn-tenant-add-user": "addUserClicked",
60 "click button.btn-tenant-save": "saveClicked",
61 },
62
63 createClicked: function(e) {
Scott Bakercd07a592015-01-12 12:37:38 -080064 XOSTenantApp.addSlice();
Scott Baker342d9b92015-01-11 13:44:30 -080065 },
66
67 deleteClicked: function(e) {
Scott Baker6b145aa2015-01-12 12:56:25 -080068 XOSTenantApp.deleteSlice(this.options.linkedView.model);
Scott Baker342d9b92015-01-11 13:44:30 -080069 },
70
71 addUserClicked: function(e) {
Scott Baker97acad92015-01-12 19:45:40 -080072 XOSTenantApp.editUsers(this.options.linkedView.model);
Scott Baker342d9b92015-01-11 13:44:30 -080073 },
74
75 saveClicked: function(e) {
Scott Baker97acad92015-01-12 19:45:40 -080076 model = this.options.linkedView.model;
77 model.tenantSiteCollection.putToSlice(model);
Scott Baker435c2c92015-01-14 00:34:45 -080078 model.attributes.users = model.usersBuffer;
Scott Baker342d9b92015-01-11 13:44:30 -080079 this.options.linkedView.submitContinueClicked.call(this.options.linkedView, e);
80 },
81 });
82
83XOSTenantApp = new XOSApplication({
84 logTableId: "#logTable",
85 statusMsgId: "#statusMsg",
Scott Bakercd07a592015-01-12 12:37:38 -080086 hideTabsByDefault: true,
87 varName: "XOSTenantApp",
Scott Baker342d9b92015-01-11 13:44:30 -080088});
89
90XOSTenantApp.addRegions({
91 tenantSliceSelector: "#tenantSliceSelector",
92 tenantSummary: "#tenantSummary",
93 tenantSiteList: "#tenantSiteList",
94 tenantButtons: "#tenantButtons",
Scott Bakercd07a592015-01-12 12:37:38 -080095 tenantAddSliceInterior: "#tenant-addslice-interior",
Scott Baker97acad92015-01-12 19:45:40 -080096 tenantEditUsersInterior: "#tenant-edit-users-interior",
Scott Baker342d9b92015-01-11 13:44:30 -080097});
98
Scott Bakercd07a592015-01-12 12:37:38 -080099XOSTenantApp.buildViews = function() {
Scott Baker342d9b92015-01-11 13:44:30 -0800100 XOSTenantApp.tenantSites = new XOSTenantSiteCollection();
101
102 tenantSummaryClass = XOSDetailView.extend({template: "#xos-detail-template",
103 app: XOSTenantApp,
Scott Baker97acad92015-01-12 19:45:40 -0800104 detailFields: ["serviceClass", "default_image", "default_flavor", "network_ports", "mount_data_sets"],
Scott Bakere56d3272015-01-14 00:47:50 -0800105 fieldDisplayNames: {serviceClass: "Service Level", "default_flavor": "Flavor", "default_image": "Image", "mount_data_sets": "Data Sets"},
Scott Bakere7a90452015-01-14 17:07:30 -0800106
107 onShow: function() {
108 // the slice selector is in a different table, so make every label cell the maximal width
109 make_same_width("#xos-tenant-view-panel", ".xos-label-cell");
110 },
Scott Baker97acad92015-01-12 19:45:40 -0800111 });
Scott Baker342d9b92015-01-11 13:44:30 -0800112
113 XOSTenantApp.tenantSummaryView = tenantSummaryClass;
114
Scott Bakercd07a592015-01-12 12:37:38 -0800115 tenantAddClass = XOSDetailView.extend({template: "#xos-detail-template",
116 app: XOSTenantApp,
117 detailFields: ["name", "description"]});
118
119 XOSTenantApp.tenantAddView = tenantAddClass;
120
Scott Baker342d9b92015-01-11 13:44:30 -0800121 tenantSiteItemClass = XOSItemView.extend({template: "#xos-listitem-template",
122 app: XOSTenantApp});
123
124 XOSTenantApp.tenantSiteItemView = tenantSiteItemClass;
125
126 tenantSiteListClass = XOSDataTableView.extend({template: "#xos-list-template",
127 app: XOSTenantApp,
128 childView: tenantSiteItemClass,
129 collection: XOSTenantApp.tenantSites,
130 title: "sites",
131 inputType: {allocated: "spinner"},
132 noDeleteColumn: true,
Scott Bakere56d3272015-01-14 00:47:50 -0800133 disablePaginate: true,
134 disableFilter: true,
Scott Bakere7a90452015-01-14 17:07:30 -0800135 fieldDisplayNames: {"name": "Site"},
Scott Baker342d9b92015-01-11 13:44:30 -0800136 });
137
138 XOSTenantApp.tenantSiteListView = tenantSiteListClass;
139
140 XOSTenantApp.tenantSliceSelectorView = SliceSelectorView.extend( {
141 sliceChanged: function(id) {
Scott Bakercd07a592015-01-12 12:37:38 -0800142 XOSTenantApp.navToSlice(id);
Scott Baker342d9b92015-01-11 13:44:30 -0800143 },
144 });
145
146 xos.sites.fetch();
147 xos.slicesPlus.fetch();
148 xos.tenantview.fetch();
149};
150
151make_choices = function(list_of_names, list_of_values) {
152 result = [];
153 if (!list_of_values) {
154 for (index in list_of_names) {
155 displayName = list_of_names[index];
156 result.push( [displayName, displayName] );
157 }
Scott Bakeraba91832015-01-12 13:37:31 -0800158 } else {
159 for (index in list_of_names) {
160 displayName = list_of_names[index];
161 id = list_of_values[index];
162 result.push( [displayName, id] );
163 }
Scott Baker342d9b92015-01-11 13:44:30 -0800164 }
165 return result;
166};
167
Scott Bakercd07a592015-01-12 12:37:38 -0800168XOSTenantApp.navToSlice = function(id) {
169 XOSTenantApp.viewSlice(xos.slicesPlus.get(id));
170};
171
172XOSTenantApp.adjustCollectionField = function(collectionName, id, fieldName, amount) {
173 model = XOSTenantApp[collectionName].get(id);
174 model.set(fieldName, Math.max(model.get(fieldName) + amount, 0));
175};
176
177XOSTenantApp.addSlice = function() {
178 var app=this;
Scott Bakerce0dfb82015-01-14 09:54:19 -0800179
180 if (!xos.tenant().current_user_can_create_slice) {
181 window.alert("You do not have sufficient rights to create a slice on your site");
182 return;
183 }
184
Scott Baker97acad92015-01-12 19:45:40 -0800185 model = new xos.slicesPlus.model({site: xos.tenant().current_user_site_id,
186 name: xos.tenant().current_user_login_base + "_"});
Scott Bakercd07a592015-01-12 12:37:38 -0800187 console.log(model);
Scott Baker97acad92015-01-12 19:45:40 -0800188 var detailView = new XOSTenantApp.tenantAddView({model: model,
189 collection: xos.slicesPlus,
190 noSubmitButton: true,
191 });
192 detailView.dialog = $("#tenant-addslice-dialog");
Scott Bakercd07a592015-01-12 12:37:38 -0800193 app.tenantAddSliceInterior.show(detailView);
194 $("#tenant-addslice-dialog").dialog({
195 autoOpen: false,
196 modal: true,
197 width: 640,
198 buttons : {
Scott Baker435c2c92015-01-14 00:34:45 -0800199 "Create Slice" : function() {
Scott Bakercd07a592015-01-12 12:37:38 -0800200 var addDialog = this;
Scott Baker97acad92015-01-12 19:45:40 -0800201 console.log("SAVE!!!");
Scott Bakercd07a592015-01-12 12:37:38 -0800202 detailView.synchronous = true;
203 detailView.afterSave = function() { $(addDialog).dialog("close"); XOSTenantApp.navToSlice(detailView.model.id); }
204 detailView.save();
205 },
206 "Cancel" : function() {
207 $(this).dialog("close");
208 }
209 }
210 });
211 $("#tenant-addslice-dialog").dialog("open");
212};
213
Scott Baker97acad92015-01-12 19:45:40 -0800214XOSTenantApp.editUsers = function(model) {
215 var app=this;
216 var detailView = new XOSEditUsersView({model: model, collection: xos.slicesPlus});
217 detailView.dialog = $("#tenant-edit-users-dialog");
218 app.tenantEditUsersInterior.show(detailView);
219 $("#tenant-edit-users-dialog").dialog({
220 autoOpen: false,
221 modal: true,
222 width: 640,
223 buttons : {
Scott Baker435c2c92015-01-14 00:34:45 -0800224 "Ok" : function() {
Scott Baker97acad92015-01-12 19:45:40 -0800225 var editDialog = this;
226 user_ids = all_options($("#tenant-edit-users-dialog").find(".select-picker-to"));
227 user_ids = user_ids.map( function(x) { return parseInt(x,10); } );
Scott Baker435c2c92015-01-14 00:34:45 -0800228 model.usersBuffer = user_ids;
Scott Baker97acad92015-01-12 19:45:40 -0800229 $(editDialog).dialog("close");
230 },
231 "Cancel" : function() {
232 $(this).dialog("close");
233 }
234 }
235 });
236 $("#tenant-edit-users-dialog").dialog("open");
237};
238
Scott Baker6b145aa2015-01-12 12:56:25 -0800239XOSTenantApp.deleteSlice = function(model) {
240 var app=this;
241 app.deleteDialog(model, function() { console.log("afterDelete"); app.viewSlice(undefined); });
242};
243
Scott Baker342d9b92015-01-11 13:44:30 -0800244XOSTenantApp.viewSlice = function(model) {
245 if (!model && xos.slicesPlus.models.length > 0) {
246 model = xos.slicesPlus.models[0];
247 }
248
Scott Bakerb52f7af2015-01-13 14:41:41 -0800249 if (model) {
250 sliceSelector = new XOSTenantApp.tenantSliceSelectorView({collection: xos.slicesPlus,
251 selectedID: model ? model.id : null,
252 } );
253 XOSTenantApp.sliceSelector = sliceSelector;
254 XOSTenantApp.tenantSliceSelector.show(sliceSelector);
Scott Baker342d9b92015-01-11 13:44:30 -0800255
Scott Bakerb52f7af2015-01-13 14:41:41 -0800256 tenantSummary = new XOSTenantApp.tenantSummaryView({model: model,
257 choices: {mount_data_sets: make_choices(xos.tenant().public_volume_names, null),
258 serviceClass: make_choices(xos.tenant().blessed_service_class_names, xos.tenant().blessed_service_classes),
259 default_image: make_choices(xos.tenant().blessed_image_names, xos.tenant().blessed_images),
260 default_flavor: make_choices(xos.tenant().blessed_flavor_names, xos.tenant().blessed_flavors),},
261 });
262 XOSTenantApp.tenantSummary.show(tenantSummary);
Scott Baker342d9b92015-01-11 13:44:30 -0800263
Scott Bakerb52f7af2015-01-13 14:41:41 -0800264 tenantSites = new XOSTenantSiteCollection();
265 tenantSites.getFromSlice(model);
Scott Baker435c2c92015-01-14 00:34:45 -0800266 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 Bakerb52f7af2015-01-13 14:41:41 -0800267 model.tenantSiteCollection = tenantSites;
268 XOSTenantApp.tenantSites = tenantSites;
Scott Baker342d9b92015-01-11 13:44:30 -0800269
Scott Bakerb52f7af2015-01-13 14:41:41 -0800270 tenantSiteList = new XOSTenantApp.tenantSiteListView({collection: tenantSites });
271 XOSTenantApp.tenantSiteList.show(tenantSiteList);
272 // on xos.slicePlus.sort, need to update xostenantapp.tenantSites
Scott Baker342d9b92015-01-11 13:44:30 -0800273
Scott Bakerb52f7af2015-01-13 14:41:41 -0800274 XOSTenantApp.tenantButtons.show( new XOSTenantButtonView( { app: XOSTenantApp,
275 linkedView: tenantSummary } ) );
276 } else {
277 XOSTenantApp.tenantSliceSelector.show(new HTMLView({html: ""}));
278 XOSTenantApp.tenantSummary.show(new HTMLView({html: "You have no slices"}));
279 XOSTenantApp.tenantSiteList.show(new HTMLView({html: ""}));
280 XOSTenantApp.tenantButtons.show( new XOSTenantButtonView( { template: "#xos-tenant-buttons-noslice-template",
281 app: XOSTenantApp,
282 linkedView: tenantSummary } ) );
283 }
Scott Baker342d9b92015-01-11 13:44:30 -0800284};
285
Scott Bakeraba91832015-01-12 13:37:31 -0800286XOSTenantApp.sanityCheck = function() {
287 errors = [];
288 if (xos.tenant().blessed_service_classes.length == 0) {
289 errors.push("no blessed service classes");
290 }
291 if (xos.tenant().blessed_flavors.length == 0) {
292 errors.push("no blessed flavors");
293 }
294 if (xos.tenant().blessed_images.length == 0) {
295 errors.push("no blessed images");
296 }
297 if (xos.tenant().blessed_sites.length == 0) {
298 errors.push("no blessed sites");
299 }
300
301 if (errors.length > 0) {
302 $("#tenantSummary").html("Tenant view sanity check failed<br>" + errors.join("<br>"));
303 return false;
304 }
305
306 return true;
Scott Baker342d9b92015-01-11 13:44:30 -0800307}
308
309XOSTenantApp.collectionLoadChange = function() {
310 stats = xos.getCollectionStatus();
311
312 if (!XOSTenantApp.navigationStarted) {
313 if (stats["isLoaded"] + stats["failedLoad"] >= stats["startedLoad"]) {
Scott Bakeraba91832015-01-12 13:37:31 -0800314 if (XOSTenantApp.sanityCheck()) {
315 XOSTenantApp.viewSlice(undefined);
316 }
Scott Baker342d9b92015-01-11 13:44:30 -0800317 } else {
318 $("#tenantSummary").html("<h3>Loading...</h3><div id='xos-startup-progress'></div>");
319 $("#xos-startup-progress").progressbar({value: stats["completedLoad"], max: stats["startedLoad"]});
320 }
321 }
322};
323
324XOSTenantApp.on("start", function() {
325 XOSTenantApp.buildViews();
326
Scott Baker342d9b92015-01-11 13:44:30 -0800327 // fire it once to initially show the progress bar
328 XOSTenantApp.collectionLoadChange();
329
330 // fire it each time the collection load status is updated
331 Backbone.on("xoslib:collectionLoadChange", XOSTenantApp.collectionLoadChange);
332});
333
334$(document).ready(function(){
335 XOSTenantApp.start();
336});
337