Skip to content

Commit

Permalink
CLOUDSTACK-658 - Scaleup vm support for Xenserver
Browse files Browse the repository at this point in the history
Added the framweork so that it can be extended for vmware and kvm as well.
Added unitests and marvin tests.
  • Loading branch information
nitinmeh committed Mar 28, 2013
1 parent f8b8f60 commit 3e4430d
Show file tree
Hide file tree
Showing 24 changed files with 1,545 additions and 79 deletions.
6 changes: 3 additions & 3 deletions api/src/com/cloud/agent/api/ScaleVmCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ public int getCpus() {
}

public ScaleVmCommand(String vmName, int cpus,
Integer speed, long minRam, long maxRam) {
Integer speed, long minRam, long maxRam, boolean limitCpuUse) {
super();
this.vmName = vmName;
this.cpus = cpus;
//this.speed = speed;
this.speed = speed;
this.minRam = minRam;
this.maxRam = maxRam;
this.vm = new VirtualMachineTO(1L, vmName, null, cpus, null, minRam, maxRam, null, null, false, false, null);
this.vm = new VirtualMachineTO(1L, vmName, null, cpus, speed, minRam, maxRam, null, null, false, false, null);
/*vm.setName(vmName);
vm.setCpus(cpus);
vm.setRam(minRam, maxRam);*/
Expand Down
1 change: 1 addition & 0 deletions api/src/com/cloud/event/EventTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class EventTypes {
public static final String EVENT_VM_REBOOT = "VM.REBOOT";
public static final String EVENT_VM_UPDATE = "VM.UPDATE";
public static final String EVENT_VM_UPGRADE = "VM.UPGRADE";
public static final String EVENT_VM_SCALE = "VM.SCALE";
public static final String EVENT_VM_RESETPASSWORD = "VM.RESETPASSWORD";
public static final String EVENT_VM_RESETSSHKEY = "VM.RESETSSHKEY";
public static final String EVENT_VM_MIGRATE = "VM.MIGRATE";
Expand Down
16 changes: 4 additions & 12 deletions api/src/com/cloud/vm/UserVmService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,7 @@

import org.apache.cloudstack.api.command.admin.vm.AssignVMCmd;
import org.apache.cloudstack.api.command.admin.vm.RecoverVMCmd;
import org.apache.cloudstack.api.command.user.vm.AddNicToVMCmd;
import org.apache.cloudstack.api.command.user.vm.DeployVMCmd;
import org.apache.cloudstack.api.command.user.vm.DestroyVMCmd;
import org.apache.cloudstack.api.command.user.vm.RebootVMCmd;
import org.apache.cloudstack.api.command.user.vm.RemoveNicFromVMCmd;
import org.apache.cloudstack.api.command.user.vm.ResetVMPasswordCmd;
import org.apache.cloudstack.api.command.user.vm.ResetVMSSHKeyCmd;
import org.apache.cloudstack.api.command.user.vm.RestoreVMCmd;
import org.apache.cloudstack.api.command.user.vm.StartVMCmd;
import org.apache.cloudstack.api.command.user.vm.UpdateDefaultNicForVMCmd;
import org.apache.cloudstack.api.command.user.vm.UpdateVMCmd;
import org.apache.cloudstack.api.command.user.vm.UpgradeVMCmd;
import org.apache.cloudstack.api.command.user.vm.*;
import org.apache.cloudstack.api.command.user.vmgroup.CreateVMGroupCmd;
import org.apache.cloudstack.api.command.user.vmgroup.DeleteVMGroupCmd;
import org.apache.cloudstack.api.command.user.volume.AttachVolumeCmd;
Expand Down Expand Up @@ -402,4 +391,7 @@ UserVm createVirtualMachine(DeployVMCmd cmd) throws InsufficientCapacityExceptio
VirtualMachine vmStorageMigration(Long vmId, StoragePool destPool);

UserVm restoreVM(RestoreVMCmd cmd);

UserVm upgradeVirtualMachine(ScaleVMCmd scaleVMCmd) throws ResourceUnavailableException, ConcurrentOperationException, ManagementServerException, VirtualMachineMigrationException;

}
2 changes: 1 addition & 1 deletion api/src/com/cloud/vm/VirtualMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public enum Event {
AgentReportShutdowned,
AgentReportMigrated,
RevertRequested,
SnapshotRequested
SnapshotRequested,
};

public enum Type {
Expand Down
110 changes: 110 additions & 0 deletions api/src/org/apache/cloudstack/api/command/user/vm/ScaleVMCmd.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// 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.user.vm;

import com.cloud.exception.*;
import com.cloud.user.Account;
import com.cloud.user.UserContext;
import com.cloud.uservm.UserVm;
import org.apache.cloudstack.api.*;
import org.apache.cloudstack.api.response.ServiceOfferingResponse;
import org.apache.cloudstack.api.response.UserVmResponse;
import org.apache.log4j.Logger;


@APICommand(name = "scaleVirtualMachine", description="Scales the virtual machine to a new service offering.", responseObject=UserVmResponse.class)
public class ScaleVMCmd extends BaseCmd {
public static final Logger s_logger = Logger.getLogger(ScaleVMCmd.class.getName());
private static final String s_name = "scalevirtualmachineresponse";

/////////////////////////////////////////////////////
//////////////// API parameters /////////////////////
/////////////////////////////////////////////////////

@ACL
@Parameter(name=ApiConstants.ID, type=CommandType.UUID, entityType=UserVmResponse.class,
required=true, description="The ID of the virtual machine")
private Long id;

@ACL
@Parameter(name=ApiConstants.SERVICE_OFFERING_ID, type=CommandType.UUID, entityType=ServiceOfferingResponse.class,
required=true, description="the ID of the service offering for the virtual machine")
private Long serviceOfferingId;

/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////

public Long getId() {
return id;
}

public Long getServiceOfferingId() {
return serviceOfferingId;
}

/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////

@Override
public String getCommandName() {
return s_name;
}

public static String getResultObjectName() {
return "virtualmachine";
}

@Override
public long getEntityOwnerId() {
UserVm userVm = _entityMgr.findById(UserVm.class, getId());
if (userVm != null) {
return userVm.getAccountId();
}

return Account.ACCOUNT_ID_SYSTEM; // no account info given, parent this command to SYSTEM so ERROR events are tracked
}

@Override
public void execute(){
//UserContext.current().setEventDetails("Vm Id: "+getId());
UserVm result = null;
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){
UserVmResponse response = _responseGenerator.createUserVmResponse("virtualmachine", result).get(0);
response.setResponseName(getCommandName());
this.setResponseObject(response);
} else {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to scale vm");
}
}
}
122 changes: 122 additions & 0 deletions api/test/org/apache/cloudstack/api/command/test/ScaleVMCmdTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// 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.test;

import com.cloud.user.Account;
import com.cloud.user.UserContext;
import com.cloud.uservm.UserVm;
import com.cloud.vm.UserVmService;
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.admin.region.AddRegionCmd;
import org.apache.cloudstack.api.command.user.vm.ScaleVMCmd;
import org.apache.cloudstack.api.response.RegionResponse;
import org.apache.cloudstack.api.response.UserVmResponse;
import org.apache.cloudstack.region.Region;
import org.apache.cloudstack.region.RegionService;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mockito;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


public class ScaleVMCmdTest extends TestCase{

private ScaleVMCmd scaleVMCmd;
private ResponseGenerator responseGenerator;

@Rule
public ExpectedException expectedException = ExpectedException.none();

@Before
public void setUp() {

scaleVMCmd = new ScaleVMCmd(){
@Override
public Long getId() {
return 2L;
}
};

//Account account = new AccountVO("testaccount", 1L, "networkdomain", (short) 0, "uuid");
//UserContext.registerContext(1, account, null, true);


}


@Test
public void testCreateSuccess() {

UserVmService userVmService = Mockito.mock(UserVmService.class);

UserVm uservm = Mockito.mock(UserVm.class);
try {
Mockito.when(
userVmService.upgradeVirtualMachine(scaleVMCmd))
.thenReturn(uservm);
}catch (Exception e){
Assert.fail("Received exception when success expected " +e.getMessage());
}

scaleVMCmd._userVmService = userVmService;
responseGenerator = Mockito.mock(ResponseGenerator.class);

UserVmResponse userVmResponse = Mockito.mock(UserVmResponse.class);
List<UserVmResponse> responseList = new ArrayList<UserVmResponse>();
responseList.add(userVmResponse);

Mockito.when(responseGenerator.createUserVmResponse("virtualmachine",uservm))
.thenReturn(responseList);

scaleVMCmd._responseGenerator = responseGenerator;
scaleVMCmd.execute();

}

@Test
public void testCreateFailure() {

UserVmService userVmService = Mockito.mock(UserVmService.class);

try {
UserVm uservm = Mockito.mock(UserVm.class);
Mockito.when(
userVmService.upgradeVirtualMachine(scaleVMCmd))
.thenReturn(null);
}catch (Exception e){
Assert.fail("Received exception when success expected " +e.getMessage());
}

scaleVMCmd._userVmService = userVmService;

try {
scaleVMCmd.execute();
} catch (ServerApiException exception) {
Assert.assertEquals("Failed to scale vm",
exception.getDescription());
}

}
}
1 change: 1 addition & 0 deletions client/tomcatconf/commands.properties.in
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ listVirtualMachines=15
getVMPassword=15
restoreVirtualMachine=15
changeServiceForVirtualMachine=15
scaleVirtualMachine=15
assignVirtualMachine=1
migrateVirtualMachine=1
recoverVirtualMachine=7
Expand Down
2 changes: 1 addition & 1 deletion core/src/com/cloud/vm/VMInstanceVO.java
Original file line number Diff line number Diff line change
Expand Up @@ -480,4 +480,4 @@ public Long getDiskOfferingId() {
return diskOfferingId;
}

}
}
Loading

0 comments on commit 3e4430d

Please sign in to comment.