Batch system config update (#1402)

* Added an endpoint for batch update of system configurations

Signed-off-by: Denislav Prinov <denislav.prinov@bosch.com>

* batch db save

Signed-off-by: Denislav Prinov <denislav.prinov@bosch.com>

* Review changes and added tests

Signed-off-by: Denislav Prinov <denislav.prinov@bosch.com>

* Evict cache only if transaction is commited - such as @CacheEvict

Signed-off-by: Denislav Prinov <denislav.prinov@bosch.com>

* refactoring

Signed-off-by: Denislav Prinov <denislav.prinov@bosch.com>

* Using AfterTransactionCommitExecutor for cache eviction

Signed-off-by: Denislav Prinov <denislav.prinov@bosch.com>

* Change request body

Signed-off-by: Denislav Prinov <denislav.prinov@bosch.com>

---------

Signed-off-by: Denislav Prinov <denislav.prinov@bosch.com>
This commit is contained in:
Denislav Prinov
2023-08-02 11:15:27 +03:00
committed by GitHub
parent 1dc1bdbe94
commit fb30999d73
7 changed files with 201 additions and 28 deletions

View File

@@ -34,15 +34,15 @@ public class MgmtSystemTenantConfigurationValueRequest {
}
/**
* Sets the MgmtSystemTenantConfigurationValueRequest
* Sets the value of the MgmtSystemTenantConfigurationValueRequest
*
* @param value
*/
public void setValue(final Object value) {
if (!(value instanceof Serializable)) {
throw new IllegalArgumentException("The value muste be a instance of " + Serializable.class.getName());
throw new IllegalArgumentException("The value must be a instance of " + Serializable.class.getName());
}
this.value = (Serializable) value;
}
}

View File

@@ -8,6 +8,8 @@
*/
package org.eclipse.hawkbit.mgmt.rest.api;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import org.eclipse.hawkbit.mgmt.json.model.system.MgmtSystemTenantConfigurationValue;
@@ -19,6 +21,7 @@ import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
/**
* REST Resource for handling tenant specific configuration operations.
@@ -83,4 +86,20 @@ public interface MgmtTenantManagementRestApi {
ResponseEntity<MgmtSystemTenantConfigurationValue> updateTenantConfigurationValue(
@PathVariable("keyName") String keyName, MgmtSystemTenantConfigurationValueRequest configurationValueRest);
/**
* Handles the PUT request for updating a batch of tenant specific configurations
*
* @param configurationValueMap
* a Map of name - value pairs for the configurations
*
* @return if the given configurations values exists and could be get HTTP OK.
* In any failure the JsonResponseExceptionHandler is handling the
* response.
*/
@PutMapping(value = MgmtRestConstants.SYSTEM_V1_REQUEST_MAPPING + "/configs", consumes = {
MediaTypes.HAL_JSON_VALUE, MediaType.APPLICATION_JSON_VALUE }, produces = { MediaTypes.HAL_JSON_VALUE,
MediaType.APPLICATION_JSON_VALUE })
ResponseEntity<List<MgmtSystemTenantConfigurationValue>> updateTenantConfiguration(
@RequestBody Map<String, Serializable> configurationValueMap);
}

View File

@@ -9,7 +9,9 @@
package org.eclipse.hawkbit.mgmt.rest.resource;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.eclipse.hawkbit.mgmt.json.model.system.MgmtSystemTenantConfigurationValue;
import org.eclipse.hawkbit.mgmt.json.model.system.MgmtSystemTenantConfigurationValueRequest;
@@ -76,4 +78,22 @@ public class MgmtTenantManagementResource implements MgmtTenantManagementRestApi
return ResponseEntity.ok(MgmtTenantManagementMapper.toResponse(keyName, updatedValue));
}
@Override
public ResponseEntity<List<MgmtSystemTenantConfigurationValue>> updateTenantConfiguration(
Map<String, Serializable> configurationValueMap) {
boolean containsNull = configurationValueMap.keySet().stream()
.anyMatch(Objects::isNull);
if (containsNull) {
return ResponseEntity.badRequest().build();
}
Map<String, TenantConfigurationValue<Serializable>> tenantConfigurationValues = tenantConfigurationManagement
.addOrUpdateConfiguration(configurationValueMap);
return ResponseEntity.ok(tenantConfigurationValues.entrySet().stream().map(entry ->
MgmtTenantManagementMapper.toResponse(entry.getKey(), entry.getValue())).toList());
}
}

View File

@@ -32,6 +32,14 @@ public class MgmtTenantManagementResourceTest extends AbstractManagementApiInteg
private static final String KEY_MULTI_ASSIGNMENTS = "multi.assignments.enabled";
private static final String KEY_AUTO_CLOSE = "repository.actions.autoclose.enabled";
private static final String ROLLOUT_APPROVAL_ENABLED = "rollout.approval.enabled";
private static final String AUTHENTICATION_GATEWAYTOKEN_ENABLED = "authentication.gatewaytoken.enabled";
private static final String AUTHENTICATION_GATEWAYTOKEN_KEY = "authentication.gatewaytoken.key";
@Test
@Description("The 'multi.assignments.enabled' property must not be changed to false.")
@@ -48,6 +56,36 @@ public class MgmtTenantManagementResourceTest extends AbstractManagementApiInteg
.andExpect(status().isForbidden());
}
@Test
@Description("The Batch configuration should be applied")
public void changeBatchConfiguration() throws Exception {
JSONObject configuration = new JSONObject();
configuration.put(ROLLOUT_APPROVAL_ENABLED, true);
configuration.put(AUTHENTICATION_GATEWAYTOKEN_ENABLED, true);
configuration.put(AUTHENTICATION_GATEWAYTOKEN_KEY, "1234");
String body = configuration.toString();
mvc.perform(put(MgmtRestConstants.SYSTEM_V1_REQUEST_MAPPING + "/configs")
.content(body).contentType(MediaType.APPLICATION_JSON)).andDo(MockMvcResultPrinter.print())
.andExpect(status().isOk());
}
@Test
@Description("The Batch configuration should not be applied")
public void changeBatchConfigurationFail() throws Exception {
JSONObject configuration = new JSONObject();
configuration.put(ROLLOUT_APPROVAL_ENABLED, true);
configuration.put(AUTHENTICATION_GATEWAYTOKEN_ENABLED, "wrong");
configuration.put(AUTHENTICATION_GATEWAYTOKEN_KEY, "1234");
String body = configuration.toString();
mvc.perform(put(MgmtRestConstants.SYSTEM_V1_REQUEST_MAPPING + "/configs")
.content(body).contentType(MediaType.APPLICATION_JSON)).andDo(MockMvcResultPrinter.print())
.andExpect(status().isBadRequest());
}
@Test
@Description("The 'repository.actions.autoclose.enabled' property must not be modified if Multi-Assignments is enabled.")
public void autoCloseCannotBeModifiedIfMultiAssignmentIsEnabled() throws Exception {