use of org.ligoj.app.plugin.prov.model.ProvQuote in project plugin-prov by ligoj.
the class ProvQuoteInstanceResource method saveOrUpdate.
/**
* Save or update the given entity from the {@link QuoteInstanceEditionVo}. The computed cost are recursively
* updated from the instance to the quote total cost.
*/
private UpdatedCost saveOrUpdate(final ProvQuoteInstance entity, final QuoteInstanceEditionVo vo) {
// Compute the unbound cost delta
final int deltaUnbound = BooleanUtils.toInteger(vo.getMaxQuantity() == null) - BooleanUtils.toInteger(entity.isUnboundCost());
// Check the associations and copy attributes to the entity
final ProvQuote configuration = getQuoteFromSubscription(vo.getSubscription());
final Subscription subscription = configuration.getSubscription();
final String providerId = subscription.getNode().getRefined().getId();
DescribedBean.copy(vo, entity);
entity.setConfiguration(configuration);
final ProvLocation oldLocation = getLocation(entity);
entity.setPrice(ipRepository.findOneExpected(vo.getPrice()));
entity.setLocation(resource.findLocation(providerId, vo.getLocation()));
entity.setUsage(Optional.ofNullable(vo.getUsage()).map(u -> resource.findConfiguredByName(usageRepository, u, subscription.getId())).orElse(null));
entity.setOs(ObjectUtils.defaultIfNull(vo.getOs(), entity.getPrice().getOs()));
entity.setRam(vo.getRam());
entity.setCpu(vo.getCpu());
entity.setConstant(vo.getConstant());
entity.setEphemeral(vo.isEphemeral());
entity.setInternet(vo.getInternet());
entity.setMaxVariableCost(vo.getMaxVariableCost());
entity.setMinQuantity(vo.getMinQuantity());
entity.setMaxQuantity(vo.getMaxQuantity());
resource.checkVisibility(entity.getPrice().getType(), providerId);
checkConstraints(entity);
checkOs(entity);
// Update the unbound increment of the global quote
configuration.setUnboundCostCounter(configuration.getUnboundCostCounter() + deltaUnbound);
// Save and update the costs
final UpdatedCost cost = newUpdateCost(entity);
final Map<Integer, FloatingCost> storagesCosts = new HashMap<>();
final boolean dirtyPrice = !oldLocation.equals(getLocation(entity));
CollectionUtils.emptyIfNull(entity.getStorages()).stream().peek(s -> {
if (dirtyPrice) {
// Location has changed, the available storage price is invalidated
storageResource.refresh(s);
storageResource.refreshCost(s);
}
}).forEach(s -> storagesCosts.put(s.getId(), addCost(s, storageResource::updateCost)));
cost.setRelatedCosts(storagesCosts);
cost.setTotalCost(toFloatingCost(entity.getConfiguration()));
return cost;
}
use of org.ligoj.app.plugin.prov.model.ProvQuote in project plugin-prov by ligoj.
the class ProvUsageResource method delete.
/**
* Delete an usage. When the usage is associated to a quote or a resource, it is replaced by a <code>null</code>
* reference.
*
* @param subscription
* The subscription identifier, will be used to filter the usages from the associated provider.
* @param name
* The {@link ProvUsage} name.
* @return The updated cost. Only relevant when at least one resource was associated to this usage.
*/
@DELETE
@Consumes(MediaType.APPLICATION_JSON)
@Path("{subscription:\\d+}/usage/{name}")
public UpdatedCost delete(@PathParam("subscription") final int subscription, @PathParam("name") final String name) {
final ProvUsage entity = resource.findConfiguredByName(repository, name, subscription);
final ProvQuote quote = entity.getConfiguration();
final UpdatedCost cost = new UpdatedCost();
// 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.setUsage(null);
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())).peek(i -> i.setUsage(null)).forEach(i -> costs.put(i.getId(), instanceResource.addCost(i, instanceResource::refresh)));
// All references are deleted, delete the usage entity
repository.delete(entity);
// Save and update the costs
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 ProvResource method update.
/**
* Update the configuration details. The costs and the related resources are refreshed with lookups.
*
* @param subscription
* The subscription to update
* @param quote
* The new quote.
* @return The new updated cost.
*/
@PUT
@Path("{subscription:\\d+}")
@Consumes(MediaType.APPLICATION_JSON)
public FloatingCost update(@PathParam("subscription") final int subscription, final QuoteEditionVo quote) {
final ProvQuote entity = getQuoteFromSubscription(subscription);
entity.setName(quote.getName());
entity.setDescription(quote.getDescription());
// TODO Check the location/usage change to avoid useless compute
entity.setLocation(findLocation(entity.getSubscription().getNode().getId(), quote.getLocation()));
entity.setUsage(Optional.ofNullable(quote.getUsage()).map(u -> findConfiguredByName(usageRepository, u, subscription)).orElse(null));
return refresh(entity);
}
use of org.ligoj.app.plugin.prov.model.ProvQuote in project plugin-prov by ligoj.
the class QuoteRelated method addCost.
/**
* Add a cost to the quote related to given resource entity. The global cost is not deeply computed, only delta is
* applied.
*
* @param entity
* The configured entity, related to a quote.
* @param costUpdater
* The function used to compute the new cost.
* @param <T>
* The entity type holding the cost.
* @return The new computed cost.
*/
default <T extends Costed> FloatingCost addCost(final T entity, final Function<T, FloatingCost> costUpdater) {
// Save the previous costs
final double oldCost = ObjectUtils.defaultIfNull(entity.getCost(), 0d);
final double oldMaxCost = ObjectUtils.defaultIfNull(entity.getMaxCost(), 0d);
// Process the update of this entity
final FloatingCost newCost = costUpdater.apply(entity);
// Report the delta to the quote
final ProvQuote configuration = entity.getConfiguration();
configuration.setCost(round(configuration.getCost() + entity.getCost() - oldCost));
configuration.setMaxCost(round(configuration.getMaxCost() + entity.getMaxCost() - oldMaxCost));
return newCost;
}
use of org.ligoj.app.plugin.prov.model.ProvQuote in project plugin-prov by ligoj.
the class ProvQuoteInstanceResource method refresh.
@Override
public FloatingCost refresh(final ProvQuoteInstance qi) {
final ProvQuote quote = qi.getConfiguration();
// Find the lowest price
qi.setPrice(validateLookup("instance", lookup(quote, qi.getCpu(), qi.getRam(), qi.getConstant(), qi.getOs(), null, qi.isEphemeral(), getLocation(qi).getName(), getUsageName(qi)), qi.getName()));
return updateCost(qi);
}
Aggregations