From d252d95c2645c61e0bc8845a7ff4267e700c081d Mon Sep 17 00:00:00 2001 From: Min Chen Date: Mon, 9 Dec 2013 17:30:57 -0800 Subject: [PATCH] Separate all vm related user APIs to have two different views. --- .../cloudstack/api/ResponseGenerator.java | 4 +- .../UpdateVMAffinityGroupCmdByAdmin.java | 63 +++++++++++++++ ...stLoadBalancerRuleInstancesCmdByAdmin.java | 57 +++++++++++++ .../admin/vm/AddNicToVMCmdByAdmin.java | 55 +++++++++++++ .../api/command/admin/vm/AssignVMCmd.java | 8 +- .../command/admin/vm/DeployVMCmdByAdmin.java | 79 ++++++++++++++++++ .../command/admin/vm/DestroyVMCmdByAdmin.java | 57 +++++++++++++ .../api/command/admin/vm/MigrateVMCmd.java | 9 ++- .../MigrateVirtualMachineWithVolumeCmd.java | 16 ++-- .../command/admin/vm/RebootVMCmdByAdmin.java | 51 ++++++++++++ .../api/command/admin/vm/RecoverVMCmd.java | 8 +- .../admin/vm/RemoveNicFromVMCmdByAdmin.java | 55 +++++++++++++ .../admin/vm/ResetVMPasswordCmdByAdmin.java | 54 +++++++++++++ .../admin/vm/ResetVMSSHKeyCmdByAdmin.java | 57 +++++++++++++ .../command/admin/vm/RestoreVMCmdByAdmin.java | 54 +++++++++++++ .../command/admin/vm/ScaleVMCmdByAdmin.java | 69 ++++++++++++++++ .../command/admin/vm/StartVMCmdByAdmin.java | 80 +++++++++++++++++++ .../command/admin/vm/StopVMCmdByAdmin.java | 53 ++++++++++++ .../vm/UpdateDefaultNicForVMCmdByAdmin.java | 56 +++++++++++++ .../command/admin/vm/UpdateVMCmdByAdmin.java | 53 ++++++++++++ .../command/admin/vm/UpgradeVMCmdByAdmin.java | 59 ++++++++++++++ .../RevertToVMSnapshotCmdByAdmin.java | 57 +++++++++++++ .../UpdateVMAffinityGroupCmd.java | 13 +-- .../api/command/user/iso/AttachIsoCmd.java | 11 +-- .../api/command/user/iso/DetachIsoCmd.java | 11 +-- .../ListLoadBalancerRuleInstancesCmd.java | 11 +-- .../api/command/user/vm/AddNicToVMCmd.java | 20 ++--- .../api/command/user/vm/DeployVMCmd.java | 5 +- .../api/command/user/vm/DestroyVMCmd.java | 16 ++-- .../api/command/user/vm/RebootVMCmd.java | 14 ++-- .../command/user/vm/RemoveNicFromVMCmd.java | 20 ++--- .../command/user/vm/ResetVMPasswordCmd.java | 15 ++-- .../api/command/user/vm/ResetVMSSHKeyCmd.java | 18 +++-- .../api/command/user/vm/RestoreVMCmd.java | 11 +-- .../api/command/user/vm/ScaleVMCmd.java | 37 +++++---- .../api/command/user/vm/StartVMCmd.java | 13 +-- .../api/command/user/vm/StopVMCmd.java | 12 +-- .../user/vm/UpdateDefaultNicForVMCmd.java | 20 ++--- .../api/command/user/vm/UpdateVMCmd.java | 11 +-- .../api/command/user/vm/UpgradeVMCmd.java | 7 +- .../vmsnapshot/RevertToVMSnapshotCmd.java | 7 +- .../api/command/test/ScaleVMCmdTest.java | 22 ++--- .../src/com/cloud/api/ApiResponseHelper.java | 2 + .../com/cloud/user/AccountManagerImpl.java | 1 + 44 files changed, 1205 insertions(+), 146 deletions(-) create mode 100644 api/src/org/apache/cloudstack/api/command/admin/affinitygroup/UpdateVMAffinityGroupCmdByAdmin.java create mode 100644 api/src/org/apache/cloudstack/api/command/admin/loadbalancer/ListLoadBalancerRuleInstancesCmdByAdmin.java create mode 100644 api/src/org/apache/cloudstack/api/command/admin/vm/AddNicToVMCmdByAdmin.java create mode 100755 api/src/org/apache/cloudstack/api/command/admin/vm/DeployVMCmdByAdmin.java create mode 100644 api/src/org/apache/cloudstack/api/command/admin/vm/DestroyVMCmdByAdmin.java create mode 100644 api/src/org/apache/cloudstack/api/command/admin/vm/RebootVMCmdByAdmin.java create mode 100644 api/src/org/apache/cloudstack/api/command/admin/vm/RemoveNicFromVMCmdByAdmin.java create mode 100644 api/src/org/apache/cloudstack/api/command/admin/vm/ResetVMPasswordCmdByAdmin.java create mode 100644 api/src/org/apache/cloudstack/api/command/admin/vm/ResetVMSSHKeyCmdByAdmin.java create mode 100644 api/src/org/apache/cloudstack/api/command/admin/vm/RestoreVMCmdByAdmin.java create mode 100644 api/src/org/apache/cloudstack/api/command/admin/vm/ScaleVMCmdByAdmin.java create mode 100644 api/src/org/apache/cloudstack/api/command/admin/vm/StartVMCmdByAdmin.java create mode 100644 api/src/org/apache/cloudstack/api/command/admin/vm/StopVMCmdByAdmin.java create mode 100644 api/src/org/apache/cloudstack/api/command/admin/vm/UpdateDefaultNicForVMCmdByAdmin.java create mode 100644 api/src/org/apache/cloudstack/api/command/admin/vm/UpdateVMCmdByAdmin.java create mode 100644 api/src/org/apache/cloudstack/api/command/admin/vm/UpgradeVMCmdByAdmin.java create mode 100644 api/src/org/apache/cloudstack/api/command/admin/vmsnapshot/RevertToVMSnapshotCmdByAdmin.java diff --git a/api/src/org/apache/cloudstack/api/ResponseGenerator.java b/api/src/org/apache/cloudstack/api/ResponseGenerator.java index 4b222d2f0a3..77ba4cefefb 100644 --- a/api/src/org/apache/cloudstack/api/ResponseGenerator.java +++ b/api/src/org/apache/cloudstack/api/ResponseGenerator.java @@ -216,9 +216,9 @@ public interface ResponseGenerator { SnapshotPolicyResponse createSnapshotPolicyResponse(SnapshotPolicy policy); - List createUserVmResponse(String objectName, UserVm... userVms); + //List createUserVmResponse(String objectName, UserVm... userVms); - List createUserVmResponse(String objectName, EnumSet details, UserVm... userVms); + //List createUserVmResponse(String objectName, EnumSet details, UserVm... userVms); List createUserVmResponse(ResponseView view, String objectName, UserVm... userVms); diff --git a/api/src/org/apache/cloudstack/api/command/admin/affinitygroup/UpdateVMAffinityGroupCmdByAdmin.java b/api/src/org/apache/cloudstack/api/command/admin/affinitygroup/UpdateVMAffinityGroupCmdByAdmin.java new file mode 100644 index 00000000000..85b8dd711d1 --- /dev/null +++ b/api/src/org/apache/cloudstack/api/command/admin/affinitygroup/UpdateVMAffinityGroupCmdByAdmin.java @@ -0,0 +1,63 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package org.apache.cloudstack.api.command.admin.affinitygroup; + +import java.util.ArrayList; +import java.util.EnumSet; + +import org.apache.log4j.Logger; + +import org.apache.cloudstack.api.APICommand; +import org.apache.cloudstack.api.ApiConstants.VMDetails; +import org.apache.cloudstack.api.ApiErrorCode; +import org.apache.cloudstack.api.ResponseObject.ResponseView; +import org.apache.cloudstack.api.ServerApiException; +import org.apache.cloudstack.api.command.user.affinitygroup.UpdateVMAffinityGroupCmd; +import org.apache.cloudstack.api.response.UserVmResponse; +import org.apache.cloudstack.context.CallContext; + +import com.cloud.exception.InsufficientCapacityException; +import com.cloud.exception.ResourceUnavailableException; +import com.cloud.uservm.UserVm; + + +@APICommand(name = "updateVMAffinityGroup", description = "Updates the affinity/anti-affinity group associations of a virtual machine. The VM has to be stopped and restarted for the " + + "new properties to take effect.", responseObject = UserVmResponse.class, responseView = ResponseView.Full) +public class UpdateVMAffinityGroupCmdByAdmin extends UpdateVMAffinityGroupCmd { + public static final Logger s_logger = Logger.getLogger(UpdateVMAffinityGroupCmdByAdmin.class.getName()); + + + @Override + public void execute() throws ResourceUnavailableException, + InsufficientCapacityException, ServerApiException { + CallContext.current().setEventDetails("Vm Id: "+getId()); + UserVm result = _affinityGroupService.updateVMAffinityGroups(getId(), getAffinityGroupIdList()); + ArrayList dc = new ArrayList(); + dc.add(VMDetails.valueOf("affgrp")); + EnumSet details = EnumSet.copyOf(dc); + + if (result != null){ + UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Full, "virtualmachine", details, result).get(0); + response.setResponseName(getCommandName()); + setResponseObject(response); + } else { + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to update vm's affinity groups"); + } + } + + +} diff --git a/api/src/org/apache/cloudstack/api/command/admin/loadbalancer/ListLoadBalancerRuleInstancesCmdByAdmin.java b/api/src/org/apache/cloudstack/api/command/admin/loadbalancer/ListLoadBalancerRuleInstancesCmdByAdmin.java new file mode 100644 index 00000000000..26202b9bae5 --- /dev/null +++ b/api/src/org/apache/cloudstack/api/command/admin/loadbalancer/ListLoadBalancerRuleInstancesCmdByAdmin.java @@ -0,0 +1,57 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package org.apache.cloudstack.api.command.admin.loadbalancer; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.log4j.Logger; + +import org.apache.cloudstack.api.APICommand; +import org.apache.cloudstack.api.ResponseObject.ResponseView; +import org.apache.cloudstack.api.command.user.loadbalancer.ListLoadBalancerRuleInstancesCmd; +import org.apache.cloudstack.api.response.ListResponse; +import org.apache.cloudstack.api.response.UserVmResponse; + +import com.cloud.uservm.UserVm; +import com.cloud.utils.Pair; + +@APICommand(name = "listLoadBalancerRuleInstances", description = "List all virtual machine instances that are assigned to a load balancer rule.", responseObject = UserVmResponse.class, responseView = ResponseView.Full) +public class ListLoadBalancerRuleInstancesCmdByAdmin extends ListLoadBalancerRuleInstancesCmd { + public static final Logger s_logger = Logger.getLogger (ListLoadBalancerRuleInstancesCmdByAdmin.class.getName()); + + + + @Override + public void execute(){ + Pair, List> vmServiceMap = _lbService.listLoadBalancerInstances(this); + List result = vmServiceMap.first(); + List serviceStates = vmServiceMap.second(); + ListResponse response = new ListResponse(); + List vmResponses = new ArrayList(); + if (result != null) { + vmResponses = _responseGenerator.createUserVmResponse(ResponseView.Full, "loadbalancerruleinstance", result.toArray(new UserVm[result.size()])); + } + + for (int i=0;i dc = new ArrayList(); + dc.add(VMDetails.valueOf("nics")); + EnumSet details = EnumSet.copyOf(dc); + if (result != null){ + UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Full, "virtualmachine", details, result).get(0); + response.setResponseName(getCommandName()); + setResponseObject(response); + } else { + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to add NIC to vm. Refer to server logs for details."); + } + } +} diff --git a/api/src/org/apache/cloudstack/api/command/admin/vm/AssignVMCmd.java b/api/src/org/apache/cloudstack/api/command/admin/vm/AssignVMCmd.java index 6da4b6c9034..96823f0c466 100644 --- a/api/src/org/apache/cloudstack/api/command/admin/vm/AssignVMCmd.java +++ b/api/src/org/apache/cloudstack/api/command/admin/vm/AssignVMCmd.java @@ -19,18 +19,20 @@ package org.apache.cloudstack.api.command.admin.vm; import java.util.List; +import org.apache.log4j.Logger; + import org.apache.cloudstack.api.APICommand; import org.apache.cloudstack.api.ApiConstants; import org.apache.cloudstack.api.ApiErrorCode; import org.apache.cloudstack.api.BaseCmd; import org.apache.cloudstack.api.Parameter; +import org.apache.cloudstack.api.ResponseObject.ResponseView; import org.apache.cloudstack.api.ServerApiException; import org.apache.cloudstack.api.command.user.vm.DeployVMCmd; import org.apache.cloudstack.api.response.DomainResponse; import org.apache.cloudstack.api.response.NetworkResponse; import org.apache.cloudstack.api.response.SecurityGroupResponse; import org.apache.cloudstack.api.response.UserVmResponse; -import org.apache.log4j.Logger; import com.cloud.user.Account; import com.cloud.uservm.UserVm; @@ -106,9 +108,9 @@ public class AssignVMCmd extends BaseCmd { if (userVm == null){ throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to move vm"); } - UserVmResponse response = _responseGenerator.createUserVmResponse("virtualmachine", userVm).get(0); + UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Full, "virtualmachine", userVm).get(0); response.setResponseName(DeployVMCmd.getResultObjectName()); - this.setResponseObject(response); + setResponseObject(response); }catch (Exception e){ e.printStackTrace(); throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to move vm " + e.getMessage()); diff --git a/api/src/org/apache/cloudstack/api/command/admin/vm/DeployVMCmdByAdmin.java b/api/src/org/apache/cloudstack/api/command/admin/vm/DeployVMCmdByAdmin.java new file mode 100755 index 00000000000..99fec6b5a7d --- /dev/null +++ b/api/src/org/apache/cloudstack/api/command/admin/vm/DeployVMCmdByAdmin.java @@ -0,0 +1,79 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package org.apache.cloudstack.api.command.admin.vm; + +import org.apache.log4j.Logger; + +import org.apache.cloudstack.api.APICommand; +import org.apache.cloudstack.api.ApiErrorCode; +import org.apache.cloudstack.api.ResponseObject.ResponseView; +import org.apache.cloudstack.api.ServerApiException; +import org.apache.cloudstack.api.command.user.vm.DeployVMCmd; +import org.apache.cloudstack.api.response.UserVmResponse; +import org.apache.cloudstack.context.CallContext; + +import com.cloud.exception.ConcurrentOperationException; +import com.cloud.exception.InsufficientCapacityException; +import com.cloud.exception.InsufficientServerCapacityException; +import com.cloud.exception.ResourceUnavailableException; +import com.cloud.uservm.UserVm; + + +@APICommand(name = "deployVirtualMachine", description = "Creates and automatically starts a virtual machine based on a service offering, disk offering, and template.", responseObject = UserVmResponse.class, responseView = ResponseView.Full) +public class DeployVMCmdByAdmin extends DeployVMCmd { + public static final Logger s_logger = Logger.getLogger(DeployVMCmdByAdmin.class.getName()); + + + @Override + public void execute(){ + UserVm result; + + if (getStartVm()) { + try { + CallContext.current().setEventDetails("Vm Id: "+getEntityId()); + result = _userVmService.startVirtualMachine(this); + } catch (ResourceUnavailableException ex) { + s_logger.warn("Exception: ", ex); + throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, ex.getMessage()); + } catch (ConcurrentOperationException ex) { + s_logger.warn("Exception: ", ex); + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage()); + } catch (InsufficientCapacityException ex) { + StringBuilder message = new StringBuilder(ex.getMessage()); + if (ex instanceof InsufficientServerCapacityException) { + if(((InsufficientServerCapacityException)ex).isAffinityApplied()){ + message.append(", Please check the affinity groups provided, there may not be sufficient capacity to follow them"); + } + } + s_logger.info(ex); + s_logger.info(message.toString(), ex); + throw new ServerApiException(ApiErrorCode.INSUFFICIENT_CAPACITY_ERROR, message.toString()); + } + } else { + result = _userVmService.getUserVm(getEntityId()); + } + + if (result != null) { + UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Restricted, "virtualmachine", result).get(0); + response.setResponseName(getCommandName()); + setResponseObject(response); + } else { + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to deploy vm"); + } + } + +} diff --git a/api/src/org/apache/cloudstack/api/command/admin/vm/DestroyVMCmdByAdmin.java b/api/src/org/apache/cloudstack/api/command/admin/vm/DestroyVMCmdByAdmin.java new file mode 100644 index 00000000000..b14e5b02511 --- /dev/null +++ b/api/src/org/apache/cloudstack/api/command/admin/vm/DestroyVMCmdByAdmin.java @@ -0,0 +1,57 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package org.apache.cloudstack.api.command.admin.vm; + +import java.util.List; + +import org.apache.log4j.Logger; + +import org.apache.cloudstack.api.APICommand; +import org.apache.cloudstack.api.ApiErrorCode; +import org.apache.cloudstack.api.ResponseObject.ResponseView; +import org.apache.cloudstack.api.ServerApiException; +import org.apache.cloudstack.api.command.user.vm.DestroyVMCmd; +import org.apache.cloudstack.api.response.UserVmResponse; +import org.apache.cloudstack.context.CallContext; + +import com.cloud.exception.ConcurrentOperationException; +import com.cloud.exception.ResourceUnavailableException; +import com.cloud.uservm.UserVm; + +@APICommand(name = "destroyVirtualMachine", description = "Destroys a virtual machine. Once destroyed, only the administrator can recover it.", responseObject = UserVmResponse.class, responseView = ResponseView.Full) +public class DestroyVMCmdByAdmin extends DestroyVMCmd { + public static final Logger s_logger = Logger.getLogger(DestroyVMCmdByAdmin.class.getName()); + + + @Override + public void execute() throws ResourceUnavailableException, ConcurrentOperationException{ + CallContext.current().setEventDetails("Vm Id: "+getId()); + UserVm result = _userVmService.destroyVm(this); + + UserVmResponse response = new UserVmResponse(); + if (result != null) { + List responses = _responseGenerator.createUserVmResponse(ResponseView.Full, "virtualmachine", result); + if (responses != null && !responses.isEmpty()) { + response = responses.get(0); + } + response.setResponseName("virtualmachine"); + setResponseObject(response); + } else { + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to destroy vm"); + } + } +} diff --git a/api/src/org/apache/cloudstack/api/command/admin/vm/MigrateVMCmd.java b/api/src/org/apache/cloudstack/api/command/admin/vm/MigrateVMCmd.java index 852f6b75bc3..2532d1fd5ac 100644 --- a/api/src/org/apache/cloudstack/api/command/admin/vm/MigrateVMCmd.java +++ b/api/src/org/apache/cloudstack/api/command/admin/vm/MigrateVMCmd.java @@ -16,19 +16,20 @@ // under the License. package org.apache.cloudstack.api.command.admin.vm; +import org.apache.log4j.Logger; + import org.apache.cloudstack.api.APICommand; import org.apache.cloudstack.api.ApiConstants; import org.apache.cloudstack.api.ApiErrorCode; import org.apache.cloudstack.api.BaseAsyncCmd; import org.apache.cloudstack.api.Parameter; +import org.apache.cloudstack.api.ResponseObject.ResponseView; import org.apache.cloudstack.api.ServerApiException; import org.apache.cloudstack.api.response.HostResponse; import org.apache.cloudstack.api.response.StoragePoolResponse; import org.apache.cloudstack.api.response.UserVmResponse; import org.apache.cloudstack.context.CallContext; -import org.apache.log4j.Logger; - import com.cloud.event.EventTypes; import com.cloud.exception.ConcurrentOperationException; import com.cloud.exception.InvalidParameterValueException; @@ -150,9 +151,9 @@ public class MigrateVMCmd extends BaseAsyncCmd { migratedVm = _userVmService.vmStorageMigration(getVirtualMachineId(), destStoragePool); } if (migratedVm != null) { - UserVmResponse response = _responseGenerator.createUserVmResponse("virtualmachine", (UserVm)migratedVm).get(0); + UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Full, "virtualmachine", (UserVm)migratedVm).get(0); response.setResponseName(getCommandName()); - this.setResponseObject(response); + setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to migrate vm"); } diff --git a/api/src/org/apache/cloudstack/api/command/admin/vm/MigrateVirtualMachineWithVolumeCmd.java b/api/src/org/apache/cloudstack/api/command/admin/vm/MigrateVirtualMachineWithVolumeCmd.java index ab0dcd52133..5cb62c748d2 100644 --- a/api/src/org/apache/cloudstack/api/command/admin/vm/MigrateVirtualMachineWithVolumeCmd.java +++ b/api/src/org/apache/cloudstack/api/command/admin/vm/MigrateVirtualMachineWithVolumeCmd.java @@ -21,16 +21,17 @@ import java.util.HashMap; import java.util.Iterator; import java.util.Map; -import org.apache.cloudstack.api.*; - import org.apache.log4j.Logger; import org.apache.cloudstack.api.APICommand; -import org.apache.cloudstack.api.BaseCmd.CommandType; +import org.apache.cloudstack.api.ApiConstants; +import org.apache.cloudstack.api.ApiErrorCode; +import org.apache.cloudstack.api.BaseAsyncCmd; +import org.apache.cloudstack.api.Parameter; +import org.apache.cloudstack.api.ResponseObject.ResponseView; +import org.apache.cloudstack.api.ServerApiException; import org.apache.cloudstack.api.response.HostResponse; -import org.apache.cloudstack.api.response.StoragePoolResponse; import org.apache.cloudstack.api.response.UserVmResponse; -import org.apache.cloudstack.context.CallContext; import com.cloud.event.EventTypes; import com.cloud.exception.ConcurrentOperationException; @@ -39,7 +40,6 @@ import com.cloud.exception.ManagementServerException; import com.cloud.exception.ResourceUnavailableException; import com.cloud.exception.VirtualMachineMigrationException; import com.cloud.host.Host; -import com.cloud.storage.StoragePool; import com.cloud.user.Account; import com.cloud.uservm.UserVm; import com.cloud.vm.VirtualMachine; @@ -138,9 +138,9 @@ public class MigrateVirtualMachineWithVolumeCmd extends BaseAsyncCmd { VirtualMachine migratedVm = _userVmService.migrateVirtualMachineWithVolume(getVirtualMachineId(), destinationHost, getVolumeToPool()); if (migratedVm != null) { - UserVmResponse response = _responseGenerator.createUserVmResponse("virtualmachine", (UserVm)migratedVm).get(0); + UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Full, "virtualmachine", (UserVm)migratedVm).get(0); response.setResponseName(getCommandName()); - this.setResponseObject(response); + setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to migrate vm"); } diff --git a/api/src/org/apache/cloudstack/api/command/admin/vm/RebootVMCmdByAdmin.java b/api/src/org/apache/cloudstack/api/command/admin/vm/RebootVMCmdByAdmin.java new file mode 100644 index 00000000000..7fa8edad839 --- /dev/null +++ b/api/src/org/apache/cloudstack/api/command/admin/vm/RebootVMCmdByAdmin.java @@ -0,0 +1,51 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package org.apache.cloudstack.api.command.admin.vm; + +import org.apache.log4j.Logger; + +import org.apache.cloudstack.api.APICommand; +import org.apache.cloudstack.api.ApiErrorCode; +import org.apache.cloudstack.api.ResponseObject.ResponseView; +import org.apache.cloudstack.api.ServerApiException; +import org.apache.cloudstack.api.command.user.vm.RebootVMCmd; +import org.apache.cloudstack.api.response.UserVmResponse; +import org.apache.cloudstack.context.CallContext; + +import com.cloud.exception.InsufficientCapacityException; +import com.cloud.exception.ResourceUnavailableException; +import com.cloud.uservm.UserVm; + +@APICommand(name = "rebootVirtualMachine", description = "Reboots a virtual machine.", responseObject = UserVmResponse.class, responseView = ResponseView.Full) +public class RebootVMCmdByAdmin extends RebootVMCmd { + public static final Logger s_logger = Logger.getLogger(RebootVMCmdByAdmin.class.getName()); + + @Override + public void execute() throws ResourceUnavailableException, InsufficientCapacityException{ + CallContext.current().setEventDetails("Vm Id: "+getId()); + UserVm result; + result = _userVmService.rebootVirtualMachine(this); + + if (result !=null){ + UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Full, "virtualmachine", result).get(0); + response.setResponseName(getCommandName()); + setResponseObject(response); + } else { + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to reboot vm instance"); + } + } +} diff --git a/api/src/org/apache/cloudstack/api/command/admin/vm/RecoverVMCmd.java b/api/src/org/apache/cloudstack/api/command/admin/vm/RecoverVMCmd.java index 13e755c5791..0e04742849f 100644 --- a/api/src/org/apache/cloudstack/api/command/admin/vm/RecoverVMCmd.java +++ b/api/src/org/apache/cloudstack/api/command/admin/vm/RecoverVMCmd.java @@ -16,14 +16,16 @@ // under the License. package org.apache.cloudstack.api.command.admin.vm; +import org.apache.log4j.Logger; + import org.apache.cloudstack.api.APICommand; import org.apache.cloudstack.api.ApiConstants; import org.apache.cloudstack.api.ApiErrorCode; import org.apache.cloudstack.api.BaseCmd; import org.apache.cloudstack.api.Parameter; +import org.apache.cloudstack.api.ResponseObject.ResponseView; import org.apache.cloudstack.api.ServerApiException; import org.apache.cloudstack.api.response.UserVmResponse; -import org.apache.log4j.Logger; import com.cloud.exception.ResourceAllocationException; import com.cloud.user.Account; @@ -74,9 +76,9 @@ public class RecoverVMCmd extends BaseCmd { public void execute() throws ResourceAllocationException{ UserVm result = _userVmService.recoverVirtualMachine(this); if (result != null){ - UserVmResponse recoverVmResponse = _responseGenerator.createUserVmResponse("virtualmachine", result).get(0); + UserVmResponse recoverVmResponse = _responseGenerator.createUserVmResponse(ResponseView.Full, "virtualmachine", result).get(0); recoverVmResponse.setResponseName(getCommandName()); - this.setResponseObject(recoverVmResponse); + setResponseObject(recoverVmResponse); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to recover vm"); } diff --git a/api/src/org/apache/cloudstack/api/command/admin/vm/RemoveNicFromVMCmdByAdmin.java b/api/src/org/apache/cloudstack/api/command/admin/vm/RemoveNicFromVMCmdByAdmin.java new file mode 100644 index 00000000000..ba0ff27f12b --- /dev/null +++ b/api/src/org/apache/cloudstack/api/command/admin/vm/RemoveNicFromVMCmdByAdmin.java @@ -0,0 +1,55 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package org.apache.cloudstack.api.command.admin.vm; + +import java.util.ArrayList; +import java.util.EnumSet; + +import org.apache.log4j.Logger; + +import org.apache.cloudstack.api.APICommand; +import org.apache.cloudstack.api.ApiConstants.VMDetails; +import org.apache.cloudstack.api.ApiErrorCode; +import org.apache.cloudstack.api.ResponseObject.ResponseView; +import org.apache.cloudstack.api.ServerApiException; +import org.apache.cloudstack.api.command.user.vm.RemoveNicFromVMCmd; +import org.apache.cloudstack.api.response.UserVmResponse; +import org.apache.cloudstack.context.CallContext; + +import com.cloud.uservm.UserVm; + +@APICommand(name = "removeNicFromVirtualMachine", description = "Removes VM from specified network by deleting a NIC", responseObject = UserVmResponse.class, responseView = ResponseView.Full) + +public class RemoveNicFromVMCmdByAdmin extends RemoveNicFromVMCmd { + public static final Logger s_logger = Logger.getLogger(RemoveNicFromVMCmdByAdmin.class); + + @Override + public void execute(){ + CallContext.current().setEventDetails("Vm Id: "+getVmId() + " Nic Id: " + getNicId()); + UserVm result = _userVmService.removeNicFromVirtualMachine(this); + ArrayList dc = new ArrayList(); + dc.add(VMDetails.valueOf("nics")); + EnumSet details = EnumSet.copyOf(dc); + if (result != null){ + UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Full, "virtualmachine", details, result).get(0); + response.setResponseName(getCommandName()); + setResponseObject(response); + } else { + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to remove NIC from vm, see error log for details"); + } + } +} diff --git a/api/src/org/apache/cloudstack/api/command/admin/vm/ResetVMPasswordCmdByAdmin.java b/api/src/org/apache/cloudstack/api/command/admin/vm/ResetVMPasswordCmdByAdmin.java new file mode 100644 index 00000000000..1b3e1cc1dcf --- /dev/null +++ b/api/src/org/apache/cloudstack/api/command/admin/vm/ResetVMPasswordCmdByAdmin.java @@ -0,0 +1,54 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package org.apache.cloudstack.api.command.admin.vm; + +import org.apache.log4j.Logger; + +import org.apache.cloudstack.api.APICommand; +import org.apache.cloudstack.api.ApiErrorCode; +import org.apache.cloudstack.api.ResponseObject.ResponseView; +import org.apache.cloudstack.api.ServerApiException; +import org.apache.cloudstack.api.command.user.vm.ResetVMPasswordCmd; +import org.apache.cloudstack.api.response.UserVmResponse; +import org.apache.cloudstack.context.CallContext; + +import com.cloud.exception.InsufficientCapacityException; +import com.cloud.exception.ResourceUnavailableException; +import com.cloud.uservm.UserVm; + +@APICommand(name = "resetPasswordForVirtualMachine", responseObject=UserVmResponse.class, description="Resets the password for virtual machine. " + + "The virtual machine must be in a \"Stopped\" state and the template must already " + + "support this feature for this command to take effect. [async]", responseView = ResponseView.Full) +public class ResetVMPasswordCmdByAdmin extends ResetVMPasswordCmd { + public static final Logger s_logger = Logger.getLogger(ResetVMPasswordCmdByAdmin.class.getName()); + + + + @Override + public void execute() throws ResourceUnavailableException, InsufficientCapacityException{ + password = _mgr.generateRandomPassword(); + CallContext.current().setEventDetails("Vm Id: "+getId()); + UserVm result = _userVmService.resetVMPassword(this, password); + if (result != null){ + UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Full, "virtualmachine", result).get(0); + response.setResponseName(getCommandName()); + setResponseObject(response); + } else { + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to reset vm password"); + } + } +} diff --git a/api/src/org/apache/cloudstack/api/command/admin/vm/ResetVMSSHKeyCmdByAdmin.java b/api/src/org/apache/cloudstack/api/command/admin/vm/ResetVMSSHKeyCmdByAdmin.java new file mode 100644 index 00000000000..1da083cec34 --- /dev/null +++ b/api/src/org/apache/cloudstack/api/command/admin/vm/ResetVMSSHKeyCmdByAdmin.java @@ -0,0 +1,57 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.cloudstack.api.command.admin.vm; + +import org.apache.log4j.Logger; + +import org.apache.cloudstack.api.APICommand; +import org.apache.cloudstack.api.ApiErrorCode; +import org.apache.cloudstack.api.ResponseObject.ResponseView; +import org.apache.cloudstack.api.ServerApiException; +import org.apache.cloudstack.api.command.user.vm.ResetVMSSHKeyCmd; +import org.apache.cloudstack.api.response.UserVmResponse; +import org.apache.cloudstack.context.CallContext; + +import com.cloud.exception.InsufficientCapacityException; +import com.cloud.exception.ResourceUnavailableException; +import com.cloud.uservm.UserVm; + +@APICommand(name = "resetSSHKeyForVirtualMachine", responseObject = UserVmResponse.class, description = "Resets the SSH Key for virtual machine. " + + "The virtual machine must be in a \"Stopped\" state. [async]", responseView = ResponseView.Full) +public class ResetVMSSHKeyCmdByAdmin extends ResetVMSSHKeyCmd { + + public static final Logger s_logger = Logger.getLogger(ResetVMSSHKeyCmdByAdmin.class.getName()); + + + @Override + public void execute() throws ResourceUnavailableException, + InsufficientCapacityException { + + CallContext.current().setEventDetails("Vm Id: " + getId()); + UserVm result = _userVmService.resetVMSSHKey(this); + + if (result != null) { + UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Full, "virtualmachine", result).get(0); + response.setResponseName(getCommandName()); + setResponseObject(response); + } else { + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to reset vm SSHKey"); + } + } + +} diff --git a/api/src/org/apache/cloudstack/api/command/admin/vm/RestoreVMCmdByAdmin.java b/api/src/org/apache/cloudstack/api/command/admin/vm/RestoreVMCmdByAdmin.java new file mode 100644 index 00000000000..fea2016c8c3 --- /dev/null +++ b/api/src/org/apache/cloudstack/api/command/admin/vm/RestoreVMCmdByAdmin.java @@ -0,0 +1,54 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package org.apache.cloudstack.api.command.admin.vm; + +import org.apache.log4j.Logger; + +import org.apache.cloudstack.api.APICommand; +import org.apache.cloudstack.api.ApiErrorCode; +import org.apache.cloudstack.api.ResponseObject.ResponseView; +import org.apache.cloudstack.api.ServerApiException; +import org.apache.cloudstack.api.command.user.vm.RestoreVMCmd; +import org.apache.cloudstack.api.response.UserVmResponse; +import org.apache.cloudstack.context.CallContext; + +import com.cloud.exception.ConcurrentOperationException; +import com.cloud.exception.InsufficientCapacityException; +import com.cloud.exception.ResourceAllocationException; +import com.cloud.exception.ResourceUnavailableException; +import com.cloud.uservm.UserVm; + +@APICommand(name = "restoreVirtualMachine", description = "Restore a VM to original template/ISO or new template/ISO", responseObject = UserVmResponse.class, since = "3.0.0", responseView = ResponseView.Full) +public class RestoreVMCmdByAdmin extends RestoreVMCmd { + public static final Logger s_logger = Logger.getLogger(RestoreVMCmdByAdmin.class); + + @Override + public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, + ResourceAllocationException { + UserVm result; + CallContext.current().setEventDetails("Vm Id: " + getVmId()); + result = _userVmService.restoreVM(this); + if (result != null) { + UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Full, "virtualmachine", result).get(0); + response.setResponseName(getCommandName()); + setResponseObject(response); + } else { + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to restore vm " + getVmId()); + } + } + +} diff --git a/api/src/org/apache/cloudstack/api/command/admin/vm/ScaleVMCmdByAdmin.java b/api/src/org/apache/cloudstack/api/command/admin/vm/ScaleVMCmdByAdmin.java new file mode 100644 index 00000000000..68d3c332ddf --- /dev/null +++ b/api/src/org/apache/cloudstack/api/command/admin/vm/ScaleVMCmdByAdmin.java @@ -0,0 +1,69 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package org.apache.cloudstack.api.command.admin.vm; + +import java.util.List; + +import org.apache.log4j.Logger; + +import org.apache.cloudstack.api.APICommand; +import org.apache.cloudstack.api.ApiErrorCode; +import org.apache.cloudstack.api.ResponseObject.ResponseView; +import org.apache.cloudstack.api.ServerApiException; +import org.apache.cloudstack.api.command.user.vm.ScaleVMCmd; +import org.apache.cloudstack.api.response.SuccessResponse; +import org.apache.cloudstack.api.response.UserVmResponse; + +import com.cloud.exception.ConcurrentOperationException; +import com.cloud.exception.ManagementServerException; +import com.cloud.exception.ResourceUnavailableException; +import com.cloud.exception.VirtualMachineMigrationException; +import com.cloud.uservm.UserVm; + + +@APICommand(name = "scaleVirtualMachine", description = "Scales the virtual machine to a new service offering.", responseObject = SuccessResponse.class, responseView = ResponseView.Full) +public class ScaleVMCmdByAdmin extends ScaleVMCmd { + public static final Logger s_logger = Logger.getLogger(ScaleVMCmdByAdmin.class.getName()); + + @Override + public void execute(){ + UserVm result; + try { + result = _userVmService.upgradeVirtualMachine(this); + } catch (ResourceUnavailableException ex) { + s_logger.warn("Exception: ", ex); + throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, ex.getMessage()); + } catch (ConcurrentOperationException ex) { + s_logger.warn("Exception: ", ex); + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage()); + } catch (ManagementServerException ex) { + s_logger.warn("Exception: ", ex); + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage()); + } catch (VirtualMachineMigrationException ex) { + s_logger.warn("Exception: ", ex); + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage()); + } + if (result != null){ + List responseList = _responseGenerator.createUserVmResponse(ResponseView.Full, "virtualmachine", result); + UserVmResponse response = responseList.get(0); + response.setResponseName(getCommandName()); + setResponseObject(response); + } else { + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to scale vm"); + } + } +} \ No newline at end of file diff --git a/api/src/org/apache/cloudstack/api/command/admin/vm/StartVMCmdByAdmin.java b/api/src/org/apache/cloudstack/api/command/admin/vm/StartVMCmdByAdmin.java new file mode 100644 index 00000000000..e814784ba3d --- /dev/null +++ b/api/src/org/apache/cloudstack/api/command/admin/vm/StartVMCmdByAdmin.java @@ -0,0 +1,80 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package org.apache.cloudstack.api.command.admin.vm; + +import org.apache.log4j.Logger; + +import org.apache.cloudstack.api.APICommand; +import org.apache.cloudstack.api.ApiErrorCode; +import org.apache.cloudstack.api.ResponseObject.ResponseView; +import org.apache.cloudstack.api.ServerApiException; +import org.apache.cloudstack.api.command.user.vm.StartVMCmd; +import org.apache.cloudstack.api.response.UserVmResponse; +import org.apache.cloudstack.context.CallContext; + +import com.cloud.exception.ConcurrentOperationException; +import com.cloud.exception.InsufficientCapacityException; +import com.cloud.exception.InsufficientServerCapacityException; +import com.cloud.exception.ResourceAllocationException; +import com.cloud.exception.ResourceUnavailableException; +import com.cloud.exception.StorageUnavailableException; +import com.cloud.uservm.UserVm; +import com.cloud.utils.exception.ExecutionException; + +@APICommand(name = "startVirtualMachine", responseObject = UserVmResponse.class, description = "Starts a virtual machine.", responseView = ResponseView.Full) +public class StartVMCmdByAdmin extends StartVMCmd { + public static final Logger s_logger = Logger.getLogger(StartVMCmdByAdmin.class.getName()); + + + @Override + public void execute() throws ResourceUnavailableException, ResourceAllocationException { + try { + CallContext.current().setEventDetails("Vm Id: " + getId()); + + UserVm result ; + result = _userVmService.startVirtualMachine(this); + + if (result != null) { + UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Full, "virtualmachine", result).get(0); + response.setResponseName(getCommandName()); + setResponseObject(response); + } else { + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to start a vm"); + } + } catch (ConcurrentOperationException ex) { + s_logger.warn("Exception: ", ex); + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage()); + } catch (StorageUnavailableException ex) { + s_logger.warn("Exception: ", ex); + throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, ex.getMessage()); + } catch (ExecutionException ex) { + s_logger.warn("Exception: ", ex); + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage()); + } catch (InsufficientCapacityException ex) { + StringBuilder message = new StringBuilder(ex.getMessage()); + if (ex instanceof InsufficientServerCapacityException) { + if (((InsufficientServerCapacityException) ex).isAffinityApplied()) { + message.append(", Please check the affinity groups provided, there may not be sufficient capacity to follow them"); + } + } + s_logger.info(ex); + s_logger.info(message.toString(), ex); + throw new ServerApiException(ApiErrorCode.INSUFFICIENT_CAPACITY_ERROR, message.toString()); + } + } + +} diff --git a/api/src/org/apache/cloudstack/api/command/admin/vm/StopVMCmdByAdmin.java b/api/src/org/apache/cloudstack/api/command/admin/vm/StopVMCmdByAdmin.java new file mode 100644 index 00000000000..6c906c74227 --- /dev/null +++ b/api/src/org/apache/cloudstack/api/command/admin/vm/StopVMCmdByAdmin.java @@ -0,0 +1,53 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package org.apache.cloudstack.api.command.admin.vm; + +import org.apache.log4j.Logger; + +import org.apache.cloudstack.api.APICommand; +import org.apache.cloudstack.api.ApiErrorCode; +import org.apache.cloudstack.api.ResponseObject.ResponseView; +import org.apache.cloudstack.api.ServerApiException; +import org.apache.cloudstack.api.command.user.vm.StopVMCmd; +import org.apache.cloudstack.api.response.UserVmResponse; +import org.apache.cloudstack.context.CallContext; + +import com.cloud.exception.ConcurrentOperationException; +import com.cloud.uservm.UserVm; + +@APICommand(name = "stopVirtualMachine", responseObject = UserVmResponse.class, description = "Stops a virtual machine.", responseView = ResponseView.Full) +public class StopVMCmdByAdmin extends StopVMCmd { + public static final Logger s_logger = Logger.getLogger(StopVMCmdByAdmin.class.getName()); + + + + @Override + public void execute() throws ServerApiException, ConcurrentOperationException { + CallContext.current().setEventDetails("Vm Id: " + getId()); + UserVm result; + + result = _userVmService.stopVirtualMachine(getId(), isForced()); + + if (result != null) { + UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Full, "virtualmachine", result).get(0); + response.setResponseName(getCommandName()); + setResponseObject(response); + } else { + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to stop vm"); + } + } +} diff --git a/api/src/org/apache/cloudstack/api/command/admin/vm/UpdateDefaultNicForVMCmdByAdmin.java b/api/src/org/apache/cloudstack/api/command/admin/vm/UpdateDefaultNicForVMCmdByAdmin.java new file mode 100644 index 00000000000..82385ba8f26 --- /dev/null +++ b/api/src/org/apache/cloudstack/api/command/admin/vm/UpdateDefaultNicForVMCmdByAdmin.java @@ -0,0 +1,56 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package org.apache.cloudstack.api.command.admin.vm; + +import java.util.ArrayList; +import java.util.EnumSet; + +import org.apache.log4j.Logger; + +import org.apache.cloudstack.api.APICommand; +import org.apache.cloudstack.api.ApiConstants.VMDetails; +import org.apache.cloudstack.api.ApiErrorCode; +import org.apache.cloudstack.api.ResponseObject.ResponseView; +import org.apache.cloudstack.api.ServerApiException; +import org.apache.cloudstack.api.command.user.vm.UpdateDefaultNicForVMCmd; +import org.apache.cloudstack.api.response.UserVmResponse; +import org.apache.cloudstack.context.CallContext; + +import com.cloud.uservm.UserVm; + +@APICommand(name = "updateDefaultNicForVirtualMachine", description = "Changes the default NIC on a VM", responseObject = UserVmResponse.class, responseView = ResponseView.Full) + +public class UpdateDefaultNicForVMCmdByAdmin extends UpdateDefaultNicForVMCmd { + public static final Logger s_logger = Logger.getLogger(UpdateDefaultNicForVMCmdByAdmin.class); + + + @Override + public void execute(){ + CallContext.current().setEventDetails("Vm Id: "+getVmId() + " Nic Id: " + getNicId()); + UserVm result = _userVmService.updateDefaultNicForVirtualMachine(this); + ArrayList dc = new ArrayList(); + dc.add(VMDetails.valueOf("nics")); + EnumSet details = EnumSet.copyOf(dc); + if (result != null){ + UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Full, "virtualmachine", details, result).get(0); + response.setResponseName(getCommandName()); + setResponseObject(response); + } else { + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to set default nic for VM. Refer to server logs for details."); + } + } +} diff --git a/api/src/org/apache/cloudstack/api/command/admin/vm/UpdateVMCmdByAdmin.java b/api/src/org/apache/cloudstack/api/command/admin/vm/UpdateVMCmdByAdmin.java new file mode 100644 index 00000000000..ef598b1a2ae --- /dev/null +++ b/api/src/org/apache/cloudstack/api/command/admin/vm/UpdateVMCmdByAdmin.java @@ -0,0 +1,53 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package org.apache.cloudstack.api.command.admin.vm; + +import org.apache.log4j.Logger; + +import org.apache.cloudstack.api.APICommand; +import org.apache.cloudstack.api.ApiErrorCode; +import org.apache.cloudstack.api.ResponseObject.ResponseView; +import org.apache.cloudstack.api.ServerApiException; +import org.apache.cloudstack.api.command.user.vm.UpdateVMCmd; +import org.apache.cloudstack.api.response.UserVmResponse; +import org.apache.cloudstack.context.CallContext; + +import com.cloud.exception.InsufficientCapacityException; +import com.cloud.exception.ResourceUnavailableException; +import com.cloud.uservm.UserVm; + + +@APICommand(name = "updateVirtualMachine", description="Updates properties of a virtual machine. The VM has to be stopped and restarted for the " + + "new properties to take effect. UpdateVirtualMachine does not first check whether the VM is stopped. " + + "Therefore, stop the VM manually before issuing this call.", responseObject = UserVmResponse.class, responseView = ResponseView.Full) +public class UpdateVMCmdByAdmin extends UpdateVMCmd { + public static final Logger s_logger = Logger.getLogger(UpdateVMCmdByAdmin.class.getName()); + + @Override + public void execute() throws ResourceUnavailableException, + InsufficientCapacityException, ServerApiException { + CallContext.current().setEventDetails("Vm Id: "+getId()); + UserVm result = _userVmService.updateVirtualMachine(this); + if (result != null){ + UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Full, "virtualmachine", result).get(0); + response.setResponseName(getCommandName()); + setResponseObject(response); + } else { + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to update vm"); + } + } +} diff --git a/api/src/org/apache/cloudstack/api/command/admin/vm/UpgradeVMCmdByAdmin.java b/api/src/org/apache/cloudstack/api/command/admin/vm/UpgradeVMCmdByAdmin.java new file mode 100644 index 00000000000..bfbe3d6ff0b --- /dev/null +++ b/api/src/org/apache/cloudstack/api/command/admin/vm/UpgradeVMCmdByAdmin.java @@ -0,0 +1,59 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package org.apache.cloudstack.api.command.admin.vm; + +import org.apache.log4j.Logger; + +import org.apache.cloudstack.api.APICommand; +import org.apache.cloudstack.api.ApiErrorCode; +import org.apache.cloudstack.api.ResponseObject.ResponseView; +import org.apache.cloudstack.api.ServerApiException; +import org.apache.cloudstack.api.command.user.vm.UpgradeVMCmd; +import org.apache.cloudstack.api.response.UserVmResponse; +import org.apache.cloudstack.context.CallContext; + +import com.cloud.exception.InvalidParameterValueException; +import com.cloud.exception.ResourceAllocationException; +import com.cloud.offering.ServiceOffering; +import com.cloud.uservm.UserVm; + +@APICommand(name = "changeServiceForVirtualMachine", responseObject=UserVmResponse.class, description="Changes the service offering for a virtual machine. " + + "The virtual machine must be in a \"Stopped\" state for " + + "this command to take effect.", responseView = ResponseView.Full) +public class UpgradeVMCmdByAdmin extends UpgradeVMCmd { + public static final Logger s_logger = Logger.getLogger(UpgradeVMCmdByAdmin.class.getName()); + + + @Override + public void execute() throws ResourceAllocationException{ + CallContext.current().setEventDetails("Vm Id: "+getId()); + + ServiceOffering serviceOffering = _entityMgr.findById(ServiceOffering.class, serviceOfferingId); + if (serviceOffering == null) { + throw new InvalidParameterValueException("Unable to find service offering: " + serviceOfferingId); + } + + UserVm result = _userVmService.upgradeVirtualMachine(this); + if (result != null){ + UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Full, "virtualmachine", result).get(0); + response.setResponseName(getCommandName()); + setResponseObject(response); + } else { + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to upgrade vm"); + } + } +} diff --git a/api/src/org/apache/cloudstack/api/command/admin/vmsnapshot/RevertToVMSnapshotCmdByAdmin.java b/api/src/org/apache/cloudstack/api/command/admin/vmsnapshot/RevertToVMSnapshotCmdByAdmin.java new file mode 100644 index 00000000000..92df872b0b5 --- /dev/null +++ b/api/src/org/apache/cloudstack/api/command/admin/vmsnapshot/RevertToVMSnapshotCmdByAdmin.java @@ -0,0 +1,57 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package org.apache.cloudstack.api.command.admin.vmsnapshot; + +import java.util.logging.Logger; + +import org.apache.cloudstack.api.APICommand; +import org.apache.cloudstack.api.ApiErrorCode; +import org.apache.cloudstack.api.ResponseObject.ResponseView; +import org.apache.cloudstack.api.ServerApiException; +import org.apache.cloudstack.api.command.user.vmsnapshot.RevertToVMSnapshotCmd; +import org.apache.cloudstack.api.response.UserVmResponse; +import org.apache.cloudstack.context.CallContext; + +import com.cloud.exception.ConcurrentOperationException; +import com.cloud.exception.InsufficientCapacityException; +import com.cloud.exception.ResourceAllocationException; +import com.cloud.exception.ResourceUnavailableException; +import com.cloud.uservm.UserVm; + +@APICommand(name = "revertToVMSnapshot", description = "Revert VM from a vmsnapshot.", responseObject = UserVmResponse.class, since = "4.2.0", responseView = ResponseView.Full) +public class RevertToVMSnapshotCmdByAdmin extends RevertToVMSnapshotCmd { + public static final Logger s_logger = Logger + .getLogger(RevertToVMSnapshotCmdByAdmin.class.getName()); + + + @Override + public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ResourceAllocationException, ConcurrentOperationException { + CallContext.current().setEventDetails( + "vmsnapshot id: " + getVmSnapShotId()); + UserVm result = _vmSnapshotService.revertToSnapshot(getVmSnapShotId()); + if (result != null) { + UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Full, + "virtualmachine", result).get(0); + response.setResponseName(getCommandName()); + setResponseObject(response); + } else { + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR,"Failed to revert VM snapshot"); + } + } + + +} diff --git a/api/src/org/apache/cloudstack/api/command/user/affinitygroup/UpdateVMAffinityGroupCmd.java b/api/src/org/apache/cloudstack/api/command/user/affinitygroup/UpdateVMAffinityGroupCmd.java index 11099a43a86..53e86e108c5 100644 --- a/api/src/org/apache/cloudstack/api/command/user/affinitygroup/UpdateVMAffinityGroupCmd.java +++ b/api/src/org/apache/cloudstack/api/command/user/affinitygroup/UpdateVMAffinityGroupCmd.java @@ -20,21 +20,22 @@ import java.util.ArrayList; import java.util.EnumSet; import java.util.List; +import org.apache.log4j.Logger; + import org.apache.cloudstack.affinity.AffinityGroupResponse; import org.apache.cloudstack.api.ACL; import org.apache.cloudstack.api.APICommand; import org.apache.cloudstack.api.ApiCommandJobType; import org.apache.cloudstack.api.ApiConstants; +import org.apache.cloudstack.api.ApiConstants.VMDetails; import org.apache.cloudstack.api.ApiErrorCode; import org.apache.cloudstack.api.BaseAsyncCmd; import org.apache.cloudstack.api.Parameter; +import org.apache.cloudstack.api.ResponseObject.ResponseView; import org.apache.cloudstack.api.ServerApiException; -import org.apache.cloudstack.api.ApiConstants.VMDetails; import org.apache.cloudstack.api.response.UserVmResponse; import org.apache.cloudstack.context.CallContext; -import org.apache.log4j.Logger; - import com.cloud.event.EventTypes; import com.cloud.exception.InsufficientCapacityException; import com.cloud.exception.InvalidParameterValueException; @@ -44,7 +45,7 @@ import com.cloud.uservm.UserVm; @APICommand(name = "updateVMAffinityGroup", description = "Updates the affinity/anti-affinity group associations of a virtual machine. The VM has to be stopped and restarted for the " - + "new properties to take effect.", responseObject = UserVmResponse.class) + + "new properties to take effect.", responseObject = UserVmResponse.class, responseView = ResponseView.Restricted) public class UpdateVMAffinityGroupCmd extends BaseAsyncCmd { public static final Logger s_logger = Logger.getLogger(UpdateVMAffinityGroupCmd.class.getName()); private static final String s_name = "updatevirtualmachineresponse"; @@ -139,9 +140,9 @@ public class UpdateVMAffinityGroupCmd extends BaseAsyncCmd { EnumSet details = EnumSet.copyOf(dc); if (result != null){ - UserVmResponse response = _responseGenerator.createUserVmResponse("virtualmachine", details, result).get(0); + UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Restricted, "virtualmachine", details, result).get(0); response.setResponseName(getCommandName()); - this.setResponseObject(response); + setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to update vm's affinity groups"); } diff --git a/api/src/org/apache/cloudstack/api/command/user/iso/AttachIsoCmd.java b/api/src/org/apache/cloudstack/api/command/user/iso/AttachIsoCmd.java index 11e42708376..d021350a964 100644 --- a/api/src/org/apache/cloudstack/api/command/user/iso/AttachIsoCmd.java +++ b/api/src/org/apache/cloudstack/api/command/user/iso/AttachIsoCmd.java @@ -16,24 +16,25 @@ // under the License. package org.apache.cloudstack.api.command.user.iso; +import org.apache.log4j.Logger; + import org.apache.cloudstack.api.APICommand; import org.apache.cloudstack.api.ApiConstants; import org.apache.cloudstack.api.ApiErrorCode; import org.apache.cloudstack.api.BaseAsyncCmd; import org.apache.cloudstack.api.Parameter; +import org.apache.cloudstack.api.ResponseObject.ResponseView; import org.apache.cloudstack.api.ServerApiException; import org.apache.cloudstack.api.command.user.vm.DeployVMCmd; import org.apache.cloudstack.api.response.TemplateResponse; import org.apache.cloudstack.api.response.UserVmResponse; import org.apache.cloudstack.context.CallContext; -import org.apache.log4j.Logger; - import com.cloud.event.EventTypes; import com.cloud.exception.InvalidParameterValueException; import com.cloud.uservm.UserVm; -@APICommand(name = "attachIso", description="Attaches an ISO to a virtual machine.", responseObject=UserVmResponse.class) +@APICommand(name = "attachIso", description = "Attaches an ISO to a virtual machine.", responseObject = UserVmResponse.class, responseView = ResponseView.Restricted) public class AttachIsoCmd extends BaseAsyncCmd { public static final Logger s_logger = Logger.getLogger(AttachIsoCmd.class.getName()); @@ -101,9 +102,9 @@ public class AttachIsoCmd extends BaseAsyncCmd { if (result) { UserVm userVm = _responseGenerator.findUserVmById(virtualMachineId); if (userVm != null) { - UserVmResponse response = _responseGenerator.createUserVmResponse("virtualmachine", userVm).get(0); + UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Restricted, "virtualmachine", userVm).get(0); response.setResponseName(DeployVMCmd.getResultObjectName()); - this.setResponseObject(response); + setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to attach iso"); } diff --git a/api/src/org/apache/cloudstack/api/command/user/iso/DetachIsoCmd.java b/api/src/org/apache/cloudstack/api/command/user/iso/DetachIsoCmd.java index c04fba50295..00aebb2e9ad 100644 --- a/api/src/org/apache/cloudstack/api/command/user/iso/DetachIsoCmd.java +++ b/api/src/org/apache/cloudstack/api/command/user/iso/DetachIsoCmd.java @@ -16,22 +16,23 @@ // under the License. package org.apache.cloudstack.api.command.user.iso; +import org.apache.log4j.Logger; + import org.apache.cloudstack.api.APICommand; import org.apache.cloudstack.api.ApiConstants; import org.apache.cloudstack.api.ApiErrorCode; import org.apache.cloudstack.api.BaseAsyncCmd; import org.apache.cloudstack.api.Parameter; +import org.apache.cloudstack.api.ResponseObject.ResponseView; import org.apache.cloudstack.api.ServerApiException; import org.apache.cloudstack.api.command.user.vm.DeployVMCmd; -import org.apache.cloudstack.api.response.TemplateResponse; import org.apache.cloudstack.api.response.UserVmResponse; -import org.apache.log4j.Logger; import com.cloud.event.EventTypes; import com.cloud.exception.InvalidParameterValueException; import com.cloud.uservm.UserVm; -@APICommand(name = "detachIso", description="Detaches any ISO file (if any) currently attached to a virtual machine.", responseObject=UserVmResponse.class) +@APICommand(name = "detachIso", description = "Detaches any ISO file (if any) currently attached to a virtual machine.", responseObject = UserVmResponse.class, responseView = ResponseView.Restricted) public class DetachIsoCmd extends BaseAsyncCmd { public static final Logger s_logger = Logger.getLogger(DetachIsoCmd.class.getName()); @@ -87,9 +88,9 @@ public class DetachIsoCmd extends BaseAsyncCmd { boolean result = _templateService.detachIso(virtualMachineId); if (result) { UserVm userVm = _entityMgr.findById(UserVm.class, virtualMachineId); - UserVmResponse response = _responseGenerator.createUserVmResponse("virtualmachine", userVm).get(0); + UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Restricted, "virtualmachine", userVm).get(0); response.setResponseName(DeployVMCmd.getResultObjectName()); - this.setResponseObject(response); + setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to detach iso"); } diff --git a/api/src/org/apache/cloudstack/api/command/user/loadbalancer/ListLoadBalancerRuleInstancesCmd.java b/api/src/org/apache/cloudstack/api/command/user/loadbalancer/ListLoadBalancerRuleInstancesCmd.java index fcd41c4963f..be6a5d6099c 100644 --- a/api/src/org/apache/cloudstack/api/command/user/loadbalancer/ListLoadBalancerRuleInstancesCmd.java +++ b/api/src/org/apache/cloudstack/api/command/user/loadbalancer/ListLoadBalancerRuleInstancesCmd.java @@ -17,22 +17,23 @@ package org.apache.cloudstack.api.command.user.loadbalancer; import java.util.ArrayList; -import java.util.Iterator; import java.util.List; +import org.apache.log4j.Logger; + import org.apache.cloudstack.api.APICommand; import org.apache.cloudstack.api.ApiConstants; import org.apache.cloudstack.api.BaseListCmd; import org.apache.cloudstack.api.Parameter; +import org.apache.cloudstack.api.ResponseObject.ResponseView; import org.apache.cloudstack.api.response.FirewallRuleResponse; import org.apache.cloudstack.api.response.ListResponse; import org.apache.cloudstack.api.response.UserVmResponse; -import org.apache.log4j.Logger; import com.cloud.uservm.UserVm; import com.cloud.utils.Pair; -@APICommand(name = "listLoadBalancerRuleInstances", description="List all virtual machine instances that are assigned to a load balancer rule.", responseObject=UserVmResponse.class) +@APICommand(name = "listLoadBalancerRuleInstances", description = "List all virtual machine instances that are assigned to a load balancer rule.", responseObject = UserVmResponse.class, responseView = ResponseView.Restricted) public class ListLoadBalancerRuleInstancesCmd extends BaseListCmd { public static final Logger s_logger = Logger.getLogger (ListLoadBalancerRuleInstancesCmd.class.getName()); @@ -78,7 +79,7 @@ public class ListLoadBalancerRuleInstancesCmd extends BaseListCmd { ListResponse response = new ListResponse(); List vmResponses = new ArrayList(); if (result != null) { - vmResponses = _responseGenerator.createUserVmResponse("loadbalancerruleinstance", result.toArray(new UserVm[result.size()])); + vmResponses = _responseGenerator.createUserVmResponse(ResponseView.Restricted, "loadbalancerruleinstance", result.toArray(new UserVm[result.size()])); } for (int i=0;i details = EnumSet.copyOf(dc); if (result != null){ - UserVmResponse response = _responseGenerator.createUserVmResponse("virtualmachine", details, result).get(0); + UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Restricted, "virtualmachine", details, result).get(0); response.setResponseName(getCommandName()); - this.setResponseObject(response); + setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to add NIC to vm. Refer to server logs for details."); } diff --git a/api/src/org/apache/cloudstack/api/command/user/vm/DeployVMCmd.java b/api/src/org/apache/cloudstack/api/command/user/vm/DeployVMCmd.java index 8a6cea79091..636410e2976 100755 --- a/api/src/org/apache/cloudstack/api/command/user/vm/DeployVMCmd.java +++ b/api/src/org/apache/cloudstack/api/command/user/vm/DeployVMCmd.java @@ -35,6 +35,7 @@ import org.apache.cloudstack.api.ApiConstants; import org.apache.cloudstack.api.ApiErrorCode; import org.apache.cloudstack.api.BaseAsyncCreateCmd; import org.apache.cloudstack.api.Parameter; +import org.apache.cloudstack.api.ResponseObject.ResponseView; import org.apache.cloudstack.api.ServerApiException; import org.apache.cloudstack.api.response.DiskOfferingResponse; import org.apache.cloudstack.api.response.DomainResponse; @@ -67,7 +68,7 @@ import com.cloud.user.Account; import com.cloud.uservm.UserVm; -@APICommand(name = "deployVirtualMachine", description="Creates and automatically starts a virtual machine based on a service offering, disk offering, and template.", responseObject=UserVmResponse.class) +@APICommand(name = "deployVirtualMachine", description = "Creates and automatically starts a virtual machine based on a service offering, disk offering, and template.", responseObject = UserVmResponse.class, responseView = ResponseView.Restricted) public class DeployVMCmd extends BaseAsyncCreateCmd { public static final Logger s_logger = Logger.getLogger(DeployVMCmd.class.getName()); @@ -441,7 +442,7 @@ public class DeployVMCmd extends BaseAsyncCreateCmd { } if (result != null) { - UserVmResponse response = _responseGenerator.createUserVmResponse("virtualmachine", result).get(0); + UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Restricted, "virtualmachine", result).get(0); response.setResponseName(getCommandName()); setResponseObject(response); } else { diff --git a/api/src/org/apache/cloudstack/api/command/user/vm/DestroyVMCmd.java b/api/src/org/apache/cloudstack/api/command/user/vm/DestroyVMCmd.java index b3e8d1f83fe..520814d6f01 100644 --- a/api/src/org/apache/cloudstack/api/command/user/vm/DestroyVMCmd.java +++ b/api/src/org/apache/cloudstack/api/command/user/vm/DestroyVMCmd.java @@ -18,16 +18,18 @@ package org.apache.cloudstack.api.command.user.vm; import java.util.List; +import org.apache.log4j.Logger; + import org.apache.cloudstack.api.APICommand; import org.apache.cloudstack.api.ApiCommandJobType; import org.apache.cloudstack.api.ApiConstants; import org.apache.cloudstack.api.ApiErrorCode; import org.apache.cloudstack.api.BaseAsyncCmd; import org.apache.cloudstack.api.Parameter; +import org.apache.cloudstack.api.ResponseObject.ResponseView; import org.apache.cloudstack.api.ServerApiException; import org.apache.cloudstack.api.response.UserVmResponse; import org.apache.cloudstack.context.CallContext; -import org.apache.log4j.Logger; import com.cloud.event.EventTypes; import com.cloud.exception.ConcurrentOperationException; @@ -35,7 +37,7 @@ import com.cloud.exception.ResourceUnavailableException; import com.cloud.user.Account; import com.cloud.uservm.UserVm; -@APICommand(name = "destroyVirtualMachine", description="Destroys a virtual machine. Once destroyed, only the administrator can recover it.", responseObject=UserVmResponse.class) +@APICommand(name = "destroyVirtualMachine", description = "Destroys a virtual machine. Once destroyed, only the administrator can recover it.", responseObject = UserVmResponse.class, responseView = ResponseView.Restricted) public class DestroyVMCmd extends BaseAsyncCmd { public static final Logger s_logger = Logger.getLogger(DestroyVMCmd.class.getName()); @@ -50,7 +52,7 @@ public class DestroyVMCmd extends BaseAsyncCmd { private Long id; - @Parameter(name=ApiConstants.EXPUNGE, type=CommandType.BOOLEAN, + @Parameter(name=ApiConstants.EXPUNGE, type=CommandType.BOOLEAN, description="If true is passed, the vm is expunged immediately. False by default. Parameter can be passed to the call by ROOT/Domain admin only", since="4.2.1") private Boolean expunge; @@ -65,7 +67,7 @@ public class DestroyVMCmd extends BaseAsyncCmd { public boolean getExpunge() { if (expunge == null) { return false; - } + } return expunge; } @@ -98,10 +100,12 @@ public class DestroyVMCmd extends BaseAsyncCmd { return "destroying vm: " + getId(); } + @Override public ApiCommandJobType getInstanceType() { return ApiCommandJobType.VirtualMachine; } + @Override public Long getInstanceId() { return getId(); } @@ -113,12 +117,12 @@ public class DestroyVMCmd extends BaseAsyncCmd { UserVmResponse response = new UserVmResponse(); if (result != null) { - List responses = _responseGenerator.createUserVmResponse("virtualmachine", result); + List responses = _responseGenerator.createUserVmResponse(ResponseView.Restricted, "virtualmachine", result); if (responses != null && !responses.isEmpty()) { response = responses.get(0); } response.setResponseName("virtualmachine"); - this.setResponseObject(response); + setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to destroy vm"); } diff --git a/api/src/org/apache/cloudstack/api/command/user/vm/RebootVMCmd.java b/api/src/org/apache/cloudstack/api/command/user/vm/RebootVMCmd.java index 86bcc191ada..cd99387d6ba 100644 --- a/api/src/org/apache/cloudstack/api/command/user/vm/RebootVMCmd.java +++ b/api/src/org/apache/cloudstack/api/command/user/vm/RebootVMCmd.java @@ -16,26 +16,26 @@ // under the License. package org.apache.cloudstack.api.command.user.vm; +import org.apache.log4j.Logger; + import org.apache.cloudstack.api.APICommand; import org.apache.cloudstack.api.ApiCommandJobType; import org.apache.cloudstack.api.ApiConstants; import org.apache.cloudstack.api.ApiErrorCode; import org.apache.cloudstack.api.BaseAsyncCmd; import org.apache.cloudstack.api.Parameter; +import org.apache.cloudstack.api.ResponseObject.ResponseView; import org.apache.cloudstack.api.ServerApiException; import org.apache.cloudstack.api.response.UserVmResponse; import org.apache.cloudstack.context.CallContext; -import org.apache.log4j.Logger; - import com.cloud.event.EventTypes; import com.cloud.exception.InsufficientCapacityException; import com.cloud.exception.ResourceUnavailableException; -import com.cloud.hypervisor.Hypervisor.HypervisorType; import com.cloud.user.Account; import com.cloud.uservm.UserVm; -@APICommand(name = "rebootVirtualMachine", description="Reboots a virtual machine.", responseObject=UserVmResponse.class) +@APICommand(name = "rebootVirtualMachine", description = "Reboots a virtual machine.", responseObject = UserVmResponse.class, responseView = ResponseView.Restricted) public class RebootVMCmd extends BaseAsyncCmd { public static final Logger s_logger = Logger.getLogger(RebootVMCmd.class.getName()); private static final String s_name = "rebootvirtualmachineresponse"; @@ -85,10 +85,12 @@ public class RebootVMCmd extends BaseAsyncCmd { return "rebooting user vm: " + getId(); } + @Override public ApiCommandJobType getInstanceType() { return ApiCommandJobType.VirtualMachine; } + @Override public Long getInstanceId() { return getId(); } @@ -100,9 +102,9 @@ public class RebootVMCmd extends BaseAsyncCmd { result = _userVmService.rebootVirtualMachine(this); if (result !=null){ - UserVmResponse response = _responseGenerator.createUserVmResponse("virtualmachine", result).get(0); + UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Restricted, "virtualmachine", result).get(0); response.setResponseName(getCommandName()); - this.setResponseObject(response); + setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to reboot vm instance"); } diff --git a/api/src/org/apache/cloudstack/api/command/user/vm/RemoveNicFromVMCmd.java b/api/src/org/apache/cloudstack/api/command/user/vm/RemoveNicFromVMCmd.java index ef133d97170..fa80103b25f 100644 --- a/api/src/org/apache/cloudstack/api/command/user/vm/RemoveNicFromVMCmd.java +++ b/api/src/org/apache/cloudstack/api/command/user/vm/RemoveNicFromVMCmd.java @@ -21,21 +21,23 @@ import java.util.EnumSet; import org.apache.log4j.Logger; -import org.apache.cloudstack.api.*; +import org.apache.cloudstack.api.APICommand; +import org.apache.cloudstack.api.ApiConstants; import org.apache.cloudstack.api.ApiConstants.VMDetails; -import org.apache.cloudstack.api.response.UserVmResponse; +import org.apache.cloudstack.api.ApiErrorCode; +import org.apache.cloudstack.api.BaseAsyncCmd; +import org.apache.cloudstack.api.Parameter; +import org.apache.cloudstack.api.ResponseObject.ResponseView; +import org.apache.cloudstack.api.ServerApiException; import org.apache.cloudstack.api.response.NicResponse; +import org.apache.cloudstack.api.response.UserVmResponse; import org.apache.cloudstack.context.CallContext; import com.cloud.event.EventTypes; -import com.cloud.exception.ConcurrentOperationException; -import com.cloud.exception.InsufficientCapacityException; -import com.cloud.exception.ResourceAllocationException; -import com.cloud.exception.ResourceUnavailableException; import com.cloud.user.Account; import com.cloud.uservm.UserVm; -@APICommand(name = "removeNicFromVirtualMachine", description="Removes VM from specified network by deleting a NIC", responseObject=UserVmResponse.class) +@APICommand(name = "removeNicFromVirtualMachine", description = "Removes VM from specified network by deleting a NIC", responseObject = UserVmResponse.class, responseView = ResponseView.Restricted) public class RemoveNicFromVMCmd extends BaseAsyncCmd { public static final Logger s_logger = Logger.getLogger(RemoveNicFromVMCmd.class); @@ -106,9 +108,9 @@ public class RemoveNicFromVMCmd extends BaseAsyncCmd { dc.add(VMDetails.valueOf("nics")); EnumSet details = EnumSet.copyOf(dc); if (result != null){ - UserVmResponse response = _responseGenerator.createUserVmResponse("virtualmachine", details, result).get(0); + UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Restricted, "virtualmachine", details, result).get(0); response.setResponseName(getCommandName()); - this.setResponseObject(response); + setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to remove NIC from vm, see error log for details"); } diff --git a/api/src/org/apache/cloudstack/api/command/user/vm/ResetVMPasswordCmd.java b/api/src/org/apache/cloudstack/api/command/user/vm/ResetVMPasswordCmd.java index c8ab5e88657..e1a7b87405e 100644 --- a/api/src/org/apache/cloudstack/api/command/user/vm/ResetVMPasswordCmd.java +++ b/api/src/org/apache/cloudstack/api/command/user/vm/ResetVMPasswordCmd.java @@ -16,18 +16,19 @@ // under the License. package org.apache.cloudstack.api.command.user.vm; +import org.apache.log4j.Logger; + import org.apache.cloudstack.api.APICommand; import org.apache.cloudstack.api.ApiCommandJobType; import org.apache.cloudstack.api.ApiConstants; import org.apache.cloudstack.api.ApiErrorCode; import org.apache.cloudstack.api.BaseAsyncCmd; import org.apache.cloudstack.api.Parameter; +import org.apache.cloudstack.api.ResponseObject.ResponseView; import org.apache.cloudstack.api.ServerApiException; import org.apache.cloudstack.api.response.UserVmResponse; import org.apache.cloudstack.context.CallContext; -import org.apache.log4j.Logger; - import com.cloud.event.EventTypes; import com.cloud.exception.InsufficientCapacityException; import com.cloud.exception.ResourceUnavailableException; @@ -36,7 +37,7 @@ import com.cloud.uservm.UserVm; @APICommand(name = "resetPasswordForVirtualMachine", responseObject=UserVmResponse.class, description="Resets the password for virtual machine. " + "The virtual machine must be in a \"Stopped\" state and the template must already " + - "support this feature for this command to take effect. [async]") + "support this feature for this command to take effect. [async]", responseView = ResponseView.Restricted) public class ResetVMPasswordCmd extends BaseAsyncCmd { public static final Logger s_logger = Logger.getLogger(ResetVMPasswordCmd.class.getName()); @@ -52,7 +53,7 @@ public class ResetVMPasswordCmd extends BaseAsyncCmd { // unexposed parameter needed for serializing/deserializing the command @Parameter(name=ApiConstants.PASSWORD, type=CommandType.STRING, expose=false) - private String password; + protected String password; ///////////////////////////////////////////////////// /////////////////// Accessors /////////////////////// @@ -99,10 +100,12 @@ public class ResetVMPasswordCmd extends BaseAsyncCmd { return "resetting password for vm: " + getId(); } + @Override public ApiCommandJobType getInstanceType() { return ApiCommandJobType.VirtualMachine; } + @Override public Long getInstanceId() { return getId(); } @@ -113,9 +116,9 @@ public class ResetVMPasswordCmd extends BaseAsyncCmd { CallContext.current().setEventDetails("Vm Id: "+getId()); UserVm result = _userVmService.resetVMPassword(this, password); if (result != null){ - UserVmResponse response = _responseGenerator.createUserVmResponse("virtualmachine", result).get(0); + UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Restricted, "virtualmachine", result).get(0); response.setResponseName(getCommandName()); - this.setResponseObject(response); + setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to reset vm password"); } diff --git a/api/src/org/apache/cloudstack/api/command/user/vm/ResetVMSSHKeyCmd.java b/api/src/org/apache/cloudstack/api/command/user/vm/ResetVMSSHKeyCmd.java index fa063447336..6d00daf383f 100644 --- a/api/src/org/apache/cloudstack/api/command/user/vm/ResetVMSSHKeyCmd.java +++ b/api/src/org/apache/cloudstack/api/command/user/vm/ResetVMSSHKeyCmd.java @@ -19,27 +19,27 @@ package org.apache.cloudstack.api.command.user.vm; import org.apache.log4j.Logger; +import org.apache.cloudstack.api.APICommand; import org.apache.cloudstack.api.ApiCommandJobType; import org.apache.cloudstack.api.ApiConstants; import org.apache.cloudstack.api.ApiErrorCode; import org.apache.cloudstack.api.BaseAsyncCmd; -import org.apache.cloudstack.api.BaseCmd; -import org.apache.cloudstack.api.APICommand; import org.apache.cloudstack.api.Parameter; +import org.apache.cloudstack.api.ResponseObject.ResponseView; import org.apache.cloudstack.api.ServerApiException; -import org.apache.cloudstack.api.response.UserVmResponse; import org.apache.cloudstack.api.response.DomainResponse; import org.apache.cloudstack.api.response.ProjectResponse; +import org.apache.cloudstack.api.response.UserVmResponse; import org.apache.cloudstack.context.CallContext; -import com.cloud.user.Account; -import com.cloud.uservm.UserVm; import com.cloud.event.EventTypes; import com.cloud.exception.InsufficientCapacityException; import com.cloud.exception.ResourceUnavailableException; +import com.cloud.user.Account; +import com.cloud.uservm.UserVm; @APICommand(name = "resetSSHKeyForVirtualMachine", responseObject = UserVmResponse.class, description = "Resets the SSH Key for virtual machine. " + - "The virtual machine must be in a \"Stopped\" state. [async]") + "The virtual machine must be in a \"Stopped\" state. [async]", responseView = ResponseView.Restricted) public class ResetVMSSHKeyCmd extends BaseAsyncCmd { public static final Logger s_logger = Logger.getLogger(ResetVMSSHKeyCmd.class.getName()); @@ -108,6 +108,7 @@ public class ResetVMSSHKeyCmd extends BaseAsyncCmd { return "resetting SSHKey for vm: " + getId(); } + @Override public ApiCommandJobType getInstanceType() { return ApiCommandJobType.VirtualMachine; } @@ -128,6 +129,7 @@ public class ResetVMSSHKeyCmd extends BaseAsyncCmd { return Account.ACCOUNT_ID_SYSTEM; // no account info given, parent this command to SYSTEM so ERROR events are tracked } + @Override public Long getInstanceId() { return getId(); } @@ -141,9 +143,9 @@ public class ResetVMSSHKeyCmd extends BaseAsyncCmd { UserVm result = _userVmService.resetVMSSHKey(this); if (result != null) { - UserVmResponse response = _responseGenerator.createUserVmResponse("virtualmachine", result).get(0); + UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Restricted, "virtualmachine", result).get(0); response.setResponseName(getCommandName()); - this.setResponseObject(response); + setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to reset vm SSHKey"); } diff --git a/api/src/org/apache/cloudstack/api/command/user/vm/RestoreVMCmd.java b/api/src/org/apache/cloudstack/api/command/user/vm/RestoreVMCmd.java index 2cfcc97402d..8cf5013a88a 100644 --- a/api/src/org/apache/cloudstack/api/command/user/vm/RestoreVMCmd.java +++ b/api/src/org/apache/cloudstack/api/command/user/vm/RestoreVMCmd.java @@ -16,18 +16,19 @@ // under the License. package org.apache.cloudstack.api.command.user.vm; +import org.apache.log4j.Logger; + import org.apache.cloudstack.api.APICommand; import org.apache.cloudstack.api.ApiConstants; import org.apache.cloudstack.api.ApiErrorCode; import org.apache.cloudstack.api.BaseAsyncCmd; import org.apache.cloudstack.api.Parameter; +import org.apache.cloudstack.api.ResponseObject.ResponseView; import org.apache.cloudstack.api.ServerApiException; import org.apache.cloudstack.api.response.TemplateResponse; import org.apache.cloudstack.api.response.UserVmResponse; import org.apache.cloudstack.context.CallContext; -import org.apache.log4j.Logger; - import com.cloud.event.EventTypes; import com.cloud.exception.ConcurrentOperationException; import com.cloud.exception.InsufficientCapacityException; @@ -36,7 +37,7 @@ import com.cloud.exception.ResourceUnavailableException; import com.cloud.user.Account; import com.cloud.uservm.UserVm; -@APICommand(name = "restoreVirtualMachine", description="Restore a VM to original template/ISO or new template/ISO", responseObject=UserVmResponse.class, since="3.0.0") +@APICommand(name = "restoreVirtualMachine", description = "Restore a VM to original template/ISO or new template/ISO", responseObject = UserVmResponse.class, since = "3.0.0", responseView = ResponseView.Restricted) public class RestoreVMCmd extends BaseAsyncCmd { public static final Logger s_logger = Logger.getLogger(RestoreVMCmd.class); private static final String s_name = "restorevmresponse"; @@ -66,9 +67,9 @@ public class RestoreVMCmd extends BaseAsyncCmd { CallContext.current().setEventDetails("Vm Id: " + getVmId()); result = _userVmService.restoreVM(this); if (result != null) { - UserVmResponse response = _responseGenerator.createUserVmResponse("virtualmachine", result).get(0); + UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Restricted, "virtualmachine", result).get(0); response.setResponseName(getCommandName()); - this.setResponseObject(response); + setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to restore vm " + getVmId()); } diff --git a/api/src/org/apache/cloudstack/api/command/user/vm/ScaleVMCmd.java b/api/src/org/apache/cloudstack/api/command/user/vm/ScaleVMCmd.java index 44f55753545..dfec0cc71be 100644 --- a/api/src/org/apache/cloudstack/api/command/user/vm/ScaleVMCmd.java +++ b/api/src/org/apache/cloudstack/api/command/user/vm/ScaleVMCmd.java @@ -16,23 +16,32 @@ // under the License. package org.apache.cloudstack.api.command.user.vm; -import com.cloud.event.EventTypes; -import com.cloud.exception.*; -import com.cloud.user.Account; -import com.cloud.uservm.UserVm; - -import org.apache.cloudstack.api.*; -import org.apache.cloudstack.api.response.ServiceOfferingResponse; -import org.apache.cloudstack.api.response.SuccessResponse; -import org.apache.cloudstack.api.response.UserVmResponse; -import org.apache.cloudstack.context.CallContext; +import java.util.List; import org.apache.log4j.Logger; -import java.util.List; +import org.apache.cloudstack.api.ACL; +import org.apache.cloudstack.api.APICommand; +import org.apache.cloudstack.api.ApiConstants; +import org.apache.cloudstack.api.ApiErrorCode; +import org.apache.cloudstack.api.BaseAsyncCmd; +import org.apache.cloudstack.api.Parameter; +import org.apache.cloudstack.api.ResponseObject.ResponseView; +import org.apache.cloudstack.api.ServerApiException; +import org.apache.cloudstack.api.response.ServiceOfferingResponse; +import org.apache.cloudstack.api.response.SuccessResponse; +import org.apache.cloudstack.api.response.UserVmResponse; + +import com.cloud.event.EventTypes; +import com.cloud.exception.ConcurrentOperationException; +import com.cloud.exception.ManagementServerException; +import com.cloud.exception.ResourceUnavailableException; +import com.cloud.exception.VirtualMachineMigrationException; +import com.cloud.user.Account; +import com.cloud.uservm.UserVm; -@APICommand(name = "scaleVirtualMachine", description="Scales the virtual machine to a new service offering.", responseObject=SuccessResponse.class) +@APICommand(name = "scaleVirtualMachine", description = "Scales the virtual machine to a new service offering.", responseObject = SuccessResponse.class, responseView = ResponseView.Restricted) public class ScaleVMCmd extends BaseAsyncCmd { public static final Logger s_logger = Logger.getLogger(ScaleVMCmd.class.getName()); private static final String s_name = "scalevirtualmachineresponse"; @@ -115,10 +124,10 @@ public class ScaleVMCmd extends BaseAsyncCmd { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage()); } if (result != null){ - List responseList = _responseGenerator.createUserVmResponse("virtualmachine", result); + List responseList = _responseGenerator.createUserVmResponse(ResponseView.Restricted, "virtualmachine", result); UserVmResponse response = responseList.get(0); response.setResponseName(getCommandName()); - this.setResponseObject(response); + setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to scale vm"); } diff --git a/api/src/org/apache/cloudstack/api/command/user/vm/StartVMCmd.java b/api/src/org/apache/cloudstack/api/command/user/vm/StartVMCmd.java index e971ff2ee45..6497306b253 100644 --- a/api/src/org/apache/cloudstack/api/command/user/vm/StartVMCmd.java +++ b/api/src/org/apache/cloudstack/api/command/user/vm/StartVMCmd.java @@ -16,6 +16,8 @@ // under the License. package org.apache.cloudstack.api.command.user.vm; +import org.apache.log4j.Logger; + import org.apache.cloudstack.acl.SecurityChecker.AccessType; import org.apache.cloudstack.api.ACL; import org.apache.cloudstack.api.APICommand; @@ -24,13 +26,12 @@ import org.apache.cloudstack.api.ApiConstants; import org.apache.cloudstack.api.ApiErrorCode; import org.apache.cloudstack.api.BaseAsyncCmd; import org.apache.cloudstack.api.Parameter; +import org.apache.cloudstack.api.ResponseObject.ResponseView; import org.apache.cloudstack.api.ServerApiException; import org.apache.cloudstack.api.response.HostResponse; import org.apache.cloudstack.api.response.UserVmResponse; import org.apache.cloudstack.context.CallContext; -import org.apache.log4j.Logger; - import com.cloud.event.EventTypes; import com.cloud.exception.ConcurrentOperationException; import com.cloud.exception.InsufficientCapacityException; @@ -42,7 +43,7 @@ import com.cloud.user.Account; import com.cloud.uservm.UserVm; import com.cloud.utils.exception.ExecutionException; -@APICommand(name = "startVirtualMachine", responseObject = UserVmResponse.class, description = "Starts a virtual machine.") +@APICommand(name = "startVirtualMachine", responseObject = UserVmResponse.class, description = "Starts a virtual machine.", responseView = ResponseView.Restricted) public class StartVMCmd extends BaseAsyncCmd { public static final Logger s_logger = Logger.getLogger(StartVMCmd.class.getName()); @@ -107,10 +108,12 @@ public class StartVMCmd extends BaseAsyncCmd { return "starting user vm: " + getId(); } + @Override public ApiCommandJobType getInstanceType() { return ApiCommandJobType.VirtualMachine; } + @Override public Long getInstanceId() { return getId(); } @@ -124,9 +127,9 @@ public class StartVMCmd extends BaseAsyncCmd { result = _userVmService.startVirtualMachine(this); if (result != null) { - UserVmResponse response = _responseGenerator.createUserVmResponse("virtualmachine", result).get(0); + UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Restricted, "virtualmachine", result).get(0); response.setResponseName(getCommandName()); - this.setResponseObject(response); + setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to start a vm"); } diff --git a/api/src/org/apache/cloudstack/api/command/user/vm/StopVMCmd.java b/api/src/org/apache/cloudstack/api/command/user/vm/StopVMCmd.java index 90d565df3f0..638e423cbcd 100644 --- a/api/src/org/apache/cloudstack/api/command/user/vm/StopVMCmd.java +++ b/api/src/org/apache/cloudstack/api/command/user/vm/StopVMCmd.java @@ -16,6 +16,8 @@ // under the License. package org.apache.cloudstack.api.command.user.vm; +import org.apache.log4j.Logger; + import org.apache.cloudstack.api.ACL; import org.apache.cloudstack.api.APICommand; import org.apache.cloudstack.api.ApiCommandJobType; @@ -23,19 +25,17 @@ import org.apache.cloudstack.api.ApiConstants; import org.apache.cloudstack.api.ApiErrorCode; import org.apache.cloudstack.api.BaseAsyncCmd; import org.apache.cloudstack.api.Parameter; +import org.apache.cloudstack.api.ResponseObject.ResponseView; import org.apache.cloudstack.api.ServerApiException; import org.apache.cloudstack.api.response.UserVmResponse; import org.apache.cloudstack.context.CallContext; -import org.apache.log4j.Logger; - import com.cloud.event.EventTypes; import com.cloud.exception.ConcurrentOperationException; -import com.cloud.hypervisor.Hypervisor.HypervisorType; import com.cloud.user.Account; import com.cloud.uservm.UserVm; -@APICommand(name = "stopVirtualMachine", responseObject = UserVmResponse.class, description = "Stops a virtual machine.") +@APICommand(name = "stopVirtualMachine", responseObject = UserVmResponse.class, description = "Stops a virtual machine.", responseView = ResponseView.Restricted) public class StopVMCmd extends BaseAsyncCmd { public static final Logger s_logger = Logger.getLogger(StopVMCmd.class.getName()); @@ -118,9 +118,9 @@ public class StopVMCmd extends BaseAsyncCmd { result = _userVmService.stopVirtualMachine(getId(), isForced()); if (result != null) { - UserVmResponse response = _responseGenerator.createUserVmResponse("virtualmachine", result).get(0); + UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Restricted, "virtualmachine", result).get(0); response.setResponseName(getCommandName()); - this.setResponseObject(response); + setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to stop vm"); } diff --git a/api/src/org/apache/cloudstack/api/command/user/vm/UpdateDefaultNicForVMCmd.java b/api/src/org/apache/cloudstack/api/command/user/vm/UpdateDefaultNicForVMCmd.java index 57ec9fb6729..5cb7657a83b 100644 --- a/api/src/org/apache/cloudstack/api/command/user/vm/UpdateDefaultNicForVMCmd.java +++ b/api/src/org/apache/cloudstack/api/command/user/vm/UpdateDefaultNicForVMCmd.java @@ -21,21 +21,23 @@ import java.util.EnumSet; import org.apache.log4j.Logger; -import org.apache.cloudstack.api.*; +import org.apache.cloudstack.api.APICommand; +import org.apache.cloudstack.api.ApiConstants; import org.apache.cloudstack.api.ApiConstants.VMDetails; -import org.apache.cloudstack.api.response.UserVmResponse; +import org.apache.cloudstack.api.ApiErrorCode; +import org.apache.cloudstack.api.BaseAsyncCmd; +import org.apache.cloudstack.api.Parameter; +import org.apache.cloudstack.api.ResponseObject.ResponseView; +import org.apache.cloudstack.api.ServerApiException; import org.apache.cloudstack.api.response.NicResponse; +import org.apache.cloudstack.api.response.UserVmResponse; import org.apache.cloudstack.context.CallContext; import com.cloud.event.EventTypes; -import com.cloud.exception.ConcurrentOperationException; -import com.cloud.exception.InsufficientCapacityException; -import com.cloud.exception.ResourceAllocationException; -import com.cloud.exception.ResourceUnavailableException; import com.cloud.user.Account; import com.cloud.uservm.UserVm; -@APICommand(name = "updateDefaultNicForVirtualMachine", description="Changes the default NIC on a VM", responseObject=UserVmResponse.class) +@APICommand(name = "updateDefaultNicForVirtualMachine", description = "Changes the default NIC on a VM", responseObject = UserVmResponse.class, responseView = ResponseView.Restricted) public class UpdateDefaultNicForVMCmd extends BaseAsyncCmd { public static final Logger s_logger = Logger.getLogger(UpdateDefaultNicForVMCmd.class); @@ -106,9 +108,9 @@ public class UpdateDefaultNicForVMCmd extends BaseAsyncCmd { dc.add(VMDetails.valueOf("nics")); EnumSet details = EnumSet.copyOf(dc); if (result != null){ - UserVmResponse response = _responseGenerator.createUserVmResponse("virtualmachine", details, result).get(0); + UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Restricted, "virtualmachine", details, result).get(0); response.setResponseName(getCommandName()); - this.setResponseObject(response); + setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to set default nic for VM. Refer to server logs for details."); } diff --git a/api/src/org/apache/cloudstack/api/command/user/vm/UpdateVMCmd.java b/api/src/org/apache/cloudstack/api/command/user/vm/UpdateVMCmd.java index ba466b05a04..fe51fa40e98 100644 --- a/api/src/org/apache/cloudstack/api/command/user/vm/UpdateVMCmd.java +++ b/api/src/org/apache/cloudstack/api/command/user/vm/UpdateVMCmd.java @@ -16,18 +16,19 @@ // under the License. package org.apache.cloudstack.api.command.user.vm; +import org.apache.log4j.Logger; + import org.apache.cloudstack.api.APICommand; import org.apache.cloudstack.api.ApiConstants; import org.apache.cloudstack.api.ApiErrorCode; import org.apache.cloudstack.api.BaseCmd; import org.apache.cloudstack.api.Parameter; +import org.apache.cloudstack.api.ResponseObject.ResponseView; import org.apache.cloudstack.api.ServerApiException; import org.apache.cloudstack.api.response.GuestOSResponse; import org.apache.cloudstack.api.response.UserVmResponse; import org.apache.cloudstack.context.CallContext; -import org.apache.log4j.Logger; - import com.cloud.exception.InsufficientCapacityException; import com.cloud.exception.ResourceUnavailableException; import com.cloud.user.Account; @@ -36,7 +37,7 @@ import com.cloud.uservm.UserVm; @APICommand(name = "updateVirtualMachine", description="Updates properties of a virtual machine. The VM has to be stopped and restarted for the " + "new properties to take effect. UpdateVirtualMachine does not first check whether the VM is stopped. " + - "Therefore, stop the VM manually before issuing this call.", responseObject=UserVmResponse.class) + "Therefore, stop the VM manually before issuing this call.", responseObject = UserVmResponse.class, responseView = ResponseView.Restricted) public class UpdateVMCmd extends BaseCmd{ public static final Logger s_logger = Logger.getLogger(UpdateVMCmd.class.getName()); private static final String s_name = "updatevirtualmachineresponse"; @@ -137,9 +138,9 @@ public class UpdateVMCmd extends BaseCmd{ CallContext.current().setEventDetails("Vm Id: "+getId()); UserVm result = _userVmService.updateVirtualMachine(this); if (result != null){ - UserVmResponse response = _responseGenerator.createUserVmResponse("virtualmachine", result).get(0); + UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Restricted, "virtualmachine", result).get(0); response.setResponseName(getCommandName()); - this.setResponseObject(response); + setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to update vm"); } diff --git a/api/src/org/apache/cloudstack/api/command/user/vm/UpgradeVMCmd.java b/api/src/org/apache/cloudstack/api/command/user/vm/UpgradeVMCmd.java index 161131b3955..c98a484da9a 100644 --- a/api/src/org/apache/cloudstack/api/command/user/vm/UpgradeVMCmd.java +++ b/api/src/org/apache/cloudstack/api/command/user/vm/UpgradeVMCmd.java @@ -23,6 +23,7 @@ import org.apache.cloudstack.api.ApiConstants; import org.apache.cloudstack.api.ApiErrorCode; import org.apache.cloudstack.api.BaseCmd; import org.apache.cloudstack.api.Parameter; +import org.apache.cloudstack.api.ResponseObject.ResponseView; import org.apache.cloudstack.api.ServerApiException; import org.apache.cloudstack.api.response.ServiceOfferingResponse; import org.apache.cloudstack.api.response.UserVmResponse; @@ -36,7 +37,7 @@ import com.cloud.uservm.UserVm; @APICommand(name = "changeServiceForVirtualMachine", responseObject=UserVmResponse.class, description="Changes the service offering for a virtual machine. " + "The virtual machine must be in a \"Stopped\" state for " + - "this command to take effect.") + "this command to take effect.", responseView = ResponseView.Restricted) public class UpgradeVMCmd extends BaseCmd { public static final Logger s_logger = Logger.getLogger(UpgradeVMCmd.class.getName()); private static final String s_name = "changeserviceforvirtualmachineresponse"; @@ -51,7 +52,7 @@ public class UpgradeVMCmd extends BaseCmd { @Parameter(name=ApiConstants.SERVICE_OFFERING_ID, type=CommandType.UUID, entityType=ServiceOfferingResponse.class, required=true, description="the service offering ID to apply to the virtual machine") - private Long serviceOfferingId; + protected Long serviceOfferingId; ///////////////////////////////////////////////////// /////////////////// Accessors /////////////////////// @@ -99,7 +100,7 @@ public class UpgradeVMCmd extends BaseCmd { UserVm result = _userVmService.upgradeVirtualMachine(this); if (result != null){ - UserVmResponse response = _responseGenerator.createUserVmResponse("virtualmachine", result).get(0); + UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Restricted, "virtualmachine", result).get(0); response.setResponseName(getCommandName()); setResponseObject(response); } else { diff --git a/api/src/org/apache/cloudstack/api/command/user/vmsnapshot/RevertToVMSnapshotCmd.java b/api/src/org/apache/cloudstack/api/command/user/vmsnapshot/RevertToVMSnapshotCmd.java index 9344406e5c5..6239a7771d7 100644 --- a/api/src/org/apache/cloudstack/api/command/user/vmsnapshot/RevertToVMSnapshotCmd.java +++ b/api/src/org/apache/cloudstack/api/command/user/vmsnapshot/RevertToVMSnapshotCmd.java @@ -23,6 +23,7 @@ import org.apache.cloudstack.api.ApiConstants; import org.apache.cloudstack.api.ApiErrorCode; import org.apache.cloudstack.api.BaseAsyncCmd; import org.apache.cloudstack.api.Parameter; +import org.apache.cloudstack.api.ResponseObject.ResponseView; import org.apache.cloudstack.api.ServerApiException; import org.apache.cloudstack.api.response.UserVmResponse; import org.apache.cloudstack.api.response.VMSnapshotResponse; @@ -37,7 +38,7 @@ import com.cloud.user.Account; import com.cloud.uservm.UserVm; import com.cloud.vm.snapshot.VMSnapshot; -@APICommand(name = "revertToVMSnapshot",description = "Revert VM from a vmsnapshot.", responseObject = UserVmResponse.class, since="4.2.0") +@APICommand(name = "revertToVMSnapshot", description = "Revert VM from a vmsnapshot.", responseObject = UserVmResponse.class, since = "4.2.0", responseView = ResponseView.Restricted) public class RevertToVMSnapshotCmd extends BaseAsyncCmd { public static final Logger s_logger = Logger .getLogger(RevertToVMSnapshotCmd.class.getName()); @@ -70,10 +71,10 @@ public class RevertToVMSnapshotCmd extends BaseAsyncCmd { "vmsnapshot id: " + getVmSnapShotId()); UserVm result = _vmSnapshotService.revertToSnapshot(getVmSnapShotId()); if (result != null) { - UserVmResponse response = _responseGenerator.createUserVmResponse( + UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Restricted, "virtualmachine", result).get(0); response.setResponseName(getCommandName()); - this.setResponseObject(response); + setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR,"Failed to revert VM snapshot"); } diff --git a/api/test/org/apache/cloudstack/api/command/test/ScaleVMCmdTest.java b/api/test/org/apache/cloudstack/api/command/test/ScaleVMCmdTest.java index 1e71739fe10..6c90a51732e 100644 --- a/api/test/org/apache/cloudstack/api/command/test/ScaleVMCmdTest.java +++ b/api/test/org/apache/cloudstack/api/command/test/ScaleVMCmdTest.java @@ -16,22 +16,26 @@ // under the License. package org.apache.cloudstack.api.command.test; -import com.cloud.uservm.UserVm; -import com.cloud.vm.UserVmService; +import java.util.LinkedList; +import java.util.List; + import junit.framework.Assert; import junit.framework.TestCase; -import org.apache.cloudstack.api.ResponseGenerator; -import org.apache.cloudstack.api.ServerApiException; -import org.apache.cloudstack.api.command.user.vm.ScaleVMCmd; -import org.apache.cloudstack.api.response.UserVmResponse; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.mockito.Mockito; -import java.util.LinkedList; -import java.util.List; + +import org.apache.cloudstack.api.ResponseGenerator; +import org.apache.cloudstack.api.ResponseObject.ResponseView; +import org.apache.cloudstack.api.ServerApiException; +import org.apache.cloudstack.api.command.user.vm.ScaleVMCmd; +import org.apache.cloudstack.api.response.UserVmResponse; + +import com.cloud.uservm.UserVm; +import com.cloud.vm.UserVmService; public class ScaleVMCmdTest extends TestCase{ @@ -85,7 +89,7 @@ public class ScaleVMCmdTest extends TestCase{ List list = new LinkedList(); list.add(userVmResponse); - Mockito.when(responseGenerator.createUserVmResponse("virtualmachine", userVm)).thenReturn( + Mockito.when(responseGenerator.createUserVmResponse(ResponseView.Restricted, "virtualmachine", userVm)).thenReturn( list); scaleVMCmd._userVmService = userVmService; diff --git a/server/src/com/cloud/api/ApiResponseHelper.java b/server/src/com/cloud/api/ApiResponseHelper.java index 271c7d946df..4cbe6f94bc2 100755 --- a/server/src/com/cloud/api/ApiResponseHelper.java +++ b/server/src/com/cloud/api/ApiResponseHelper.java @@ -1104,6 +1104,7 @@ public class ApiResponseHelper implements ResponseGenerator { return response; } + /* @Override public List createUserVmResponse(String objectName, UserVm... userVms) { return createUserVmResponse(null, objectName, userVms); @@ -1113,6 +1114,7 @@ public class ApiResponseHelper implements ResponseGenerator { public List createUserVmResponse(String objectName, EnumSet details, UserVm... userVms) { return createUserVmResponse(null, objectName, userVms); } + */ @Override public List createUserVmResponse(ResponseView view, String objectName, EnumSet details, UserVm... userVms) { diff --git a/server/src/com/cloud/user/AccountManagerImpl.java b/server/src/com/cloud/user/AccountManagerImpl.java index 3decaf06183..65ba15f2a24 100755 --- a/server/src/com/cloud/user/AccountManagerImpl.java +++ b/server/src/com/cloud/user/AccountManagerImpl.java @@ -2325,6 +2325,7 @@ public class AccountManagerImpl extends ManagerBase implements AccountManager, M // } // } + //TODO: deprecate this to use the new buildACLSearchParameters with permittedDomains, permittedAccounts, and permittedResources as return @Override public void buildACLSearchParameters(Account caller, Long id, String accountName, Long projectId, List permittedAccounts, Ternary domainIdRecursiveListProject,