Skip to content

Commit 68406ba

Browse files
bvbharatkoushik-das
authored andcommitted
CLOUDSTACK-5161 enable scaling of a vm using custom offering
Signed-off-by: Koushik Das <koushik@apache.org>
1 parent abd4a82 commit 68406ba

24 files changed

Lines changed: 388 additions & 88 deletions

File tree

api/src/org/apache/cloudstack/api/ApiConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public class ApiConstants {
7272
public static final String DISPLAY_VM = "displayvm";
7373
public static final String DISPLAY_OFFERING = "displayoffering";
7474
public static final String DISPLAY_VOLUME = "displayvolume";
75+
public static final String CUSTOM_PARAMETERS = "customparameters";
7576
public static final String DNS1 = "dns1";
7677
public static final String DNS2 = "dns2";
7778
public static final String IP6_DNS1 = "ip6dns1";

api/src/org/apache/cloudstack/api/command/admin/systemvm/ScaleSystemVMCmd.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
import com.cloud.user.Account;
4040
import com.cloud.vm.VirtualMachine;
4141

42+
import java.util.Collection;
43+
import java.util.HashMap;
44+
import java.util.Iterator;
45+
import java.util.Map;
46+
4247
@APICommand(name = "scaleSystemVm",
4348
responseObject = SystemVmResponse.class,
4449
description = "Scale the service offering for a system vm (console proxy or secondary storage). " + "The system vm must be in a \"Stopped\" state for "
@@ -61,6 +66,11 @@ public class ScaleSystemVMCmd extends BaseAsyncCmd {
6166
description = "the service offering ID to apply to the system vm")
6267
private Long serviceOfferingId;
6368

69+
@Parameter(name=ApiConstants.CUSTOM_PARAMETERS,
70+
type = CommandType.MAP,
71+
description = "name value pairs of custom parameters for cpu, memory and cpunumber. example customparameters[i].name=value")
72+
private Map<String, String> customParameters;
73+
6474
/////////////////////////////////////////////////////
6575
/////////////////// Accessors ///////////////////////
6676
/////////////////////////////////////////////////////
@@ -73,6 +83,21 @@ public Long getServiceOfferingId() {
7383
return serviceOfferingId;
7484
}
7585

86+
public Map<String, String> getCustomParameters() {
87+
Map<String,String> customparameterMap = new HashMap<String, String>();
88+
if (customParameters != null && customParameters.size() != 0) {
89+
Collection parameterCollection = customParameters.values();
90+
Iterator iter = parameterCollection.iterator();
91+
while (iter.hasNext()) {
92+
HashMap<String, String> value = (HashMap<String, String>) iter.next();
93+
for (String key : value.keySet()) {
94+
customparameterMap.put(key, value.get(key));
95+
}
96+
}
97+
}
98+
return customparameterMap;
99+
}
100+
76101
/////////////////////////////////////////////////////
77102
/////////////// API Implementation///////////////////
78103
/////////////////////////////////////////////////////

api/src/org/apache/cloudstack/api/command/admin/systemvm/UpgradeSystemVMCmd.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
import com.cloud.user.Account;
3535
import com.cloud.vm.VirtualMachine;
3636

37+
import java.util.Collection;
38+
import java.util.HashMap;
39+
import java.util.Iterator;
40+
import java.util.Map;
41+
3742
@APICommand(name = "changeServiceForSystemVm",
3843
responseObject = SystemVmResponse.class,
3944
description = "Changes the service offering for a system vm (console proxy or secondary storage). " + "The system vm must be in a \"Stopped\" state for "
@@ -56,6 +61,11 @@ public class UpgradeSystemVMCmd extends BaseCmd {
5661
description = "the service offering ID to apply to the system vm")
5762
private Long serviceOfferingId;
5863

64+
@Parameter(name=ApiConstants.CUSTOM_PARAMETERS,
65+
type = CommandType.MAP,
66+
description = "name value pairs of custom parameters for cpu, memory and cpunumber. example customparameters[i].name=value")
67+
private Map<String, String> customParameters;
68+
5969
/////////////////////////////////////////////////////
6070
/////////////////// Accessors ///////////////////////
6171
/////////////////////////////////////////////////////
@@ -68,6 +78,21 @@ public Long getServiceOfferingId() {
6878
return serviceOfferingId;
6979
}
7080

81+
public Map<String, String> getCustomParameters() {
82+
Map<String,String> customparameterMap = new HashMap<String, String>();
83+
if (customParameters != null && customParameters.size() !=0){
84+
Collection parameterCollection = customParameters.values();
85+
Iterator iter = parameterCollection.iterator();
86+
while (iter.hasNext()) {
87+
HashMap<String, String> value = (HashMap<String, String>) iter.next();
88+
for (String key : value.keySet()) {
89+
customparameterMap.put(key, value.get(key));
90+
}
91+
}
92+
}
93+
return customparameterMap;
94+
}
95+
7196
/////////////////////////////////////////////////////
7297
/////////////// API Implementation///////////////////
7398
/////////////////////////////////////////////////////

api/src/org/apache/cloudstack/api/command/user/vm/ScaleVMCmd.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@
3939
import com.cloud.user.Account;
4040
import com.cloud.uservm.UserVm;
4141

42+
import java.util.Collection;
43+
import java.util.HashMap;
44+
import java.util.Iterator;
45+
import java.util.Map;
46+
47+
4248
@APICommand(name = "scaleVirtualMachine", description = "Scales the virtual machine to a new service offering.", responseObject = SuccessResponse.class)
4349
public class ScaleVMCmd extends BaseAsyncCmd {
4450
public static final Logger s_logger = Logger.getLogger(ScaleVMCmd.class.getName());
@@ -60,6 +66,11 @@ public class ScaleVMCmd extends BaseAsyncCmd {
6066
description = "the ID of the service offering for the virtual machine")
6167
private Long serviceOfferingId;
6268

69+
@Parameter(name=ApiConstants.CUSTOM_PARAMETERS,
70+
type = CommandType.MAP,
71+
description = "name value pairs of custom parameters for cpu, memory and cpunumber. example customparameters[i].name=value")
72+
private Map<String, String> customParameters;
73+
6374
/////////////////////////////////////////////////////
6475
/////////////////// Accessors ///////////////////////
6576
/////////////////////////////////////////////////////
@@ -72,6 +83,24 @@ public Long getServiceOfferingId() {
7283
return serviceOfferingId;
7384
}
7485

86+
//instead of reading a map directly we are using collections.
87+
//it is because customParameters.values() cannot be cast to a map.
88+
//it gives a exception
89+
public Map<String, String> getCustomParameters() {
90+
Map<String,String> customparameterMap = new HashMap<String, String>();
91+
if (customParameters != null && customParameters.size() !=0){
92+
Collection parameterCollection = customParameters.values();
93+
Iterator iter = parameterCollection.iterator();
94+
while (iter.hasNext()) {
95+
HashMap<String, String> value = (HashMap<String, String>) iter.next();
96+
for (String key : value.keySet()) {
97+
customparameterMap.put(key, value.get(key));
98+
}
99+
}
100+
}
101+
return customparameterMap;
102+
}
103+
75104
/////////////////////////////////////////////////////
76105
/////////////// API Implementation///////////////////
77106
/////////////////////////////////////////////////////

api/src/org/apache/cloudstack/api/command/user/vm/UpgradeVMCmd.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
import com.cloud.user.Account;
3535
import com.cloud.uservm.UserVm;
3636

37+
import java.util.Collection;
38+
import java.util.HashMap;
39+
import java.util.Iterator;
40+
import java.util.Map;
41+
3742
@APICommand(name = "changeServiceForVirtualMachine", responseObject = UserVmResponse.class, description = "Changes the service offering for a virtual machine. "
3843
+ "The virtual machine must be in a \"Stopped\" state for " + "this command to take effect.")
3944
public class UpgradeVMCmd extends BaseCmd {
@@ -54,6 +59,11 @@ public class UpgradeVMCmd extends BaseCmd {
5459
description = "the service offering ID to apply to the virtual machine")
5560
private Long serviceOfferingId;
5661

62+
@Parameter(name=ApiConstants.CUSTOM_PARAMETERS,
63+
type = CommandType.MAP,
64+
description = "name value pairs of custom parameters for cpu, memory and cpunumber. example customparameters[i].name=value")
65+
private Map<String, String> customParameters;
66+
5767
/////////////////////////////////////////////////////
5868
/////////////////// Accessors ///////////////////////
5969
/////////////////////////////////////////////////////
@@ -66,6 +76,21 @@ public Long getServiceOfferingId() {
6676
return serviceOfferingId;
6777
}
6878

79+
public Map<String, String> getCustomParameters() {
80+
Map<String,String> customparameterMap = new HashMap<String, String>();
81+
if (customParameters != null && customParameters.size() !=0){
82+
Collection parameterCollection = customParameters.values();
83+
Iterator iter = parameterCollection.iterator();
84+
while (iter.hasNext()) {
85+
HashMap<String, String> value = (HashMap<String, String>) iter.next();
86+
for (String key : value.keySet()) {
87+
customparameterMap.put(key, value.get(key));
88+
}
89+
}
90+
}
91+
return customparameterMap;
92+
}
93+
6994
/////////////////////////////////////////////////////
7095
/////////////// API Implementation///////////////////
7196
/////////////////////////////////////////////////////

api/src/org/apache/cloudstack/api/response/ServiceOfferingResponse.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ public class ServiceOfferingResponse extends BaseResponse {
4444

4545
@SerializedName("cpunumber")
4646
@Param(description = "the number of CPU")
47-
private int cpuNumber;
47+
private Integer cpuNumber;
4848

4949
@SerializedName("cpuspeed")
5050
@Param(description = "the clock rate CPU speed in Mhz")
51-
private int cpuSpeed;
51+
private Integer cpuSpeed;
5252

5353
@SerializedName("memory")
5454
@Param(description = "the memory in MB")
55-
private int memory;
55+
private Integer memory;
5656

5757
@SerializedName("created")
5858
@Param(description = "the date this service offering was created")
@@ -130,6 +130,10 @@ public class ServiceOfferingResponse extends BaseResponse {
130130
@Param(description = "additional key/value details tied with this service offering", since = "4.2.0")
131131
private Map<String, String> details;
132132

133+
@SerializedName("iscustomized")
134+
@Param(description = "is true if the offering is customized", since = "4.3.0")
135+
private Boolean isCustomized;
136+
133137
public ServiceOfferingResponse() {
134138
}
135139

@@ -185,23 +189,23 @@ public int getCpuNumber() {
185189
return cpuNumber;
186190
}
187191

188-
public void setCpuNumber(int cpuNumber) {
192+
public void setCpuNumber(Integer cpuNumber) {
189193
this.cpuNumber = cpuNumber;
190194
}
191195

192196
public int getCpuSpeed() {
193197
return cpuSpeed;
194198
}
195199

196-
public void setCpuSpeed(int cpuSpeed) {
200+
public void setCpuSpeed(Integer cpuSpeed) {
197201
this.cpuSpeed = cpuSpeed;
198202
}
199203

200204
public int getMemory() {
201205
return memory;
202206
}
203207

204-
public void setMemory(int memory) {
208+
public void setMemory(Integer memory) {
205209
this.memory = memory;
206210
}
207211

@@ -309,4 +313,9 @@ public void setDetails(Map<String, String> details) {
309313
this.details = details;
310314
}
311315

316+
public void setIscutomized(boolean iscutomized) {
317+
this.isCustomized = iscutomized;
318+
319+
}
320+
312321
}

engine/api/src/com/cloud/vm/VirtualMachineManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,9 @@ void advanceReboot(String vmUuid, Map<VirtualMachineProfile.Param, Object> param
145145

146146
/**
147147
* @param vmInstance
148-
* @param newServiceOfferingId
148+
* @param newServiceOffering
149149
*/
150-
void checkIfCanUpgrade(VirtualMachine vmInstance, long newServiceOfferingId);
150+
void checkIfCanUpgrade(VirtualMachine vmInstance, ServiceOffering newServiceOffering);
151151

152152
/**
153153
* @param vmId

engine/orchestration/src/com/cloud/vm/VirtualMachineManagerImpl.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3011,10 +3011,9 @@ public VMInstanceVO findById(long vmId) {
30113011
}
30123012

30133013
@Override
3014-
public void checkIfCanUpgrade(VirtualMachine vmInstance, long newServiceOfferingId) {
3015-
ServiceOfferingVO newServiceOffering = _offeringDao.findById(vmInstance.getId(), newServiceOfferingId);
3014+
public void checkIfCanUpgrade(VirtualMachine vmInstance, ServiceOffering newServiceOffering) {
30163015
if (newServiceOffering == null) {
3017-
throw new InvalidParameterValueException("Unable to find a service offering with id " + newServiceOfferingId);
3016+
throw new InvalidParameterValueException("Unable to find a service offering with id " + newServiceOffering.getId());
30183017
}
30193018

30203019
// Check that the VM is stopped / running
@@ -3025,7 +3024,7 @@ public void checkIfCanUpgrade(VirtualMachine vmInstance, long newServiceOffering
30253024
}
30263025

30273026
// Check if the service offering being upgraded to is what the VM is already running with
3028-
if (vmInstance.getServiceOfferingId() == newServiceOffering.getId()) {
3027+
if (!newServiceOffering.isDynamic() && vmInstance.getServiceOfferingId() == newServiceOffering.getId()) {
30293028
if (s_logger.isInfoEnabled()) {
30303029
s_logger.info("Not upgrading vm " + vmInstance.toString() + " since it already has the requested " + "service offering (" + newServiceOffering.getName() +
30313030
")");
@@ -3734,7 +3733,7 @@ public VMInstanceVO orchestrateReConfigureVm(String vmUuid, ServiceOffering oldS
37343733
VMInstanceVO vm = _vmDao.findByUuid(vmUuid);
37353734

37363735
long newServiceofferingId = vm.getServiceOfferingId();
3737-
ServiceOffering newServiceOffering = _entityMgr.findById(ServiceOffering.class, newServiceofferingId);
3736+
ServiceOffering newServiceOffering = _offeringDao.findById(vm.getId(), newServiceofferingId);
37383737
HostVO hostVo = _hostDao.findById(vm.getHostId());
37393738

37403739
Float memoryOvercommitRatio = CapacityManager.MemOverprovisioningFactor.valueIn(hostVo.getClusterId());

engine/orchestration/src/org/apache/cloudstack/engine/cloud/entity/api/VMEntityManagerImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ public String reserveVirtualMachine(VMEntityVO vmEntityVO, String plannerToUse,
146146
//FIXME: profile should work on VirtualMachineEntity
147147
VMInstanceVO vm = _vmDao.findByUuid(vmEntityVO.getUuid());
148148
VirtualMachineProfileImpl vmProfile = new VirtualMachineProfileImpl(vm);
149+
vmProfile.setServiceOffering(_serviceOfferingDao.findByIdIncludingRemoved(vm.getId(), vm.getServiceOfferingId()));
149150
DataCenterDeployment plan = new DataCenterDeployment(vm.getDataCenterId(), vm.getPodIdToDeployIn(), null, null, null, null);
150151
if (planToDeploy != null && planToDeploy.getDataCenterId() != 0) {
151152
plan =

engine/schema/src/com/cloud/event/UsageEventVO.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
@Entity
3131
@Table(name = "usage_event")
3232
public class UsageEventVO implements UsageEvent {
33+
public enum DynamicParameters {
34+
cpuSpeed, cpuNumber, memory
35+
};
36+
3337
@Id
3438
@GeneratedValue(strategy = GenerationType.IDENTITY)
3539
@Column(name = "id")

0 commit comments

Comments
 (0)