use of io.gravitee.rest.api.service.cockpit.services.CockpitReply in project gravitee-management-rest-api by gravitee-io.
the class PromotionServiceImpl method promote.
@Override
public PromotionEntity promote(String apiId, PromotionRequestEntity promotionRequest, String userId) {
// TODO: do we have to use filteredFields like for duplicate (i think no need members and groups)
// FIXME: can we get the version from target environment
String apiDefinition = apiDuplicatorService.exportAsJson(apiId, ApiSerializer.Version.DEFAULT.getVersion(), "id", "members", "groups");
EnvironmentEntity currentEnvironmentEntity = environmentService.findById(GraviteeContext.getCurrentEnvironment());
UserEntity author = userService.findById(userId);
PromotionQuery promotionQuery = new PromotionQuery();
promotionQuery.setStatuses(List.of(PromotionEntityStatus.CREATED, PromotionEntityStatus.TO_BE_VALIDATED));
promotionQuery.setApiId(apiId);
List<PromotionEntity> inProgressPromotions = search(promotionQuery, null, null).getContent().stream().filter(promotionEntity -> promotionEntity.getTargetEnvCockpitId().equals(promotionRequest.getTargetEnvCockpitId())).collect(Collectors.toList());
if (!inProgressPromotions.isEmpty()) {
throw new PromotionAlreadyInProgressException(inProgressPromotions.get(0).getId());
}
Promotion promotionToSave = convert(apiId, apiDefinition, currentEnvironmentEntity, promotionRequest, author);
promotionToSave.setId(UuidString.generateRandom());
Promotion createdPromotion = null;
try {
createdPromotion = promotionRepository.create(promotionToSave);
auditService.createApiAuditLog(createdPromotion.getApiId(), emptyMap(), PROMOTION_CREATED, createdPromotion.getCreatedAt(), null, createdPromotion);
} catch (TechnicalException exception) {
throw new TechnicalManagementException(String.format("An error occurs while trying to create a promotion request for API %s", apiId), exception);
}
PromotionEntity promotionEntity = convert(createdPromotion);
CockpitReply<PromotionEntity> cockpitReply = cockpitService.requestPromotion(promotionEntity);
promotionEntity.setStatus(cockpitReply.getStatus() != CockpitReplyStatus.SUCCEEDED ? PromotionEntityStatus.ERROR : PromotionEntityStatus.TO_BE_VALIDATED);
try {
promotionRepository.update(convert(promotionEntity));
} catch (TechnicalException exception) {
throw new TechnicalManagementException(String.format("An error occurs while trying to update promotion %s", promotionEntity.getId()), exception);
}
if (cockpitReply.getStatus() != CockpitReplyStatus.SUCCEEDED) {
throw new BridgeOperationException(BridgeOperation.PROMOTE_API);
}
return promotionEntity;
}
Aggregations