Added new field to updateBackupOffering API. (#6190)

* Added new field to updateBackupOffering API.

* Fix some unit tests.

Co-authored-by: João Paraquetti <joao@scclouds.com.br>
Co-authored-by: Joao <JoaoJandre@gitlab.com>
This commit is contained in:
JoaoJandre 2022-04-06 09:56:35 -03:00 committed by GitHub
parent 133b195688
commit afc819e5c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 27 deletions

View File

@ -27,6 +27,7 @@ import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.response.BackupOfferingResponse;
import org.apache.cloudstack.backup.BackupManager;
import org.apache.cloudstack.backup.BackupOffering;
import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
@ -55,6 +56,9 @@ public class UpdateBackupOfferingCmd extends BaseCmd {
@Parameter(name = ApiConstants.NAME, type = CommandType.STRING, description = "The name of the Backup Offering to be updated")
private String name;
@Parameter(name = ApiConstants.ALLOW_USER_DRIVEN_BACKUPS, type = CommandType.BOOLEAN, description = "Whether to allow user driven backups or not")
private Boolean allowUserDrivenBackups;
/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////
@ -70,20 +74,26 @@ public class UpdateBackupOfferingCmd extends BaseCmd {
return description;
}
public Boolean getAllowUserDrivenBackups() {
return allowUserDrivenBackups;
}
/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////
@Override
public void execute() {
try {
if (StringUtils.isAllEmpty(name, description)) {
throw new InvalidParameterValueException(String.format("Can't update Backup Offering [id: %s] because there is no change in name or description.", id));
if (StringUtils.isAllEmpty(getName(), getDescription()) && getAllowUserDrivenBackups() == null) {
throw new InvalidParameterValueException(String.format("Can't update Backup Offering [id: %s] because there are no parameters to be updated, at least one of the",
"following should be informed: name, description or allowUserDrivenBackups.", id));
}
BackupOffering result = backupManager.updateBackupOffering(this);
if (result == null) {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, String.format("Failed to update backup offering [id: %s, name: %s, description: %s].", id, name, description));
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, String.format("Failed to update backup offering %s.",
ReflectionToStringBuilderUtils.reflectOnlySelectedFields(this, "id", "name", "description", "allowUserDrivenBackups")));
}
BackupOfferingResponse response = _responseGenerator.createBackupOfferingResponse(result);
response.setResponseName(getCommandName());

View File

@ -1081,14 +1081,14 @@ public class BackupManagerImpl extends ManagerBase implements BackupManager {
Long id = updateBackupOfferingCmd.getId();
String name = updateBackupOfferingCmd.getName();
String description = updateBackupOfferingCmd.getDescription();
Boolean allowUserDrivenBackups = updateBackupOfferingCmd.getAllowUserDrivenBackups();
BackupOfferingVO backupOfferingVO = backupOfferingDao.findById(id);
if (backupOfferingVO == null) {
throw new InvalidParameterValueException(String.format("Unable to find Backup Offering with id: [%s].", id));
}
LOG.debug(String.format("Trying to update Backup Offering [id: %s, name: %s, description: %s] to [name: %s, description: %s].",
backupOfferingVO.getUuid(), backupOfferingVO.getName(), backupOfferingVO.getDescription(), name, description));
LOG.debug(String.format("Trying to update Backup Offering %s to %s.", ReflectionToStringBuilderUtils.reflectOnlySelectedFields(backupOfferingVO,"uuid", "name",
"description", "userDrivenBackupAllowed"), ReflectionToStringBuilderUtils.reflectOnlySelectedFields(this,"name", "description", "allowUserDrivenBackups")));
BackupOfferingVO offering = backupOfferingDao.createForUpdate(id);
List<String> fields = new ArrayList<>();
@ -1102,13 +1102,19 @@ public class BackupManagerImpl extends ManagerBase implements BackupManager {
fields.add("description: " + description);
}
if (allowUserDrivenBackups != null){
offering.setUserDrivenBackupAllowed(allowUserDrivenBackups);
fields.add("allowUserDrivenBackups: " + allowUserDrivenBackups);
}
if (!backupOfferingDao.update(id, offering)) {
LOG.warn(String.format("Couldn't update Backup offering [id: %s] with [%s].", id, String.join(", ", fields)));
}
BackupOfferingVO response = backupOfferingDao.findById(id);
CallContext.current().setEventDetails(String.format("Backup Offering updated [%s].",
ReflectionToStringBuilderUtils.reflectOnlySelectedFields(response, "id", "name", "description", "externalId")));
ReflectionToStringBuilderUtils.reflectOnlySelectedFields(response, "id", "name", "description", "userDrivenBackupAllowed", "externalId")));
return response;
}

View File

@ -18,6 +18,7 @@ package org.apache.cloudstack.backup;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.command.admin.backup.UpdateBackupOfferingCmd;
import org.apache.cloudstack.backup.dao.BackupOfferingDao;
import org.junit.Before;
@ -48,6 +49,7 @@ public class BackupManagerTest {
when(offering.getId()).thenReturn(1234l);
when(offering.getName()).thenCallRealMethod();
when(offering.getDescription()).thenCallRealMethod();
when(offering.isUserDrivenBackupAllowed()).thenCallRealMethod();
BackupOfferingVO offeringUpdate = Mockito.spy(BackupOfferingVO.class);
when(offeringUpdate.getId()).thenReturn(1234l);
@ -59,6 +61,7 @@ public class BackupManagerTest {
when(backupOfferingDao.update(1234l, offeringUpdate)).thenAnswer(answer -> {
offering.setName("New name");
offering.setDescription("New description");
offering.setUserDrivenBackupAllowed(true);
return true;
});
}
@ -77,34 +80,26 @@ public class BackupManagerTest {
}
}
@Test
@Test (expected = InvalidParameterValueException.class)
public void testExceptionWhenUpdateWithNonExistentId() {
try {
Long id = 123l;
Long id = 123l;
UpdateBackupOfferingCmd cmd = Mockito.spy(UpdateBackupOfferingCmd.class);
when(cmd.getId()).thenReturn(id);
UpdateBackupOfferingCmd cmd = Mockito.spy(UpdateBackupOfferingCmd.class);
when(cmd.getId()).thenReturn(id);
backupManager.updateBackupOffering(cmd);
} catch (InvalidParameterValueException e) {
assertEquals("Unable to find Backup Offering with id: [123].", e.getMessage());
}
backupManager.updateBackupOffering(cmd);
}
@Test
@Test (expected = ServerApiException.class)
public void testExceptionWhenUpdateWithoutChanges() {
try {
Long id = 1234l;
UpdateBackupOfferingCmd cmd = Mockito.spy(UpdateBackupOfferingCmd.class);
when(cmd.getName()).thenReturn(null);
when(cmd.getDescription()).thenReturn(null);
when(cmd.getAllowUserDrivenBackups()).thenReturn(null);
UpdateBackupOfferingCmd cmd = Mockito.spy(UpdateBackupOfferingCmd.class);
when(cmd.getId()).thenReturn(id);
when(cmd.getName()).thenReturn(null);
when(cmd.getDescription()).thenReturn(null);
Mockito.doCallRealMethod().when(cmd).execute();
backupManager.updateBackupOffering(cmd);
} catch (InvalidParameterValueException e) {
assertEquals("Can't update Backup Offering [id: 1234] because there is no change in name or description.", e.getMessage());
}
cmd.execute();
}
@Test
@ -115,9 +110,11 @@ public class BackupManagerTest {
when(cmd.getId()).thenReturn(id);
when(cmd.getName()).thenReturn("New name");
when(cmd.getDescription()).thenReturn("New description");
when(cmd.getAllowUserDrivenBackups()).thenReturn(true);
BackupOffering updated = backupManager.updateBackupOffering(cmd);
assertEquals("New name", updated.getName());
assertEquals("New description", updated.getDescription());
assertEquals(true, updated.isUserDrivenBackupAllowed());
}
}