Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public class CreateVolumeCmd extends BaseAsyncCreateCustomIdCmd {
description = "the ID of the disk offering. Either diskOfferingId or snapshotId must be passed in.")
private Long diskOfferingId;

@Parameter(name = ApiConstants.NAME, type = CommandType.STRING, required = true, description = "the name of the disk volume")
@Parameter(name = ApiConstants.NAME, type = CommandType.STRING, description = "the name of the disk volume")
private String volumeName;

@Parameter(name = ApiConstants.SIZE, type = CommandType.LONG, description = "Arbitrary volume size")
Expand Down
24 changes: 20 additions & 4 deletions server/src/com/cloud/storage/VolumeApiServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,25 @@ public VolumeVO doInTransaction(TransactionStatus status) {
});
}

/**
* Retrieves the volume name from CreateVolumeCmd object.
*
* If the retrieved volume name is null, empty or blank, then A random name
* will be generated using getRandomVolumeName method.
*
* @param cmd
* @return Either the retrieved name or a random name.
*/
public String getVolumeNameFromCommand(CreateVolumeCmd cmd) {
String userSpecifiedName = cmd.getVolumeName();

if (org.apache.commons.lang.StringUtils.isBlank(userSpecifiedName)) {
userSpecifiedName = getRandomVolumeName();
}

return userSpecifiedName;
}

/*
* Just allocate a volume in the database, don't send the createvolume cmd
* to hypervisor. The volume will be finally created only when it's attached
Expand Down Expand Up @@ -671,10 +690,7 @@ public VolumeVO allocVolume(CreateVolumeCmd cmd) throws ResourceAllocationExcept
throw new InvalidParameterValueException("Zone is not configured to use local storage but volume's disk offering " + diskOffering.getName() + " uses it");
}

String userSpecifiedName = cmd.getVolumeName();
if (userSpecifiedName == null) {
userSpecifiedName = getRandomVolumeName();
}
String userSpecifiedName = getVolumeNameFromCommand(cmd);

VolumeVO volume = commitVolume(cmd, caller, owner, displayVolume, zoneId, diskOfferingId, provisioningType, size,
minIops, maxIops, parentVolume, userSpecifiedName, _uuidMgr.generateUuid(Volume.class, cmd.getCustomId()));
Expand Down
28 changes: 28 additions & 0 deletions server/test/com/cloud/storage/VolumeApiServiceImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import javax.inject.Inject;

import com.cloud.user.User;
import junit.framework.Assert;
import org.apache.cloudstack.api.command.user.volume.CreateVolumeCmd;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
Expand Down Expand Up @@ -97,6 +99,8 @@ public class VolumeApiServiceImplTest {
SnapshotInfo snapshotInfoMock;
@Mock
VolumeService volService;
@Mock
CreateVolumeCmd createVol;

DetachVolumeCmd detachCmd = new DetachVolumeCmd();
Class<?> _detachCmdClass = detachCmd.getClass();
Expand Down Expand Up @@ -355,6 +359,30 @@ public void testTakeSnapshotF2() throws ResourceAllocationException {
_svc.takeSnapshot(5L, Snapshot.MANUAL_POLICY_ID, 3L, null, false);
}

@Test
public void testNullGetVolumeNameFromCmd() {
when(createVol.getVolumeName()).thenReturn(null);
Assert.assertNotNull(_svc.getVolumeNameFromCommand(createVol));
}

@Test
public void testEmptyGetVolumeNameFromCmd() {
when(createVol.getVolumeName()).thenReturn("");
Assert.assertNotNull(_svc.getVolumeNameFromCommand(createVol));
}

@Test
public void testBlankGetVolumeNameFromCmd() {
when(createVol.getVolumeName()).thenReturn(" ");
Assert.assertNotNull(_svc.getVolumeNameFromCommand(createVol));
}

@Test
public void testNonEmptyGetVolumeNameFromCmd() {
when(createVol.getVolumeName()).thenReturn("abc");
Assert.assertSame(_svc.getVolumeNameFromCommand(createVol), "abc");
}

@After
public void tearDown() {
CallContext.unregister();
Expand Down
2 changes: 1 addition & 1 deletion ui/scripts/docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,7 @@ cloudStack.docs = {
},
// Add volume
helpVolumeName: {
desc: 'Give the volume a unique name so you can find it later.',
desc: 'Give a unique volume name. If it is not provided, a name will be generated randomly.',
externalLink: ''
},
helpVolumeAvailabilityZone: {
Expand Down
5 changes: 1 addition & 4 deletions ui/scripts/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,7 @@
fields: {
name: {
docID: 'helpVolumeName',
label: 'label.name',
validation: {
required: true
}
label: 'label.name'
},
availabilityZone: {
label: 'label.availability.zone',
Expand Down