diff --git a/api/src/com/cloud/api/commands/UpdateVMCmd.java b/api/src/com/cloud/api/commands/UpdateVMCmd.java index f380abc23f8..16298f6d194 100644 --- a/api/src/com/cloud/api/commands/UpdateVMCmd.java +++ b/api/src/com/cloud/api/commands/UpdateVMCmd.java @@ -52,6 +52,10 @@ public class UpdateVMCmd extends BaseCmd{ @Parameter(name=ApiConstants.OS_TYPE_ID, type=CommandType.LONG, description="the ID of the OS type that best represents this VM.") private Long osTypeId; + + @Parameter(name=ApiConstants.USER_DATA, type=CommandType.STRING, description="an optional binary data that can be sent to the virtual machine upon a successful deployment. This binary data must be base64 encoded before adding it to the request. Currently only HTTP GET is supported. Using HTTP GET (via querystring), you can send up to 2KB of data after base64 encoding.") + private String userData; + ///////////////////////////////////////////////////// /////////////////// Accessors /////////////////////// @@ -72,6 +76,10 @@ public class UpdateVMCmd extends BaseCmd{ public Long getId() { return id; } + + public String getUserData() { + return userData; + } ///////////////////////////////////////////////////// /////////////// API Implementation/////////////////// diff --git a/server/src/com/cloud/vm/UserVmManagerImpl.java b/server/src/com/cloud/vm/UserVmManagerImpl.java index 57c7cc10eba..adabfb8dcd5 100755 --- a/server/src/com/cloud/vm/UserVmManagerImpl.java +++ b/server/src/com/cloud/vm/UserVmManagerImpl.java @@ -1607,7 +1607,8 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager Long osTypeId = cmd.getOsTypeId(); Account account = UserContext.current().getCaller(); Long userId = UserContext.current().getCallerUserId(); - + String userData = cmd.getUserData(); + //Input validation UserVmVO vmInstance = null; @@ -1625,6 +1626,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager userId = accountAndUserValidation(id, account, userId,vmInstance); + if (displayName == null) { displayName = vmInstance.getDisplayName(); } @@ -1643,6 +1645,13 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager throw new InvalidParameterValueException("Vm with id " + id + " is not in the right state"); } + if(userData != null){ + validateUserData(userData); + //update userData on domain router. + }else{ + userData = vmInstance.getUserData(); + } + String description = ""; if(displayName != vmInstance.getDisplayName()){ @@ -1668,7 +1677,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager } } - _vmDao.updateVM(id, displayName, ha, osTypeId); + _vmDao.updateVM(id, displayName, ha, osTypeId, userData); return _vmDao.findById(id); } @@ -2222,19 +2231,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager } } - byte [] decodedUserData = null; - if (userData != null) { - if (userData.length() >= 2 * MAX_USER_DATA_LENGTH_BYTES) { - throw new InvalidParameterValueException("User data is too long"); - } - decodedUserData = org.apache.commons.codec.binary.Base64.decodeBase64(userData.getBytes()); - if (decodedUserData.length > MAX_USER_DATA_LENGTH_BYTES){ - throw new InvalidParameterValueException("User data is too long"); - } - if (decodedUserData.length < 1) { - throw new InvalidParameterValueException("User data is too short"); - } - } + validateUserData(userData); // Find an SSH public key corresponding to the key pair name, if one is given String sshPublicKey = null; @@ -2342,6 +2339,22 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager return vm; } + private void validateUserData(String userData){ + byte [] decodedUserData = null; + if (userData != null) { + if (userData.length() >= 2 * MAX_USER_DATA_LENGTH_BYTES) { + throw new InvalidParameterValueException("User data is too long"); + } + decodedUserData = org.apache.commons.codec.binary.Base64.decodeBase64(userData.getBytes()); + if (decodedUserData.length > MAX_USER_DATA_LENGTH_BYTES){ + throw new InvalidParameterValueException("User data is too long"); + } + if (decodedUserData.length < 1) { + throw new InvalidParameterValueException("User data is too short"); + } + } + } + @Override @ActionEvent (eventType=EventTypes.EVENT_VM_CREATE, eventDescription="starting Vm", async=true) public UserVm startVirtualMachine(DeployVMCmd cmd) throws ResourceUnavailableException, InsufficientCapacityException, ConcurrentOperationException { return startVirtualMachine(cmd, null); diff --git a/server/src/com/cloud/vm/dao/UserVmDao.java b/server/src/com/cloud/vm/dao/UserVmDao.java index 4ab64106165..010f8f5e7ec 100755 --- a/server/src/com/cloud/vm/dao/UserVmDao.java +++ b/server/src/com/cloud/vm/dao/UserVmDao.java @@ -39,8 +39,9 @@ public interface UserVmDao extends GenericDao { * Updates display name and group for vm; enables/disables ha * @param id vm id. * @param displan name and enable for ha + * @param userData updates the userData of the vm */ - void updateVM(long id, String displayName, boolean enable, Long osTypeId); + void updateVM(long id, String displayName, boolean enable, Long osTypeId, String userData); List findDestroyedVms(Date date); diff --git a/server/src/com/cloud/vm/dao/UserVmDaoImpl.java b/server/src/com/cloud/vm/dao/UserVmDaoImpl.java index e360eb91746..f0458ab669e 100755 --- a/server/src/com/cloud/vm/dao/UserVmDaoImpl.java +++ b/server/src/com/cloud/vm/dao/UserVmDaoImpl.java @@ -167,11 +167,12 @@ public class UserVmDaoImpl extends GenericDaoBase implements Use } @Override - public void updateVM(long id, String displayName, boolean enable, Long osTypeId) { + public void updateVM(long id, String displayName, boolean enable, Long osTypeId, String userData) { UserVmVO vo = createForUpdate(); vo.setDisplayName(displayName); vo.setHaEnabled(enable); vo.setGuestOSId(osTypeId); + vo.setUserData(userData); update(id, vo); }