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) { |
| 33 | model = this.models[index]; |
| 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 | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 78 | this.options.linkedView.submitContinueClicked.call(this.options.linkedView, e); |
| 79 | }, |
| 80 | }); |
| 81 | |
| 82 | XOSTenantApp = new XOSApplication({ |
| 83 | logTableId: "#logTable", |
| 84 | statusMsgId: "#statusMsg", |
Scott Baker | cd07a59 | 2015-01-12 12:37:38 -0800 | [diff] [blame] | 85 | hideTabsByDefault: true, |
| 86 | varName: "XOSTenantApp", |
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 87 | }); |
| 88 | |
| 89 | XOSTenantApp.addRegions({ |
| 90 | tenantSliceSelector: "#tenantSliceSelector", |
| 91 | tenantSummary: "#tenantSummary", |
| 92 | tenantSiteList: "#tenantSiteList", |
| 93 | tenantButtons: "#tenantButtons", |
Scott Baker | cd07a59 | 2015-01-12 12:37:38 -0800 | [diff] [blame] | 94 | tenantAddSliceInterior: "#tenant-addslice-interior", |
Scott Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 95 | tenantEditUsersInterior: "#tenant-edit-users-interior", |
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 96 | }); |
| 97 | |
Scott Baker | cd07a59 | 2015-01-12 12:37:38 -0800 | [diff] [blame] | 98 | XOSTenantApp.buildViews = function() { |
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 99 | XOSTenantApp.tenantSites = new XOSTenantSiteCollection();
|
| 100 |
|
| 101 | tenantSummaryClass = XOSDetailView.extend({template: "#xos-detail-template",
|
| 102 | app: XOSTenantApp,
|
Scott Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 103 | detailFields: ["serviceClass", "default_image", "default_flavor", "network_ports", "mount_data_sets"],
|
| 104 | fieldDisplayNames: {serviceClass: "Service Level", "default_flavor": "Flavor", "default_image": "Image"},
|
| 105 | });
|
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 106 |
|
| 107 | XOSTenantApp.tenantSummaryView = tenantSummaryClass;
|
| 108 |
|
Scott Baker | cd07a59 | 2015-01-12 12:37:38 -0800 | [diff] [blame] | 109 | tenantAddClass = XOSDetailView.extend({template: "#xos-detail-template",
|
| 110 | app: XOSTenantApp,
|
| 111 | detailFields: ["name", "description"]});
|
| 112 |
|
| 113 | XOSTenantApp.tenantAddView = tenantAddClass;
|
| 114 |
|
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 115 | 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 Baker | cd07a59 | 2015-01-12 12:37:38 -0800 | [diff] [blame] | 133 | XOSTenantApp.navToSlice(id);
|
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 134 | },
|
| 135 | });
|
| 136 |
|
| 137 | xos.sites.fetch();
|
| 138 | xos.slicesPlus.fetch();
|
| 139 | xos.tenantview.fetch();
|
| 140 | };
|
| 141 |
|
| 142 | make_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 Baker | aba9183 | 2015-01-12 13:37:31 -0800 | [diff] [blame] | 149 | } 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 Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 155 | }
|
| 156 | return result;
|
| 157 | };
|
| 158 |
|
Scott Baker | cd07a59 | 2015-01-12 12:37:38 -0800 | [diff] [blame] | 159 | XOSTenantApp.navToSlice = function(id) {
|
| 160 | XOSTenantApp.viewSlice(xos.slicesPlus.get(id));
|
| 161 | };
|
| 162 |
|
| 163 | XOSTenantApp.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 |
|
| 168 | XOSTenantApp.addSlice = function() {
|
| 169 | var app=this;
|
Scott Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 170 | model = new xos.slicesPlus.model({site: xos.tenant().current_user_site_id,
|
| 171 | name: xos.tenant().current_user_login_base + "_"});
|
Scott Baker | cd07a59 | 2015-01-12 12:37:38 -0800 | [diff] [blame] | 172 | console.log(model);
|
Scott Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 173 | var detailView = new XOSTenantApp.tenantAddView({model: model,
|
| 174 | collection: xos.slicesPlus,
|
| 175 | noSubmitButton: true,
|
| 176 | });
|
| 177 | detailView.dialog = $("#tenant-addslice-dialog");
|
Scott Baker | cd07a59 | 2015-01-12 12:37:38 -0800 | [diff] [blame] | 178 | 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 Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 186 | console.log("SAVE!!!"); |
Scott Baker | cd07a59 | 2015-01-12 12:37:38 -0800 | [diff] [blame] | 187 | 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 Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 199 | XOSTenantApp.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 Baker | 6b145aa | 2015-01-12 12:56:25 -0800 | [diff] [blame] | 224 | XOSTenantApp.deleteSlice = function(model) {
|
| 225 | var app=this;
|
| 226 | app.deleteDialog(model, function() { console.log("afterDelete"); app.viewSlice(undefined); });
|
| 227 | };
|
| 228 |
|
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 229 | XOSTenantApp.viewSlice = function(model) {
|
| 230 | if (!model && xos.slicesPlus.models.length > 0) {
|
| 231 | model = xos.slicesPlus.models[0];
|
| 232 | }
|
| 233 |
|
Scott Baker | b52f7af | 2015-01-13 14:41:41 -0800 | [diff] [blame] | 234 | 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 Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 240 |
|
Scott Baker | b52f7af | 2015-01-13 14:41:41 -0800 | [diff] [blame] | 241 | 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 Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 248 |
|
Scott Baker | b52f7af | 2015-01-13 14:41:41 -0800 | [diff] [blame] | 249 | tenantSites = new XOSTenantSiteCollection();
|
| 250 | tenantSites.getFromSlice(model);
|
| 251 | model.tenantSiteCollection = tenantSites;
|
| 252 | XOSTenantApp.tenantSites = tenantSites;
|
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 253 |
|
Scott Baker | b52f7af | 2015-01-13 14:41:41 -0800 | [diff] [blame] | 254 | tenantSiteList = new XOSTenantApp.tenantSiteListView({collection: tenantSites });
|
| 255 | XOSTenantApp.tenantSiteList.show(tenantSiteList);
|
| 256 | // on xos.slicePlus.sort, need to update xostenantapp.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 | 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 Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 268 | };
|
| 269 |
|
Scott Baker | aba9183 | 2015-01-12 13:37:31 -0800 | [diff] [blame] | 270 | XOSTenantApp.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 Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 291 | }
|
| 292 |
|
| 293 | XOSTenantApp.collectionLoadChange = function() {
|
| 294 | stats = xos.getCollectionStatus();
|
| 295 |
|
| 296 | if (!XOSTenantApp.navigationStarted) {
|
| 297 | if (stats["isLoaded"] + stats["failedLoad"] >= stats["startedLoad"]) {
|
Scott Baker | aba9183 | 2015-01-12 13:37:31 -0800 | [diff] [blame] | 298 | if (XOSTenantApp.sanityCheck()) {
|
| 299 | XOSTenantApp.viewSlice(undefined);
|
| 300 | }
|
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 301 | } 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 |
|
| 308 | XOSTenantApp.on("start", function() {
|
| 309 | XOSTenantApp.buildViews(); |
| 310 | |
Scott Baker | 342d9b9 | 2015-01-11 13:44:30 -0800 | [diff] [blame] | 311 | // 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 | |