Added REST interfaces for tenant configuration

- added GET, DELETE and PUT function on configuration value interfaces
- changed Exception in TenantConfigurationManagement for correct mapping to HTTP Status
- added function to get global configurations from TenantConfigurationManagement, to have
  only one class for handling these configuration values

Signed-off-by: Nonnenmacher Fabian <fabian.nonnenmacher@bosch-si.com>
This commit is contained in:
Fabian Nonnenmacher
2016-01-28 18:44:31 +01:00
committed by Nonnenmacher Fabian
parent 4be880587a
commit ec79e9bd19
20 changed files with 389 additions and 246 deletions

View File

@@ -66,6 +66,7 @@ public class ResponseExceptionHandler {
ERROR_TO_HTTP_STATUS.put(SpServerError.SP_ENTITY_LOCKED, HttpStatus.LOCKED);
ERROR_TO_HTTP_STATUS.put(SpServerError.SP_ROLLOUT_ILLEGAL_STATE, HttpStatus.BAD_REQUEST);
ERROR_TO_HTTP_STATUS.put(SpServerError.SP_CONFIGURATION_VALUE_INVALID, HttpStatus.BAD_REQUEST);
ERROR_TO_HTTP_STATUS.put(SpServerError.SP_CONFIGURATION_KEY_INVALID, HttpStatus.BAD_REQUEST);
}
private static HttpStatus getStatusOrDefault(final SpServerError error) {

View File

@@ -148,8 +148,8 @@ public class SystemManagementResource {
public ResponseEntity<Void> addUpdateConfig(@RequestBody final TenantConfigurationRest configuration,
@PathVariable final String key) {
// TODO Quick and dirty to stay compatible, but these rest interface
// won't be necessary in future
// TODO Quick and dirty solution to stay compatible, but these rest
// interface won't be necessary in future
Object value;
if (configuration.getValue().equals(Boolean.TRUE)) {

View File

@@ -5,50 +5,55 @@ import java.util.Map;
import org.eclipse.hawkbit.repository.TenantConfigurationManagement;
import org.eclipse.hawkbit.repository.model.TenantConfigurationValue;
import org.eclipse.hawkbit.rest.resource.model.system.SystemConfigurationRest;
import org.eclipse.hawkbit.rest.resource.model.system.TenantConfigurationValueRest;
import org.eclipse.hawkbit.tenancy.configuration.DurationHelper;
import org.eclipse.hawkbit.tenancy.configuration.TenantConfigurationKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A mapper which maps repository model to RESTful model representation and
* back.
*/
final public class SystemMapper {
private static final Logger LOG = LoggerFactory.getLogger(SystemMapper.class);
private SystemMapper() {
// Utility class
}
private static DurationHelper dh = new DurationHelper();
public static SystemConfigurationRest toResponse(
/**
* @param tenantConfigurationManagement
* instance of TenantConfigurationManagement
* @return a map of all existing configuration values
*/
public static Map<String, TenantConfigurationValueRest> toResponse(
final TenantConfigurationManagement tenantConfigurationManagement) {
final SystemConfigurationRest sysconf = new SystemConfigurationRest();
final Map<String, TenantConfigurationValueRest> authconf = new HashMap<String, TenantConfigurationValueRest>();
final Map<String, TenantConfigurationValueRest> configurationMap = new HashMap<String, TenantConfigurationValueRest>();
for (final TenantConfigurationKey key : TenantConfigurationKey.values()) {
final TenantConfigurationValueRest value = toResponse(
tenantConfigurationManagement.getConfigurationValue(key, key.getDataType()));
authconf.put(key.getKeyName(), value);
configurationMap.put(key.getKeyName(),
toResponse(tenantConfigurationManagement.getConfigurationValue(key)));
}
sysconf.setConfiguration(authconf);
return sysconf;
return configurationMap;
}
public static TenantConfigurationValueRest toResponse(final TenantConfigurationValue<?> confValue) {
final TenantConfigurationValueRest response = new TenantConfigurationValueRest();
/**
* maps a TenantConfigurationValue from the repository model to a
* TenantConfigurationValueRest, the RESTful model.
*
* @param repoConfValue
* configuration value as repository model
* @return configuration value as RESTful model
*/
public static TenantConfigurationValueRest toResponse(final TenantConfigurationValue<?> repoConfValue) {
final TenantConfigurationValueRest restConfValue = new TenantConfigurationValueRest();
response.setValue(confValue.getValue());
response.setGlobal(confValue.isGlobal());
response.setCreatedAt(confValue.getCreatedAt());
response.setCreatedBy(confValue.getCreatedBy());
response.setLastModifiedAt(confValue.getLastModifiedAt());
response.setLastModifiedBy(confValue.getLastModifiedBy());
restConfValue.setValue(repoConfValue.getValue());
restConfValue.setGlobal(repoConfValue.isGlobal());
restConfValue.setCreatedAt(repoConfValue.getCreatedAt());
restConfValue.setCreatedBy(repoConfValue.getCreatedBy());
restConfValue.setLastModifiedAt(repoConfValue.getLastModifiedAt());
restConfValue.setLastModifiedBy(repoConfValue.getLastModifiedBy());
return response;
return restConfValue;
}
}

View File

@@ -1,46 +1,115 @@
package org.eclipse.hawkbit.rest.resource;
import org.eclipse.hawkbit.repository.DistributionSetManagement;
import java.util.Map;
import org.eclipse.hawkbit.repository.TenantConfigurationManagement;
import org.eclipse.hawkbit.rest.resource.model.system.SystemConfigurationRequestBodyPut;
import org.eclipse.hawkbit.rest.resource.model.system.SystemConfigurationRest;
import org.eclipse.hawkbit.repository.model.TenantConfigurationValue;
import org.eclipse.hawkbit.rest.resource.model.system.TenantConfigurationValueRequest;
import org.eclipse.hawkbit.rest.resource.model.system.TenantConfigurationValueRest;
import org.eclipse.hawkbit.tenancy.configuration.TenantConfigurationKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* REST Resource handling tenant specific configuration operations.
*
*
*
*
*/
@RestController
@RequestMapping(RestConstants.SYSTEM_V1_REQUEST_MAPPING)
public class SystemResource {
private static final Logger LOGGER = LoggerFactory.getLogger(SystemResource.class);
private static final Logger LOG = LoggerFactory.getLogger(SystemResource.class);
@Autowired
private TenantConfigurationManagement tenantConfigurationManagement;
@Autowired
private DistributionSetManagement distributionSetManagement;
/**
* @return a Map of all configuration values.
*/
@RequestMapping(method = RequestMethod.GET, value = "/conf", produces = { "application/hal+json",
MediaType.APPLICATION_JSON_VALUE })
public ResponseEntity<SystemConfigurationRest> getSystemConfiguration() {
public ResponseEntity<Map<String, TenantConfigurationValueRest>> getSystemConfiguration() {
return new ResponseEntity<>(SystemMapper.toResponse(tenantConfigurationManagement), HttpStatus.OK);
}
@RequestMapping(method = RequestMethod.PUT, value = "/conf", consumes = { "application/hal+json",
/**
* Handles the DELETE request of deleting a tenant specific configuration
* value within SP.
*
* @param keyName
* the Name of the configuration key
* @return If the given configuration value exists and could be deleted Http
* OK. In any failure the JsonResponseExceptionHandler is handling
* the response.
*/
@RequestMapping(method = RequestMethod.DELETE, value = "/conf/{keyName}", produces = { "application/hal+json",
MediaType.APPLICATION_JSON_VALUE })
public ResponseEntity<Void> deleteConfigurationValue(@PathVariable final String keyName) {
final TenantConfigurationKey configKey = TenantConfigurationKey.fromKeyName(keyName);
tenantConfigurationManagement.deleteConfiguration(configKey);
LOG.debug("{} config value deleted, return status {}", keyName, HttpStatus.OK);
return new ResponseEntity<>(HttpStatus.OK);
}
/**
* Handles the GET request of deleting a tenant specific configuration value
* within SP.
*
* @param keyName
* the Name of the configuration key
* @return If the given configuration value exists and could be get Http OK.
* In any failure the JsonResponseExceptionHandler is handling the
* response.
*/
@RequestMapping(method = RequestMethod.GET, value = "/conf/{keyName}", produces = { "application/hal+json",
MediaType.APPLICATION_JSON_VALUE })
public ResponseEntity<TenantConfigurationValueRest> getConfigurationValue(@PathVariable final String keyName) {
final TenantConfigurationKey configKey = TenantConfigurationKey.fromKeyName(keyName);
LOG.debug("{} config value getted, return status {}", keyName, HttpStatus.OK);
return new ResponseEntity<>(
SystemMapper.toResponse(tenantConfigurationManagement.getConfigurationValue(configKey)), HttpStatus.OK);
}
/**
* Handles the GET request of deleting a tenant specific configuration value
* within SP.
*
* @param keyName
* the Name of the configuration key
* @param configurationValueRest
* the new value for the configuration
* @return If the given configuration value exists and could be get Http OK.
* In any failure the JsonResponseExceptionHandler is handling the
* response.
*/
@RequestMapping(method = RequestMethod.PUT, value = "/conf/{keyName}", consumes = { "application/hal+json",
MediaType.APPLICATION_JSON_VALUE }, produces = { "application/hal+json", MediaType.APPLICATION_JSON_VALUE })
public ResponseEntity<SystemConfigurationRest> updateSoftwareModuleType(
@RequestBody final SystemConfigurationRequestBodyPut systemConReq) {
public ResponseEntity<TenantConfigurationValueRest> updateConfigurationValue(@PathVariable final String keyName,
@RequestBody final TenantConfigurationValueRequest configurationValueRest) {
// systemManagement.updateTenantConfiguration(systemConReq);
final TenantConfigurationKey configKey = TenantConfigurationKey.fromKeyName(keyName);
return new ResponseEntity<>(SystemMapper.toResponse(tenantConfigurationManagement), HttpStatus.OK);
final TenantConfigurationValue<Object> updatedValue = tenantConfigurationManagement
.addOrUpdateConfiguration(configKey, configurationValueRest.getValue());
return new ResponseEntity<>(SystemMapper.toResponse(updatedValue), HttpStatus.OK);
}
}
}