Fix DistributionSet type should not be updatable (#659)
* DistributionSet type is no longer changable for an existing distribution set in the ui. Signed-off-by: Markus Block <markus.block@bosch-si.com> * Removed field "type" from a distribution update. Signed-off-by: Markus Block <markus.block@bosch-si.com> * Incorporated review comments. Signed-off-by: Markus Block <markus.block@bosch-si.com>
This commit is contained in:
committed by
Kai Zimmermann
parent
a55c34d0bb
commit
e700acc312
@@ -8,13 +8,10 @@
|
||||
*/
|
||||
package org.eclipse.hawkbit.repository.builder;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
import org.eclipse.hawkbit.repository.model.DistributionSet;
|
||||
import org.eclipse.hawkbit.repository.model.DistributionSetType;
|
||||
import org.eclipse.hawkbit.repository.model.NamedEntity;
|
||||
import org.eclipse.hawkbit.repository.model.NamedVersionedEntity;
|
||||
|
||||
@@ -45,22 +42,6 @@ public interface DistributionSetUpdate {
|
||||
*/
|
||||
DistributionSetUpdate description(@Size(max = NamedEntity.DESCRIPTION_MAX_SIZE) String description);
|
||||
|
||||
/**
|
||||
* @param typeKey
|
||||
* for {@link DistributionSet#getType()}
|
||||
* @return updated builder instance
|
||||
*/
|
||||
DistributionSetUpdate type(@Size(min = 1, max = DistributionSetType.KEY_MAX_SIZE) @NotNull String typeKey);
|
||||
|
||||
/**
|
||||
* @param type
|
||||
* for {@link DistributionSet#getType()}
|
||||
* @return updated builder instance
|
||||
*/
|
||||
default DistributionSetUpdate type(final DistributionSetType type) {
|
||||
return type(Optional.ofNullable(type).map(DistributionSetType::getKey).orElse(null));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param requiredMigrationStep
|
||||
* for {@link DistributionSet#isRequiredMigrationStep()}
|
||||
|
||||
@@ -25,8 +25,7 @@ public abstract class AbstractDistributionSetUpdateCreate<T> extends AbstractNam
|
||||
protected String version;
|
||||
protected Boolean requiredMigrationStep;
|
||||
|
||||
@ValidString
|
||||
protected String type;
|
||||
|
||||
protected Collection<Long> modules;
|
||||
|
||||
public T modules(final Collection<Long> modules) {
|
||||
@@ -38,15 +37,6 @@ public abstract class AbstractDistributionSetUpdateCreate<T> extends AbstractNam
|
||||
return modules;
|
||||
}
|
||||
|
||||
public T type(final String type) {
|
||||
this.type = StringUtils.trimWhitespace(type);
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public T requiredMigrationStep(final Boolean requiredMigrationStep) {
|
||||
this.requiredMigrationStep = requiredMigrationStep;
|
||||
return (T) this;
|
||||
|
||||
@@ -38,7 +38,6 @@ import org.eclipse.hawkbit.repository.jpa.model.DsMetadataCompositeKey;
|
||||
import org.eclipse.hawkbit.repository.jpa.model.JpaDistributionSet;
|
||||
import org.eclipse.hawkbit.repository.jpa.model.JpaDistributionSetMetadata;
|
||||
import org.eclipse.hawkbit.repository.jpa.model.JpaDistributionSetMetadata_;
|
||||
import org.eclipse.hawkbit.repository.jpa.model.JpaDistributionSetType;
|
||||
import org.eclipse.hawkbit.repository.jpa.model.JpaDistributionSet_;
|
||||
import org.eclipse.hawkbit.repository.jpa.model.JpaSoftwareModule;
|
||||
import org.eclipse.hawkbit.repository.jpa.rsql.RSQLUtility;
|
||||
@@ -225,28 +224,13 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
|
||||
|
||||
if (update.isRequiredMigrationStep() != null
|
||||
&& !update.isRequiredMigrationStep().equals(set.isRequiredMigrationStep())) {
|
||||
checkDistributionSetIsAssignedToTargets(update.getId());
|
||||
assertDistributionSetIsNotAssignedToTargets(update.getId());
|
||||
set.setRequiredMigrationStep(update.isRequiredMigrationStep());
|
||||
}
|
||||
|
||||
if (update.getType() != null) {
|
||||
final DistributionSetType type = findDistributionSetTypeAndThrowExceptionIfNotFound(update.getType());
|
||||
if (!type.getId().equals(set.getType().getId())) {
|
||||
checkDistributionSetIsAssignedToTargets(update.getId());
|
||||
|
||||
set.setType(type);
|
||||
}
|
||||
}
|
||||
|
||||
return distributionSetRepository.save(set);
|
||||
}
|
||||
|
||||
private JpaDistributionSetType findDistributionSetTypeAndThrowExceptionIfNotFound(final String key) {
|
||||
return (JpaDistributionSetType) distributionSetTypeManagement.getByKey(key)
|
||||
.orElseThrow(() -> new EntityNotFoundException(DistributionSetType.class, key));
|
||||
|
||||
}
|
||||
|
||||
private JpaDistributionSet findDistributionSetAndThrowExceptionIfNotFound(final Long setId) {
|
||||
return (JpaDistributionSet) get(setId)
|
||||
.orElseThrow(() -> new EntityNotFoundException(DistributionSet.class, setId));
|
||||
@@ -330,7 +314,7 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
|
||||
modules.stream().map(SoftwareModule::getId).collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
checkDistributionSetIsAssignedToTargets(setId);
|
||||
assertDistributionSetIsNotAssignedToTargets(setId);
|
||||
|
||||
final JpaDistributionSet set = findDistributionSetAndThrowExceptionIfNotFound(setId);
|
||||
modules.forEach(set::addModule);
|
||||
@@ -346,7 +330,7 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
|
||||
final JpaDistributionSet set = findDistributionSetAndThrowExceptionIfNotFound(setId);
|
||||
final JpaSoftwareModule module = findSoftwareModuleAndThrowExceptionIfNotFound(moduleId);
|
||||
|
||||
checkDistributionSetIsAssignedToTargets(setId);
|
||||
assertDistributionSetIsNotAssignedToTargets(setId);
|
||||
|
||||
set.removeModule(module);
|
||||
|
||||
@@ -636,10 +620,10 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
|
||||
return specList;
|
||||
}
|
||||
|
||||
private void checkDistributionSetIsAssignedToTargets(final Long distributionSet) {
|
||||
private void assertDistributionSetIsNotAssignedToTargets(final Long distributionSet) {
|
||||
if (actionRepository.countByDistributionSetId(distributionSet) > 0) {
|
||||
throw new EntityReadOnlyException(String.format(
|
||||
"distribution set %s is already assigned to targets and cannot be changed", distributionSet));
|
||||
"Distribution set %s is already assigned to targets and cannot be changed", distributionSet));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ import java.util.Optional;
|
||||
|
||||
import org.eclipse.hawkbit.repository.DistributionSetTypeManagement;
|
||||
import org.eclipse.hawkbit.repository.SoftwareModuleManagement;
|
||||
import org.eclipse.hawkbit.repository.ValidString;
|
||||
import org.eclipse.hawkbit.repository.builder.AbstractDistributionSetUpdateCreate;
|
||||
import org.eclipse.hawkbit.repository.builder.DistributionSetCreate;
|
||||
import org.eclipse.hawkbit.repository.exception.EntityNotFoundException;
|
||||
@@ -21,6 +22,7 @@ import org.eclipse.hawkbit.repository.jpa.model.JpaDistributionSet;
|
||||
import org.eclipse.hawkbit.repository.model.DistributionSetType;
|
||||
import org.eclipse.hawkbit.repository.model.SoftwareModule;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Create/build implementation.
|
||||
@@ -29,6 +31,9 @@ import org.springframework.util.CollectionUtils;
|
||||
public class JpaDistributionSetCreate extends AbstractDistributionSetUpdateCreate<DistributionSetCreate>
|
||||
implements DistributionSetCreate {
|
||||
|
||||
@ValidString
|
||||
private String type;
|
||||
|
||||
private final DistributionSetTypeManagement distributionSetTypeManagement;
|
||||
private final SoftwareModuleManagement softwareModuleManagement;
|
||||
|
||||
@@ -46,6 +51,16 @@ public class JpaDistributionSetCreate extends AbstractDistributionSetUpdateCreat
|
||||
Optional.ofNullable(requiredMigrationStep).orElse(Boolean.FALSE));
|
||||
}
|
||||
|
||||
@Override
|
||||
public DistributionSetCreate type(final String type) {
|
||||
this.type = StringUtils.trimWhitespace(type);
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
private DistributionSetType findDistributionSetTypeWithExceptionIfNotFound(final String distributionSetTypekey) {
|
||||
return distributionSetTypeManagement.getByKey(distributionSetTypekey)
|
||||
.orElseThrow(() -> new EntityNotFoundException(DistributionSetType.class, distributionSetTypekey));
|
||||
|
||||
Reference in New Issue
Block a user