Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions api/src/main/java/com/cloud/vm/UserVmService.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import com.cloud.template.VirtualMachineTemplate;
import com.cloud.user.Account;
import com.cloud.uservm.UserVm;
import com.cloud.utils.Pair;
import com.cloud.utils.exception.ExecutionException;

public interface UserVmService {
Expand Down Expand Up @@ -538,9 +539,10 @@ UserVm importVM(final DataCenter zone, final Host host, final VirtualMachineTemp

/**
* Unmanage a guest VM from CloudStack
* @return true if the VM is successfully unmanaged, false if not.
*
* @return (true if successful, false if not, hostUuid) if the VM is successfully unmanaged.
*/
boolean unmanageUserVM(Long vmId);
Pair<Boolean, String> unmanageUserVM(Long vmId, Long targetHostId);

UserVm allocateVMFromBackup(CreateVMFromBackupCmd cmd) throws InsufficientCapacityException, ResourceAllocationException, ResourceUnavailableException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.cloud.exception.ResourceUnavailableException;
import com.cloud.user.Account;
import com.cloud.uservm.UserVm;
import com.cloud.utils.Pair;
import com.cloud.vm.VirtualMachine;
import org.apache.cloudstack.acl.RoleType;
import org.apache.cloudstack.api.APICommand;
Expand All @@ -36,10 +37,12 @@
import org.apache.cloudstack.api.BaseAsyncCmd;
import org.apache.cloudstack.api.Parameter;
import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.response.HostResponse;
import org.apache.cloudstack.api.response.UnmanageVMInstanceResponse;
import org.apache.cloudstack.api.response.UserVmResponse;
import org.apache.cloudstack.context.CallContext;
import org.apache.cloudstack.vm.UnmanagedVMsManager;
import org.apache.commons.lang3.BooleanUtils;

import javax.inject.Inject;

Expand All @@ -65,6 +68,20 @@ public class UnmanageVMInstanceCmd extends BaseAsyncCmd {
description = "The ID of the virtual machine to unmanage")
private Long vmId;

@Parameter(name = ApiConstants.HOST_ID, type = CommandType.UUID,
entityType = HostResponse.class, required = false,
description = "ID of the host which will be used for unmanaging the Instance. " +
"Applicable only for KVM hypervisor and stopped Instances. Domain XML will be stored on this host.",
since = "4.22.0")
private Long hostId;

@Parameter(name = ApiConstants.FORCED,
type = CommandType.BOOLEAN,
required = false,
description = "Force unmanaging Instance with config drive. Applicable only for KVM Hypervisor.",
since = "4.22.0")
private Boolean forced;

/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////
Expand All @@ -83,6 +100,18 @@ public String getEventDescription() {
return "unmanaging VM. VM ID = " + vmId;
}

public Long getHostId() {
return hostId;
}

public void setHostId(Long hostId) {
this.hostId = hostId;
}

public Boolean isForced() {
return BooleanUtils.isTrue(forced);
}

/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////
Expand All @@ -93,9 +122,10 @@ public void execute() throws ResourceUnavailableException, InsufficientCapacityE
UnmanageVMInstanceResponse response = new UnmanageVMInstanceResponse();
try {
CallContext.current().setEventDetails("VM ID = " + vmId);
boolean result = unmanagedVMsManager.unmanageVMInstance(vmId);
response.setSuccess(result);
if (result) {
Pair<Boolean, String> result = unmanagedVMsManager.unmanageVMInstance(vmId, hostId, isForced());
if (result.first()) {
response.setSuccess(true);
response.setHostId(result.second());
response.setDetails("VM unmanaged successfully");
}
} catch (Exception e) {
Expand Down Expand Up @@ -124,5 +154,4 @@ public ApiCommandResourceType getApiResourceType() {
public Long getApiResourceId() {
return vmId;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public class UnmanageVMInstanceResponse extends BaseResponse {
@Param(description = "details of the unmanage VM operation")
private String details;

@SerializedName(ApiConstants.HOST_ID)
@Param(description = "The ID of the host used for unmanaged Instance")
private String hostId;

public UnmanageVMInstanceResponse() {
}

Expand All @@ -55,4 +59,12 @@ public String getDetails() {
public void setDetails(String details) {
this.details = details;
}

public String getHostId() {
return hostId;
}

public void setHostId(String hostId) {
this.hostId = hostId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@

package org.apache.cloudstack.vm;

import com.cloud.utils.Pair;

public interface UnmanageVMService {

/**
* Unmanage a guest VM from CloudStack
* @return true if the VM is successfully unmanaged, false if not.
*
* @return (true if successful, false if not, hostUuid) if the VM is successfully unmanaged.
*/
boolean unmanageVMInstance(long vmId);
Pair<Boolean, String> unmanageVMInstance(long vmId, Long paramHostId, boolean isForced);
}
27 changes: 27 additions & 0 deletions core/src/main/java/com/cloud/agent/api/UnmanageInstanceAnswer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// 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 com.cloud.agent.api;

public class UnmanageInstanceAnswer extends Answer {

public UnmanageInstanceAnswer(UnmanageInstanceCommand cmd, boolean success, String details) {
super(cmd, success, details);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// 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 com.cloud.agent.api;

import com.cloud.agent.api.to.VirtualMachineTO;

/**
*/
public class UnmanageInstanceCommand extends Command {
String instanceName;
boolean executeInSequence = false;
VirtualMachineTO vm;
boolean isConfigDriveAttached;

@Override
public boolean executeInSequence() {
return executeInSequence;
}

public UnmanageInstanceCommand(VirtualMachineTO vm) {
this.vm = vm;
this.instanceName = vm.getName();
}

public UnmanageInstanceCommand(String instanceName) {
this.instanceName = instanceName;
}

public String getInstanceName() {
return instanceName;
}

public VirtualMachineTO getVm() {
return vm;
}

public boolean isConfigDriveAttached() {
return isConfigDriveAttached;
}

public void setConfigDriveAttached(boolean configDriveAttached) {
isConfigDriveAttached = configDriveAttached;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ static String getHypervisorHostname(String name) {
* - Remove the references of the VM and its volumes, nics, IPs from database
* - Keep the VM as it is on the hypervisor
*/
boolean unmanage(String vmUuid);
Pair<Boolean, String> unmanage(String vmUuid, Long paramHostId);

UserVm restoreVirtualMachine(long vmId, Long newTemplateId, Long rootDiskOfferingId, boolean expunge, Map<String, String> details) throws ResourceUnavailableException, InsufficientCapacityException, ResourceAllocationException;

Expand Down
Loading
Loading