Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 1 | XOSTenantSite = XOSModel.extend( { |
| 2 | listFields: ["name", "allocated"], |
| 3 | modelName: "tenantSite", |
| 4 | collectionName: "tenantSites" |
| 5 | }); |
| 6 | |
| 7 | XOSTenantSiteCollection = XOSCollection.extend( { |
| 8 | listFields: ["name", "allocated"], |
| 9 | modelName: "tenantSite", |
| 10 | collectionName: "tenantSites", |
| 11 | |
Scott Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 12 | getFromSlice: function(slice) { |
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 13 | 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 Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 29 | |
| 30 | putToSlice: function(slice) { |
| 31 | slice.attributes.site_allocation = {}; |
| 32 | for (index in this.models) { |
Scott Baker | 435c2c9 | 2015-01-14 00:34:45 -0800 | [diff] [blame^] | 33 | var model = this.models[index]; |
Scott Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 34 | slice.attributes.site_allocation[ model.attributes.name ] = model.attributes.allocated; |
| 35 | } |
| 36 | }, |
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 37 | }); |
| 38 | |
Scott Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 39 | XOSEditUsersView = 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 Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 54 | XOSTenantButtonView = 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 Baker | cd07a59 | 2015-01-12 12:37:38 -0800 | [diff] [blame] | 64 | XOSTenantApp.addSlice(); |
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 65 | }, |
| 66 | |
| 67 | deleteClicked: function(e) { |
Scott Baker | 6b145aa | 2015-01-12 12:56:25 -0800 | [diff] [blame] | 68 | XOSTenantApp.deleteSlice(this.options.linkedView.model); |
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 69 | }, |
| 70 | |
| 71 | addUserClicked: function(e) { |
Scott Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 72 | XOSTenantApp.editUsers(this.options.linkedView.model); |
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 73 | }, |
| 74 | |
| 75 | saveClicked: function(e) { |
Scott Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 76 | model = this.options.linkedView.model; |
| 77 | model.tenantSiteCollection.putToSlice(model); |
Scott Baker | 435c2c9 | 2015-01-14 00:34:45 -0800 | [diff] [blame^] | 78 | model.attributes.users = model.usersBuffer; |
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 79 | this.options.linkedView.submitContinueClicked.call(this.options.linkedView, e); |
| 80 | }, |
| 81 | }); |
| 82 | |
| 83 | XOSTenantApp = new XOSApplication({ |
| 84 | logTableId: "#logTable", |
| 85 | statusMsgId: "#statusMsg", |
Scott Baker | cd07a59 | 2015-01-12 12:37:38 -0800 | [diff] [blame] | 86 | hideTabsByDefault: true, |
| 87 | varName: "XOSTenantApp", |
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 88 | }); |
| 89 | |
| 90 | XOSTenantApp.addRegions({ |
| 91 | tenantSliceSelector: "#tenantSliceSelector", |
| 92 | tenantSummary: "#tenantSummary", |
| 93 | tenantSiteList: "#tenantSiteList", |
| 94 | tenantButtons: "#tenantButtons", |
Scott Baker | cd07a59 | 2015-01-12 12:37:38 -0800 | [diff] [blame] | 95 | tenantAddSliceInterior: "#tenant-addslice-interior", |
Scott Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 96 | tenantEditUsersInterior: "#tenant-edit-users-interior", |
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 97 | }); |
| 98 | |
Scott Baker | cd07a59 | 2015-01-12 12:37:38 -0800 | [diff] [blame] | 99 | XOSTenantApp.buildViews = function() { |
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 100 | XOSTenantApp.tenantSites = new XOSTenantSiteCollection();
|
| 101 |
|
| 102 | tenantSummaryClass = XOSDetailView.extend({template: "#xos-detail-template",
|
| 103 | app: XOSTenantApp,
|
Scott Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 104 | detailFields: ["serviceClass", "default_image", "default_flavor", "network_ports", "mount_data_sets"],
|
| 105 | fieldDisplayNames: {serviceClass: "Service Level", "default_flavor": "Flavor", "default_image": "Image"},
|
| 106 | });
|
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 107 |
|
| 108 | XOSTenantApp.tenantSummaryView = tenantSummaryClass;
|
| 109 |
|
Scott Baker | cd07a59 | 2015-01-12 12:37:38 -0800 | [diff] [blame] | 110 | tenantAddClass = XOSDetailView.extend({template: "#xos-detail-template",
|
| 111 | app: XOSTenantApp,
|
| 112 | detailFields: ["name", "description"]});
|
| 113 |
|
| 114 | XOSTenantApp.tenantAddView = tenantAddClass;
|
| 115 |
|
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 116 | 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,
|
| 128 | });
|
| 129 |
|
| 130 | XOSTenantApp.tenantSiteListView = tenantSiteListClass;
|
| 131 |
|
| 132 | XOSTenantApp.tenantSliceSelectorView = SliceSelectorView.extend( {
|
| 133 | sliceChanged: function(id) {
|
Scott Baker | cd07a59 | 2015-01-12 12:37:38 -0800 | [diff] [blame] | 134 | XOSTenantApp.navToSlice(id);
|
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 135 | },
|
| 136 | });
|
| 137 |
|
| 138 | xos.sites.fetch();
|
| 139 | xos.slicesPlus.fetch();
|
| 140 | xos.tenantview.fetch();
|
| 141 | };
|
| 142 |
|
| 143 | make_choices = function(list_of_names, list_of_values) {
|
| 144 | result = [];
|
| 145 | if (!list_of_values) {
|
| 146 | for (index in list_of_names) {
|
| 147 | displayName = list_of_names[index];
|
| 148 | result.push( [displayName, displayName] );
|
| 149 | }
|
Scott Baker | aba9183 | 2015-01-12 13:37:31 -0800 | [diff] [blame] | 150 | } else {
|
| 151 | for (index in list_of_names) {
|
| 152 | displayName = list_of_names[index];
|
| 153 | id = list_of_values[index];
|
| 154 | result.push( [displayName, id] );
|
| 155 | }
|
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 156 | }
|
| 157 | return result;
|
| 158 | };
|
| 159 |
|
Scott Baker | cd07a59 | 2015-01-12 12:37:38 -0800 | [diff] [blame] | 160 | XOSTenantApp.navToSlice = function(id) {
|
| 161 | XOSTenantApp.viewSlice(xos.slicesPlus.get(id));
|
| 162 | };
|
| 163 |
|
| 164 | XOSTenantApp.adjustCollectionField = function(collectionName, id, fieldName, amount) {
|
| 165 | model = XOSTenantApp[collectionName].get(id);
|
| 166 | model.set(fieldName, Math.max(model.get(fieldName) + amount, 0));
|
| 167 | };
|
| 168 |
|
| 169 | XOSTenantApp.addSlice = function() {
|
| 170 | var app=this;
|
Scott Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 171 | model = new xos.slicesPlus.model({site: xos.tenant().current_user_site_id,
|
| 172 | name: xos.tenant().current_user_login_base + "_"});
|
Scott Baker | cd07a59 | 2015-01-12 12:37:38 -0800 | [diff] [blame] | 173 | console.log(model);
|
Scott Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 174 | var detailView = new XOSTenantApp.tenantAddView({model: model,
|
| 175 | collection: xos.slicesPlus,
|
| 176 | noSubmitButton: true,
|
| 177 | });
|
| 178 | detailView.dialog = $("#tenant-addslice-dialog");
|
Scott Baker | cd07a59 | 2015-01-12 12:37:38 -0800 | [diff] [blame] | 179 | app.tenantAddSliceInterior.show(detailView);
|
| 180 | $("#tenant-addslice-dialog").dialog({
|
| 181 | autoOpen: false, |
| 182 | modal: true, |
| 183 | width: 640, |
| 184 | buttons : { |
Scott Baker | 435c2c9 | 2015-01-14 00:34:45 -0800 | [diff] [blame^] | 185 | "Create Slice" : function() { |
Scott Baker | cd07a59 | 2015-01-12 12:37:38 -0800 | [diff] [blame] | 186 | var addDialog = this; |
Scott Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 187 | console.log("SAVE!!!"); |
Scott Baker | cd07a59 | 2015-01-12 12:37:38 -0800 | [diff] [blame] | 188 | detailView.synchronous = true; |
| 189 | detailView.afterSave = function() { $(addDialog).dialog("close"); XOSTenantApp.navToSlice(detailView.model.id); } |
| 190 | detailView.save(); |
| 191 | }, |
| 192 | "Cancel" : function() { |
| 193 | $(this).dialog("close"); |
| 194 | } |
| 195 | } |
| 196 | }); |
| 197 | $("#tenant-addslice-dialog").dialog("open");
|
| 198 | };
|
| 199 |
|
Scott Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 200 | XOSTenantApp.editUsers = function(model) {
|
| 201 | var app=this;
|
| 202 | var detailView = new XOSEditUsersView({model: model, collection: xos.slicesPlus});
|
| 203 | detailView.dialog = $("#tenant-edit-users-dialog");
|
| 204 | app.tenantEditUsersInterior.show(detailView);
|
| 205 | $("#tenant-edit-users-dialog").dialog({
|
| 206 | autoOpen: false, |
| 207 | modal: true, |
| 208 | width: 640, |
| 209 | buttons : { |
Scott Baker | 435c2c9 | 2015-01-14 00:34:45 -0800 | [diff] [blame^] | 210 | "Ok" : function() { |
Scott Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 211 | var editDialog = this; |
| 212 | user_ids = all_options($("#tenant-edit-users-dialog").find(".select-picker-to")); |
| 213 | user_ids = user_ids.map( function(x) { return parseInt(x,10); } ); |
Scott Baker | 435c2c9 | 2015-01-14 00:34:45 -0800 | [diff] [blame^] | 214 | model.usersBuffer = user_ids; |
Scott Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 215 | $(editDialog).dialog("close"); |
| 216 | }, |
| 217 | "Cancel" : function() { |
| 218 | $(this).dialog("close"); |
| 219 | } |
| 220 | } |
| 221 | }); |
| 222 | $("#tenant-edit-users-dialog").dialog("open");
|
| 223 | };
|
| 224 |
|
Scott Baker | 6b145aa | 2015-01-12 12:56:25 -0800 | [diff] [blame] | 225 | XOSTenantApp.deleteSlice = function(model) {
|
| 226 | var app=this;
|
| 227 | app.deleteDialog(model, function() { console.log("afterDelete"); app.viewSlice(undefined); });
|
| 228 | };
|
| 229 |
|
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 230 | XOSTenantApp.viewSlice = function(model) {
|
| 231 | if (!model && xos.slicesPlus.models.length > 0) {
|
| 232 | model = xos.slicesPlus.models[0];
|
| 233 | }
|
| 234 |
|
Scott Baker | b52f7af | 2015-01-13 14:41:41 -0800 | [diff] [blame] | 235 | if (model) {
|
| 236 | sliceSelector = new XOSTenantApp.tenantSliceSelectorView({collection: xos.slicesPlus,
|
| 237 | selectedID: model ? model.id : null,
|
| 238 | } );
|
| 239 | XOSTenantApp.sliceSelector = sliceSelector;
|
| 240 | XOSTenantApp.tenantSliceSelector.show(sliceSelector);
|
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 241 |
|
Scott Baker | b52f7af | 2015-01-13 14:41:41 -0800 | [diff] [blame] | 242 | tenantSummary = new XOSTenantApp.tenantSummaryView({model: model,
|
| 243 | choices: {mount_data_sets: make_choices(xos.tenant().public_volume_names, null),
|
| 244 | serviceClass: make_choices(xos.tenant().blessed_service_class_names, xos.tenant().blessed_service_classes),
|
| 245 | default_image: make_choices(xos.tenant().blessed_image_names, xos.tenant().blessed_images),
|
| 246 | default_flavor: make_choices(xos.tenant().blessed_flavor_names, xos.tenant().blessed_flavors),},
|
| 247 | });
|
| 248 | XOSTenantApp.tenantSummary.show(tenantSummary);
|
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 249 |
|
Scott Baker | b52f7af | 2015-01-13 14:41:41 -0800 | [diff] [blame] | 250 | tenantSites = new XOSTenantSiteCollection();
|
| 251 | tenantSites.getFromSlice(model);
|
Scott Baker | 435c2c9 | 2015-01-14 00:34:45 -0800 | [diff] [blame^] | 252 | 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 Baker | b52f7af | 2015-01-13 14:41:41 -0800 | [diff] [blame] | 253 | model.tenantSiteCollection = tenantSites;
|
| 254 | XOSTenantApp.tenantSites = tenantSites;
|
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 255 |
|
Scott Baker | b52f7af | 2015-01-13 14:41:41 -0800 | [diff] [blame] | 256 | tenantSiteList = new XOSTenantApp.tenantSiteListView({collection: tenantSites });
|
| 257 | XOSTenantApp.tenantSiteList.show(tenantSiteList);
|
| 258 | // on xos.slicePlus.sort, need to update xostenantapp.tenantSites
|
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 259 |
|
Scott Baker | b52f7af | 2015-01-13 14:41:41 -0800 | [diff] [blame] | 260 | XOSTenantApp.tenantButtons.show( new XOSTenantButtonView( { app: XOSTenantApp,
|
| 261 | linkedView: tenantSummary } ) );
|
| 262 | } else {
|
| 263 | XOSTenantApp.tenantSliceSelector.show(new HTMLView({html: ""}));
|
| 264 | XOSTenantApp.tenantSummary.show(new HTMLView({html: "You have no slices"}));
|
| 265 | XOSTenantApp.tenantSiteList.show(new HTMLView({html: ""}));
|
| 266 | XOSTenantApp.tenantButtons.show( new XOSTenantButtonView( { template: "#xos-tenant-buttons-noslice-template",
|
| 267 | app: XOSTenantApp,
|
| 268 | linkedView: tenantSummary } ) );
|
| 269 | }
|
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 270 | };
|
| 271 |
|
Scott Baker | aba9183 | 2015-01-12 13:37:31 -0800 | [diff] [blame] | 272 | XOSTenantApp.sanityCheck = function() {
|
| 273 | errors = [];
|
| 274 | if (xos.tenant().blessed_service_classes.length == 0) {
|
| 275 | errors.push("no blessed service classes");
|
| 276 | }
|
| 277 | if (xos.tenant().blessed_flavors.length == 0) {
|
| 278 | errors.push("no blessed flavors");
|
| 279 | }
|
| 280 | if (xos.tenant().blessed_images.length == 0) {
|
| 281 | errors.push("no blessed images");
|
| 282 | }
|
| 283 | if (xos.tenant().blessed_sites.length == 0) {
|
| 284 | errors.push("no blessed sites");
|
| 285 | }
|
| 286 |
|
| 287 | if (errors.length > 0) {
|
| 288 | $("#tenantSummary").html("Tenant view sanity check failed<br>" + errors.join("<br>"));
|
| 289 | return false;
|
| 290 | }
|
| 291 |
|
| 292 | return true;
|
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 293 | }
|
| 294 |
|
| 295 | XOSTenantApp.collectionLoadChange = function() {
|
| 296 | stats = xos.getCollectionStatus();
|
| 297 |
|
| 298 | if (!XOSTenantApp.navigationStarted) {
|
| 299 | if (stats["isLoaded"] + stats["failedLoad"] >= stats["startedLoad"]) {
|
Scott Baker | aba9183 | 2015-01-12 13:37:31 -0800 | [diff] [blame] | 300 | if (XOSTenantApp.sanityCheck()) {
|
| 301 | XOSTenantApp.viewSlice(undefined);
|
| 302 | }
|
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 303 | } else {
|
| 304 | $("#tenantSummary").html("<h3>Loading...</h3><div id='xos-startup-progress'></div>");
|
| 305 | $("#xos-startup-progress").progressbar({value: stats["completedLoad"], max: stats["startedLoad"]});
|
| 306 | }
|
| 307 | }
|
| 308 | };
|
| 309 |
|
| 310 | XOSTenantApp.on("start", function() {
|
| 311 | XOSTenantApp.buildViews(); |
| 312 | |
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 313 | // fire it once to initially show the progress bar |
| 314 | XOSTenantApp.collectionLoadChange(); |
| 315 | |
| 316 | // fire it each time the collection load status is updated |
| 317 | Backbone.on("xoslib:collectionLoadChange", XOSTenantApp.collectionLoadChange); |
| 318 | }); |
| 319 | |
| 320 | $(document).ready(function(){ |
| 321 | XOSTenantApp.start(); |
| 322 | }); |
| 323 | |