Skip to content

Commit 2237486

Browse files
server: Allow renaming cluster, host, and storage (#4165)
This PR adds implementation for changing host and storage name, additionally, it fixes a Bug on cluster updateCluster API command. This PRs also enhances the UI by allowing editing field name on Host and Storage pool. Due to the fact that there is no support to editing cluster via UI, it was not edited. TODO: I will address Host, Cluster, and Storage Pool name edition on CloudStack Primate once the API implementation gets merged. Details: Prior to this PR the following API commands did not offer support for updating name: updateHost (enhancement) updateStoragePool (enhancement) Additionally, updateCluster claims to support changing a cluster name (via clustername parameter); however, such operation did not work. (bug)
1 parent c3554ec commit 2237486

9 files changed

Lines changed: 72 additions & 11 deletions

File tree

api/src/main/java/com/cloud/resource/ResourceService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.apache.cloudstack.api.command.admin.cluster.AddClusterCmd;
2222
import org.apache.cloudstack.api.command.admin.cluster.DeleteClusterCmd;
23+
import org.apache.cloudstack.api.command.admin.cluster.UpdateClusterCmd;
2324
import org.apache.cloudstack.api.command.admin.host.AddHostCmd;
2425
import org.apache.cloudstack.api.command.admin.host.AddSecondaryStorageCmd;
2526
import org.apache.cloudstack.api.command.admin.host.CancelMaintenanceCmd;
@@ -58,7 +59,7 @@ public interface ResourceService {
5859

5960
boolean deleteCluster(DeleteClusterCmd cmd);
6061

61-
Cluster updateCluster(Cluster cluster, String clusterType, String hypervisor, String allocationState, String managedstate);
62+
Cluster updateCluster(UpdateClusterCmd cmd);
6263

6364
List<? extends Host> discoverHosts(AddHostCmd cmd) throws IllegalArgumentException, DiscoveryException, InvalidParameterValueException;
6465

api/src/main/java/org/apache/cloudstack/api/command/admin/cluster/UpdateClusterCmd.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ public Long getId() {
6363
return id;
6464
}
6565

66+
public void setId(Long id) {
67+
this.id = id;
68+
}
69+
6670
public String getHypervisor() {
6771
return hypervisor;
6872
}
@@ -107,7 +111,7 @@ public void execute() {
107111
if (cluster == null) {
108112
throw new InvalidParameterValueException("Unable to find the cluster by id=" + getId());
109113
}
110-
Cluster result = _resourceService.updateCluster(cluster, getClusterType(), getHypervisor(), getAllocationState(), getManagedstate());
114+
Cluster result = _resourceService.updateCluster(this);
111115
if (result != null) {
112116
ClusterResponse clusterResponse = _responseGenerator.createClusterResponse(cluster, false);
113117
clusterResponse.setResponseName(getCommandName());

api/src/main/java/org/apache/cloudstack/api/command/admin/host/UpdateHostCmd.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ public class UpdateHostCmd extends BaseCmd {
4545
@Parameter(name = ApiConstants.ID, type = CommandType.UUID, entityType = HostResponse.class, required = true, description = "the ID of the host to update")
4646
private Long id;
4747

48+
@Parameter(name = ApiConstants.NAME, type = CommandType.STRING, description = "Change the name of host", since = "4.15", authorized = {RoleType.Admin})
49+
private String name;
50+
4851
@Parameter(name = ApiConstants.OS_CATEGORY_ID,
4952
type = CommandType.UUID,
5053
entityType = GuestOSCategoryResponse.class,
@@ -73,6 +76,10 @@ public Long getId() {
7376
return id;
7477
}
7578

79+
public String getName() {
80+
return name;
81+
}
82+
7683
public Long getOsCategoryId() {
7784
return osCategoryId;
7885
}

api/src/main/java/org/apache/cloudstack/api/command/admin/storage/UpdateStoragePoolCmd.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ public class UpdateStoragePoolCmd extends BaseCmd {
4545
@Parameter(name = ApiConstants.ID, type = CommandType.UUID, entityType = StoragePoolResponse.class, required = true, description = "the Id of the storage pool")
4646
private Long id;
4747

48+
@Parameter(name = ApiConstants.NAME, type = CommandType.STRING, entityType = StoragePoolResponse.class, description = "Change the name of the storage pool", since = "4.15")
49+
private String name;
50+
4851
@Parameter(name = ApiConstants.TAGS, type = CommandType.LIST, collectionType = CommandType.STRING, description = "comma-separated list of tags for the storage pool")
4952
private List<String> tags;
5053

@@ -66,6 +69,10 @@ public Long getId() {
6669
return id;
6770
}
6871

72+
public String getName() {
73+
return name;
74+
}
75+
6976
public List<String> getTags() {
7077
return tags;
7178
}

server/src/main/java/com/cloud/resource/ResourceManagerImpl.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.apache.cloudstack.api.ApiConstants;
3434
import org.apache.cloudstack.api.command.admin.cluster.AddClusterCmd;
3535
import org.apache.cloudstack.api.command.admin.cluster.DeleteClusterCmd;
36+
import org.apache.cloudstack.api.command.admin.cluster.UpdateClusterCmd;
3637
import org.apache.cloudstack.api.command.admin.host.AddHostCmd;
3738
import org.apache.cloudstack.api.command.admin.host.AddSecondaryStorageCmd;
3839
import org.apache.cloudstack.api.command.admin.host.CancelMaintenanceCmd;
@@ -1026,12 +1027,26 @@ public void doInTransactionWithoutResult(final TransactionStatus status) {
10261027

10271028
@Override
10281029
@DB
1029-
public Cluster updateCluster(final Cluster clusterToUpdate, final String clusterType, final String hypervisor, final String allocationState, final String managedstate) {
1030+
public Cluster updateCluster(UpdateClusterCmd cmd) {
1031+
ClusterVO cluster = (ClusterVO) getCluster(cmd.getId());
1032+
String clusterType = cmd.getClusterType();
1033+
String hypervisor = cmd.getHypervisor();
1034+
String allocationState = cmd.getAllocationState();
1035+
String managedstate = cmd.getManagedstate();
1036+
String name = cmd.getClusterName();
10301037

1031-
final ClusterVO cluster = (ClusterVO)clusterToUpdate;
10321038
// Verify cluster information and update the cluster if needed
10331039
boolean doUpdate = false;
10341040

1041+
if (org.apache.commons.lang.StringUtils.isNotBlank(name)) {
1042+
if(cluster.getHypervisorType() == HypervisorType.VMware) {
1043+
throw new InvalidParameterValueException("Renaming VMware cluster is not supported as it could cause problems if the updated cluster name is not mapped on VCenter.");
1044+
}
1045+
s_logger.debug("Updating Cluster name to: " + name);
1046+
cluster.setName(name);
1047+
doUpdate = true;
1048+
}
1049+
10351050
if (hypervisor != null && !hypervisor.isEmpty()) {
10361051
final Hypervisor.HypervisorType hypervisorType = Hypervisor.HypervisorType.getType(hypervisor);
10371052
if (hypervisorType == null) {
@@ -1476,8 +1491,9 @@ public boolean checkAndMaintain(final long hostId) {
14761491

14771492
@Override
14781493
public Host updateHost(final UpdateHostCmd cmd) throws NoTransitionException {
1479-
final Long hostId = cmd.getId();
1480-
final Long guestOSCategoryId = cmd.getOsCategoryId();
1494+
Long hostId = cmd.getId();
1495+
String name = cmd.getName();
1496+
Long guestOSCategoryId = cmd.getOsCategoryId();
14811497

14821498
// Verify that the host exists
14831499
final HostVO host = _hostDao.findById(hostId);
@@ -1494,6 +1510,12 @@ public Host updateHost(final UpdateHostCmd cmd) throws NoTransitionException {
14941510
resourceStateTransitTo(host, resourceEvent, _nodeId);
14951511
}
14961512

1513+
if (org.apache.commons.lang.StringUtils.isNotBlank(name)) {
1514+
s_logger.debug("Updating Host name to: " + name);
1515+
host.setName(name);
1516+
_hostDao.update(host.getId(), host);
1517+
}
1518+
14971519
if (guestOSCategoryId != null) {
14981520
// Verify that the guest OS Category exists
14991521
if (!(guestOSCategoryId > 0) || _guestOSCategoryDao.findById(guestOSCategoryId) == null) {

server/src/main/java/com/cloud/resource/RollingMaintenanceManagerImpl.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import com.cloud.vm.VirtualMachineProfileImpl;
5050
import com.cloud.vm.dao.VMInstanceDao;
5151
import org.apache.cloudstack.affinity.AffinityGroupProcessor;
52+
import org.apache.cloudstack.api.command.admin.cluster.UpdateClusterCmd;
5253
import org.apache.cloudstack.api.command.admin.host.PrepareForMaintenanceCmd;
5354
import org.apache.cloudstack.api.command.admin.resource.StartRollingMaintenanceCmd;
5455
import org.apache.cloudstack.context.CallContext;
@@ -114,12 +115,15 @@ public boolean configure(String name, Map<String, Object> params) throws Configu
114115
return true;
115116
}
116117

117-
private void updateCluster(long clusterId, String state) {
118+
private void updateCluster(long clusterId, String allocationState) {
118119
Cluster cluster = resourceManager.getCluster(clusterId);
119120
if (cluster == null) {
120121
throw new InvalidParameterValueException("Unable to find the cluster by id=" + clusterId);
121122
}
122-
resourceManager.updateCluster(cluster, "", "", state, "");
123+
UpdateClusterCmd updateClusterCmd = new UpdateClusterCmd();
124+
updateClusterCmd.setId(clusterId);
125+
updateClusterCmd.setAllocationState(allocationState);
126+
resourceManager.updateCluster(updateClusterCmd);
123127
}
124128

125129
private void generateReportAndFinishingEvent(StartRollingMaintenanceCmd cmd, boolean success, String details,

server/src/main/java/com/cloud/storage/StorageManagerImpl.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,14 @@ public PrimaryDataStoreInfo updateStoragePool(UpdateStoragePoolCmd cmd) throws I
792792
throw new IllegalArgumentException("Unable to find storage pool with ID: " + id);
793793
}
794794

795+
String name = cmd.getName();
796+
if(org.apache.commons.lang.StringUtils.isNotBlank(name)) {
797+
s_logger.debug("Updating Storage Pool name to: " + name);
798+
pool.setName(name);
799+
_storagePoolDao.update(pool.getId(), pool);
800+
}
801+
802+
795803
final List<String> storagePoolTags = cmd.getTags();
796804
if (storagePoolTags != null) {
797805
if (s_logger.isDebugEnabled()) {

server/src/test/java/com/cloud/resource/MockResourceManagerImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import org.apache.cloudstack.api.command.admin.cluster.AddClusterCmd;
2727
import org.apache.cloudstack.api.command.admin.cluster.DeleteClusterCmd;
28+
import org.apache.cloudstack.api.command.admin.cluster.UpdateClusterCmd;
2829
import org.apache.cloudstack.api.command.admin.host.AddHostCmd;
2930
import org.apache.cloudstack.api.command.admin.host.AddSecondaryStorageCmd;
3031
import org.apache.cloudstack.api.command.admin.host.CancelMaintenanceCmd;
@@ -109,7 +110,7 @@ public boolean deleteCluster(final DeleteClusterCmd cmd) {
109110
* @see com.cloud.resource.ResourceService#updateCluster(com.cloud.org.Cluster, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
110111
*/
111112
@Override
112-
public Cluster updateCluster(final Cluster cluster, final String clusterType, final String hypervisor, final String allocationState, final String managedstate) {
113+
public Cluster updateCluster(UpdateClusterCmd cmd) {
113114
// TODO Auto-generated method stub
114115
return null;
115116
}

ui/scripts/system.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17023,6 +17023,8 @@
1702317023
if (args.data.annotation != null && args.data.annotation.length > 0)
1702417024
array1.push("&annotation=" + args.data.annotation);
1702517025

17026+
if (args.data.name != null && args.data.name.length > 0)
17027+
array1.push("&name=" + args.data.name);
1702617028
$.ajax({
1702717029
url: createURL("updateHost&id=" + args.context.hosts[0].id + array1.join("")),
1702817030
dataType: "json",
@@ -17961,7 +17963,8 @@
1796117963

1796217964
fields:[ {
1796317965
name: {
17964-
label: 'label.name'
17966+
label: 'label.name',
17967+
isEditable: true
1796517968
}
1796617969
},
1796717970
{
@@ -19556,6 +19559,9 @@
1955619559
array1.push("&capacityiops=" + capacityIops);
1955719560
}
1955819561

19562+
if (args.data.name != null && args.data.name.length > 0)
19563+
array1.push("&name=" + args.data.name);
19564+
1955919565
$.ajax({
1956019566
url: createURL("updateStoragePool&id=" + args.context.primarystorages[0].id + array1.join("")),
1956119567
dataType: "json",
@@ -19688,7 +19694,8 @@
1968819694
title: 'label.details',
1968919695
fields:[ {
1969019696
name: {
19691-
label: 'label.name'
19697+
label: 'label.name',
19698+
isEditable: true
1969219699
}
1969319700
},
1969419701
{

0 commit comments

Comments
 (0)