20250828 cleanup (#2639)

* Cleanup

* Refactor artifact management
This commit is contained in:
Avgustin Marinov
2025-09-02 16:08:14 +03:00
committed by GitHub
parent 4f0a8893c7
commit 2a636328a0
305 changed files with 2253 additions and 4566 deletions

View File

@@ -13,8 +13,8 @@ import java.io.InputStream;
import jakarta.servlet.http.HttpServletRequest;
import org.eclipse.hawkbit.repository.artifact.exception.ArtifactBinaryNoLongerExistsException;
import org.eclipse.hawkbit.repository.artifact.model.DbArtifact;
import org.eclipse.hawkbit.artifact.exception.ArtifactBinaryNoLongerExistsException;
import org.eclipse.hawkbit.artifact.model.ArtifactStream;
import org.eclipse.hawkbit.mgmt.rest.api.MgmtDownloadArtifactRestApi;
import org.eclipse.hawkbit.repository.ArtifactManagement;
import org.eclipse.hawkbit.repository.SoftwareModuleManagement;
@@ -38,7 +38,8 @@ public class MgmtDownloadArtifactResource implements MgmtDownloadArtifactRestApi
private final SoftwareModuleManagement<? extends SoftwareModule> softwareModuleManagement;
private final ArtifactManagement artifactManagement;
public MgmtDownloadArtifactResource(final SoftwareModuleManagement<? extends SoftwareModule> softwareModuleManagement, final ArtifactManagement artifactManagement) {
public MgmtDownloadArtifactResource(final SoftwareModuleManagement<? extends SoftwareModule> softwareModuleManagement,
final ArtifactManagement artifactManagement) {
this.softwareModuleManagement = softwareModuleManagement;
this.artifactManagement = artifactManagement;
}
@@ -60,7 +61,7 @@ public class MgmtDownloadArtifactResource implements MgmtDownloadArtifactRestApi
final Artifact artifact = module.getArtifact(artifactId)
.orElseThrow(() -> new EntityNotFoundException(Artifact.class, artifactId));
final DbArtifact file = artifactManagement.loadArtifactBinary(artifact.getSha1Hash(), module.getId(), module.isEncrypted());
final ArtifactStream file = artifactManagement.getArtifactStream(artifact.getSha1Hash(), module.getId(), module.isEncrypted());
final HttpServletRequest request = RequestResponseContextHolder.getHttpServletRequest();
final String ifMatch = request.getHeader(HttpHeaders.IF_MATCH);
if (ifMatch != null && !HttpUtil.matchesHttpHeader(ifMatch, artifact.getSha1Hash())) {

View File

@@ -106,10 +106,7 @@ public class MgmtRolloutResource implements MgmtRolloutRestApi {
@Override
public ResponseEntity<MgmtRolloutResponseBody> getRollout(final Long rolloutId) {
final Rollout findRolloutById = rolloutManagement.getWithDetailedStatus(rolloutId)
.orElseThrow(() -> new EntityNotFoundException(Rollout.class, rolloutId));
return ResponseEntity.ok(MgmtRolloutMapper.toResponseRollout(findRolloutById, true));
return ResponseEntity.ok(MgmtRolloutMapper.toResponseRollout(rolloutManagement.getWithDetailedStatus(rolloutId), true));
}
@Override
@@ -238,8 +235,7 @@ public class MgmtRolloutResource implements MgmtRolloutRestApi {
public ResponseEntity<MgmtRolloutGroupResponseBody> getRolloutGroup(final Long rolloutId, final Long groupId) {
findRolloutOrThrowException(rolloutId);
final RolloutGroup rolloutGroup = rolloutGroupManagement.getWithDetailedStatus(groupId)
.orElseThrow(() -> new EntityNotFoundException(RolloutGroup.class, rolloutId));
final RolloutGroup rolloutGroup = rolloutGroupManagement.getWithDetailedStatus(groupId);
if (!Objects.equals(rolloutId, rolloutGroup.getRollout().getId())) {
throw new EntityNotFoundException(RolloutGroup.class, groupId);
}
@@ -273,8 +269,7 @@ public class MgmtRolloutResource implements MgmtRolloutRestApi {
@Override
public ResponseEntity<MgmtRolloutResponseBody> retryRollout(final Long rolloutId) {
final Rollout rolloutForRetry = rolloutManagement.find(rolloutId)
.orElseThrow(() -> new EntityNotFoundException(Rollout.class, rolloutId));
final Rollout rolloutForRetry = rolloutManagement.get(rolloutId);
if (rolloutForRetry.isDeleted()) {
throw new EntityNotFoundException(Rollout.class, rolloutId);
}

View File

@@ -22,7 +22,6 @@ import java.util.Optional;
import jakarta.validation.ValidationException;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.hawkbit.repository.artifact.urlhandler.ArtifactUrlHandler;
import org.eclipse.hawkbit.audit.AuditLog;
import org.eclipse.hawkbit.mgmt.json.model.PagedList;
import org.eclipse.hawkbit.mgmt.json.model.artifact.MgmtArtifact;
@@ -39,6 +38,8 @@ import org.eclipse.hawkbit.repository.ArtifactManagement;
import org.eclipse.hawkbit.repository.SoftwareModuleManagement;
import org.eclipse.hawkbit.repository.SoftwareModuleTypeManagement;
import org.eclipse.hawkbit.repository.SystemManagement;
import org.eclipse.hawkbit.artifact.model.ArtifactHashes;
import org.eclipse.hawkbit.artifact.urlresolver.ArtifactUrlResolver;
import org.eclipse.hawkbit.repository.exception.EntityNotFoundException;
import org.eclipse.hawkbit.repository.model.Artifact;
import org.eclipse.hawkbit.repository.model.ArtifactUpload;
@@ -65,7 +66,7 @@ public class MgmtSoftwareModuleResource implements MgmtSoftwareModuleRestApi {
private final ArtifactManagement artifactManagement;
private final SoftwareModuleManagement<? extends SoftwareModule> softwareModuleManagement;
private final SoftwareModuleTypeManagement<? extends SoftwareModuleType> softwareModuleTypeManagement;
private final ArtifactUrlHandler artifactUrlHandler;
private final ArtifactUrlResolver artifactUrlHandler;
private final MgmtSoftwareModuleMapper mgmtSoftwareModuleMapper;
private final SystemManagement systemManagement;
@@ -73,7 +74,7 @@ public class MgmtSoftwareModuleResource implements MgmtSoftwareModuleRestApi {
final ArtifactManagement artifactManagement,
final SoftwareModuleManagement<? extends SoftwareModule> softwareModuleManagement,
final SoftwareModuleTypeManagement<? extends SoftwareModuleType> softwareModuleTypeManagement,
final ArtifactUrlHandler artifactUrlHandler,
final ArtifactUrlResolver artifactUrlHandler,
final MgmtSoftwareModuleMapper mgmtSoftwareModuleMapper,
final SystemManagement systemManagement) {
this.artifactManagement = artifactManagement;
@@ -98,14 +99,17 @@ public class MgmtSoftwareModuleResource implements MgmtSoftwareModuleRestApi {
}
try (final InputStream in = file.getInputStream()) {
final Artifact result = artifactManagement.create(new ArtifactUpload(in, softwareModuleId, fileName,
md5Sum == null ? null : md5Sum.toLowerCase(), sha1Sum == null ? null : sha1Sum.toLowerCase(),
sha256Sum == null ? null : sha256Sum.toLowerCase(), false, file.getContentType(), file.getSize()));
final Artifact result = artifactManagement.create(new ArtifactUpload(
in, file.getContentType(), file.getSize(),
new ArtifactHashes(
sha1Sum == null ? null : sha1Sum.toLowerCase(),
md5Sum == null ? null : md5Sum.toLowerCase(),
sha256Sum == null ? null : sha256Sum.toLowerCase()),
softwareModuleId, fileName, false));
final MgmtArtifact reponse = MgmtSoftwareModuleMapper.toResponse(result);
MgmtSoftwareModuleMapper.addLinks(result, reponse);
return ResponseEntity.status(HttpStatus.CREATED).body(reponse);
final MgmtArtifact response = MgmtSoftwareModuleMapper.toResponse(result);
MgmtSoftwareModuleMapper.addLinks(result, response);
return ResponseEntity.status(HttpStatus.CREATED).body(response);
} catch (final IOException e) {
log.error("Failed to store artifact", e);
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);

View File

@@ -20,8 +20,8 @@ import org.eclipse.hawkbit.mgmt.json.model.systemmanagement.MgmtSystemStatistics
import org.eclipse.hawkbit.mgmt.json.model.systemmanagement.MgmtSystemTenantServiceUsage;
import org.eclipse.hawkbit.mgmt.rest.api.MgmtSystemManagementRestApi;
import org.eclipse.hawkbit.repository.SystemManagement;
import org.eclipse.hawkbit.repository.report.model.SystemUsageReportWithTenants;
import org.eclipse.hawkbit.repository.report.model.TenantUsage;
import org.eclipse.hawkbit.repository.model.report.SystemUsageReportWithTenants;
import org.eclipse.hawkbit.repository.model.report.TenantUsage;
import org.springframework.cache.CacheManager;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;

View File

@@ -108,7 +108,7 @@ public class MgmtTargetResource implements MgmtTargetRestApi {
@Override
public ResponseEntity<MgmtTarget> getTarget(final String targetId) {
final Target findTarget = findTargetWithExceptionIfNotFound(targetId);
final Target findTarget = targetManagement.getByControllerId(targetId);
// to single response include poll status
final MgmtTarget response = MgmtTargetMapper.toResponse(findTarget, tenantConfigHelper, null);
MgmtTargetMapper.addTargetLinks(response);
@@ -142,11 +142,10 @@ public class MgmtTargetResource implements MgmtTargetRestApi {
@Override
@AuditLog(entity = "Target", type = AuditLog.Type.UPDATE, description = "Update Target")
public ResponseEntity<MgmtTarget> updateTarget(final String targetId, final MgmtTargetRequestBody targetRest) {
if (targetRest.getRequestAttributes() != null && !Boolean.TRUE.equals(targetRest.getRequestAttributes())) {
if (targetRest.getRequestAttributes() != null && !targetRest.getRequestAttributes()) {
return ResponseEntity.badRequest().build();
}
final Target targetToUpdate = targetManagement.getByControllerId(targetId)
.orElseThrow(() -> new EntityNotFoundException(Target.class, targetId));
final Target targetToUpdate = targetManagement.getByControllerId(targetId);
final TargetType targetType = Optional.ofNullable(targetRest.getTargetType())
.map(targetTypeId -> {
if (targetTypeId == -1L) {
@@ -211,7 +210,7 @@ public class MgmtTargetResource implements MgmtTargetRestApi {
public ResponseEntity<PagedList<MgmtAction>> getActionHistory(
final String targetId, final String rsqlParam,
final int pagingOffsetParam, final int pagingLimitParam, final String sortParam) {
findTargetWithExceptionIfNotFound(targetId);
targetManagement.getByControllerId(targetId);
final Pageable pageable = PagingUtility.toPageable(pagingOffsetParam, pagingLimitParam, sanitizeActionSortParam(sortParam));
final Slice<Action> activeActions;
@@ -331,7 +330,7 @@ public class MgmtTargetResource implements MgmtTargetRestApi {
public ResponseEntity<PagedList<MgmtActionStatus>> getActionStatusList(
final String targetId, final Long actionId,
final int pagingOffsetParam, final int pagingLimitParam, final String sortParam) {
final Target target = findTargetWithExceptionIfNotFound(targetId);
final Target target = targetManagement.getByControllerId(targetId);
final Action action = deploymentManagement.findAction(actionId)
.orElseThrow(() -> new EntityNotFoundException(Action.class, actionId));
@@ -350,7 +349,7 @@ public class MgmtTargetResource implements MgmtTargetRestApi {
@Override
public ResponseEntity<MgmtDistributionSet> getAssignedDistributionSet(final String targetId) {
final MgmtDistributionSet distributionSetRest = deploymentManagement.getAssignedDistributionSet(targetId)
final MgmtDistributionSet distributionSetRest = deploymentManagement.findAssignedDistributionSet(targetId)
.map(ds -> {
final MgmtDistributionSet response = MgmtDistributionSetMapper.toResponse(ds);
MgmtDistributionSetMapper.addLinks(ds, response);
@@ -375,7 +374,7 @@ public class MgmtTargetResource implements MgmtTargetRestApi {
return ResponseEntity.ok(mgmtDistributionSetMapper
.toResponse(deploymentManagement.offlineAssignedDistributionSets(offlineAssignments)));
}
findTargetWithExceptionIfNotFound(targetId);
targetManagement.getByControllerId(targetId);
final List<DeploymentRequest> deploymentRequests = dsAssignments.stream().map(dsAssignment -> {
final boolean isConfirmationRequired = dsAssignment.getConfirmationRequired() == null
@@ -392,19 +391,14 @@ public class MgmtTargetResource implements MgmtTargetRestApi {
@Override
public ResponseEntity<MgmtDistributionSet> getInstalledDistributionSet(final String targetId) {
final MgmtDistributionSet distributionSetRest = deploymentManagement.getInstalledDistributionSet(targetId)
return deploymentManagement.findInstalledDistributionSet(targetId)
.map(set -> {
final MgmtDistributionSet response = MgmtDistributionSetMapper.toResponse(set);
MgmtDistributionSetMapper.addLinks(set, response);
return response;
}).orElse(null);
if (distributionSetRest == null) {
return ResponseEntity.noContent().build();
}
return ResponseEntity.ok(distributionSetRest);
})
.map(distributionSetRest -> ResponseEntity.ok(distributionSetRest))
.orElseGet(() -> ResponseEntity.noContent().build());
}
@Override
@@ -467,11 +461,6 @@ public class MgmtTargetResource implements MgmtTargetRestApi {
return new ResponseEntity<>(HttpStatus.OK);
}
private Target findTargetWithExceptionIfNotFound(final String targetId) {
return targetManagement.getByControllerId(targetId)
.orElseThrow(() -> new EntityNotFoundException(Target.class, targetId));
}
private <T, R> R getNullIfEmpty(final T object, final Function<T, R> extractMethod) {
return object == null ? null : extractMethod.apply(object);
}

View File

@@ -20,6 +20,9 @@ import java.util.stream.Collectors;
import jakarta.validation.ValidationException;
import org.eclipse.hawkbit.artifact.urlresolver.ArtifactUrl;
import org.eclipse.hawkbit.artifact.urlresolver.ArtifactUrlResolver;
import org.eclipse.hawkbit.artifact.urlresolver.ArtifactUrlResolver.DownloadDescriptor;
import org.eclipse.hawkbit.mgmt.json.model.artifact.MgmtArtifact;
import org.eclipse.hawkbit.mgmt.json.model.artifact.MgmtArtifactHash;
import org.eclipse.hawkbit.mgmt.json.model.softwaremodule.MgmtSoftwareModule;
@@ -33,10 +36,6 @@ import org.eclipse.hawkbit.mgmt.rest.resource.MgmtSoftwareModuleResource;
import org.eclipse.hawkbit.repository.SoftwareModuleManagement;
import org.eclipse.hawkbit.repository.SoftwareModuleTypeManagement;
import org.eclipse.hawkbit.repository.SystemManagement;
import org.eclipse.hawkbit.repository.artifact.urlhandler.ApiType;
import org.eclipse.hawkbit.repository.artifact.urlhandler.ArtifactUrl;
import org.eclipse.hawkbit.repository.artifact.urlhandler.ArtifactUrlHandler;
import org.eclipse.hawkbit.repository.artifact.urlhandler.URLPlaceholder;
import org.eclipse.hawkbit.repository.exception.EntityNotFoundException;
import org.eclipse.hawkbit.repository.model.Artifact;
import org.eclipse.hawkbit.repository.model.SoftwareModule;
@@ -157,13 +156,13 @@ public final class MgmtSoftwareModuleMapper {
}
public static void addLinks(final Artifact artifact, final MgmtArtifact response,
final ArtifactUrlHandler artifactUrlHandler, final SystemManagement systemManagement) {
final ArtifactUrlResolver artifactUrlHandler, final SystemManagement systemManagement) {
final List<ArtifactUrl> urls = artifactUrlHandler.getUrls(
new URLPlaceholder(systemManagement.getTenantMetadata().getTenant(),
systemManagement.getTenantMetadata().getId(), null, null,
new URLPlaceholder.SoftwareData(artifact.getSoftwareModule().getId(), artifact.getFilename(),
artifact.getId(), artifact.getSha1Hash())), ApiType.MGMT, null);
urls.forEach(entry -> response.add(Link.of(entry.getRef()).withRel(entry.getRel()).expand()));
new DownloadDescriptor(
systemManagement.getTenantMetadata().getTenant(), null,
artifact.getSoftwareModule().getId(), artifact.getFilename(), artifact.getSha1Hash()),
ArtifactUrlResolver.ApiType.MGMT, null);
urls.forEach(entry -> response.add(Link.of(entry.ref()).withRel(entry.rel()).expand()));
}
private SoftwareModuleManagement.Create fromRequest(

View File

@@ -331,9 +331,9 @@ public final class MgmtTargetMapper {
if (pollStatus != null) {
final MgmtPollStatus pollStatusRest = new MgmtPollStatus();
pollStatusRest.setLastRequestAt(
Date.from(pollStatus.getLastPollDate().atZone(ZoneId.systemDefault()).toInstant()).getTime());
Date.from(pollStatus.lastPollDate().atZone(ZoneId.systemDefault()).toInstant()).getTime());
pollStatusRest.setNextExpectedRequestAt(
Date.from(pollStatus.getNextPollDate().atZone(ZoneId.systemDefault()).toInstant()).getTime());
Date.from(pollStatus.nextPollDate().atZone(ZoneId.systemDefault()).toInstant()).getTime());
pollStatusRest.setOverdue(pollStatus.isOverdue());
targetRest.setPollStatus(pollStatusRest);
}