|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with 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, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package org.apache.cloudstack.api.command.admin.vm; |
| 18 | + |
| 19 | +import javax.inject.Inject; |
| 20 | + |
| 21 | +import org.apache.cloudstack.acl.RoleType; |
| 22 | +import org.apache.cloudstack.api.ACL; |
| 23 | +import org.apache.cloudstack.api.APICommand; |
| 24 | +import org.apache.cloudstack.api.ApiConstants; |
| 25 | +import org.apache.cloudstack.api.ApiErrorCode; |
| 26 | +import org.apache.cloudstack.api.BaseCmd; |
| 27 | +import org.apache.cloudstack.api.Parameter; |
| 28 | +import org.apache.cloudstack.api.ServerApiException; |
| 29 | +import org.apache.cloudstack.api.response.SuccessResponse; |
| 30 | +import org.apache.cloudstack.api.response.UserVmResponse; |
| 31 | +import org.apache.cloudstack.vmstateupdate.UpdateVmStateService; |
| 32 | + |
| 33 | +import com.cloud.user.Account; |
| 34 | +import com.cloud.vm.VirtualMachine; |
| 35 | + |
| 36 | +@APICommand(name = "updateVirtualMachineState", |
| 37 | + description = "Updates the state of a virtual machine. This is an admin-only operation.", |
| 38 | + responseObject = SuccessResponse.class, |
| 39 | + entityType = {VirtualMachine.class}, |
| 40 | + requestHasSensitiveInfo = false, |
| 41 | + responseHasSensitiveInfo = false, |
| 42 | + authorized = {RoleType.Admin}) |
| 43 | +public class UpdateVmStateCmd extends BaseCmd { |
| 44 | + |
| 45 | + @ACL |
| 46 | + @Parameter(name = ApiConstants.ID, |
| 47 | + type = CommandType.UUID, |
| 48 | + entityType = UserVmResponse.class, |
| 49 | + required = true, |
| 50 | + description = "The ID of the virtual machine") |
| 51 | + private Long id; |
| 52 | + |
| 53 | + @Parameter(name = ApiConstants.STATE, |
| 54 | + type = CommandType.STRING, |
| 55 | + required = true, |
| 56 | + description = "The state to set for the virtual machine. Valid values are: Starting, Running, Stopping, Stopped, Destroyed, Expunging, Migrating, Error, Unknown, Shutdown, Restoring") |
| 57 | + private String state; |
| 58 | + |
| 59 | + @Inject |
| 60 | + UpdateVmStateService _updateVmStateService; |
| 61 | + |
| 62 | + public Long getId() { |
| 63 | + return id; |
| 64 | + } |
| 65 | + |
| 66 | + public String getState() { |
| 67 | + return state; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void execute() throws ServerApiException { |
| 72 | + try { |
| 73 | + boolean result = _updateVmStateService.updateVmState(getId(), getState()); |
| 74 | + if (result) { |
| 75 | + SuccessResponse response = new SuccessResponse(getCommandName()); |
| 76 | + response.setDisplayText("Virtual machine state updated successfully to " + getState()); |
| 77 | + setResponseObject(response); |
| 78 | + } else { |
| 79 | + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to update virtual machine state"); |
| 80 | + } |
| 81 | + } catch (IllegalArgumentException e) { |
| 82 | + throw new ServerApiException(ApiErrorCode.PARAM_ERROR, e.getMessage()); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public long getEntityOwnerId() { |
| 88 | + return Account.ACCOUNT_ID_SYSTEM; |
| 89 | + } |
| 90 | +} |
0 commit comments