Bug 9187: modify userdata with update vm

Changes:
- UpdateVMCmd is changed to take in userData as a parameter
-The userData is updated in the DB if it is non-null.
This commit is contained in:
prachi 2011-04-13 19:22:18 -07:00
parent 1a3483ac4f
commit 88c0126878
4 changed files with 40 additions and 17 deletions

View File

@ -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///////////////////

View File

@ -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);

View File

@ -39,8 +39,9 @@ public interface UserVmDao extends GenericDao<UserVmVO, Long> {
* 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<UserVmVO> findDestroyedVms(Date date);

View File

@ -167,11 +167,12 @@ public class UserVmDaoImpl extends GenericDaoBase<UserVmVO, Long> 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);
}