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"],
|
Scott Baker | e56d327 | 2015-01-14 00:47:50 -0800 | [diff] [blame] | 105 | fieldDisplayNames: {serviceClass: "Service Level", "default_flavor": "Flavor", "default_image": "Image", "mount_data_sets": "Data Sets"},
|
Scott Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 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,
|
Scott Baker | e56d327 | 2015-01-14 00:47:50 -0800 | [diff] [blame] | 128 | disablePaginate: true,
|
| 129 | disableFilter: true,
|
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 130 | });
|
| 131 |
|
| 132 | XOSTenantApp.tenantSiteListView = tenantSiteListClass;
|
| 133 |
|
| 134 | XOSTenantApp.tenantSliceSelectorView = SliceSelectorView.extend( {
|
| 135 | sliceChanged: function(id) {
|
Scott Baker | cd07a59 | 2015-01-12 12:37:38 -0800 | [diff] [blame] | 136 | XOSTenantApp.navToSlice(id);
|
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 137 | },
|
| 138 | });
|
| 139 |
|
| 140 | xos.sites.fetch();
|
| 141 | xos.slicesPlus.fetch();
|
| 142 | xos.tenantview.fetch();
|
| 143 | };
|
| 144 |
|
| 145 | make_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 Baker | aba9183 | 2015-01-12 13:37:31 -0800 | [diff] [blame] | 152 | } 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 Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 158 | }
|
| 159 | return result;
|
| 160 | };
|
| 161 |
|
Scott Baker | cd07a59 | 2015-01-12 12:37:38 -0800 | [diff] [blame] | 162 | XOSTenantApp.navToSlice = function(id) {
|
| 163 | XOSTenantApp.viewSlice(xos.slicesPlus.get(id));
|
| 164 | };
|
| 165 |
|
| 166 | XOSTenantApp.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 |
|
| 171 | XOSTenantApp.addSlice = function() {
|
| 172 | var app=this;
|
Scott Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 173 | model = new xos.slicesPlus.model({site: xos.tenant().current_user_site_id,
|
| 174 | name: xos.tenant().current_user_login_base + "_"});
|
Scott Baker | cd07a59 | 2015-01-12 12:37:38 -0800 | [diff] [blame] | 175 | console.log(model);
|
Scott Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 176 | var detailView = new XOSTenantApp.tenantAddView({model: model,
|
| 177 | collection: xos.slicesPlus,
|
| 178 | noSubmitButton: true,
|
| 179 | });
|
| 180 | detailView.dialog = $("#tenant-addslice-dialog");
|
Scott Baker | cd07a59 | 2015-01-12 12:37:38 -0800 | [diff] [blame] | 181 | app.tenantAddSliceInterior.show(detailView);
|
| 182 | $("#tenant-addslice-dialog").dialog({
|
| 183 | autoOpen: false, |
| 184 | modal: true, |
| 185 | width: 640, |
| 186 | buttons : { |
Scott Baker | 435c2c9 | 2015-01-14 00:34:45 -0800 | [diff] [blame] | 187 | "Create Slice" : function() { |
Scott Baker | cd07a59 | 2015-01-12 12:37:38 -0800 | [diff] [blame] | 188 | var addDialog = this; |
Scott Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 189 | console.log("SAVE!!!"); |
Scott Baker | cd07a59 | 2015-01-12 12:37:38 -0800 | [diff] [blame] | 190 | detailView.synchronous = true; |
| 191 | detailView.afterSave = function() { $(addDialog).dialog("close"); XOSTenantApp.navToSlice(detailView.model.id); } |
| 192 | detailView.save(); |
| 193 | }, |
| 194 | "Cancel" : function() { |
| 195 | $(this).dialog("close"); |
| 196 | } |
| 197 | } |
| 198 | }); |
| 199 | $("#tenant-addslice-dialog").dialog("open");
|
| 200 | };
|
| 201 |
|
Scott Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 202 | XOSTenantApp.editUsers = function(model) {
|
| 203 | var app=this;
|
| 204 | var detailView = new XOSEditUsersView({model: model, collection: xos.slicesPlus});
|
| 205 | detailView.dialog = $("#tenant-edit-users-dialog");
|
| 206 | app.tenantEditUsersInterior.show(detailView);
|
| 207 | $("#tenant-edit-users-dialog").dialog({
|
| 208 | autoOpen: false, |
| 209 | modal: true, |
| 210 | width: 640, |
| 211 | buttons : { |
Scott Baker | 435c2c9 | 2015-01-14 00:34:45 -0800 | [diff] [blame] | 212 | "Ok" : function() { |
Scott Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 213 | var editDialog = this; |
| 214 | user_ids = all_options($("#tenant-edit-users-dialog").find(".select-picker-to")); |
| 215 | user_ids = user_ids.map( function(x) { return parseInt(x,10); } ); |
Scott Baker | 435c2c9 | 2015-01-14 00:34:45 -0800 | [diff] [blame] | 216 | model.usersBuffer = user_ids; |
Scott Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 217 | $(editDialog).dialog("close"); |
| 218 | }, |
| 219 | "Cancel" : function() { |
| 220 | $(this).dialog("close"); |
| 221 | } |
| 222 | } |
| 223 | }); |
| 224 | $("#tenant-edit-users-dialog").dialog("open");
|
| 225 | };
|
| 226 |
|
Scott Baker | 6b145aa | 2015-01-12 12:56:25 -0800 | [diff] [blame] | 227 | XOSTenantApp.deleteSlice = function(model) {
|
| 228 | var app=this;
|
| 229 | app.deleteDialog(model, function() { console.log("afterDelete"); app.viewSlice(undefined); });
|
| 230 | };
|
| 231 |
|
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 232 | XOSTenantApp.viewSlice = function(model) {
|
| 233 | if (!model && xos.slicesPlus.models.length > 0) {
|
| 234 | model = xos.slicesPlus.models[0];
|
| 235 | }
|
| 236 |
|
Scott Baker | b52f7af | 2015-01-13 14:41:41 -0800 | [diff] [blame] | 237 | if (model) {
|
| 238 | sliceSelector = new XOSTenantApp.tenantSliceSelectorView({collection: xos.slicesPlus,
|
| 239 | selectedID: model ? model.id : null,
|
| 240 | } );
|
| 241 | XOSTenantApp.sliceSelector = sliceSelector;
|
| 242 | XOSTenantApp.tenantSliceSelector.show(sliceSelector);
|
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 243 |
|
Scott Baker | b52f7af | 2015-01-13 14:41:41 -0800 | [diff] [blame] | 244 | tenantSummary = new XOSTenantApp.tenantSummaryView({model: model,
|
| 245 | choices: {mount_data_sets: make_choices(xos.tenant().public_volume_names, null),
|
| 246 | serviceClass: make_choices(xos.tenant().blessed_service_class_names, xos.tenant().blessed_service_classes),
|
| 247 | default_image: make_choices(xos.tenant().blessed_image_names, xos.tenant().blessed_images),
|
| 248 | default_flavor: make_choices(xos.tenant().blessed_flavor_names, xos.tenant().blessed_flavors),},
|
| 249 | });
|
| 250 | XOSTenantApp.tenantSummary.show(tenantSummary);
|
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 251 |
|
Scott Baker | b52f7af | 2015-01-13 14:41:41 -0800 | [diff] [blame] | 252 | tenantSites = new XOSTenantSiteCollection();
|
| 253 | tenantSites.getFromSlice(model);
|
Scott Baker | 435c2c9 | 2015-01-14 00:34:45 -0800 | [diff] [blame] | 254 | 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] | 255 | model.tenantSiteCollection = tenantSites;
|
| 256 | XOSTenantApp.tenantSites = tenantSites;
|
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 257 |
|
Scott Baker | b52f7af | 2015-01-13 14:41:41 -0800 | [diff] [blame] | 258 | tenantSiteList = new XOSTenantApp.tenantSiteListView({collection: tenantSites });
|
| 259 | XOSTenantApp.tenantSiteList.show(tenantSiteList);
|
| 260 | // on xos.slicePlus.sort, need to update xostenantapp.tenantSites
|
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 261 |
|
Scott Baker | b52f7af | 2015-01-13 14:41:41 -0800 | [diff] [blame] | 262 | XOSTenantApp.tenantButtons.show( new XOSTenantButtonView( { app: XOSTenantApp,
|
| 263 | linkedView: tenantSummary } ) );
|
| 264 | } else {
|
| 265 | XOSTenantApp.tenantSliceSelector.show(new HTMLView({html: ""}));
|
| 266 | XOSTenantApp.tenantSummary.show(new HTMLView({html: "You have no slices"}));
|
| 267 | XOSTenantApp.tenantSiteList.show(new HTMLView({html: ""}));
|
| 268 | XOSTenantApp.tenantButtons.show( new XOSTenantButtonView( { template: "#xos-tenant-buttons-noslice-template",
|
| 269 | app: XOSTenantApp,
|
| 270 | linkedView: tenantSummary } ) );
|
| 271 | }
|
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 272 | };
|
| 273 |
|
Scott Baker | aba9183 | 2015-01-12 13:37:31 -0800 | [diff] [blame] | 274 | XOSTenantApp.sanityCheck = function() {
|
| 275 | errors = [];
|
| 276 | if (xos.tenant().blessed_service_classes.length == 0) {
|
| 277 | errors.push("no blessed service classes");
|
| 278 | }
|
| 279 | if (xos.tenant().blessed_flavors.length == 0) {
|
| 280 | errors.push("no blessed flavors");
|
| 281 | }
|
| 282 | if (xos.tenant().blessed_images.length == 0) {
|
| 283 | errors.push("no blessed images");
|
| 284 | }
|
| 285 | if (xos.tenant().blessed_sites.length == 0) {
|
| 286 | errors.push("no blessed sites");
|
| 287 | }
|
| 288 |
|
| 289 | if (errors.length > 0) {
|
| 290 | $("#tenantSummary").html("Tenant view sanity check failed<br>" + errors.join("<br>"));
|
| 291 | return false;
|
| 292 | }
|
| 293 |
|
| 294 | return true;
|
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 295 | }
|
| 296 |
|
| 297 | XOSTenantApp.collectionLoadChange = function() {
|
| 298 | stats = xos.getCollectionStatus();
|
| 299 |
|
| 300 | if (!XOSTenantApp.navigationStarted) {
|
| 301 | if (stats["isLoaded"] + stats["failedLoad"] >= stats["startedLoad"]) {
|
Scott Baker | aba9183 | 2015-01-12 13:37:31 -0800 | [diff] [blame] | 302 | if (XOSTenantApp.sanityCheck()) {
|
| 303 | XOSTenantApp.viewSlice(undefined);
|
| 304 | }
|
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 305 | } else {
|
| 306 | $("#tenantSummary").html("<h3>Loading...</h3><div id='xos-startup-progress'></div>");
|
| 307 | $("#xos-startup-progress").progressbar({value: stats["completedLoad"], max: stats["startedLoad"]});
|
| 308 | }
|
| 309 | }
|
| 310 | };
|
| 311 |
|
| 312 | XOSTenantApp.on("start", function() {
|
| 313 | XOSTenantApp.buildViews(); |
| 314 | |
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 315 | // fire it once to initially show the progress bar |
| 316 | XOSTenantApp.collectionLoadChange(); |
| 317 | |
| 318 | // fire it each time the collection load status is updated |
| 319 | Backbone.on("xoslib:collectionLoadChange", XOSTenantApp.collectionLoadChange); |
| 320 | }); |
| 321 | |
| 322 | $(document).ready(function(){ |
| 323 | XOSTenantApp.start(); |
| 324 | }); |
| 325 | |