|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.cloudstack.api.command.admin.vm; |
| 20 | + |
| 21 | +import org.apache.cloudstack.api.APICommand; |
| 22 | +import org.apache.cloudstack.api.ApiConstants; |
| 23 | +import org.apache.cloudstack.api.BaseCmd; |
| 24 | +import org.apache.cloudstack.api.Parameter; |
| 25 | +import org.apache.cloudstack.api.response.UserVmResponse; |
| 26 | +import org.apache.cloudstack.api.response.VMUserDataResponse; |
| 27 | +import org.apache.log4j.Logger; |
| 28 | + |
| 29 | +import com.cloud.user.Account; |
| 30 | +import com.cloud.uservm.UserVm; |
| 31 | + |
| 32 | +@APICommand(name = "getVirtualMachineUserData", description = "Returns user data associated with the VM", responseObject = VMUserDataResponse.class, since = "4.4") |
| 33 | +public class GetVMUserDataCmd extends BaseCmd { |
| 34 | + public static final Logger s_logger = Logger.getLogger(GetVMUserDataCmd.class); |
| 35 | + private static final String s_name = "getvirtualmachineuserdataresponse"; |
| 36 | + |
| 37 | + ///////////////////////////////////////////////////// |
| 38 | + //////////////// API parameters ///////////////////// |
| 39 | + ///////////////////////////////////////////////////// |
| 40 | + |
| 41 | + @Parameter(name = ApiConstants.VIRTUAL_MACHINE_ID, type = CommandType.UUID, entityType = UserVmResponse.class, required = true, description = "The ID of the virtual machine") |
| 42 | + private Long vmId; |
| 43 | + |
| 44 | + ///////////////////////////////////////////////////// |
| 45 | + /////////////////// Accessors /////////////////////// |
| 46 | + ///////////////////////////////////////////////////// |
| 47 | + |
| 48 | + public long getId() { |
| 49 | + return vmId; |
| 50 | + } |
| 51 | + |
| 52 | + ///////////////////////////////////////////////////// |
| 53 | + /////////////// API Implementation/////////////////// |
| 54 | + ///////////////////////////////////////////////////// |
| 55 | + |
| 56 | + @Override |
| 57 | + public void execute() { |
| 58 | + String userData = _userVmService.getVmUserData(getId()); |
| 59 | + VMUserDataResponse resp = new VMUserDataResponse(); |
| 60 | + resp.setVmId(_entityMgr.findById(UserVm.class, getId()).getUuid()); |
| 61 | + resp.setUserData(userData); |
| 62 | + resp.setObjectName("virtualmachineuserdata"); |
| 63 | + resp.setResponseName(getCommandName()); |
| 64 | + this.setResponseObject(resp); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public long getEntityOwnerId() { |
| 69 | + UserVm userVm = _entityMgr.findById(UserVm.class, getId()); |
| 70 | + if (userVm != null) { |
| 71 | + return userVm.getAccountId(); |
| 72 | + } |
| 73 | + |
| 74 | + return Account.ACCOUNT_ID_SYSTEM; // no account info given, parent this command to SYSTEM so ERROR events are tracked |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public String getCommandName() { |
| 79 | + return s_name; |
| 80 | + } |
| 81 | +} |
0 commit comments