blob: cb816a860fb28c138ee241410ea02832467c80f0 [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 Baker97acad92015-01-12 19:45:40 -0800106 });
Scott Baker342d9b92015-01-11 13:44:30 -0800107
108 XOSTenantApp.tenantSummaryView = tenantSummaryClass;
109
Scott Bakercd07a592015-01-12 12:37:38 -0800110 tenantAddClass = XOSDetailView.extend({template: "#xos-detail-template",
111 app: XOSTenantApp,
112 detailFields: ["name", "description"]});
113
114 XOSTenantApp.tenantAddView = tenantAddClass;
115
Scott Baker342d9b92015-01-11 13:44:30 -0800116 tenantSiteItemClass = XOSItemView.extend({template: "#xos-listitem-template",
117 app: XOSTenantApp});
118
119 XOSTenantApp.tenantSiteItemView = tenantSiteItemClass;
120
121 tenantSiteListClass = XOSDataTableView.extend({template: "#xos-list-template",
122 app: XOSTenantApp,
123 childView: tenantSiteItemClass,
124 collection: XOSTenantApp.tenantSites,
125 title: "sites",
126 inputType: {allocated: "spinner"},
127 noDeleteColumn: true,
Scott Bakere56d3272015-01-14 00:47:50 -0800128 disablePaginate: true,
129 disableFilter: true,
Scott Baker342d9b92015-01-11 13:44:30 -0800130 });
131
132 XOSTenantApp.tenantSiteListView = tenantSiteListClass;
133
134 XOSTenantApp.tenantSliceSelectorView = SliceSelectorView.extend( {
135 sliceChanged: function(id) {
Scott Bakercd07a592015-01-12 12:37:38 -0800136 XOSTenantApp.navToSlice(id);
Scott Baker342d9b92015-01-11 13:44:30 -0800137 },
138 });
139
140 xos.sites.fetch();
141 xos.slicesPlus.fetch();
142 xos.tenantview.fetch();
143};
144
145make_choices = function(list_of_names, list_of_values) {
146 result = [];
147 if (!list_of_values) {
148 for (index in list_of_names) {
149 displayName = list_of_names[index];
150 result.push( [displayName, displayName] );
151 }
Scott Bakeraba91832015-01-12 13:37:31 -0800152 } else {
153 for (index in list_of_names) {
154 displayName = list_of_names[index];
155 id = list_of_values[index];
156 result.push( [displayName, id] );
157 }
Scott Baker342d9b92015-01-11 13:44:30 -0800158 }
159 return result;
160};
161
Scott Bakercd07a592015-01-12 12:37:38 -0800162XOSTenantApp.navToSlice = function(id) {
163 XOSTenantApp.viewSlice(xos.slicesPlus.get(id));
164};
165
166XOSTenantApp.adjustCollectionField = function(collectionName, id, fieldName, amount) {
167 model = XOSTenantApp[collectionName].get(id);
168 model.set(fieldName, Math.max(model.get(fieldName) + amount, 0));
169};
170
171XOSTenantApp.addSlice = function() {
172 var app=this;
Scott Bakerce0dfb82015-01-14 09:54:19 -0800173
174 if (!xos.tenant().current_user_can_create_slice) {
175 window.alert("You do not have sufficient rights to create a slice on your site");
176 return;
177 }
178
Scott Baker97acad92015-01-12 19:45:40 -0800179 model = new xos.slicesPlus.model({site: xos.tenant().current_user_site_id,
180 name: xos.tenant().current_user_login_base + "_"});
Scott Bakercd07a592015-01-12 12:37:38 -0800181 console.log(model);
Scott Baker97acad92015-01-12 19:45:40 -0800182 var detailView = new XOSTenantApp.tenantAddView({model: model,
183 collection: xos.slicesPlus,
184 noSubmitButton: true,
185 });
186 detailView.dialog = $("#tenant-addslice-dialog");
Scott Bakercd07a592015-01-12 12:37:38 -0800187 app.tenantAddSliceInterior.show(detailView);
188 $("#tenant-addslice-dialog").dialog({
189 autoOpen: false,
190 modal: true,
191 width: 640,
192 buttons : {
Scott Baker435c2c92015-01-14 00:34:45 -0800193 "Create Slice" : function() {
Scott Bakercd07a592015-01-12 12:37:38 -0800194 var addDialog = this;
Scott Baker97acad92015-01-12 19:45:40 -0800195 console.log("SAVE!!!");
Scott Bakercd07a592015-01-12 12:37:38 -0800196 detailView.synchronous = true;
197 detailView.afterSave = function() { $(addDialog).dialog("close"); XOSTenantApp.navToSlice(detailView.model.id); }
198 detailView.save();
199 },
200 "Cancel" : function() {
201 $(this).dialog("close");
202 }
203 }
204 });
205 $("#tenant-addslice-dialog").dialog("open");
206};
207
Scott Baker97acad92015-01-12 19:45:40 -0800208XOSTenantApp.editUsers = function(model) {
209 var app=this;
210 var detailView = new XOSEditUsersView({model: model, collection: xos.slicesPlus});
211 detailView.dialog = $("#tenant-edit-users-dialog");
212 app.tenantEditUsersInterior.show(detailView);
213 $("#tenant-edit-users-dialog").dialog({
214 autoOpen: false,
215 modal: true,
216 width: 640,
217 buttons : {
Scott Baker435c2c92015-01-14 00:34:45 -0800218 "Ok" : function() {
Scott Baker97acad92015-01-12 19:45:40 -0800219 var editDialog = this;
220 user_ids = all_options($("#tenant-edit-users-dialog").find(".select-picker-to"));
221 user_ids = user_ids.map( function(x) { return parseInt(x,10); } );
Scott Baker435c2c92015-01-14 00:34:45 -0800222 model.usersBuffer = user_ids;
Scott Baker97acad92015-01-12 19:45:40 -0800223 $(editDialog).dialog("close");
224 },
225 "Cancel" : function() {
226 $(this).dialog("close");
227 }
228 }
229 });
230 $("#tenant-edit-users-dialog").dialog("open");
231};
232
Scott Baker6b145aa2015-01-12 12:56:25 -0800233XOSTenantApp.deleteSlice = function(model) {
234 var app=this;
235 app.deleteDialog(model, function() { console.log("afterDelete"); app.viewSlice(undefined); });
236};
237
Scott Baker342d9b92015-01-11 13:44:30 -0800238XOSTenantApp.viewSlice = function(model) {
239 if (!model && xos.slicesPlus.models.length > 0) {
240 model = xos.slicesPlus.models[0];
241 }
242
Scott Bakerb52f7af2015-01-13 14:41:41 -0800243 if (model) {
244 sliceSelector = new XOSTenantApp.tenantSliceSelectorView({collection: xos.slicesPlus,
245 selectedID: model ? model.id : null,
246 } );
247 XOSTenantApp.sliceSelector = sliceSelector;
248 XOSTenantApp.tenantSliceSelector.show(sliceSelector);
Scott Baker342d9b92015-01-11 13:44:30 -0800249
Scott Bakerb52f7af2015-01-13 14:41:41 -0800250 tenantSummary = new XOSTenantApp.tenantSummaryView({model: model,
251 choices: {mount_data_sets: make_choices(xos.tenant().public_volume_names, null),
252 serviceClass: make_choices(xos.tenant().blessed_service_class_names, xos.tenant().blessed_service_classes),
253 default_image: make_choices(xos.tenant().blessed_image_names, xos.tenant().blessed_images),
254 default_flavor: make_choices(xos.tenant().blessed_flavor_names, xos.tenant().blessed_flavors),},
255 });
256 XOSTenantApp.tenantSummary.show(tenantSummary);
Scott Baker342d9b92015-01-11 13:44:30 -0800257
Scott Bakerb52f7af2015-01-13 14:41:41 -0800258 tenantSites = new XOSTenantSiteCollection();
259 tenantSites.getFromSlice(model);
Scott Baker435c2c92015-01-14 00:34:45 -0800260 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 -0800261 model.tenantSiteCollection = tenantSites;
262 XOSTenantApp.tenantSites = tenantSites;
Scott Baker342d9b92015-01-11 13:44:30 -0800263
Scott Bakerb52f7af2015-01-13 14:41:41 -0800264 tenantSiteList = new XOSTenantApp.tenantSiteListView({collection: tenantSites });
265 XOSTenantApp.tenantSiteList.show(tenantSiteList);
266 // on xos.slicePlus.sort, need to update xostenantapp.tenantSites
Scott Baker342d9b92015-01-11 13:44:30 -0800267
Scott Bakerb52f7af2015-01-13 14:41:41 -0800268 XOSTenantApp.tenantButtons.show( new XOSTenantButtonView( { app: XOSTenantApp,
269 linkedView: tenantSummary } ) );
270 } else {
271 XOSTenantApp.tenantSliceSelector.show(new HTMLView({html: ""}));
272 XOSTenantApp.tenantSummary.show(new HTMLView({html: "You have no slices"}));
273 XOSTenantApp.tenantSiteList.show(new HTMLView({html: ""}));
274 XOSTenantApp.tenantButtons.show( new XOSTenantButtonView( { template: "#xos-tenant-buttons-noslice-template",
275 app: XOSTenantApp,
276 linkedView: tenantSummary } ) );
277 }
Scott Baker342d9b92015-01-11 13:44:30 -0800278};
279
Scott Bakeraba91832015-01-12 13:37:31 -0800280XOSTenantApp.sanityCheck = function() {
281 errors = [];
282 if (xos.tenant().blessed_service_classes.length == 0) {
283 errors.push("no blessed service classes");
284 }
285 if (xos.tenant().blessed_flavors.length == 0) {
286 errors.push("no blessed flavors");
287 }
288 if (xos.tenant().blessed_images.length == 0) {
289 errors.push("no blessed images");
290 }
291 if (xos.tenant().blessed_sites.length == 0) {
292 errors.push("no blessed sites");
293 }
294
295 if (errors.length > 0) {
296 $("#tenantSummary").html("Tenant view sanity check failed<br>" + errors.join("<br>"));
297 return false;
298 }
299
300 return true;
Scott Baker342d9b92015-01-11 13:44:30 -0800301}
302
303XOSTenantApp.collectionLoadChange = function() {
304 stats = xos.getCollectionStatus();
305
306 if (!XOSTenantApp.navigationStarted) {
307 if (stats["isLoaded"] + stats["failedLoad"] >= stats["startedLoad"]) {
Scott Bakeraba91832015-01-12 13:37:31 -0800308 if (XOSTenantApp.sanityCheck()) {
309 XOSTenantApp.viewSlice(undefined);
310 }
Scott Baker342d9b92015-01-11 13:44:30 -0800311 } else {
312 $("#tenantSummary").html("<h3>Loading...</h3><div id='xos-startup-progress'></div>");
313 $("#xos-startup-progress").progressbar({value: stats["completedLoad"], max: stats["startedLoad"]});
314 }
315 }
316};
317
318XOSTenantApp.on("start", function() {
319 XOSTenantApp.buildViews();
320
Scott Baker342d9b92015-01-11 13:44:30 -0800321 // fire it once to initially show the progress bar
322 XOSTenantApp.collectionLoadChange();
323
324 // fire it each time the collection load status is updated
325 Backbone.on("xoslib:collectionLoadChange", XOSTenantApp.collectionLoadChange);
326});
327
328$(document).ready(function(){
329 XOSTenantApp.start();
330});
331