CLOUDSTACK-5870: API support for retrieving user data

This commit is contained in:
Alena Prokharchyk 2014-01-16 15:31:01 -08:00
parent 07f73ec054
commit bd79fb33a6
6 changed files with 112 additions and 0 deletions

View File

@ -461,4 +461,12 @@ public interface UserVmService {
UserVm expungeVm(long vmId) throws ResourceUnavailableException, ConcurrentOperationException;
/**
* Finds and returns an encrypted password for a VM.
*
* @param userVmId
* @return Base64 encoded userdata
*/
String getVmUserData(long vmId);
}

View File

@ -0,0 +1,63 @@
package org.apache.cloudstack.api.command.admin.vm;
import org.apache.cloudstack.api.APICommand;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.BaseCmd;
import org.apache.cloudstack.api.Parameter;
import org.apache.cloudstack.api.response.UserVmResponse;
import org.apache.cloudstack.api.response.VMUserDataResponse;
import org.apache.log4j.Logger;
import com.cloud.user.Account;
import com.cloud.uservm.UserVm;
@APICommand(name = "getVirtualMachineUserData", description = "Returns user data associated with the VM", responseObject = VMUserDataResponse.class, since = "4.4")
public class GetVMUserDataCmd extends BaseCmd {
public static final Logger s_logger = Logger.getLogger(GetVMUserDataCmd.class);
private static final String s_name = "getvirtualmachineuserdataresponse";
/////////////////////////////////////////////////////
//////////////// API parameters /////////////////////
/////////////////////////////////////////////////////
@Parameter(name = ApiConstants.VIRTUAL_MACHINE_ID, type = CommandType.UUID, entityType = UserVmResponse.class, required = true, description = "The ID of the virtual machine")
private Long vmId;
/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////
public long getId() {
return vmId;
}
/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////
@Override
public void execute() {
String userData = _userVmService.getVmUserData(getId());
VMUserDataResponse resp = new VMUserDataResponse();
resp.setVmId(_entityMgr.findById(UserVm.class, getId()).getUuid());
resp.setUserData(userData);
resp.setObjectName("virtualmachineuserdata");
resp.setResponseName(getCommandName());
this.setResponseObject(resp);
}
@Override
public long getEntityOwnerId() {
UserVm userVm = _entityMgr.findById(UserVm.class, getId());
if (userVm != null) {
return userVm.getAccountId();
}
return Account.ACCOUNT_ID_SYSTEM; // no account info given, parent this command to SYSTEM so ERROR events are tracked
}
@Override
public String getCommandName() {
return s_name;
}
}

View File

@ -0,0 +1,26 @@
package org.apache.cloudstack.api.response;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.BaseResponse;
import com.cloud.serializer.Param;
import com.google.gson.annotations.SerializedName;
public class VMUserDataResponse extends BaseResponse {
@SerializedName(ApiConstants.VIRTUAL_MACHINE_ID)
@Param(description = "the ID of the virtual machine")
private String vmId;
@SerializedName(ApiConstants.USER_DATA)
@Param(description = "Base 64 encoded VM user data")
private String userData;
public void setUserData(String userData) {
this.userData = userData;
}
public void setVmId(String vmId) {
this.vmId = vmId;
}
}

View File

@ -72,6 +72,7 @@ migrateVirtualMachine=1
migrateVirtualMachineWithVolume=1
recoverVirtualMachine=7
expungeVirtualMachine=7
getVirtualMachineUserData=15
#### snapshot commands
createSnapshot=15

View File

@ -198,6 +198,7 @@ import org.apache.cloudstack.api.command.admin.vlan.ListVlanIpRangesCmd;
import org.apache.cloudstack.api.command.admin.vlan.ReleasePublicIpRangeCmd;
import org.apache.cloudstack.api.command.admin.vm.AssignVMCmd;
import org.apache.cloudstack.api.command.admin.vm.ExpungeVMCmd;
import org.apache.cloudstack.api.command.admin.vm.GetVMUserDataCmd;
import org.apache.cloudstack.api.command.admin.vm.MigrateVMCmd;
import org.apache.cloudstack.api.command.admin.vm.MigrateVirtualMachineWithVolumeCmd;
import org.apache.cloudstack.api.command.admin.vm.RecoverVMCmd;
@ -2854,6 +2855,7 @@ public class ManagementServerImpl extends ManagerBase implements ManagementServe
cmdList.add(GenerateAlertCmd.class);
cmdList.add(ListOvsElementsCmd.class);
cmdList.add(ConfigureOvsElementCmd.class);
cmdList.add(GetVMUserDataCmd.class);
return cmdList;
}

View File

@ -4806,4 +4806,16 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
public ConfigKey<?>[] getConfigKeys() {
return new ConfigKey<?>[] {EnableDynamicallyScaleVm};
}
@Override
public String getVmUserData(long vmId) {
UserVmVO vm = _vmDao.findById(vmId);
if (vm == null) {
throw new InvalidParameterValueException("Unable to find virual machine with id " + vmId);
}
//check permissions
_accountMgr.checkAccess(CallContext.current().getCallingAccount(), null, true, vm);
return vm.getUserData();
}
}