diff --git a/ui/new/scripts/cloud.core2.instance.js b/ui/new/scripts/cloud.core2.instance.js index 5a4fbc75467..4eb3bd7c7cb 100644 --- a/ui/new/scripts/cloud.core2.instance.js +++ b/ui/new/scripts/cloud.core2.instance.js @@ -1,5 +1,4 @@ -function clickInstanceGroupHeader($arrowIcon) { - +function clickInstanceGroupHeader($arrowIcon) { //midmenu needs multiple-selection for actions like start VM, stop VM, reboot VM. $("#midmenu_container").selectable({ selecting: function(event, ui) { @@ -25,565 +24,12 @@ function clickInstanceGroupHeader($arrowIcon) { } } } - }); - - - //***** VM Detail (begin) ****************************************************************************** - var $vmPopup - var $rightPanelHeader; - var $rightPanelContent; - - var $midmenuItem = $("#midmenu_item"); - - var noGroupName = "(no group name)"; - - var vmListAPIMap = { - listAPI: "listVirtualMachines", - listAPIResponse: "listvirtualmachinesresponse", - listAPIResponseObj: "virtualmachine" - }; - - var vmActionMap = { - "Stop Instance": { - api: "stopVirtualMachine", - isAsyncJob: true, - asyncJobResponse: "stopvirtualmachineresponse", - afterActionSeccessFn: vmToMidmenu - }, - "Start Instance": { - api: "startVirtualMachine", - isAsyncJob: true, - asyncJobResponse: "startvirtualmachineresponse", - afterActionSeccessFn: vmToMidmenu - }, - "Reboot Instance": { - api: "rebootVirtualMachine", - isAsyncJob: true, - asyncJobResponse: "rebootvirtualmachineresponse", - afterActionSeccessFn: vmToMidmenu - }, - "Destroy Instance": { - api: "destroyVirtualMachine", - isAsyncJob: true, - asyncJobResponse: "destroyvirtualmachineresponse", - afterActionSeccessFn: vmToMidmenu - }, - "Restore Instance": { - api: "recoverVirtualMachine", - isAsyncJob: false, - afterActionSeccessFn: vmToMidmenu - }, - "Attach ISO": { - isAsyncJob: true, - asyncJobResponse: "attachisoresponse", - dialogBeforeActionFn : doAttachISO, - afterActionSeccessFn: vmToMidmenu - }, - "Detach ISO": { - isAsyncJob: true, - asyncJobResponse: "detachisoresponse", - dialogBeforeActionFn : doDetachISO, - afterActionSeccessFn: vmToMidmenu - }, - "Reset Password": { - isAsyncJob: true, - asyncJobResponse: "resetpasswordforvirtualmachineresponse", - dialogBeforeActionFn : doResetPassword, - afterActionSeccessFn: function(){} - }, - "Change Name": { - isAsyncJob: false, - dialogBeforeActionFn : doChangeName, - afterActionSeccessFn: vmToMidmenu - }, - "Change Service": { - isAsyncJob: true, - asyncJobResponse: "changeserviceforvirtualmachineresponse", - dialogBeforeActionFn : doChangeService, - afterActionSeccessFn: vmToMidmenu - }, - "Change Group": { - isAsyncJob: false, - dialogBeforeActionFn : doChangeGroup, - afterActionSeccessFn: vmToMidmenu - }, - "Enable HA": { - isAsyncJob: false, - dialogBeforeActionFn : doEnableHA, - afterActionSeccessFn: vmToMidmenu - }, - "Disable HA": { - isAsyncJob: false, - dialogBeforeActionFn : doDisableHA, - afterActionSeccessFn: vmToMidmenu - } - } - - function doAttachISO($t, selectedItemsInMidMenu, vmListAPIMap) { - $.ajax({ - data: createURL("command=listIsos&isReady=true"), - dataType: "json", - async: false, - success: function(json) { - var isos = json.listisosresponse.iso; - var isoSelect = $("#dialog_attach_iso #attach_iso_select"); - if (isos != null && isos.length > 0) { - isoSelect.empty(); - for (var i = 0; i < isos.length; i++) { - isoSelect.append("");; - } - } - } - }); - - $("#dialog_attach_iso") - .dialog('option', 'buttons', { - "Confirm": function() { - $(this).dialog("close"); - var isoId = $("#dialog_attach_iso #attach_iso_select").val(); - if (isoId == "none") { - $("#dialog_alert").html("

There is no ISO file to attach to the virtual machine.

"); - $("#dialog_alert").dialog("open"); - return false; - } - for(var id in selectedItemsInMidMenu) { - var apiCommand = "command=attachIso&virtualmachineid="+id+"&id="+isoId; - doActionForMidMenu(id, $t, apiCommand, vmListAPIMap); - } - }, - "Cancel": function() { - $(this).dialog("close"); - } - }).dialog("open"); - } - - function doDetachISO($t, selectedItemsInMidMenu, vmListAPIMap) { - $("#dialog_confirmation") - .html("

Please confirm you want to detach an ISO from the virtual machine(s)

") - .dialog('option', 'buttons', { - "Confirm": function() { - $(this).dialog("close"); - for(var id in selectedItemsInMidMenu) { - var apiCommand = "command=detachIso&virtualmachineid="+id; - doActionForMidMenu(id, $t, apiCommand, vmListAPIMap); - } - }, - "Cancel": function() { - $(this).dialog("close"); - } - }).dialog("open"); - } - - function doResetPassword($t, selectedItemsInMidMenu, vmListAPIMap) { - $("#dialog_confirmation") - .html("

Please confirm you want to change the ROOT password for your virtual machine(s)

") - .dialog('option', 'buttons', { - "Confirm": function() { - $(this).dialog("close"); - for(var id in selectedItemsInMidMenu) { - var $midMenuItem = selectedItemsInMidMenu[id]; - var jsonObj = $midMenuItem.data("jsonObj"); - if(jsonObj.state != "Stopped") { - $midMenuItem.find("#info_icon").addClass("error").show(); - $midMenuItem.data("afterActionInfo", ($t.data("label") + " action failed. Reason: This instance needs to be stopped before you can reset password")); - continue; - } - if(jsonObj.passwordEnabled != "true") { - $midMenuItem.find("#info_icon").addClass("error").show(); - $midMenuItem.data("afterActionInfo", ($t.data("label") + " action failed. Reason: This instance is not using a template that has the password reset feature enabled. If you have forgotten your root password, please contact support.")); - continue; - } - var apiCommand = "command=resetPasswordForVirtualMachine&id="+id; - doActionForMidMenu(id, $t, apiCommand, vmListAPIMap); - } - }, - "Cancel": function() { - $(this).dialog("close"); - } - }).dialog("open"); - } - - function doChangeName($t, selectedItemsInMidMenu, vmListAPIMap) { - $("#dialog_change_name") - .dialog('option', 'buttons', { - "Confirm": function() { - var thisDialog = $(this); - thisDialog.dialog("close"); - - // validate values - var isValid = true; - isValid &= validateString("Name", thisDialog.find("#change_instance_name"), thisDialog.find("#change_instance_name_errormsg")); - if (!isValid) return; - - var name = trim(thisDialog.find("#change_instance_name").val()); - - for(var id in selectedItemsInMidMenu) { - var apiCommand = "command=updateVirtualMachine&id="+id+"&displayName="+todb(name); - doActionForMidMenu(id, $t, apiCommand, vmListAPIMap); - } - }, - "Cancel": function() { - $(this).dialog("close"); - } - }).dialog("open"); - } - - function doChangeService($t, selectedItemsInMidMenu, vmListAPIMap) { - $.ajax({ - //data: createURL("command=listServiceOfferings&VirtualMachineId="+vmId), //can not specifiy VirtualMachineId since we allow multiple-item-selection. - data: createURL("command=listServiceOfferings"), //can not specifiy VirtualMachineId since we support multiple-item-selection. - dataType: "json", - success: function(json) { - var offerings = json.listserviceofferingsresponse.serviceoffering; - var offeringSelect = $("#dialog_change_service_offering #change_service_offerings").empty(); - - if (offerings != null && offerings.length > 0) { - for (var i = 0; i < offerings.length; i++) { - var option = $("").data("name", fromdb(offerings[i].name)); - offeringSelect.append(option); - } - } - } - }); - - $("#dialog_change_service_offering") - .dialog('option', 'buttons', { - "Change": function() { - var thisDialog = $(this); - thisDialog.dialog("close"); - - for(var id in selectedItemsInMidMenu) { - var $midMenuItem = selectedItemsInMidMenu[id]; - var jsonObj = $midMenuItem.data("jsonObj"); - if(jsonObj.state != "Stopped") { - $midMenuItem.find("#info_icon").addClass("error").show(); - $midMenuItem.data("afterActionInfo", ($t.data("label") + " action failed. Reason: virtual instance needs to be stopped before you can change its service.")); - continue; - } - var apiCommand = "command=changeServiceForVirtualMachine&id="+id+"&serviceOfferingId="+thisDialog.find("#change_service_offerings").val(); - doActionForMidMenu(id, $t, apiCommand, vmListAPIMap); - } - }, - "Cancel": function() { - $(this).dialog("close"); - } - }).dialog("open"); - } - - function doChangeGroup($t, selectedItemsInMidMenu, vmListAPIMap) { - $("#dialog_change_group") - .dialog('option', 'buttons', { - "Confirm": function() { - var thisDialog = $(this); - thisDialog.dialog("close"); - - // validate values - var isValid = true; - isValid &= validateString("Group", thisDialog.find("#change_group_name"), thisDialog.find("#change_group_name_errormsg"), true); //group name is optional - if (!isValid) return; - - for(var id in selectedItemsInMidMenu) { - var $midMenuItem = selectedItemsInMidMenu[id]; - var jsonObj = $midMenuItem.data("jsonObj"); - var group = trim(thisDialog.find("#change_group_name").val()); - var apiCommand = "command=updateVirtualMachine&id="+id+"&group="+todb(group); - doActionForMidMenu(id, $t, apiCommand, vmListAPIMap); - } - }, - "Cancel": function() { - $(this).dialog("close"); - } - }).dialog("open"); - } - - function doEnableHA($t, selectedItemsInMidMenu, vmListAPIMap) { - var message = "

Please confirm you want to enable HA for your virtual machine. Once HA is enabled, your Virtual Instance will be automatically restarted in the event it is detected to have failed.

"; - - $("#dialog_confirmation") - .html(message) - .dialog('option', 'buttons', { - "Confirm": function() { - $(this).dialog("close"); - for(var id in selectedItemsInMidMenu) { - var $midMenuItem = selectedItemsInMidMenu[id]; - var jsonObj = $midMenuItem.data("jsonObj"); - var apiCommand = "command=updateVirtualMachine&id="+id+"&haenable=true"; - doActionForMidMenu(id, $t, apiCommand, vmListAPIMap); - } - }, - "Cancel": function() { - $(this).dialog("close"); - } - }).dialog("open"); - } - - function doDisableHA($t, selectedItemsInMidMenu, vmListAPIMap) { - var message = "

Please confirm you want to disable HA for your virtual machine. Once HA is disabled, your Virtual Instance will no longer be be automatically restarted in the event of a failure.

"; - - $("#dialog_confirmation") - .html(message) - .dialog('option', 'buttons', { - "Confirm": function() { - $(this).dialog("close"); - for(var id in selectedItemsInMidMenu) { - var $midMenuItem = selectedItemsInMidMenu[id]; - var jsonObj = $midMenuItem.data("jsonObj"); - var apiCommand = "command=updateVirtualMachine&id="+id+"&haenable=false"; - doActionForMidMenu(id, $t, apiCommand, vmListAPIMap); - } - }, - "Cancel": function() { - $(this).dialog("close"); - } - }).dialog("open"); - } - - - - function updateVirtualMachineStateInRightPanel(state) { - if(state == "Running") - $rightPanelContent.find("#state").text(state).removeClass("red gray").addClass("green"); - else if(state == "Stopped") - $rightPanelContent.find("#state").text(state).removeClass("green gray").addClass("red"); - else //Destroyed, Creating, ~ - $rightPanelContent.find("#state").text(state).removeClass("green red").addClass("gray"); - } - - function vmToMidmenu(jsonObj, $midmenuItem1) { - $midmenuItem1.data("jsonObj", jsonObj); - $midmenuItem1.attr("id", ("midmenuItem_"+jsonObj.id)); - - var vmName = getVmName(jsonObj.name, jsonObj.displayname); - $midmenuItem1.find("#first_row").text(vmName); - $midmenuItem1.find("#second_row").text(jsonObj.ipaddress); - updateStateInMidMenu(jsonObj, $midmenuItem1); - - $midmenuItem1.data("toRightPanelFn", vmToRightPanel); - $midmenuItem1.bind("click", function(event) { - var $t = $(this); - var toRightPanelFn = $t.data("toRightPanelFn"); - toRightPanelFn($t); - return false; - }); - } - - function vmToRightPanel($midmenuItem) { - var jsonObj = $midmenuItem.data("jsonObj"); - - var vmName = getVmName(jsonObj.name, jsonObj.displayname); - $("right_panel_header").find("#vm_name").text(vmName); - - var $rightPanelContent = $("#right_panel_content"); - if($midmenuItem.find("#info_icon").css("display") != "none") { - $rightPanelContent.find("#after_action_info").text($midmenuItem.data("afterActionInfo")); - if($midmenuItem.find("#info_icon").hasClass("error")) - $rightPanelContent.find("#after_action_info_container").addClass("errorbox"); - else - $rightPanelContent.find("#after_action_info_container").removeClass("errorbox"); - $rightPanelContent.find("#after_action_info_container").show(); - } - else { - $rightPanelContent.find("#after_action_info").text(""); - $rightPanelContent.find("#after_action_info_container").hide(); - } - - vmJsonToDetailsTab(jsonObj, $midmenuItem); - vmJsonToVolumeTab(jsonObj); - vmJsonToRouterTab(jsonObj); - } - - function vmJsonToDetailsTab(jsonObj, $midmenuItem){ - var $detailsTab = $("#right_panel_content #tab_content_details"); - $detailsTab.data("jsonObj", jsonObj); - - //details tab - updateVirtualMachineStateInRightPanel(jsonObj.state); - $detailsTab.find("#ipAddress").text(jsonObj.ipaddress); - $detailsTab.find("#zoneName").text(fromdb(jsonObj.zonename)); - - var vmName = getVmName(jsonObj.name, jsonObj.displayname); - $detailsTab.find("#vmname").text(vmName); - $detailsTab.find("#ipaddress").text(fromdb(jsonObj.ipaddress)); - - $detailsTab.find("#templateName").text(fromdb(jsonObj.templatename)); - $detailsTab.find("#serviceOfferingName").text(fromdb(jsonObj.serviceofferingname)); - $detailsTab.find("#created").text(jsonObj.created); - $detailsTab.find("#account").text(fromdb(jsonObj.account)); - $detailsTab.find("#domain").text(fromdb(jsonObj.domain)); - $detailsTab.find("#hostName").text(fromdb(jsonObj.hostname)); - $detailsTab.find("#group").text(fromdb(jsonObj.group)); - - setBooleanField(jsonObj.haenable, $detailsTab.find("#haenable")); - setBooleanField((jsonObj.isoid != null && jsonObj.isoid.length > 0), $detailsTab.find("#iso")); - - resetViewConsoleAction(jsonObj, $detailsTab); - } - - function vmJsonToVolumeTab(jsonObj) { - //volume tab - //if (getHypervisorType() == "kvm") - //detail.find("#volume_action_create_template").show(); - - $.ajax({ - cache: false, - data: createURL("command=listVolumes&virtualMachineId="+jsonObj.id+maxPageSize), - dataType: "json", - success: function(json) { - var items = json.listvolumesresponse.volume; - if (items != null && items.length > 0) { - var container = $("#right_panel_content #tab_content_volume").empty(); - var template = $("#volume_tab_template"); - for (var i = 0; i < items.length; i++) { - var newTemplate = template.clone(true); - vmVolumeJSONToTemplate(items[i], newTemplate); - container.append(newTemplate.show()); - } - } - } - }); - } - - function vmJsonToRouterTab(jsonObj) { - $.ajax({ - cache: false, - data: createURL("command=listRouters&domainid="+jsonObj.domainid+"&account="+jsonObj.account+maxPageSize), - dataType: "json", - success: function(json) { - var items = json.listroutersresponse.router; - if (items != null && items.length > 0) { - var container = $("#right_panel_content #tab_content_router").empty(); - var template = $("#router_tab_template"); - for (var i = 0; i < items.length; i++) { - var newTemplate = template.clone(true); - vmRouterJSONToTemplate(items[i], newTemplate); - container.append(newTemplate.show()); - } - } - } - }); - } - - function vmClearRightPanel(jsonObj) { - $rightPanelHeader.find("#vm_name").text(""); - updateVirtualMachineStateInRightPanel(""); - $rightPanelContent.find("#ipAddress").text(""); - $rightPanelContent.find("#zoneName").text(""); - $rightPanelContent.find("#templateName").text(""); - $rightPanelContent.find("#serviceOfferingName").text(""); - $rightPanelContent.find("#ha").hide(); - $rightPanelContent.find("#created").text(""); - $rightPanelContent.find("#account").text(""); - $rightPanelContent.find("#domain").text(""); - $rightPanelContent.find("#hostName").text(""); - $rightPanelContent.find("#group").text(""); - $rightPanelContent.find("#iso").hide(); - } - - //***** declaration for volume tab (begin) ********************************************************* - var vmVolumeActionMap = { - "Detach Disk": { - api: "detachVolume", - isAsyncJob: true, - asyncJobResponse: "detachvolumeresponse", - inProcessText: "Detaching disk....", - afterActionSeccessFn: function(jsonObj, template){ - template.slideUp("slow", function(){ - $(this).remove(); - }); - } - }, - "Create Template": { - isAsyncJob: true, - asyncJobResponse: "createtemplateresponse", - dialogBeforeActionFn : doCreateTemplateFromVmVolume, - inProcessText: "Creating template....", - afterActionSeccessFn: function(){} - } - } - - function vmVolumeJSONToTemplate(json, $template) { - $template.attr("id","vm_volume_"+json.id); - $template.data("jsonObj", json); - $template.find("#title").text(json.name); - $template.find("#id").text(json.id); - $template.find("#name").text(json.name); - if (json.storagetype == "shared") - $template.find("#type").text(json.type + " (shared storage)"); - else - $template.find("#type").text(json.type + " (local storage)"); - - $template.find("#size").text((json.size == "0") ? "" : convertBytes(json.size)); - setDateField(json.created, $template.find("#created")); - - //***** actions (begin) ***** - var $actionLink = $template.find("#volume_action_link"); - $actionLink.bind("mouseover", function(event) { - $(this).find("#volume_action_menu").show(); - return false; - }); - $actionLink.bind("mouseout", function(event) { - $(this).find("#volume_action_menu").hide(); - return false; - }); - - var $actionMenu = $actionLink.find("#volume_action_menu"); - $actionMenu.find("#action_list").empty(); - if(json.type=="ROOT") { //"create template" is allowed(when stopped), "detach disk" is disallowed. - if (json.vmstate == "Stopped") - buildActionLinkForSubgridItem("Create Template", vmVolumeActionMap, $actionMenu, volumeListAPIMap, $template); - } - else { //json.type=="DATADISK": "detach disk" is allowed, "create template" is disallowed. - buildActionLinkForSubgridItem("Detach Disk", vmVolumeActionMap, $actionMenu, volumeListAPIMap, $template); - } - //***** actions (end) ***** - } - - function vmRouterJSONToTemplate(jsonObj, $template) { - $template.data("jsonObj", jsonObj); - $template.find("#title").text(fromdb(jsonObj.name)); - $template.find("#state").text(fromdb(jsonObj.state)); - $template.find("#ipAddress").text(jsonObj.publicip); - $template.find("#zonename").text(fromdb(jsonObj.zonename)); - $template.find("#name").text(fromdb(jsonObj.name)); - $template.find("#publicip").text(fromdb(jsonObj.publicip)); - $template.find("#privateip").text(fromdb(jsonObj.privateip)); - $template.find("#guestipaddress").text(fromdb(jsonObj.guestipaddress)); - $template.find("#hostname").text(fromdb(jsonObj.hostname)); - $template.find("#networkdomain").text(fromdb(jsonObj.networkdomain)); - $template.find("#account").text(fromdb(jsonObj.account)); - setDateField(jsonObj.created, $template.find("#created")); - resetViewConsoleAction(jsonObj, $template); - - //***** actions (begin) ***** - var $actionLink = $template.find("#router_action_link"); - $actionLink.bind("mouseover", function(event) { - $(this).find("#router_action_menu").show(); - return false; - }); - $actionLink.bind("mouseout", function(event) { - $(this).find("#router_action_menu").hide(); - return false; - }); - - var $actionMenu = $actionLink.find("#router_action_menu"); - $actionMenu.find("#action_list").empty(); - if (jsonObj.state == 'Running') { - buildActionLinkForSubgridItem("Stop Router", vmRouterActionMap, $actionMenu, routerListAPIMap, $template); - buildActionLinkForSubgridItem("Reboot Router", vmRouterActionMap, $actionMenu, routerListAPIMap, $template); - } - else if (jsonObj.state == 'Stopped') { - buildActionLinkForSubgridItem("Start Router", vmRouterActionMap, $actionMenu, routerListAPIMap, $template); - } - //***** actions (end) ***** - } - - //***** declaration for volume tab (end) ********************************************************* - + }); $("#midmenu_add_link").show(); + if($arrowIcon.hasClass("close") == true) { - $arrowIcon.removeClass("close").addClass("open"); - + $arrowIcon.removeClass("close").addClass("open"); appendInstanceGroup(-1, noGroupName); $.ajax({ @@ -609,50 +55,12 @@ function clickInstanceGroupHeader($arrowIcon) { else if($arrowIcon.hasClass("open") == true) { $arrowIcon.removeClass("open").addClass("close"); $("#leftmenu_instance_group_container").empty(); - } - - function appendInstanceGroup(groupId, groupName) { - var $leftmenuSubmenuTemplate = $("#leftmenu_submenu_template").clone().show(); - $leftmenuSubmenuTemplate.attr("id", ("leftmenu_instance_group_"+groupId)); - $leftmenuSubmenuTemplate.data("groupId", groupId) - $leftmenuSubmenuTemplate.find("#submenu_name").text(groupName); - $leftmenuSubmenuTemplate.find("#icon").attr("src", "images/instance_leftmenuicon.png").show(); - - $leftmenuSubmenuTemplate.bind("click", function(event) { - if(selected_leftmenu_id != null && selected_leftmenu_id.length > 0) - $("#"+selected_leftmenu_id).removeClass("selected"); - selected_leftmenu_id = $(this).attr("id"); - $(this).addClass("selected"); - - $("#midmenu_container").empty(); - selectedItemsInMidMenu = {}; - - var groupId = $(this).data("groupId"); - $.ajax({ - cache: false, - data: createURL("command=listVirtualMachines&groupid="+groupId+"&pagesize="+midmenuItemCount), - dataType: "json", - success: function(json) { - var instances = json.listvirtualmachinesresponse.virtualmachine; - if (instances != null && instances.length > 0) { - for(var i=0; i 0) { + isoSelect.empty(); + for (var i = 0; i < isos.length; i++) { + isoSelect.append("");; + } + } + } + }); + + $("#dialog_attach_iso") + .dialog('option', 'buttons', { + "Confirm": function() { + $(this).dialog("close"); + var isoId = $("#dialog_attach_iso #attach_iso_select").val(); + if (isoId == "none") { + $("#dialog_alert").html("

There is no ISO file to attach to the virtual machine.

"); + $("#dialog_alert").dialog("open"); + return false; + } + for(var id in selectedItemsInMidMenu) { + var apiCommand = "command=attachIso&virtualmachineid="+id+"&id="+isoId; + doActionForMidMenu(id, $t, apiCommand, vmListAPIMap); + } + }, + "Cancel": function() { + $(this).dialog("close"); + } + }).dialog("open"); +} + +function doDetachISO($t, selectedItemsInMidMenu, vmListAPIMap) { + $("#dialog_confirmation") + .html("

Please confirm you want to detach an ISO from the virtual machine(s)

") + .dialog('option', 'buttons', { + "Confirm": function() { + $(this).dialog("close"); + for(var id in selectedItemsInMidMenu) { + var apiCommand = "command=detachIso&virtualmachineid="+id; + doActionForMidMenu(id, $t, apiCommand, vmListAPIMap); + } + }, + "Cancel": function() { + $(this).dialog("close"); + } + }).dialog("open"); +} + +function doResetPassword($t, selectedItemsInMidMenu, vmListAPIMap) { + $("#dialog_confirmation") + .html("

Please confirm you want to change the ROOT password for your virtual machine(s)

") + .dialog('option', 'buttons', { + "Confirm": function() { + $(this).dialog("close"); + for(var id in selectedItemsInMidMenu) { + var $midMenuItem = selectedItemsInMidMenu[id]; + var jsonObj = $midMenuItem.data("jsonObj"); + if(jsonObj.state != "Stopped") { + $midMenuItem.find("#info_icon").addClass("error").show(); + $midMenuItem.data("afterActionInfo", ($t.data("label") + " action failed. Reason: This instance needs to be stopped before you can reset password")); + continue; + } + if(jsonObj.passwordEnabled != "true") { + $midMenuItem.find("#info_icon").addClass("error").show(); + $midMenuItem.data("afterActionInfo", ($t.data("label") + " action failed. Reason: This instance is not using a template that has the password reset feature enabled. If you have forgotten your root password, please contact support.")); + continue; + } + var apiCommand = "command=resetPasswordForVirtualMachine&id="+id; + doActionForMidMenu(id, $t, apiCommand, vmListAPIMap); + } + }, + "Cancel": function() { + $(this).dialog("close"); + } + }).dialog("open"); +} + +function doChangeName($t, selectedItemsInMidMenu, vmListAPIMap) { + $("#dialog_change_name") + .dialog('option', 'buttons', { + "Confirm": function() { + var thisDialog = $(this); + thisDialog.dialog("close"); + + // validate values + var isValid = true; + isValid &= validateString("Name", thisDialog.find("#change_instance_name"), thisDialog.find("#change_instance_name_errormsg")); + if (!isValid) return; + + var name = trim(thisDialog.find("#change_instance_name").val()); + + for(var id in selectedItemsInMidMenu) { + var apiCommand = "command=updateVirtualMachine&id="+id+"&displayName="+todb(name); + doActionForMidMenu(id, $t, apiCommand, vmListAPIMap); + } + }, + "Cancel": function() { + $(this).dialog("close"); + } + }).dialog("open"); +} + +function doChangeService($t, selectedItemsInMidMenu, vmListAPIMap) { + $.ajax({ + //data: createURL("command=listServiceOfferings&VirtualMachineId="+vmId), //can not specifiy VirtualMachineId since we allow multiple-item-selection. + data: createURL("command=listServiceOfferings"), //can not specifiy VirtualMachineId since we support multiple-item-selection. + dataType: "json", + success: function(json) { + var offerings = json.listserviceofferingsresponse.serviceoffering; + var offeringSelect = $("#dialog_change_service_offering #change_service_offerings").empty(); + + if (offerings != null && offerings.length > 0) { + for (var i = 0; i < offerings.length; i++) { + var option = $("").data("name", fromdb(offerings[i].name)); + offeringSelect.append(option); + } + } + } + }); + + $("#dialog_change_service_offering") + .dialog('option', 'buttons', { + "Change": function() { + var thisDialog = $(this); + thisDialog.dialog("close"); + + for(var id in selectedItemsInMidMenu) { + var $midMenuItem = selectedItemsInMidMenu[id]; + var jsonObj = $midMenuItem.data("jsonObj"); + if(jsonObj.state != "Stopped") { + $midMenuItem.find("#info_icon").addClass("error").show(); + $midMenuItem.data("afterActionInfo", ($t.data("label") + " action failed. Reason: virtual instance needs to be stopped before you can change its service.")); + continue; + } + var apiCommand = "command=changeServiceForVirtualMachine&id="+id+"&serviceOfferingId="+thisDialog.find("#change_service_offerings").val(); + doActionForMidMenu(id, $t, apiCommand, vmListAPIMap); + } + }, + "Cancel": function() { + $(this).dialog("close"); + } + }).dialog("open"); +} + +function doChangeGroup($t, selectedItemsInMidMenu, vmListAPIMap) { + $("#dialog_change_group") + .dialog('option', 'buttons', { + "Confirm": function() { + var thisDialog = $(this); + thisDialog.dialog("close"); + + // validate values + var isValid = true; + isValid &= validateString("Group", thisDialog.find("#change_group_name"), thisDialog.find("#change_group_name_errormsg"), true); //group name is optional + if (!isValid) return; + + for(var id in selectedItemsInMidMenu) { + var $midMenuItem = selectedItemsInMidMenu[id]; + var jsonObj = $midMenuItem.data("jsonObj"); + var group = trim(thisDialog.find("#change_group_name").val()); + var apiCommand = "command=updateVirtualMachine&id="+id+"&group="+todb(group); + doActionForMidMenu(id, $t, apiCommand, vmListAPIMap); + } + }, + "Cancel": function() { + $(this).dialog("close"); + } + }).dialog("open"); +} + +function doEnableHA($t, selectedItemsInMidMenu, vmListAPIMap) { + var message = "

Please confirm you want to enable HA for your virtual machine. Once HA is enabled, your Virtual Instance will be automatically restarted in the event it is detected to have failed.

"; + + $("#dialog_confirmation") + .html(message) + .dialog('option', 'buttons', { + "Confirm": function() { + $(this).dialog("close"); + for(var id in selectedItemsInMidMenu) { + var $midMenuItem = selectedItemsInMidMenu[id]; + var jsonObj = $midMenuItem.data("jsonObj"); + var apiCommand = "command=updateVirtualMachine&id="+id+"&haenable=true"; + doActionForMidMenu(id, $t, apiCommand, vmListAPIMap); + } + }, + "Cancel": function() { + $(this).dialog("close"); + } + }).dialog("open"); +} + +function doDisableHA($t, selectedItemsInMidMenu, vmListAPIMap) { + var message = "

Please confirm you want to disable HA for your virtual machine. Once HA is disabled, your Virtual Instance will no longer be be automatically restarted in the event of a failure.

"; + + $("#dialog_confirmation") + .html(message) + .dialog('option', 'buttons', { + "Confirm": function() { + $(this).dialog("close"); + for(var id in selectedItemsInMidMenu) { + var $midMenuItem = selectedItemsInMidMenu[id]; + var jsonObj = $midMenuItem.data("jsonObj"); + var apiCommand = "command=updateVirtualMachine&id="+id+"&haenable=false"; + doActionForMidMenu(id, $t, apiCommand, vmListAPIMap); + } + }, + "Cancel": function() { + $(this).dialog("close"); + } + }).dialog("open"); +} + + + +function updateVirtualMachineStateInRightPanel(state) { + var $rightPanelContent = $("#right_panel_content"); + if(state == "Running") + $rightPanelContent.find("#state").text(state).removeClass("red gray").addClass("green"); + else if(state == "Stopped") + $rightPanelContent.find("#state").text(state).removeClass("green gray").addClass("red"); + else //Destroyed, Creating, ~ + $rightPanelContent.find("#state").text(state).removeClass("green red").addClass("gray"); +} + +function vmToMidmenu(jsonObj, $midmenuItem1) { + $midmenuItem1.data("jsonObj", jsonObj); + $midmenuItem1.attr("id", ("midmenuItem_"+jsonObj.id)); + + var vmName = getVmName(jsonObj.name, jsonObj.displayname); + $midmenuItem1.find("#first_row").text(vmName); + $midmenuItem1.find("#second_row").text(jsonObj.ipaddress); + updateStateInMidMenu(jsonObj, $midmenuItem1); + + $midmenuItem1.data("toRightPanelFn", vmToRightPanel); + $midmenuItem1.bind("click", function(event) { + var $t = $(this); + var toRightPanelFn = $t.data("toRightPanelFn"); + toRightPanelFn($t); + return false; + }); +} + +function vmToRightPanel($midmenuItem) { + var jsonObj = $midmenuItem.data("jsonObj"); + + var vmName = getVmName(jsonObj.name, jsonObj.displayname); + $("right_panel_header").find("#vm_name").text(vmName); + + var $rightPanelContent = $("#right_panel_content"); + if($midmenuItem.find("#info_icon").css("display") != "none") { + $rightPanelContent.find("#after_action_info").text($midmenuItem.data("afterActionInfo")); + if($midmenuItem.find("#info_icon").hasClass("error")) + $rightPanelContent.find("#after_action_info_container").addClass("errorbox"); + else + $rightPanelContent.find("#after_action_info_container").removeClass("errorbox"); + $rightPanelContent.find("#after_action_info_container").show(); + } + else { + $rightPanelContent.find("#after_action_info").text(""); + $rightPanelContent.find("#after_action_info_container").hide(); + } + + vmJsonToDetailsTab(jsonObj, $midmenuItem); + vmJsonToVolumeTab(jsonObj); + vmJsonToRouterTab(jsonObj); +} + +function vmJsonToDetailsTab(jsonObj, $midmenuItem){ + var $detailsTab = $("#right_panel_content #tab_content_details"); + $detailsTab.data("jsonObj", jsonObj); + + //details tab + updateVirtualMachineStateInRightPanel(jsonObj.state); + $detailsTab.find("#ipAddress").text(jsonObj.ipaddress); + $detailsTab.find("#zoneName").text(fromdb(jsonObj.zonename)); + + var vmName = getVmName(jsonObj.name, jsonObj.displayname); + $detailsTab.find("#vmname").text(vmName); + $detailsTab.find("#ipaddress").text(fromdb(jsonObj.ipaddress)); + + $detailsTab.find("#templateName").text(fromdb(jsonObj.templatename)); + $detailsTab.find("#serviceOfferingName").text(fromdb(jsonObj.serviceofferingname)); + $detailsTab.find("#created").text(jsonObj.created); + $detailsTab.find("#account").text(fromdb(jsonObj.account)); + $detailsTab.find("#domain").text(fromdb(jsonObj.domain)); + $detailsTab.find("#hostName").text(fromdb(jsonObj.hostname)); + $detailsTab.find("#group").text(fromdb(jsonObj.group)); + + setBooleanField(jsonObj.haenable, $detailsTab.find("#haenable")); + setBooleanField((jsonObj.isoid != null && jsonObj.isoid.length > 0), $detailsTab.find("#iso")); + + resetViewConsoleAction(jsonObj, $detailsTab); +} + +function vmJsonToVolumeTab(jsonObj) { + //volume tab + //if (getHypervisorType() == "kvm") + //detail.find("#volume_action_create_template").show(); + + $.ajax({ + cache: false, + data: createURL("command=listVolumes&virtualMachineId="+jsonObj.id+maxPageSize), + dataType: "json", + success: function(json) { + var items = json.listvolumesresponse.volume; + if (items != null && items.length > 0) { + var container = $("#right_panel_content #tab_content_volume").empty(); + var template = $("#volume_tab_template"); + for (var i = 0; i < items.length; i++) { + var newTemplate = template.clone(true); + vmVolumeJSONToTemplate(items[i], newTemplate); + container.append(newTemplate.show()); + } + } + } + }); +} + +function vmJsonToRouterTab(jsonObj) { + $.ajax({ + cache: false, + data: createURL("command=listRouters&domainid="+jsonObj.domainid+"&account="+jsonObj.account+maxPageSize), + dataType: "json", + success: function(json) { + var items = json.listroutersresponse.router; + if (items != null && items.length > 0) { + var container = $("#right_panel_content #tab_content_router").empty(); + var template = $("#router_tab_template"); + for (var i = 0; i < items.length; i++) { + var newTemplate = template.clone(true); + vmRouterJSONToTemplate(items[i], newTemplate); + container.append(newTemplate.show()); + } + } + } + }); +} + +function vmClearRightPanel(jsonObj) { + $("#right_panel_header").find("#vm_name").text(""); + updateVirtualMachineStateInRightPanel(""); + + var $rightPanelContent = $("#right_panel_content"); + $rightPanelContent.find("#ipAddress").text(""); + $rightPanelContent.find("#zoneName").text(""); + $rightPanelContent.find("#templateName").text(""); + $rightPanelContent.find("#serviceOfferingName").text(""); + $rightPanelContent.find("#ha").hide(); + $rightPanelContent.find("#created").text(""); + $rightPanelContent.find("#account").text(""); + $rightPanelContent.find("#domain").text(""); + $rightPanelContent.find("#hostName").text(""); + $rightPanelContent.find("#group").text(""); + $rightPanelContent.find("#iso").hide(); +} + +//***** declaration for volume tab (begin) ********************************************************* +var vmVolumeActionMap = { + "Detach Disk": { + api: "detachVolume", + isAsyncJob: true, + asyncJobResponse: "detachvolumeresponse", + inProcessText: "Detaching disk....", + afterActionSeccessFn: function(jsonObj, $subgridItem) { + $subgridItem.slideUp("slow", function(){ + $(this).remove(); + }); + } + }, + "Create Template": { + isAsyncJob: true, + asyncJobResponse: "createtemplateresponse", + dialogBeforeActionFn : doCreateTemplateFromVmVolume, + inProcessText: "Creating template....", + afterActionSeccessFn: function(jsonObj, $subgridItem){} + } +} + +function vmVolumeJSONToTemplate(json, $template) { + $template.attr("id","vm_volume_"+json.id); + $template.data("jsonObj", json); + $template.find("#title").text(json.name); + $template.find("#id").text(json.id); + $template.find("#name").text(json.name); + if (json.storagetype == "shared") + $template.find("#type").text(json.type + " (shared storage)"); + else + $template.find("#type").text(json.type + " (local storage)"); + + $template.find("#size").text((json.size == "0") ? "" : convertBytes(json.size)); + setDateField(json.created, $template.find("#created")); + + //***** actions (begin) ***** + var $actionLink = $template.find("#volume_action_link"); + $actionLink.unbind("mouseover").bind("mouseover", function(event) { + $(this).find("#volume_action_menu").show(); + return false; + }); + $actionLink.unbind("mouseout").bind("mouseout", function(event) { + $(this).find("#volume_action_menu").hide(); + return false; + }); + + var $actionMenu = $actionLink.find("#volume_action_menu"); + $actionMenu.find("#action_list").empty(); + if(json.type=="ROOT") { //"create template" is allowed(when stopped), "detach disk" is disallowed. + if (json.vmstate == "Stopped") + buildActionLinkForSubgridItem("Create Template", vmVolumeActionMap, $actionMenu, volumeListAPIMap, $template); + } + else { //json.type=="DATADISK": "detach disk" is allowed, "create template" is disallowed. + buildActionLinkForSubgridItem("Detach Disk", vmVolumeActionMap, $actionMenu, volumeListAPIMap, $template); + } + //***** actions (end) ***** +} + +function vmRouterJSONToTemplate(jsonObj, $template) { + $template.data("jsonObj", jsonObj); + $template.find("#title").text(fromdb(jsonObj.name)); + $template.find("#state").text(fromdb(jsonObj.state)); + $template.find("#ipAddress").text(jsonObj.publicip); + $template.find("#zonename").text(fromdb(jsonObj.zonename)); + $template.find("#name").text(fromdb(jsonObj.name)); + $template.find("#publicip").text(fromdb(jsonObj.publicip)); + $template.find("#privateip").text(fromdb(jsonObj.privateip)); + $template.find("#guestipaddress").text(fromdb(jsonObj.guestipaddress)); + $template.find("#hostname").text(fromdb(jsonObj.hostname)); + $template.find("#networkdomain").text(fromdb(jsonObj.networkdomain)); + $template.find("#account").text(fromdb(jsonObj.account)); + setDateField(jsonObj.created, $template.find("#created")); + resetViewConsoleAction(jsonObj, $template); + + //***** actions (begin) ***** + var $actionLink = $template.find("#router_action_link"); + $actionLink.unbind("mouseover").bind("mouseover", function(event) { + $(this).find("#router_action_menu").show(); + return false; + }); + $actionLink.unbind("mouseout").bind("mouseout", function(event) { + $(this).find("#router_action_menu").hide(); + return false; + }); + + var $actionMenu = $actionLink.find("#router_action_menu"); + $actionMenu.find("#action_list").empty(); + if (jsonObj.state == 'Running') { + buildActionLinkForSubgridItem("Stop Router", vmRouterActionMap, $actionMenu, routerListAPIMap, $template); + buildActionLinkForSubgridItem("Reboot Router", vmRouterActionMap, $actionMenu, routerListAPIMap, $template); + } + else if (jsonObj.state == 'Stopped') { + buildActionLinkForSubgridItem("Start Router", vmRouterActionMap, $actionMenu, routerListAPIMap, $template); + } + //***** actions (end) ***** +} + + + +//***** declaration for volume tab (end) ********************************************************* + +function appendInstanceGroup(groupId, groupName) { + var $leftmenuSubmenuTemplate = $("#leftmenu_submenu_template").clone().show(); + $leftmenuSubmenuTemplate.attr("id", ("leftmenu_instance_group_"+groupId)); + $leftmenuSubmenuTemplate.data("groupId", groupId) + $leftmenuSubmenuTemplate.find("#submenu_name").text(groupName); + $leftmenuSubmenuTemplate.find("#icon").attr("src", "images/instance_leftmenuicon.png").show(); + + $leftmenuSubmenuTemplate.bind("click", function(event) { + if(selected_leftmenu_id != null && selected_leftmenu_id.length > 0) + $("#"+selected_leftmenu_id).removeClass("selected"); + selected_leftmenu_id = $(this).attr("id"); + $(this).addClass("selected"); + + $("#midmenu_container").empty(); + selectedItemsInMidMenu = {}; + + var groupId = $(this).data("groupId"); + $.ajax({ + cache: false, + data: createURL("command=listVirtualMachines&groupid="+groupId+"&pagesize="+midmenuItemCount), + dataType: "json", + success: function(json) { + var instances = json.listvirtualmachinesresponse.virtualmachine; + if (instances != null && instances.length > 0) { + var $template = $("#midmenu_item"); + for(var i=0; i