Skip to content

Commit c401dbc

Browse files
committed
CLOUDSTACK-7871: allow VM and template details update using update APIs
Allows updating details (key/value) pair which updatse entries invm_template_details and user_vm_details tables using updateVM and updateTemplate APIs. This allows sys admins to update nics, controllers etc without DB hacking. Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com> (cherry picked from commit 97fa4a0) Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
1 parent 85ac979 commit c401dbc

4 files changed

Lines changed: 45 additions & 2 deletions

File tree

‎api/src/org/apache/cloudstack/api/BaseUpdateTemplateOrIsoCmd.java‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
import org.apache.cloudstack.api.response.GuestOSResponse;
2323
import org.apache.cloudstack.api.response.TemplateResponse;
2424

25+
import java.util.Collection;
26+
import java.util.Map;
27+
2528
public abstract class BaseUpdateTemplateOrIsoCmd extends BaseCmd {
2629
public static final Logger s_logger = Logger.getLogger(UpdateIsoCmd.class.getName());
2730

@@ -64,6 +67,9 @@ public abstract class BaseUpdateTemplateOrIsoCmd extends BaseCmd {
6467
@Parameter(name = ApiConstants.ROUTING, type = CommandType.BOOLEAN, description = "true if the template type is routing i.e., if template is used to deploy router")
6568
protected Boolean isRoutingType;
6669

70+
@Parameter(name = ApiConstants.DETAILS, type = CommandType.MAP, description = "Details in key/value pairs.")
71+
protected Map details;
72+
6773
/////////////////////////////////////////////////////
6874
/////////////////// Accessors ///////////////////////
6975
/////////////////////////////////////////////////////
@@ -107,4 +113,13 @@ public Boolean isDynamicallyScalable() {
107113
public Boolean isRoutingType() {
108114
return isRoutingType;
109115
}
110-
}
116+
117+
public Map getDetails() {
118+
if (this.details == null || this.details.isEmpty()) {
119+
return null;
120+
}
121+
122+
Collection paramsCollection = this.details.values();
123+
return (Map) (paramsCollection.toArray())[0];
124+
}
125+
}

‎api/src/org/apache/cloudstack/api/command/user/vm/UpdateVMCmd.java‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
import com.cloud.uservm.UserVm;
3939
import com.cloud.vm.VirtualMachine;
4040

41+
import java.util.Collection;
42+
import java.util.Map;
43+
4144
@APICommand(name = "updateVirtualMachine", description="Updates properties of a virtual machine. The VM has to be stopped and restarted for the " +
4245
"new properties to take effect. UpdateVirtualMachine does not first check whether the VM is stopped. " +
4346
"Therefore, stop the VM manually before issuing this call.", responseObject = UserVmResponse.class, responseView = ResponseView.Restricted, entityType = {VirtualMachine.class},
@@ -90,6 +93,9 @@ public class UpdateVMCmd extends BaseCustomIdCmd {
9093
@Parameter(name = ApiConstants.INSTANCE_NAME, type = CommandType.STRING, description = "instance name of the user vm", since = "4.4", authorized = {RoleType.Admin})
9194
private String instanceName;
9295

96+
@Parameter(name = ApiConstants.DETAILS, type = CommandType.MAP, description = "Details in key/value pairs.")
97+
protected Map details;
98+
9399
/////////////////////////////////////////////////////
94100
/////////////////// Accessors ///////////////////////
95101
/////////////////////////////////////////////////////
@@ -129,6 +135,16 @@ public String getHostName() {
129135
public String getInstanceName() {
130136
return instanceName;
131137
}
138+
139+
public Map getDetails() {
140+
if (this.details == null || this.details.isEmpty()) {
141+
return null;
142+
}
143+
144+
Collection paramsCollection = this.details.values();
145+
return (Map) (paramsCollection.toArray())[0];
146+
}
147+
132148
/////////////////////////////////////////////////////
133149
/////////////// API Implementation///////////////////
134150
/////////////////////////////////////////////////////

‎server/src/com/cloud/template/TemplateManagerImpl.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1776,6 +1776,7 @@ private VMTemplateVO updateTemplateOrIso(BaseUpdateTemplateOrIsoCmd cmd) {
17761776
Boolean isRoutingTemplate = cmd.isRoutingType();
17771777
Boolean bootable = cmd.isBootable();
17781778
Integer sortKey = cmd.getSortKey();
1779+
Map details = cmd.getDetails();
17791780
Account account = CallContext.current().getCallingAccount();
17801781

17811782
// verify that template exists
@@ -1798,7 +1799,7 @@ private VMTemplateVO updateTemplateOrIso(BaseUpdateTemplateOrIsoCmd cmd) {
17981799

17991800
boolean updateNeeded =
18001801
!(name == null && displayText == null && format == null && guestOSId == null && passwordEnabled == null && bootable == null && sortKey == null &&
1801-
isDynamicallyScalable == null && isRoutingTemplate == null);
1802+
isDynamicallyScalable == null && isRoutingTemplate == null && details == null);
18021803
if (!updateNeeded) {
18031804
return template;
18041805
}
@@ -1858,6 +1859,11 @@ private VMTemplateVO updateTemplateOrIso(BaseUpdateTemplateOrIsoCmd cmd) {
18581859
}
18591860
}
18601861

1862+
if (details != null && !details.isEmpty()) {
1863+
template.setDetails(details);
1864+
_tmpltDao.saveDetails(template);
1865+
}
1866+
18611867
_tmpltDao.update(id, template);
18621868

18631869
return _tmpltDao.findById(id);

‎server/src/com/cloud/vm/UserVmManagerImpl.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1899,6 +1899,7 @@ public UserVm updateVirtualMachine(UpdateVMCmd cmd) throws ResourceUnavailableEx
18991899
String userData = cmd.getUserData();
19001900
Boolean isDynamicallyScalable = cmd.isDynamicallyScalable();
19011901
String hostName = cmd.getHostName();
1902+
Map details = cmd.getDetails();
19021903
Account caller = CallContext.current().getCallingAccount();
19031904

19041905
// Input validation and permission checks
@@ -1938,6 +1939,11 @@ public UserVm updateVirtualMachine(UpdateVMCmd cmd) throws ResourceUnavailableEx
19381939

19391940
}
19401941

1942+
if (details != null && !details.isEmpty()) {
1943+
vmInstance.setDetails(details);
1944+
_vmDao.saveDetails(vmInstance);
1945+
}
1946+
19411947
return updateVirtualMachine(id, displayName, group, ha, isDisplayVm, osTypeId, userData, isDynamicallyScalable,
19421948
cmd.getHttpMethod(), cmd.getCustomId(), hostName, cmd.getInstanceName());
19431949
}

0 commit comments

Comments
 (0)