forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CLOUDSTACK-658 - Scaleup vm support for Xenserver
Added the framweork so that it can be extended for vmware and kvm as well. Added unitests and marvin tests.
- Loading branch information
Showing
24 changed files
with
1,545 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
api/src/org/apache/cloudstack/api/command/user/vm/ScaleVMCmd.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
122
api/test/org/apache/cloudstack/api/command/test/ScaleVMCmdTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -480,4 +480,4 @@ public Long getDiskOfferingId() { | |
return diskOfferingId; | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.