Search in sources :

Example 11 with ProvQuote

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

Example 12 with ProvQuote

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

Example 13 with ProvQuote

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

Example 14 with ProvQuote

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

Example 15 with ProvQuote

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);
}
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