add sanity check; add default_flavors and default_images
[plstackapi.git] / planetstack / core / xoslib / static / js / xosTenant.js
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
12     updateFromSlice: function(slice) {
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     },
29 });
30
31 XOSTenantButtonView = Marionette.ItemView.extend({
32             template: "#xos-tenant-buttons-template",
33
34             events: {"click button.btn-tenant-create": "createClicked",
35                      "click button.btn-tenant-delete": "deleteClicked",
36                      "click button.btn-tenant-add-user": "addUserClicked",
37                      "click button.btn-tenant-save": "saveClicked",
38                      },
39
40             createClicked: function(e) {
41                      XOSTenantApp.addSlice();
42                      },
43
44             deleteClicked: function(e) {
45                      XOSTenantApp.deleteSlice(this.options.linkedView.model);
46                      },
47
48             addUserClicked: function(e) {
49                      },
50
51             saveClicked: function(e) {
52                      this.options.linkedView.submitContinueClicked.call(this.options.linkedView, e);
53                      },
54             });
55
56 XOSTenantApp = new XOSApplication({
57     logTableId: "#logTable",
58     statusMsgId: "#statusMsg",
59     hideTabsByDefault: true,
60     varName: "XOSTenantApp",
61 });
62
63 XOSTenantApp.addRegions({
64     tenantSliceSelector: "#tenantSliceSelector",
65     tenantSummary: "#tenantSummary",
66     tenantSiteList: "#tenantSiteList",
67     tenantButtons: "#tenantButtons",
68     tenantAddSliceInterior: "#tenant-addslice-interior",
69 });
70
71 XOSTenantApp.buildViews = function() {
72      XOSTenantApp.tenantSites = new XOSTenantSiteCollection();\r
73 \r
74      tenantSummaryClass = XOSDetailView.extend({template: "#xos-detail-template",\r
75                                                 app: XOSTenantApp,\r
76                                                 detailFields: ["serviceClass", "default_image", "default_flavor", "network_ports", "mount_data_sets"]});\r
77 \r
78      XOSTenantApp.tenantSummaryView = tenantSummaryClass;\r
79 \r
80      tenantAddClass = XOSDetailView.extend({template: "#xos-detail-template",\r
81                                                 app: XOSTenantApp,\r
82                                                 detailFields: ["name", "description"]});\r
83 \r
84      XOSTenantApp.tenantAddView = tenantAddClass;\r
85 \r
86      tenantSiteItemClass = XOSItemView.extend({template: "#xos-listitem-template",\r
87                                                app: XOSTenantApp});\r
88 \r
89      XOSTenantApp.tenantSiteItemView = tenantSiteItemClass;\r
90 \r
91      tenantSiteListClass = XOSDataTableView.extend({template: "#xos-list-template",\r
92                                                app: XOSTenantApp,\r
93                                                childView: tenantSiteItemClass,\r
94                                                collection: XOSTenantApp.tenantSites,\r
95                                                title: "sites",\r
96                                                inputType: {allocated: "spinner"},\r
97                                                noDeleteColumn: true,\r
98                                                });\r
99 \r
100      XOSTenantApp.tenantSiteListView = tenantSiteListClass;\r
101 \r
102      XOSTenantApp.tenantSliceSelectorView = SliceSelectorView.extend( {\r
103          sliceChanged: function(id) {\r
104              XOSTenantApp.navToSlice(id);\r
105          },\r
106      });\r
107 \r
108      xos.sites.fetch();\r
109      xos.slicesPlus.fetch();\r
110      xos.tenantview.fetch();\r
111 };\r
112 \r
113 make_choices = function(list_of_names, list_of_values) {\r
114     result = [];\r
115     if (!list_of_values) {\r
116         for (index in list_of_names) {\r
117             displayName = list_of_names[index];\r
118             result.push( [displayName, displayName] );\r
119         }\r
120     } else {\r
121         for (index in list_of_names) {\r
122             displayName = list_of_names[index];\r
123             id = list_of_values[index];\r
124             result.push( [displayName, id] );\r
125         }\r
126     }\r
127     return result;\r
128 };\r
129 \r
130 XOSTenantApp.navToSlice = function(id) {\r
131     XOSTenantApp.viewSlice(xos.slicesPlus.get(id));\r
132 };\r
133 \r
134 XOSTenantApp.adjustCollectionField = function(collectionName, id, fieldName, amount) {\r
135     model = XOSTenantApp[collectionName].get(id);\r
136     model.set(fieldName, Math.max(model.get(fieldName) + amount, 0));\r
137 };\r
138 \r
139 XOSTenantApp.addSlice = function() {\r
140     var app=this;\r
141     model = new xos.slicesPlus.model({site: xos.tenant().current_user_site_id});\r
142     console.log(model);\r
143     var detailView = new XOSTenantApp.tenantAddView({model: model, collection: xos.slicesPlus});\r
144     detailView.dialog = $("tenant-addslice-dialog");\r
145     app.tenantAddSliceInterior.show(detailView);\r
146     $("#tenant-addslice-dialog").dialog({\r
147        autoOpen: false,
148        modal: true,
149        width: 640,
150        buttons : {
151             "Save" : function() {
152               var addDialog = this;
153               detailView.synchronous = true;
154               detailView.afterSave = function() { $(addDialog).dialog("close"); XOSTenantApp.navToSlice(detailView.model.id); }
155               detailView.save();
156             },
157             "Cancel" : function() {
158               $(this).dialog("close");
159             }
160           }
161         });
162     $("#tenant-addslice-dialog").dialog("open");\r
163 };\r
164 \r
165 XOSTenantApp.deleteSlice = function(model) {\r
166     var app=this;\r
167     app.deleteDialog(model, function() { console.log("afterDelete"); app.viewSlice(undefined); });\r
168 };\r
169 \r
170 XOSTenantApp.viewSlice = function(model) {\r
171     if (!model && xos.slicesPlus.models.length > 0) {\r
172         model = xos.slicesPlus.models[0];\r
173     }\r
174 \r
175     sliceSelector = new XOSTenantApp.tenantSliceSelectorView({collection: xos.slicesPlus,\r
176                                                               selectedID: model.id,\r
177                                                              } );\r
178     XOSTenantApp.sliceSelector = sliceSelector;\r
179     XOSTenantApp.tenantSliceSelector.show(sliceSelector);\r
180 \r
181     tenantSummary = new XOSTenantApp.tenantSummaryView({model: model,\r
182                                                         choices: {mount_data_sets: make_choices(xos.tenant().public_volume_names, null),\r
183                                                                   serviceClass: make_choices(xos.tenant().blessed_service_class_names, xos.tenant().blessed_service_classes),\r
184                                                                   default_image: make_choices(xos.tenant().blessed_image_names, xos.tenant().blessed_image_ids),\r
185                                                                   default_flavor: make_choices(xos.tenant().blessed_flavor_names, xos.tenant().blessed_flavor_ids),},\r
186                                                        });\r
187     XOSTenantApp.tenantSummary.show(tenantSummary);\r
188 \r
189     tenantSites = new XOSTenantSiteCollection();\r
190     tenantSites.updateFromSlice(model);\r
191     XOSTenantApp.tenantSites = tenantSites;\r
192 \r
193     tenantSiteList = new XOSTenantApp.tenantSiteListView({collection: tenantSites });\r
194     XOSTenantApp.tenantSiteList.show(tenantSiteList);\r
195     // on xos.slicePlus.sort, need to update xostenantapp.tenantSites\r
196 \r
197     XOSTenantApp.tenantButtons.show( new XOSTenantButtonView( { app: XOSTenantApp,\r
198                                                                 linkedView: tenantSummary } ) );\r
199 };\r
200 \r
201 XOSTenantApp.sanityCheck = function() {\r
202     errors = [];\r
203     if (xos.tenant().blessed_service_classes.length == 0) {\r
204         errors.push("no blessed service classes");\r
205     }\r
206     if (xos.tenant().blessed_flavors.length == 0) {\r
207         errors.push("no blessed flavors");\r
208     }\r
209     if (xos.tenant().blessed_images.length == 0) {\r
210         errors.push("no blessed images");\r
211     }\r
212     if (xos.tenant().blessed_sites.length == 0) {\r
213         errors.push("no blessed sites");\r
214     }\r
215 \r
216     if (errors.length > 0) {\r
217          $("#tenantSummary").html("Tenant view sanity check failed<br>" + errors.join("<br>"));\r
218          return false;\r
219     }\r
220 \r
221     return true;\r
222 }\r
223 \r
224 XOSTenantApp.collectionLoadChange = function() {\r
225     stats = xos.getCollectionStatus();\r
226 \r
227     if (!XOSTenantApp.navigationStarted) {\r
228         if (stats["isLoaded"] + stats["failedLoad"] >= stats["startedLoad"]) {\r
229             if (XOSTenantApp.sanityCheck()) {\r
230                 XOSTenantApp.viewSlice(undefined);\r
231             }\r
232         } else {\r
233             $("#tenantSummary").html("<h3>Loading...</h3><div id='xos-startup-progress'></div>");\r
234             $("#xos-startup-progress").progressbar({value: stats["completedLoad"], max: stats["startedLoad"]});\r
235         }\r
236     }\r
237 };\r
238 \r
239 XOSTenantApp.on("start", function() {\r
240      XOSTenantApp.buildViews();
241
242      // fire it once to initially show the progress bar
243      XOSTenantApp.collectionLoadChange();
244
245      // fire it each time the collection load status is updated
246      Backbone.on("xoslib:collectionLoadChange", XOSTenantApp.collectionLoadChange);
247 });
248
249 $(document).ready(function(){
250     XOSTenantApp.start();
251 });
252