use of org.ligoj.app.plugin.prov.model.ProvQuote in project plugin-prov by ligoj.
the class ProvUsageResource method create.
/**
* Create the usage inside a quote. No cost are updated during this operation since this new {@link ProvUsage} is
* not yet used.
*
* @param subscription
* The subscription identifier, will be used to filter the usages from the associated provider.
* @param vo
* The quote usage.
* @return The created usage identifier.
*/
@POST
@Path("{subscription:\\d+}/usage")
@Consumes(MediaType.APPLICATION_JSON)
public int create(@PathParam("subscription") final int subscription, final UsageEditionVo vo) {
final ProvQuote configuration = resource.getQuoteFromSubscription(subscription);
final ProvUsage entity = new ProvUsage();
entity.setConfiguration(configuration);
return saveOrUpdate(entity, vo).getId();
}
use of org.ligoj.app.plugin.prov.model.ProvQuote in project plugin-prov by ligoj.
the class ProvUsageResource method saveOrUpdate.
/**
* Save or update the given usage entity from the {@link UsageEditionVo}. The computed cost are recursively updated
* from the related instances to the quote total cost.<br>
* The cost of all instances related to this usage will be updated to get the new term and related price.<br>
* An instance related to this usage is either an instance explicitly linked to this usage, either an instance
* linked to a quote having this usage as default.
*/
private UpdatedCost saveOrUpdate(final ProvUsage entity, final UsageEditionVo vo) {
// Check the associations and copy attributes to the entity
entity.setRate(vo.getRate());
entity.setDuration(vo.getDuration());
entity.setName(vo.getName());
final UpdatedCost cost = new UpdatedCost();
if (entity.getId() != null) {
final ProvQuote quote = entity.getConfiguration();
// Prepare the updated cost of updated instances
final Map<Integer, FloatingCost> costs = cost.getRelatedCosts();
cost.setRelatedCosts(costs);
// Update the cost of all related instances
if (entity.equals(quote.getUsage())) {
// Update cost of all instances without explicit usage
quote.getInstances().stream().filter(i -> i.getUsage() == null).forEach(i -> costs.put(i.getId(), instanceResource.addCost(i, instanceResource::refresh)));
}
quote.getInstances().stream().filter(i -> entity.equals(i.getUsage())).forEach(i -> costs.put(i.getId(), instanceResource.addCost(i, instanceResource::refresh)));
// Save and update the costs
cost.setRelatedCosts(costs);
}
repository.saveAndFlush(entity);
cost.setId(entity.getId());
cost.setTotalCost(resource.toFloatingCost(entity.getConfiguration()));
return cost;
}
use of org.ligoj.app.plugin.prov.model.ProvQuote in project plugin-prov by ligoj.
the class ProvQuoteInstanceResource method delete.
/**
* Delete an instance from a quote. The total cost is updated.
*
* @param id
* The {@link ProvQuoteInstance}'s identifier to delete.
* @return The updated computed cost.
*/
@DELETE
@Path("instance/{id:\\d+}")
@Consumes(MediaType.APPLICATION_JSON)
public FloatingCost delete(@PathParam("id") final int id) {
// Delete the instance and also the attached storage
return deleteAndUpdateCost(qiRepository, id, i -> {
// Delete the relate storages
i.getStorages().forEach(s -> deleteAndUpdateCost(qsRepository, s.getId(), Function.identity()::apply));
// Decrement the unbound counter
final ProvQuote configuration = i.getConfiguration();
configuration.setUnboundCostCounter(configuration.getUnboundCostCounter() - BooleanUtils.toInteger(i.isUnboundCost()));
});
}
use of org.ligoj.app.plugin.prov.model.ProvQuote in project plugin-prov by ligoj.
the class ProvQuoteStorageResource method saveOrUpdate.
/**
* Save or update the storage inside a quote.
*
* @param entity
* The storage entity to update.
* @param vo
* The new quote storage data to persist.
* @return The formal entity.
*/
private UpdatedCost saveOrUpdate(final ProvQuoteStorage entity, final QuoteStorageEditionVo vo) {
DescribedBean.copy(vo, entity);
// Check the associations
final int subscription = vo.getSubscription();
final ProvQuote quote = getQuoteFromSubscription(subscription);
final String node = quote.getSubscription().getNode().getRefined().getId();
entity.setConfiguration(quote);
entity.setLocation(resource.findLocation(node, vo.getLocation()));
entity.setPrice(findByTypeName(subscription, vo.getType(), vo.getLocation(), quote));
entity.setInstanceCompatible(vo.getInstanceCompatible());
entity.setLatency(vo.getLatency());
entity.setOptimized(vo.getOptimized());
entity.setSize(vo.getSize());
entity.setQuoteInstance(checkInstance(node, vo.getQuoteInstance()));
// Check the storage requirements to validate the linked price
final ProvStorageType type = entity.getPrice().getType();
if (!lookup(quote, entity.getSize(), entity.getLatency(), vo.getQuoteInstance(), entity.getOptimized(), vo.getLocation()).stream().map(qs -> qs.getPrice().getType()).anyMatch(type::equals)) {
// The related storage type does not match these requirements
throw new ValidationJsonException("type", "type-incompatible-requirements", type.getName());
}
// Save and update the costs
final UpdatedCost cost = refreshCost(entity);
Optional.ofNullable(entity.getQuoteInstance()).ifPresent(q -> cost.setRelatedCosts(Collections.singletonMap(q.getId(), qiResource.updateCost(q))));
return cost;
}
use of org.ligoj.app.plugin.prov.model.ProvQuote in project plugin-prov by ligoj.
the class ProvQuoteStorageResource method refresh.
@Override
public FloatingCost refresh(final ProvQuoteStorage qs) {
final ProvQuote quote = qs.getConfiguration();
// Find the lowest price
final Integer qi = Optional.ofNullable(qs.getQuoteInstance()).map(ProvQuoteInstance::getId).orElse(null);
final String location = Optional.ofNullable(qs.getLocation()).map(INamableBean::getName).orElse(null);
qs.setPrice(validateLookup("storage", lookup(quote, qs.getSize(), qs.getLatency(), qi, qs.getOptimized(), location).stream().findFirst().orElse(null), qs.getName()));
return updateCost(qs);
}
Aggregations