use of io.gravitee.repository.management.model.Promotion in project gravitee-management-rest-api by gravitee-io.
the class PromotionServiceImpl method processPromotion.
@Override
public PromotionEntity processPromotion(String promotion, boolean accepted, String user) {
try {
final Promotion existing = promotionRepository.findById(promotion).orElseThrow(() -> new PromotionNotFoundException(promotion));
EnvironmentEntity environment = environmentService.findByCockpitId(existing.getTargetEnvCockpitId());
existing.setStatus(accepted ? PromotionStatus.ACCEPTED : PromotionStatus.REJECTED);
final PromotionQuery promotionQuery = new PromotionQuery();
promotionQuery.setStatuses(Collections.singletonList(PromotionEntityStatus.ACCEPTED));
promotionQuery.setTargetEnvCockpitIds(singletonList(existing.getTargetEnvCockpitId()));
promotionQuery.setTargetApiExists(true);
promotionQuery.setApiId(existing.getApiId());
List<PromotionEntity> previousPromotions = search(promotionQuery, new SortableImpl("created_at", false), null).getContent();
// Should create a new API if there is no previous promotion for this API or if the API existed once (after a promotion) but has been deleted since
boolean shouldCreate = CollectionUtils.isEmpty(previousPromotions) || !apiService.exists(previousPromotions.get(0).getTargetApiId());
if (PromotionStatus.ACCEPTED.equals(existing.getStatus())) {
ApiEntity promoted = null;
// FIXME: All the methods should take then env id as input instead of relying on GraviteeContext.getCurrentEnv
GraviteeContext.setCurrentEnvironment(environment.getId());
if (shouldCreate) {
if (!permissionService.hasPermission(ENVIRONMENT_API, environment.getId(), CREATE)) {
throw new ForbiddenAccessException();
}
promoted = apiDuplicatorService.createWithImportedDefinition(existing.getApiDefinition(), user, GraviteeContext.getCurrentOrganization(), GraviteeContext.getCurrentEnvironment());
} else {
if (!permissionService.hasPermission(ENVIRONMENT_API, environment.getId(), UPDATE)) {
throw new ForbiddenAccessException();
}
PromotionEntity lastAcceptedPromotion = previousPromotions.get(0);
final ApiEntity existingApi = apiService.findById(lastAcceptedPromotion.getTargetApiId());
promoted = apiDuplicatorService.updateWithImportedDefinition(existingApi.getId(), existing.getApiDefinition(), user, GraviteeContext.getCurrentOrganization(), GraviteeContext.getCurrentEnvironment());
}
existing.setTargetApiId(promoted.getId());
}
final PromotionEntity promotionEntity = convert(existing);
final CockpitReply<PromotionEntity> cockpitReply = cockpitService.processPromotion(promotionEntity);
if (cockpitReply.getStatus() != CockpitReplyStatus.SUCCEEDED) {
throw new BridgeOperationException(BridgeOperation.PROMOTE_API);
}
final Promotion updated = promotionRepository.update(existing);
return convert(updated);
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to process promotion", ex);
throw new TechnicalManagementException("An error occurs while trying to process promotion", ex);
}
}
use of io.gravitee.repository.management.model.Promotion in project gravitee-management-rest-api by gravitee-io.
the class PromotionServiceImpl method createOrUpdate.
@Override
public PromotionEntity createOrUpdate(PromotionEntity promotionEntity) {
try {
final Optional<Promotion> existingPromotion = promotionRepository.findById(promotionEntity.getId());
final Promotion promotion = convert(promotionEntity);
Promotion createdOrUpdatedPromotion;
if (existingPromotion.isPresent()) {
LOGGER.debug("Updating existing promotion: {}", promotion.getId());
createdOrUpdatedPromotion = promotionRepository.update(promotion);
} else {
LOGGER.debug("Creating promotion: {}", promotion.getId());
createdOrUpdatedPromotion = promotionRepository.create(promotion);
}
return convert(createdOrUpdatedPromotion);
} catch (TechnicalException e) {
LOGGER.error("An error occurs while trying to create or update a promotion using its id {}", promotionEntity.getId(), e);
throw new TechnicalManagementException("An error occurs while trying to create or update a promotion using its id {}" + promotionEntity.getId(), e);
}
}
use of io.gravitee.repository.management.model.Promotion 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;
}
use of io.gravitee.repository.management.model.Promotion in project gravitee-management-rest-api by gravitee-io.
the class PromotionServiceImpl method search.
@Override
public Page<PromotionEntity> search(PromotionQuery query, Sortable sortable, Pageable pageable) {
try {
LOGGER.debug("Searching promotions");
PromotionCriteria criteria = queryToCriteriaBuilder(query).build();
Page<Promotion> promotions = promotionRepository.search(criteria, buildSortable(sortable), buildPageable(pageable));
List<PromotionEntity> entities = promotions.getContent().stream().map(this::convert).collect(Collectors.toList());
LOGGER.debug("Searching promotions - Done with {} elements", entities.size());
return new Page<>(entities, promotions.getPageNumber() + 1, (int) promotions.getPageElements(), promotions.getTotalElements());
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to search promotions", ex);
throw new TechnicalManagementException("An error occurs while trying to search promotions", ex);
}
}
Aggregations