|
| 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 | +} |
0 commit comments