Search in sources :

Example 6 with ProvQuote

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;
}
Also used : CsvForBean(org.ligoj.bootstrap.core.csv.CsvForBean) Produces(javax.ws.rs.Produces) ProvInstancePriceRepository(org.ligoj.app.plugin.prov.dao.ProvInstancePriceRepository) Path(javax.ws.rs.Path) ProvUsageRepository(org.ligoj.app.plugin.prov.dao.ProvUsageRepository) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) ValidationJsonException(org.ligoj.bootstrap.core.validation.ValidationJsonException) MediaType(javax.ws.rs.core.MediaType) QueryParam(javax.ws.rs.QueryParam) ByteArrayInputStream(java.io.ByteArrayInputStream) Consumes(javax.ws.rs.Consumes) ProvLocation(org.ligoj.app.plugin.prov.model.ProvLocation) ProvInstanceTypeRepository(org.ligoj.app.plugin.prov.dao.ProvInstanceTypeRepository) DataTableAttributes(org.ligoj.bootstrap.core.json.datatable.DataTableAttributes) Map(java.util.Map) Multipart(org.apache.cxf.jaxrs.ext.multipart.Multipart) DefaultValue(javax.ws.rs.DefaultValue) Subscription(org.ligoj.app.model.Subscription) DescribedBean(org.ligoj.bootstrap.core.DescribedBean) DELETE(javax.ws.rs.DELETE) Context(javax.ws.rs.core.Context) Transactional(javax.transaction.Transactional) PageRequest(org.springframework.data.domain.PageRequest) Reader(java.io.Reader) StandardCharsets(java.nio.charset.StandardCharsets) ProvInstancePrice(org.ligoj.app.plugin.prov.model.ProvInstancePrice) Objects(java.util.Objects) ProvQuoteStorageRepository(org.ligoj.app.plugin.prov.dao.ProvQuoteStorageRepository) IOUtils(org.apache.commons.io.IOUtils) Slf4j(lombok.extern.slf4j.Slf4j) Optional(java.util.Optional) UriInfo(javax.ws.rs.core.UriInfo) TableItem(org.ligoj.bootstrap.core.json.TableItem) PathParam(javax.ws.rs.PathParam) GET(javax.ws.rs.GET) HashMap(java.util.HashMap) ArrayUtils(org.apache.commons.lang3.ArrayUtils) BooleanUtils(org.apache.commons.lang3.BooleanUtils) ProvQuoteInstance(org.ligoj.app.plugin.prov.model.ProvQuoteInstance) Function(java.util.function.Function) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ObjectUtils(org.apache.commons.lang3.ObjectUtils) Service(org.springframework.stereotype.Service) ProvUsage(org.ligoj.app.plugin.prov.model.ProvUsage) POST(javax.ws.rs.POST) SequenceInputStream(java.io.SequenceInputStream) ProvQuoteInstanceRepository(org.ligoj.app.plugin.prov.dao.ProvQuoteInstanceRepository) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) ProvInstancePriceTermRepository(org.ligoj.app.plugin.prov.dao.ProvInstancePriceTermRepository) VmOs(org.ligoj.app.plugin.prov.model.VmOs) StringReader(java.io.StringReader) ProvInstancePriceTerm(org.ligoj.app.plugin.prov.model.ProvInstancePriceTerm) PUT(javax.ws.rs.PUT) ProvQuote(org.ligoj.app.plugin.prov.model.ProvQuote) BufferedReader(java.io.BufferedReader) ProvInstanceType(org.ligoj.app.plugin.prov.model.ProvInstanceType) InputStream(java.io.InputStream) HashMap(java.util.HashMap) ProvLocation(org.ligoj.app.plugin.prov.model.ProvLocation) ProvQuote(org.ligoj.app.plugin.prov.model.ProvQuote) Subscription(org.ligoj.app.model.Subscription)

Example 7 with ProvQuote

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;
}
Also used : PathParam(javax.ws.rs.PathParam) POST(javax.ws.rs.POST) Context(javax.ws.rs.core.Context) Produces(javax.ws.rs.Produces) Transactional(javax.transaction.Transactional) GET(javax.ws.rs.GET) Path(javax.ws.rs.Path) ProvUsageRepository(org.ligoj.app.plugin.prov.dao.ProvUsageRepository) Autowired(org.springframework.beans.factory.annotation.Autowired) SubscriptionResource(org.ligoj.app.resource.subscription.SubscriptionResource) Function(java.util.function.Function) MediaType(javax.ws.rs.core.MediaType) Consumes(javax.ws.rs.Consumes) DataTableAttributes(org.ligoj.bootstrap.core.json.datatable.DataTableAttributes) Service(org.springframework.stereotype.Service) Map(java.util.Map) PUT(javax.ws.rs.PUT) ProvQuote(org.ligoj.app.plugin.prov.model.ProvQuote) UriInfo(javax.ws.rs.core.UriInfo) ProvUsage(org.ligoj.app.plugin.prov.model.ProvUsage) TableItem(org.ligoj.bootstrap.core.json.TableItem) PaginationJson(org.ligoj.bootstrap.core.json.PaginationJson) DELETE(javax.ws.rs.DELETE) ProvUsage(org.ligoj.app.plugin.prov.model.ProvUsage) ProvQuote(org.ligoj.app.plugin.prov.model.ProvQuote) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Consumes(javax.ws.rs.Consumes)

Example 8 with ProvQuote

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);
}
Also used : ProvQuote(org.ligoj.app.plugin.prov.model.ProvQuote) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Example 9 with ProvQuote

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;
}
Also used : ProvQuote(org.ligoj.app.plugin.prov.model.ProvQuote)

Example 10 with ProvQuote

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);
}
Also used : ProvQuote(org.ligoj.app.plugin.prov.model.ProvQuote)

Aggregations

ProvQuote (org.ligoj.app.plugin.prov.model.ProvQuote)22 Consumes (javax.ws.rs.Consumes)8 Path (javax.ws.rs.Path)8 DELETE (javax.ws.rs.DELETE)6 POST (javax.ws.rs.POST)6 PUT (javax.ws.rs.PUT)6 ProvLocation (org.ligoj.app.plugin.prov.model.ProvLocation)6 ProvQuoteInstance (org.ligoj.app.plugin.prov.model.ProvQuoteInstance)6 Function (java.util.function.Function)5 Transactional (javax.transaction.Transactional)5 GET (javax.ws.rs.GET)5 PathParam (javax.ws.rs.PathParam)5 Produces (javax.ws.rs.Produces)5 Context (javax.ws.rs.core.Context)5 MediaType (javax.ws.rs.core.MediaType)5 UriInfo (javax.ws.rs.core.UriInfo)5 ProvUsage (org.ligoj.app.plugin.prov.model.ProvUsage)5 TableItem (org.ligoj.bootstrap.core.json.TableItem)5 DataTableAttributes (org.ligoj.bootstrap.core.json.datatable.DataTableAttributes)5 Autowired (org.springframework.beans.factory.annotation.Autowired)5