@@ -1768,43 +1768,29 @@ public UserVm updateVirtualMachine(UpdateVMCmd cmd)
17681768 Boolean isDynamicallyScalable = cmd .isDynamicallyScalable ();
17691769 Account caller = CallContext .current ().getCallingAccount ();
17701770
1771- // Input validation
1772- UserVmVO vmInstance = null ;
1773-
1774- // Verify input parameters
1775- vmInstance = _vmDao .findById (id .longValue ());
1776-
1771+ // Input validation and permission checks
1772+ UserVmVO vmInstance = _vmDao .findById (id .longValue ());
17771773 if (vmInstance == null ) {
17781774 throw new InvalidParameterValueException (
17791775 "unable to find virtual machine with id " + id );
17801776 }
17811777
1782- ServiceOffering offering = _serviceOfferingDao .findById (vmInstance
1783- .getServiceOfferingId ());
1784- if (!offering .getOfferHA () && ha != null && ha ) {
1785- throw new InvalidParameterValueException (
1786- "Can't enable ha for the vm as it's created from the Service offering having HA disabled" );
1787- }
1788-
17891778 _accountMgr .checkAccess (CallContext .current ().getCallingAccount (), null , true ,
17901779 vmInstance );
1791-
1792- if (displayName == null ) {
1793- displayName = vmInstance .getDisplayName ();
1794- }
1795-
1796- if (ha == null ) {
1797- ha = vmInstance .isHaEnabled ();
1798- }
1799-
1800- if (isDisplayVmEnabled == null ) {
1801- isDisplayVmEnabled = vmInstance .isDisplayVm ();
1802- } else {
1780+
1781+ if (isDisplayVmEnabled != null ) {
18031782 if (!_accountMgr .isRootAdmin (caller .getType ())){
18041783 throw new PermissionDeniedException ( "Cannot update parameter displayvm, only admin permitted " );
18051784 }
18061785 }
18071786
1787+ return updateVirtualMachine (id , displayName , group , ha , isDisplayVmEnabled , osTypeId , userData , isDynamicallyScalable , cmd .getHttpMethod ());
1788+ }
1789+
1790+ @ Override
1791+ public UserVm updateVirtualMachine (long id , String displayName , String group , Boolean ha ,
1792+ Boolean isDisplayVmEnabled , Long osTypeId , String userData , Boolean isDynamicallyScalable , HTTPMethod httpMethod )
1793+ throws ResourceUnavailableException , InsufficientCapacityException {
18081794 UserVmVO vm = _vmDao .findById (id );
18091795 if (vm == null ) {
18101796 throw new CloudRuntimeException (
@@ -1816,45 +1802,50 @@ public UserVm updateVirtualMachine(UpdateVMCmd cmd)
18161802 throw new InvalidParameterValueException ("Vm with id " + id
18171803 + " is not in the right state" );
18181804 }
1805+
1806+ if (displayName == null ) {
1807+ displayName = vm .getDisplayName ();
1808+ }
1809+
1810+ if (ha == null ) {
1811+ ha = vm .isHaEnabled ();
1812+ }
1813+
1814+ ServiceOffering offering = _serviceOfferingDao .findById (vm .getServiceOfferingId ());
1815+ if (!offering .getOfferHA () && ha ) {
1816+ throw new InvalidParameterValueException (
1817+ "Can't enable ha for the vm as it's created from the Service offering having HA disabled" );
1818+ }
1819+
1820+ if (isDisplayVmEnabled == null ) {
1821+ isDisplayVmEnabled = vm .isDisplayVm ();
1822+ }
18191823
18201824 boolean updateUserdata = false ;
18211825 if (userData != null ) {
18221826 // check and replace newlines
18231827 userData = userData .replace ("\\ n" , "" );
1824- validateUserData (userData , cmd . getHttpMethod () );
1828+ validateUserData (userData , httpMethod );
18251829 // update userData on domain router.
18261830 updateUserdata = true ;
18271831 } else {
1828- userData = vmInstance .getUserData ();
1829- }
1830-
1831- String description = "" ;
1832-
1833- if (displayName != null && !displayName .equals (vmInstance .getDisplayName ())) {
1834- description += "New display name: " + displayName + ". " ;
1832+ userData = vm .getUserData ();
18351833 }
18361834
1837- if (ha != vmInstance .isHaEnabled ()) {
1838- if (ha ) {
1839- description += "Enabled HA. " ;
1840- } else {
1841- description += "Disabled HA. " ;
1842- }
1835+ if (isDynamicallyScalable == null ) {
1836+ isDynamicallyScalable = vm .isDynamicallyScalable ();
18431837 }
1838+
18441839 if (osTypeId == null ) {
1845- osTypeId = vmInstance .getGuestOSId ();
1846- } else {
1847- description += "Changed Guest OS Type to " + osTypeId + ". " ;
1840+ osTypeId = vm .getGuestOSId ();
18481841 }
18491842
18501843 if (group != null ) {
1851- if (addInstanceToGroup (id , group )) {
1852- description += "Added to group: " + group + "." ;
1853- }
1844+ addInstanceToGroup (id , group );
18541845 }
18551846
18561847 if (isDynamicallyScalable == null ) {
1857- isDynamicallyScalable = vmInstance .isDynamicallyScalable ();
1848+ isDynamicallyScalable = vm .isDynamicallyScalable ();
18581849 }
18591850
18601851 _vmDao .updateVM (id , displayName , ha , osTypeId , userData , isDisplayVmEnabled , isDynamicallyScalable );
0 commit comments