display count of ready slivers in tenant view
[plstackapi.git] / planetstack / core / xoslib / static / js / xosTenant.js
index 9c45460..79e093e 100644 (file)
@@ -5,7 +5,7 @@ XOSTenantSite = XOSModel.extend( {
 });
 
 XOSTenantSiteCollection = XOSCollection.extend( {
-    listFields: ["name", "allocated"],
+    listFields: ["name", "allocated", "ready"],
     modelName: "tenantSite",
     collectionName: "tenantSites",
 
@@ -14,17 +14,33 @@ XOSTenantSiteCollection = XOSCollection.extend( {
         var id = 0;
         for (siteName in slice.attributes.site_allocation) {
             allocated = slice.attributes.site_allocation[siteName];
-            tenantSites.push(new XOSTenantSite( { name: siteName, allocated: allocated, id: id} ));
+            ready = slice.attributes.site_ready[siteName] || 0;
+            tenantSites.push(new XOSTenantSite( { name: siteName, allocated: allocated, ready: ready, id: id} ));
             id = id + 1;
         }
         for (index in xos.tenantview.models[0].attributes.blessed_site_names) {
             siteName = xos.tenantview.models[0].attributes.blessed_site_names[index];
             if (! (siteName in slice.attributes.site_allocation)) {
-                tenantSites.push(new XOSTenantSite( { name: siteName, allocated: 0, id: id} ));
+                tenantSites.push(new XOSTenantSite( { name: siteName, allocated: 0, ready: 0, id: id} ));
                 id = id + 1;
             }
         }
         this.set(tenantSites);
+
+        var that=this;
+        this.listenTo(slice, 'change', function() { that.getReadyFromSlice(slice); })
+    },
+
+    getReadyFromSlice: function(slice) {
+        for (siteName in slice.attributes.site_ready) {
+            ready = slice.attributes.site_ready[siteName];
+            for (index in this.models) {
+                tenantSite = this.models[index];
+                if (tenantSite.attributes.name == siteName) {
+                    tenantSite.set("ready", ready);
+                }
+            }
+        }
     },
 
     putToSlice: function(slice) {
@@ -50,6 +66,20 @@ XOSEditUsersView = Marionette.ItemView.extend({
 
             });
 
+XOSTenantSummaryView = XOSDetailView.extend({
+            events: {"change": "onChange"},
+
+            onChange: function(e) {
+                XOSTenantApp.setDirty(true);
+            },
+
+            saveSuccess: function() {
+                console.log("saveSuccess!");
+                XOSTenantApp.setDirty(false);
+            },
+
+            });
+
 
 XOSTenantButtonView = Marionette.ItemView.extend({
             template: "#xos-tenant-buttons-template",
@@ -58,6 +88,7 @@ XOSTenantButtonView = Marionette.ItemView.extend({
                      "click button.btn-tenant-delete": "deleteClicked",
                      "click button.btn-tenant-add-user": "addUserClicked",
                      "click button.btn-tenant-save": "saveClicked",
+                     "click button.btn-tenant-download-ssh": "downloadClicked",
                      },
 
             createClicked: function(e) {
@@ -72,11 +103,19 @@ XOSTenantButtonView = Marionette.ItemView.extend({
                      XOSTenantApp.editUsers(this.options.linkedView.model);
                      },
 
+            downloadClicked: function(e) {
+                     XOSTenantApp.downloadSSH(this.options.linkedView.model);
+                     },
+
             saveClicked: function(e) {
                      model = this.options.linkedView.model;
                      model.tenantSiteCollection.putToSlice(model);
                      model.attributes.users = model.usersBuffer;
-                     this.options.linkedView.submitContinueClicked.call(this.options.linkedView, e);
+
+                     e.preventDefault();
+                     this.options.linkedView.save();
+                     //this.options.linkedView.submitContinueClicked.call(this.options.linkedView, e);
+                     //XOSTenantApp.setDirty(false);
                      },
             });
 
@@ -84,6 +123,7 @@ XOSTenantApp = new XOSApplication({
     logTableId: "#logTable",
     statusMsgId: "#statusMsg",
     hideTabsByDefault: true,
+    dirty: false,
     varName: "XOSTenantApp",
 });
 
@@ -94,15 +134,28 @@ XOSTenantApp.addRegions({
     tenantButtons: "#tenantButtons",
     tenantAddSliceInterior: "#tenant-addslice-interior",
     tenantEditUsersInterior: "#tenant-edit-users-interior",
+    tenantSSHCommandsInterior: "#tenant-ssh-commands-interior",
 });
 
+XOSTenantApp.setDirty = function(dirty) {
+    XOSTenantApp.dirty = dirty;
+    if (dirty) {
+        $("button.btn-tenant-save").addClass("btn-success");
+    } else {
+        $("button.btn-tenant-save").removeClass("btn-success");
+    }
+};
+
 XOSTenantApp.buildViews = function() {
      XOSTenantApp.tenantSites = new XOSTenantSiteCollection();\r
 \r
-     tenantSummaryClass = XOSDetailView.extend({template: "#xos-detail-template",\r
+     tenantSummaryClass = XOSTenantSummaryView.extend({template: "#xos-detail-template",\r
                                                 app: XOSTenantApp,\r
-                                                detailFields: ["serviceClass", "default_image", "default_flavor", "network_ports", "mount_data_sets"],\r
+                                                detailFields: ["serviceClass", "default_image", "default_flavor", "network_ports"],\r
                                                 fieldDisplayNames: {serviceClass: "Service Level", "default_flavor": "Flavor", "default_image": "Image", "mount_data_sets": "Data Sets"},\r
+                                                helpText: {"serviceClass": "Existing slivers will be re-instantiated if changed",\r
+                                                           "default_image": "Existing slivers will be re-instantiated if changed",\r
+                                                           "default_flavor": "Existing slivers will be re-instantiated if changed"},\r
 \r
                                                 onShow: function() {\r
                                                     // the slice selector is in a different table, so make every label cell the maximal width\r
@@ -141,6 +194,9 @@ XOSTenantApp.buildViews = function() {
          sliceChanged: function(id) {\r
              XOSTenantApp.navToSlice(id);\r
          },\r
+         filter: function(slice) {\r
+             return slice.attributes.current_user_can_see;\r
+         },\r
      });\r
 \r
      xos.sites.fetch();\r
@@ -172,6 +228,7 @@ XOSTenantApp.navToSlice = function(id) {
 XOSTenantApp.adjustCollectionField = function(collectionName, id, fieldName, amount) {\r
     model = XOSTenantApp[collectionName].get(id);\r
     model.set(fieldName, Math.max(model.get(fieldName) + amount, 0));\r
+    XOSTenantApp.setDirty(true);\r
 };\r
 \r
 XOSTenantApp.addSlice = function() {\r
@@ -183,11 +240,12 @@ XOSTenantApp.addSlice = function() {
     }\r
 \r
     model = new xos.slicesPlus.model({site: xos.tenant().current_user_site_id,\r
-                                      name: xos.tenant().current_user_login_base + "_"});\r
+                                      name: xos.tenant().current_user_login_base + "_",\r
+                                      creator: xos.tenant().current_user_id});\r
     console.log(model);\r
     var detailView = new XOSTenantApp.tenantAddView({model: model,\r
-                                                    collection: xos.slicesPlus,\r
-                                                    noSubmitButton: true,\r
+                                                     collection: xos.slicesPlus,\r
+                                                     noSubmitButton: true,\r
                                                     });\r
     detailView.dialog = $("#tenant-addslice-dialog");\r
     app.tenantAddSliceInterior.show(detailView);\r
@@ -225,6 +283,9 @@ XOSTenantApp.editUsers = function(model) {
               var editDialog = this;
               user_ids = all_options($("#tenant-edit-users-dialog").find(".select-picker-to"));
               user_ids = user_ids.map( function(x) { return parseInt(x,10); } );
+              if (!array_same_elements(user_ids, model.usersBuffer)) {
+                  XOSTenantApp.setDirty(true);
+              }
               model.usersBuffer = user_ids;
               $(editDialog).dialog("close");
             },
@@ -236,12 +297,66 @@ XOSTenantApp.editUsers = function(model) {
     $("#tenant-edit-users-dialog").dialog("open");\r
 };\r
 \r
+XOSTenantApp.downloadSSHOld = function(model) {\r
+    sshCommands = "";\r
+    for (index in model.attributes.sliceInfo.sshCommands) {\r
+         sshCommand = model.attributes.sliceInfo.sshCommands[index];\r
+         sshCommands = sshCommands + sshCommand + "\n";\r
+    }\r
+\r
+    if (sshCommands.length == 0) {\r
+         alert("this slice has no instantiated slivers yet");\r
+         return;\r
+    }\r
+\r
+    var myWindow = window.open("", "ssh_command_list",""); // "width=640, height=480");\r
+    myWindow.document.write("<html><head><title>SSH Commands</title></head><body><pre>" + sshCommands + "</pre></body></html>");\r
+    myWindow.document.close();\r
+};\r
+\r
+XOSTenantApp.downloadSSH = function(model) {\r
+    sshCommands = "";\r
+    for (index in model.attributes.sliceInfo.sshCommands) {\r
+         sshCommand = model.attributes.sliceInfo.sshCommands[index];\r
+         sshCommands = sshCommands + sshCommand + "\n";\r
+    }\r
+\r
+    if (sshCommands.length == 0) {\r
+         alert("this slice has no instantiated slivers yet");\r
+         return;\r
+    }\r
+\r
+    var htmlView = new HTMLView({html: '<pre style="overflow: auto; word-wrap: normal; white-space: pre; word-wrap: normal;">' + sshCommands + '</pre>'});\r
+    XOSTenantApp.tenantSSHCommandsInterior.show(htmlView);\r
+\r
+    $("#tenant-ssh-commands-dialog").dialog({\r
+       autoOpen: false,
+       modal: true,
+       width: 640,
+       buttons : {
+            "Ok" : function() {
+              $(this).dialog("close");
+            },
+          }
+        });
+    $("#tenant-ssh-commands-dialog").dialog("open");\r
+};\r
+\r
 XOSTenantApp.deleteSlice = function(model) {\r
     var app=this;\r
     app.deleteDialog(model, function() { console.log("afterDelete"); app.viewSlice(undefined); });\r
 };\r
 \r
 XOSTenantApp.viewSlice = function(model) {\r
+    if (XOSTenantApp.dirty) {\r
+        if (!confirm("The current sliver has unsaved data -- view new sliver anyway ?")) {\r
+            $("#tenantSliceSelector select").val(XOSTenantApp.currentSlice.id);\r
+            return;\r
+        }\r
+    }\r
+\r
+    XOSTenantApp.setDirty(false);\r
+\r
     if (!model && xos.slicesPlus.models.length > 0) {\r
         model = xos.slicesPlus.models[0];\r
     }\r
@@ -275,6 +390,8 @@ XOSTenantApp.viewSlice = function(model) {
 \r
         XOSTenantApp.tenantButtons.show( new XOSTenantButtonView( { app: XOSTenantApp,\r
                                                                     linkedView: tenantSummary } ) );\r
+\r
+        XOSTenantApp.currentSlice = model;\r
     } else {\r
         XOSTenantApp.tenantSliceSelector.show(new HTMLView({html: ""}));\r
         XOSTenantApp.tenantSummary.show(new HTMLView({html: "You have no slices"}));\r