From 21f45d05bba0b5fb66fd41f23add38f81e93c562 Mon Sep 17 00:00:00 2001 From: Rakesh Venkatesh Date: Tue, 26 Jan 2021 14:03:35 +0100 Subject: [PATCH] Allow users to update volume name Provide an api support to update volume name by all users --- .../com/cloud/storage/VolumeApiService.java | 2 +- .../command/user/volume/UpdateVolumeCmd.java | 23 +++++++++++++++---- .../cloud/storage/VolumeApiServiceImpl.java | 14 ++++++++++- ui/src/config/section/storage.js | 15 ++++++++++++ 4 files changed, 47 insertions(+), 7 deletions(-) diff --git a/api/src/main/java/com/cloud/storage/VolumeApiService.java b/api/src/main/java/com/cloud/storage/VolumeApiService.java index 5c4130158cd8..6087ece36102 100644 --- a/api/src/main/java/com/cloud/storage/VolumeApiService.java +++ b/api/src/main/java/com/cloud/storage/VolumeApiService.java @@ -99,7 +99,7 @@ Snapshot takeSnapshot(Long volumeId, Long policyId, Long snapshotId, Account acc Snapshot allocSnapshot(Long volumeId, Long policyId, String snapshotName, Snapshot.LocationType locationType) throws ResourceAllocationException; - Volume updateVolume(long volumeId, String path, String state, Long storageId, Boolean displayVolume, String customId, long owner, String chainInfo); + Volume updateVolume(long volumeId, String path, String state, Long storageId, Boolean displayVolume, String customId, long owner, String chainInfo, String name); /** * Extracts the volume to a particular location. diff --git a/api/src/main/java/org/apache/cloudstack/api/command/user/volume/UpdateVolumeCmd.java b/api/src/main/java/org/apache/cloudstack/api/command/user/volume/UpdateVolumeCmd.java index 71fb5764ed8f..345221e1de45 100644 --- a/api/src/main/java/org/apache/cloudstack/api/command/user/volume/UpdateVolumeCmd.java +++ b/api/src/main/java/org/apache/cloudstack/api/command/user/volume/UpdateVolumeCmd.java @@ -52,23 +52,23 @@ public class UpdateVolumeCmd extends BaseAsyncCustomIdCmd implements UserCmd { @Parameter(name=ApiConstants.ID, type=CommandType.UUID, entityType=VolumeResponse.class, description="the ID of the disk volume") private Long id; - @Parameter(name = ApiConstants.PATH, type = CommandType.STRING, description = "The path of the volume") + @Parameter(name = ApiConstants.PATH, type = CommandType.STRING, description = "The path of the volume", authorized = {RoleType.Admin}) private String path; @Parameter(name = ApiConstants.CHAIN_INFO, type = CommandType.STRING, description = "The chain info of the volume", - since = "4.4") + since = "4.4", authorized = {RoleType.Admin}) private String chainInfo; @Parameter(name = ApiConstants.STORAGE_ID, type = CommandType.UUID, entityType = StoragePoolResponse.class, description = "Destination storage pool UUID for the volume", - since = "4.3") + since = "4.3", authorized = {RoleType.Admin}) private Long storageId; - @Parameter(name = ApiConstants.STATE, type = CommandType.STRING, description = "The state of the volume", since = "4.3") + @Parameter(name = ApiConstants.STATE, type = CommandType.STRING, description = "The state of the volume", since = "4.3", authorized = {RoleType.Admin}) private String state; @Parameter(name = ApiConstants.DISPLAY_VOLUME, @@ -76,6 +76,9 @@ public class UpdateVolumeCmd extends BaseAsyncCustomIdCmd implements UserCmd { description = "an optional field, whether to the display the volume to the end user or not.", authorized = {RoleType.Admin}) private Boolean displayVolume; + @Parameter(name = ApiConstants.NAME, type = CommandType.STRING, description = "new name of the volume", since = "4.16") + private String name; + ///////////////////////////////////////////////////// /////////////////// Accessors /////////////////////// ///////////////////////////////////////////////////// @@ -103,6 +106,11 @@ public Boolean getDisplayVolume() { public String getChainInfo() { return chainInfo; } + + public String getName() { + return name; + } + ///////////////////////////////////////////////////// /////////////// API Implementation/////////////////// ///////////////////////////////////////////////////// @@ -150,6 +158,11 @@ public String getEventDescription() { if (getState() != null) { desc.append(", state " + getState()); } + + if (getName() != null) { + desc.append(", name " + getName()); + } + return desc.toString(); } @@ -157,7 +170,7 @@ public String getEventDescription() { public void execute() { CallContext.current().setEventDetails("Volume Id: " + this._uuidMgr.getUuid(Volume.class, getId())); Volume result = _volumeService.updateVolume(getId(), getPath(), getState(), getStorageId(), getDisplayVolume(), - getCustomId(), getEntityOwnerId(), getChainInfo()); + getCustomId(), getEntityOwnerId(), getChainInfo(), getName()); if (result != null) { VolumeResponse response = _responseGenerator.createVolumeResponse(getResponseView(), result); response.setResponseName(getCommandName()); diff --git a/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java b/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java index 019af673847d..2c38bbf6de6d 100644 --- a/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java +++ b/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java @@ -1857,8 +1857,16 @@ public Volume attachVolumeToVM(Long vmId, Long volumeId, Long deviceId) { @Override @ActionEvent(eventType = EventTypes.EVENT_VOLUME_UPDATE, eventDescription = "updating volume", async = true) - public Volume updateVolume(long volumeId, String path, String state, Long storageId, Boolean displayVolume, String customId, long entityOwnerId, String chainInfo) { + public Volume updateVolume(long volumeId, String path, String state, Long storageId, Boolean displayVolume, + String customId, long entityOwnerId, String chainInfo, String name) { + Account caller = CallContext.current().getCallingAccount(); + if (!_accountMgr.isRootAdmin(caller.getId())) { + if (path != null || state != null || storageId != null || displayVolume != null || customId != null || chainInfo != null) { + throw new InvalidParameterValueException("The domain admin and normal user are not allowed to update volume except volume name"); + } + } + VolumeVO volume = _volsDao.findById(volumeId); if (volume == null) { @@ -1903,6 +1911,10 @@ public Volume updateVolume(long volumeId, String path, String state, Long storag volume.setUuid(customId); } + if (name != null) { + volume.setName(name); + } + updateDisplay(volume, displayVolume); _volsDao.update(volumeId, volume); diff --git a/ui/src/config/section/storage.js b/ui/src/config/section/storage.js index f22d702a4662..3eb50b60cd8e 100644 --- a/ui/src/config/section/storage.js +++ b/ui/src/config/section/storage.js @@ -115,6 +115,21 @@ export default { ['Running', 'Stopped', 'Destroyed'].includes(record.vmstate) } }, + { + api: 'updateVolume', + icon: 'edit', + label: 'label.edit', + dataView: true, + args: ['name'], + mapping: { + account: { + value: (record) => { return record.account } + }, + domainid: { + value: (record) => { return record.domainid } + } + } + }, { api: 'createSnapshot', icon: 'camera',