Refactor tenant configuration management (#2840)
Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
This commit is contained in:
@@ -9,9 +9,7 @@
|
||||
*/
|
||||
package org.eclipse.hawkbit.mgmt.rest.resource;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
@@ -22,10 +20,10 @@ import org.eclipse.hawkbit.mgmt.json.model.system.MgmtSystemTenantConfigurationV
|
||||
import org.eclipse.hawkbit.mgmt.rest.api.MgmtTenantManagementRestApi;
|
||||
import org.eclipse.hawkbit.mgmt.rest.resource.mapper.MgmtTenantManagementMapper;
|
||||
import org.eclipse.hawkbit.repository.SystemManagement;
|
||||
import org.eclipse.hawkbit.repository.exception.EntityNotFoundException;
|
||||
import org.eclipse.hawkbit.repository.exception.InsufficientPermissionException;
|
||||
import org.eclipse.hawkbit.repository.exception.TenantConfigurationValidatorException;
|
||||
import org.eclipse.hawkbit.repository.helper.TenantConfigHelper;
|
||||
import org.eclipse.hawkbit.repository.model.TenantConfigurationValue;
|
||||
import org.eclipse.hawkbit.tenancy.configuration.TenantConfigurationProperties;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
@@ -70,20 +68,6 @@ public class MgmtTenantManagementResource implements MgmtTenantManagementRestApi
|
||||
return ResponseEntity.ok(tenantConfigurationValueMap);
|
||||
}
|
||||
|
||||
@Override
|
||||
@AuditLog(entity = "TenantConfiguration", type = AuditLog.Type.DELETE, description = "Delete AccessContext Configuration Value")
|
||||
public ResponseEntity<Void> deleteTenantConfigurationValue(final String keyName) {
|
||||
// Default DistributionSet Type cannot be deleted as is part of TenantMetadata
|
||||
if (isDefaultDistributionSetTypeKey(keyName)) {
|
||||
return ResponseEntity.badRequest().build();
|
||||
}
|
||||
|
||||
TenantConfigHelper.getTenantConfigurationManagement().deleteConfiguration(keyName);
|
||||
|
||||
log.debug("{} config value deleted, return status {}", keyName, HttpStatus.OK);
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseEntity<MgmtSystemTenantConfigurationValue> getTenantConfigurationValue(final String keyName) {
|
||||
return ResponseEntity.ok(loadTenantConfigurationValueBy(keyName));
|
||||
@@ -91,61 +75,56 @@ public class MgmtTenantManagementResource implements MgmtTenantManagementRestApi
|
||||
|
||||
@Override
|
||||
@AuditLog(entity = "TenantConfiguration", type = AuditLog.Type.UPDATE, description = "Update AccessContext Configuration Value")
|
||||
public ResponseEntity<MgmtSystemTenantConfigurationValue> updateTenantConfigurationValue(
|
||||
final String keyName, final MgmtSystemTenantConfigurationValueRequest configurationValueRest) {
|
||||
Serializable configurationValue = configurationValueRest.getValue();
|
||||
final MgmtSystemTenantConfigurationValue responseUpdatedValue;
|
||||
public void updateTenantConfigurationValue(final String keyName, final MgmtSystemTenantConfigurationValueRequest configurationValueRest) {
|
||||
final Object configurationValue = configurationValueRest.getValue();
|
||||
if (isDefaultDistributionSetTypeKey(keyName)) {
|
||||
responseUpdatedValue = updateDefaultDsType(configurationValue);
|
||||
updateDefaultDsType(configurationValue);
|
||||
} else {
|
||||
final TenantConfigurationValue<? extends Serializable> updatedTenantConfigurationValue = TenantConfigHelper
|
||||
TenantConfigHelper
|
||||
.getTenantConfigurationManagement()
|
||||
.addOrUpdateConfiguration(keyName, configurationValueRest.getValue());
|
||||
responseUpdatedValue = MgmtTenantManagementMapper.toResponseTenantConfigurationValue(keyName, updatedTenantConfigurationValue);
|
||||
}
|
||||
|
||||
return ResponseEntity.ok(responseUpdatedValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
@AuditLog(entity = "TenantConfiguration", type = AuditLog.Type.UPDATE, description = "Update AccessContext Configuration")
|
||||
public ResponseEntity<List<MgmtSystemTenantConfigurationValue>> updateTenantConfiguration(
|
||||
final Map<String, Serializable> configurationValueMap) {
|
||||
public void updateTenantConfiguration(final Map<String, Object> configurationValueMap) {
|
||||
final boolean containsNull = configurationValueMap.keySet().stream().anyMatch(Objects::isNull);
|
||||
|
||||
if (containsNull) {
|
||||
return ResponseEntity.badRequest().build();
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
//Try update TenantMetadata first
|
||||
final Serializable defaultDsTypeValueUpdate = configurationValueMap.remove(MgmtTenantManagementMapper.DEFAULT_DISTRIBUTION_SET_TYPE_KEY);
|
||||
// try update TenantMetadata first
|
||||
final Object defaultDsTypeValueUpdate = configurationValueMap.remove(MgmtTenantManagementMapper.DEFAULT_DISTRIBUTION_SET_TYPE_KEY);
|
||||
Long oldDefaultDsType = null;
|
||||
MgmtSystemTenantConfigurationValue updatedDefaultDsType = null;
|
||||
if (defaultDsTypeValueUpdate != null) {
|
||||
oldDefaultDsType = systemManagement.getTenantMetadata().getDefaultDsType().getId();
|
||||
updatedDefaultDsType = updateDefaultDsType(defaultDsTypeValueUpdate);
|
||||
}
|
||||
//try update TenantConfiguration, in case of Error -> rollback TenantMetadata
|
||||
final Map<String, TenantConfigurationValue<Serializable>> tenantConfigurationValues;
|
||||
// try update TenantConfiguration, in case of Error -> rollback TenantMetadata
|
||||
try {
|
||||
tenantConfigurationValues = TenantConfigHelper.getTenantConfigurationManagement().addOrUpdateConfiguration(configurationValueMap);
|
||||
TenantConfigHelper.getTenantConfigurationManagement().addOrUpdateConfiguration(configurationValueMap);
|
||||
} catch (Exception ex) {
|
||||
//if DefaultDsType was updated, rollback it in case of TenantConfiguration update.
|
||||
// if DefaultDsType was updated, rollback it in case of TenantConfiguration update.
|
||||
if (updatedDefaultDsType != null) {
|
||||
systemManagement.updateTenantMetadata(oldDefaultDsType);
|
||||
}
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
final List<MgmtSystemTenantConfigurationValue> tenantConfigurationListUpdated = new java.util.ArrayList<>(
|
||||
tenantConfigurationValues.entrySet().stream()
|
||||
.map(entry -> MgmtTenantManagementMapper.toResponseTenantConfigurationValue(entry.getKey(), entry.getValue()))
|
||||
.toList());
|
||||
if (updatedDefaultDsType != null) {
|
||||
tenantConfigurationListUpdated.add(updatedDefaultDsType);
|
||||
@Override
|
||||
@AuditLog(entity = "TenantConfiguration", type = AuditLog.Type.DELETE, description = "Delete AccessContext Configuration Value")
|
||||
public void deleteTenantConfigurationValue(final String keyName) {
|
||||
// Default DistributionSet Type cannot be deleted as is part of TenantMetadata
|
||||
if (isDefaultDistributionSetTypeKey(keyName)) {
|
||||
throw new EntityNotFoundException("Configuration key not found", keyName);
|
||||
}
|
||||
|
||||
return ResponseEntity.ok(tenantConfigurationListUpdated);
|
||||
TenantConfigHelper.getTenantConfigurationManagement().deleteConfiguration(keyName);
|
||||
log.debug("{} config value deleted", keyName);
|
||||
}
|
||||
|
||||
private static boolean isDefaultDistributionSetTypeKey(String keyName) {
|
||||
@@ -164,7 +143,7 @@ public class MgmtTenantManagementResource implements MgmtTenantManagementRestApi
|
||||
return response;
|
||||
}
|
||||
|
||||
private MgmtSystemTenantConfigurationValue updateDefaultDsType(Serializable defaultDsType) {
|
||||
private MgmtSystemTenantConfigurationValue updateDefaultDsType(final Object defaultDsType) {
|
||||
final long updateDefaultDsType;
|
||||
try {
|
||||
updateDefaultDsType = ((Number) defaultDsType).longValue();
|
||||
|
||||
Reference in New Issue
Block a user