Skip to content

Commit bd79fb3

Browse files
author
Alena Prokharchyk
committed
CLOUDSTACK-5870: API support for retrieving user data
1 parent 07f73ec commit bd79fb3

6 files changed

Lines changed: 112 additions & 0 deletions

File tree

api/src/com/cloud/vm/UserVmService.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,4 +461,12 @@ UserVm upgradeVirtualMachine(ScaleVMCmd cmd) throws ResourceUnavailableException
461461

462462
UserVm expungeVm(long vmId) throws ResourceUnavailableException, ConcurrentOperationException;
463463

464+
/**
465+
* Finds and returns an encrypted password for a VM.
466+
*
467+
* @param userVmId
468+
* @return Base64 encoded userdata
469+
*/
470+
String getVmUserData(long vmId);
471+
464472
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package org.apache.cloudstack.api.command.admin.vm;
2+
3+
import org.apache.cloudstack.api.APICommand;
4+
import org.apache.cloudstack.api.ApiConstants;
5+
import org.apache.cloudstack.api.BaseCmd;
6+
import org.apache.cloudstack.api.Parameter;
7+
import org.apache.cloudstack.api.response.UserVmResponse;
8+
import org.apache.cloudstack.api.response.VMUserDataResponse;
9+
import org.apache.log4j.Logger;
10+
11+
import com.cloud.user.Account;
12+
import com.cloud.uservm.UserVm;
13+
14+
@APICommand(name = "getVirtualMachineUserData", description = "Returns user data associated with the VM", responseObject = VMUserDataResponse.class, since = "4.4")
15+
public class GetVMUserDataCmd extends BaseCmd {
16+
public static final Logger s_logger = Logger.getLogger(GetVMUserDataCmd.class);
17+
private static final String s_name = "getvirtualmachineuserdataresponse";
18+
19+
/////////////////////////////////////////////////////
20+
//////////////// API parameters /////////////////////
21+
/////////////////////////////////////////////////////
22+
23+
@Parameter(name = ApiConstants.VIRTUAL_MACHINE_ID, type = CommandType.UUID, entityType = UserVmResponse.class, required = true, description = "The ID of the virtual machine")
24+
private Long vmId;
25+
26+
/////////////////////////////////////////////////////
27+
/////////////////// Accessors ///////////////////////
28+
/////////////////////////////////////////////////////
29+
30+
public long getId() {
31+
return vmId;
32+
}
33+
34+
/////////////////////////////////////////////////////
35+
/////////////// API Implementation///////////////////
36+
/////////////////////////////////////////////////////
37+
38+
@Override
39+
public void execute() {
40+
String userData = _userVmService.getVmUserData(getId());
41+
VMUserDataResponse resp = new VMUserDataResponse();
42+
resp.setVmId(_entityMgr.findById(UserVm.class, getId()).getUuid());
43+
resp.setUserData(userData);
44+
resp.setObjectName("virtualmachineuserdata");
45+
resp.setResponseName(getCommandName());
46+
this.setResponseObject(resp);
47+
}
48+
49+
@Override
50+
public long getEntityOwnerId() {
51+
UserVm userVm = _entityMgr.findById(UserVm.class, getId());
52+
if (userVm != null) {
53+
return userVm.getAccountId();
54+
}
55+
56+
return Account.ACCOUNT_ID_SYSTEM; // no account info given, parent this command to SYSTEM so ERROR events are tracked
57+
}
58+
59+
@Override
60+
public String getCommandName() {
61+
return s_name;
62+
}
63+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.apache.cloudstack.api.response;
2+
3+
import org.apache.cloudstack.api.ApiConstants;
4+
import org.apache.cloudstack.api.BaseResponse;
5+
6+
import com.cloud.serializer.Param;
7+
import com.google.gson.annotations.SerializedName;
8+
9+
public class VMUserDataResponse extends BaseResponse {
10+
@SerializedName(ApiConstants.VIRTUAL_MACHINE_ID)
11+
@Param(description = "the ID of the virtual machine")
12+
private String vmId;
13+
14+
@SerializedName(ApiConstants.USER_DATA)
15+
@Param(description = "Base 64 encoded VM user data")
16+
private String userData;
17+
18+
public void setUserData(String userData) {
19+
this.userData = userData;
20+
}
21+
22+
public void setVmId(String vmId) {
23+
this.vmId = vmId;
24+
}
25+
26+
}

client/tomcatconf/commands.properties.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ migrateVirtualMachine=1
7272
migrateVirtualMachineWithVolume=1
7373
recoverVirtualMachine=7
7474
expungeVirtualMachine=7
75+
getVirtualMachineUserData=15
7576

7677
#### snapshot commands
7778
createSnapshot=15

server/src/com/cloud/server/ManagementServerImpl.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@
198198
import org.apache.cloudstack.api.command.admin.vlan.ReleasePublicIpRangeCmd;
199199
import org.apache.cloudstack.api.command.admin.vm.AssignVMCmd;
200200
import org.apache.cloudstack.api.command.admin.vm.ExpungeVMCmd;
201+
import org.apache.cloudstack.api.command.admin.vm.GetVMUserDataCmd;
201202
import org.apache.cloudstack.api.command.admin.vm.MigrateVMCmd;
202203
import org.apache.cloudstack.api.command.admin.vm.MigrateVirtualMachineWithVolumeCmd;
203204
import org.apache.cloudstack.api.command.admin.vm.RecoverVMCmd;
@@ -2854,6 +2855,7 @@ public List<Class<?>> getCommands() {
28542855
cmdList.add(GenerateAlertCmd.class);
28552856
cmdList.add(ListOvsElementsCmd.class);
28562857
cmdList.add(ConfigureOvsElementCmd.class);
2858+
cmdList.add(GetVMUserDataCmd.class);
28572859
return cmdList;
28582860
}
28592861

server/src/com/cloud/vm/UserVmManagerImpl.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4806,4 +4806,16 @@ public String getConfigComponentName() {
48064806
public ConfigKey<?>[] getConfigKeys() {
48074807
return new ConfigKey<?>[] {EnableDynamicallyScaleVm};
48084808
}
4809+
4810+
@Override
4811+
public String getVmUserData(long vmId) {
4812+
UserVmVO vm = _vmDao.findById(vmId);
4813+
if (vm == null) {
4814+
throw new InvalidParameterValueException("Unable to find virual machine with id " + vmId);
4815+
}
4816+
4817+
//check permissions
4818+
_accountMgr.checkAccess(CallContext.current().getCallingAccount(), null, true, vm);
4819+
return vm.getUserData();
4820+
}
48094821
}

0 commit comments

Comments
 (0)