use of org.ligoj.app.plugin.prov.AbstractQuoteVmEditionVo in project plugin-prov by ligoj.
the class ProvQuoteUploadResource method persist.
/**
* Validate the input object, do a lookup, then create the {@link ProvQuoteInstance} and the
* {@link ProvQuoteStorage} entities.
*/
private <V extends AbstractQuoteVmEditionVo> void persist(final VmUpload upload, final int subscription, final BiFunction<V, UploadContext, Integer> merger, final UploadContext context, final V vo, final ObjIntConsumer<QuoteStorageEditionVo> diskConsumer, final ResourceType resourceType) {
// Create the quote instance from the validated inputs
final var id = merger.apply(vo, context);
if (id == null) {
// Do not continue
return;
}
// Storage part
final var disks = IntStream.range(0, upload.getDisk().size()).filter(index -> upload.getDisk().get(index) > 0).mapToObj(index -> {
final var size = upload.getDisk().get(index).intValue();
final var sizeMax = upload.getDiskMax().size() > index ? upload.getDiskMax().get(index).intValue() : null;
// Size is provided, propagate the upload properties
final var svo = new QuoteStorageEditionVo();
svo.setName(vo.getName() + (index == 0 ? "" : index));
diskConsumer.accept(svo, id);
svo.setSize(size);
svo.setSizeMax(sizeMax);
svo.setLatency(getItem(upload.getLatency(), index));
svo.setOptimized(getItem(upload.getOptimized(), index));
// Find the nicest storage
svo.setType(storageResource.lookup(context.quote, svo).stream().findFirst().orElseThrow(() -> new ValidationJsonException("storage", "NotNull")).getPrice().getType().getCode());
// Default the storage name to the instance name
svo.setSubscription(subscription);
return storageResource.create(svo).getId();
}).collect(Collectors.toList());
// Tags part
Arrays.stream(StringUtils.split(ObjectUtils.defaultIfNull(upload.getTags(), ""), ",;")).map(StringUtils::trimToNull).filter(Objects::nonNull).forEach(t -> {
// Instance tags
final var tag = new TagEditionVo();
final var parts = StringUtils.splitPreserveAllTokens(t + ":", ':');
tag.setName(parts[0].trim());
tag.setValue(StringUtils.trimToNull(parts[1]));
tag.setResource(id);
tag.setType(resourceType);
tagResource.create(subscription, tag);
// Storage tags
tag.setType(ResourceType.STORAGE);
disks.forEach(d -> {
tag.setResource(d);
tagResource.create(subscription, tag);
});
});
}
Aggregations