blob: 4c70f704ce694a0555db06f7b184c4f05a5b5c3c [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) {
33 model = this.models[index];
34 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 Baker342d9b92015-01-11 13:44:30 -080078 this.options.linkedView.submitContinueClicked.call(this.options.linkedView, e);
79 },
80 });
81
82XOSTenantApp = new XOSApplication({
83 logTableId: "#logTable",
84 statusMsgId: "#statusMsg",
Scott Bakercd07a592015-01-12 12:37:38 -080085 hideTabsByDefault: true,
86 varName: "XOSTenantApp",
Scott Baker342d9b92015-01-11 13:44:30 -080087});
88
89XOSTenantApp.addRegions({
90 tenantSliceSelector: "#tenantSliceSelector",
91 tenantSummary: "#tenantSummary",
92 tenantSiteList: "#tenantSiteList",
93 tenantButtons: "#tenantButtons",
Scott Bakercd07a592015-01-12 12:37:38 -080094 tenantAddSliceInterior: "#tenant-addslice-interior",
Scott Baker97acad92015-01-12 19:45:40 -080095 tenantEditUsersInterior: "#tenant-edit-users-interior",
Scott Baker342d9b92015-01-11 13:44:30 -080096});
97
Scott Bakercd07a592015-01-12 12:37:38 -080098XOSTenantApp.buildViews = function() {
Scott Baker342d9b92015-01-11 13:44:30 -080099 XOSTenantApp.tenantSites = new XOSTenantSiteCollection();
100
101 tenantSummaryClass = XOSDetailView.extend({template: "#xos-detail-template",
102 app: XOSTenantApp,
Scott Baker97acad92015-01-12 19:45:40 -0800103 detailFields: ["serviceClass", "default_image", "default_flavor", "network_ports", "mount_data_sets"],
104 fieldDisplayNames: {serviceClass: "Service Level", "default_flavor": "Flavor", "default_image": "Image"},
105 });
Scott Baker342d9b92015-01-11 13:44:30 -0800106
107 XOSTenantApp.tenantSummaryView = tenantSummaryClass;
108
Scott Bakercd07a592015-01-12 12:37:38 -0800109 tenantAddClass = XOSDetailView.extend({template: "#xos-detail-template",
110 app: XOSTenantApp,
111 detailFields: ["name", "description"]});
112
113 XOSTenantApp.tenantAddView = tenantAddClass;
114
Scott Baker342d9b92015-01-11 13:44:30 -0800115 tenantSiteItemClass = XOSItemView.extend({template: "#xos-listitem-template",
116 app: XOSTenantApp});
117
118 XOSTenantApp.tenantSiteItemView = tenantSiteItemClass;
119
120 tenantSiteListClass = XOSDataTableView.extend({template: "#xos-list-template",
121 app: XOSTenantApp,
122 childView: tenantSiteItemClass,
123 collection: XOSTenantApp.tenantSites,
124 title: "sites",
125 inputType: {allocated: "spinner"},
126 noDeleteColumn: true,
127 });
128
129 XOSTenantApp.tenantSiteListView = tenantSiteListClass;
130
131 XOSTenantApp.tenantSliceSelectorView = SliceSelectorView.extend( {
132 sliceChanged: function(id) {
Scott Bakercd07a592015-01-12 12:37:38 -0800133 XOSTenantApp.navToSlice(id);
Scott Baker342d9b92015-01-11 13:44:30 -0800134 },
135 });
136
137 xos.sites.fetch();
138 xos.slicesPlus.fetch();
139 xos.tenantview.fetch();
140};
141
142make_choices = function(list_of_names, list_of_values) {
143 result = [];
144 if (!list_of_values) {
145 for (index in list_of_names) {
146 displayName = list_of_names[index];
147 result.push( [displayName, displayName] );
148 }
Scott Bakeraba91832015-01-12 13:37:31 -0800149 } else {
150 for (index in list_of_names) {
151 displayName = list_of_names[index];
152 id = list_of_values[index];
153 result.push( [displayName, id] );
154 }
Scott Baker342d9b92015-01-11 13:44:30 -0800155 }
156 return result;
157};
158
Scott Bakercd07a592015-01-12 12:37:38 -0800159XOSTenantApp.navToSlice = function(id) {
160 XOSTenantApp.viewSlice(xos.slicesPlus.get(id));
161};
162
163XOSTenantApp.adjustCollectionField = function(collectionName, id, fieldName, amount) {
164 model = XOSTenantApp[collectionName].get(id);
165 model.set(fieldName, Math.max(model.get(fieldName) + amount, 0));
166};
167
168XOSTenantApp.addSlice = function() {
169 var app=this;
Scott Baker97acad92015-01-12 19:45:40 -0800170 model = new xos.slicesPlus.model({site: xos.tenant().current_user_site_id,
171 name: xos.tenant().current_user_login_base + "_"});
Scott Bakercd07a592015-01-12 12:37:38 -0800172 console.log(model);
Scott Baker97acad92015-01-12 19:45:40 -0800173 var detailView = new XOSTenantApp.tenantAddView({model: model,
174 collection: xos.slicesPlus,
175 noSubmitButton: true,
176 });
177 detailView.dialog = $("#tenant-addslice-dialog");
Scott Bakercd07a592015-01-12 12:37:38 -0800178 app.tenantAddSliceInterior.show(detailView);
179 $("#tenant-addslice-dialog").dialog({
180 autoOpen: false,
181 modal: true,
182 width: 640,
183 buttons : {
184 "Save" : function() {
185 var addDialog = this;
Scott Baker97acad92015-01-12 19:45:40 -0800186 console.log("SAVE!!!");
Scott Bakercd07a592015-01-12 12:37:38 -0800187 detailView.synchronous = true;
188 detailView.afterSave = function() { $(addDialog).dialog("close"); XOSTenantApp.navToSlice(detailView.model.id); }
189 detailView.save();
190 },
191 "Cancel" : function() {
192 $(this).dialog("close");
193 }
194 }
195 });
196 $("#tenant-addslice-dialog").dialog("open");
197};
198
Scott Baker97acad92015-01-12 19:45:40 -0800199XOSTenantApp.editUsers = function(model) {
200 var app=this;
201 var detailView = new XOSEditUsersView({model: model, collection: xos.slicesPlus});
202 detailView.dialog = $("#tenant-edit-users-dialog");
203 app.tenantEditUsersInterior.show(detailView);
204 $("#tenant-edit-users-dialog").dialog({
205 autoOpen: false,
206 modal: true,
207 width: 640,
208 buttons : {
209 "Save" : function() {
210 var editDialog = this;
211 user_ids = all_options($("#tenant-edit-users-dialog").find(".select-picker-to"));
212 user_ids = user_ids.map( function(x) { return parseInt(x,10); } );
213 model.attributes.users = user_ids;
214 $(editDialog).dialog("close");
215 },
216 "Cancel" : function() {
217 $(this).dialog("close");
218 }
219 }
220 });
221 $("#tenant-edit-users-dialog").dialog("open");
222};
223
Scott Baker6b145aa2015-01-12 12:56:25 -0800224XOSTenantApp.deleteSlice = function(model) {
225 var app=this;
226 app.deleteDialog(model, function() { console.log("afterDelete"); app.viewSlice(undefined); });
227};
228
Scott Baker342d9b92015-01-11 13:44:30 -0800229XOSTenantApp.viewSlice = function(model) {
230 if (!model && xos.slicesPlus.models.length > 0) {
231 model = xos.slicesPlus.models[0];
232 }
233
Scott Bakerb52f7af2015-01-13 14:41:41 -0800234 if (model) {
235 sliceSelector = new XOSTenantApp.tenantSliceSelectorView({collection: xos.slicesPlus,
236 selectedID: model ? model.id : null,
237 } );
238 XOSTenantApp.sliceSelector = sliceSelector;
239 XOSTenantApp.tenantSliceSelector.show(sliceSelector);
Scott Baker342d9b92015-01-11 13:44:30 -0800240
Scott Bakerb52f7af2015-01-13 14:41:41 -0800241 tenantSummary = new XOSTenantApp.tenantSummaryView({model: model,
242 choices: {mount_data_sets: make_choices(xos.tenant().public_volume_names, null),
243 serviceClass: make_choices(xos.tenant().blessed_service_class_names, xos.tenant().blessed_service_classes),
244 default_image: make_choices(xos.tenant().blessed_image_names, xos.tenant().blessed_images),
245 default_flavor: make_choices(xos.tenant().blessed_flavor_names, xos.tenant().blessed_flavors),},
246 });
247 XOSTenantApp.tenantSummary.show(tenantSummary);
Scott Baker342d9b92015-01-11 13:44:30 -0800248
Scott Bakerb52f7af2015-01-13 14:41:41 -0800249 tenantSites = new XOSTenantSiteCollection();
250 tenantSites.getFromSlice(model);
251 model.tenantSiteCollection = tenantSites;
252 XOSTenantApp.tenantSites = tenantSites;
Scott Baker342d9b92015-01-11 13:44:30 -0800253
Scott Bakerb52f7af2015-01-13 14:41:41 -0800254 tenantSiteList = new XOSTenantApp.tenantSiteListView({collection: tenantSites });
255 XOSTenantApp.tenantSiteList.show(tenantSiteList);
256 // on xos.slicePlus.sort, need to update xostenantapp.tenantSites
Scott Baker342d9b92015-01-11 13:44:30 -0800257
Scott Bakerb52f7af2015-01-13 14:41:41 -0800258 XOSTenantApp.tenantButtons.show( new XOSTenantButtonView( { app: XOSTenantApp,
259 linkedView: tenantSummary } ) );
260 } else {
261 XOSTenantApp.tenantSliceSelector.show(new HTMLView({html: ""}));
262 XOSTenantApp.tenantSummary.show(new HTMLView({html: "You have no slices"}));
263 XOSTenantApp.tenantSiteList.show(new HTMLView({html: ""}));
264 XOSTenantApp.tenantButtons.show( new XOSTenantButtonView( { template: "#xos-tenant-buttons-noslice-template",
265 app: XOSTenantApp,
266 linkedView: tenantSummary } ) );
267 }
Scott Baker342d9b92015-01-11 13:44:30 -0800268};
269
Scott Bakeraba91832015-01-12 13:37:31 -0800270XOSTenantApp.sanityCheck = function() {
271 errors = [];
272 if (xos.tenant().blessed_service_classes.length == 0) {
273 errors.push("no blessed service classes");
274 }
275 if (xos.tenant().blessed_flavors.length == 0) {
276 errors.push("no blessed flavors");
277 }
278 if (xos.tenant().blessed_images.length == 0) {
279 errors.push("no blessed images");
280 }
281 if (xos.tenant().blessed_sites.length == 0) {
282 errors.push("no blessed sites");
283 }
284
285 if (errors.length > 0) {
286 $("#tenantSummary").html("Tenant view sanity check failed<br>" + errors.join("<br>"));
287 return false;
288 }
289
290 return true;
Scott Baker342d9b92015-01-11 13:44:30 -0800291}
292
293XOSTenantApp.collectionLoadChange = function() {
294 stats = xos.getCollectionStatus();
295
296 if (!XOSTenantApp.navigationStarted) {
297 if (stats["isLoaded"] + stats["failedLoad"] >= stats["startedLoad"]) {
Scott Bakeraba91832015-01-12 13:37:31 -0800298 if (XOSTenantApp.sanityCheck()) {
299 XOSTenantApp.viewSlice(undefined);
300 }
Scott Baker342d9b92015-01-11 13:44:30 -0800301 } else {
302 $("#tenantSummary").html("<h3>Loading...</h3><div id='xos-startup-progress'></div>");
303 $("#xos-startup-progress").progressbar({value: stats["completedLoad"], max: stats["startedLoad"]});
304 }
305 }
306};
307
308XOSTenantApp.on("start", function() {
309 XOSTenantApp.buildViews();
310
Scott Baker342d9b92015-01-11 13:44:30 -0800311 // fire it once to initially show the progress bar
312 XOSTenantApp.collectionLoadChange();
313
314 // fire it each time the collection load status is updated
315 Backbone.on("xoslib:collectionLoadChange", XOSTenantApp.collectionLoadChange);
316});
317
318$(document).ready(function(){
319 XOSTenantApp.start();
320});
321