Search in sources :

Example 6 with ProvQuoteInstance

use of org.ligoj.app.plugin.prov.model.ProvQuoteInstance in project plugin-prov by ligoj.

the class ProvQuoteStorageResourceTest method createStorageUnboundInstance.

@Test
public void createStorageUnboundInstance() {
    final ProvQuoteInstance quoteInstance = setUnboundInstance("server1");
    // Attach the new storage to this unbound instance
    final QuoteStorageEditionVo vo = new QuoteStorageEditionVo();
    vo.setSubscription(subscription);
    vo.setName("server1-root-bis");
    vo.setType("storage1");
    vo.setQuoteInstance(quoteInstance.getId());
    vo.setSize(512);
    final UpdatedCost cost = qsResource.create(vo);
    checkCost(cost.getTotalCost(), 4919.798, 4919.798, true);
    checkCost(cost.getResourceCost(), 215.04, 215.04, true);
    Assertions.assertEquals(1, cost.getRelatedCosts().size());
    checkCost(cost.getRelatedCosts().get(vo.getQuoteInstance()), 292.8, 292.8, true);
    final int id = cost.getId();
    em.flush();
    em.clear();
    // Check the exact new cost
    checkCost(subscription, 4919.798, 4919.798, true);
    final ProvQuoteStorage storage = qsRepository.findOneExpected(id);
    Assertions.assertEquals("server1-root-bis", storage.getName());
    Assertions.assertEquals(512, storage.getSize().intValue());
    Assertions.assertEquals(vo.getType(), storage.getPrice().getType().getName());
    Assertions.assertEquals(215.04, storage.getCost(), DELTA);
    Assertions.assertTrue(storage.isUnboundCost());
}
Also used : ProvQuoteStorage(org.ligoj.app.plugin.prov.model.ProvQuoteStorage) ProvQuoteInstance(org.ligoj.app.plugin.prov.model.ProvQuoteInstance) AbstractAppTest(org.ligoj.app.AbstractAppTest) Test(org.junit.jupiter.api.Test)

Example 7 with ProvQuoteInstance

use of org.ligoj.app.plugin.prov.model.ProvQuoteInstance in project plugin-prov by ligoj.

the class ProvResourceTest method findConfigured.

@Test
public void findConfigured() {
    final ProvQuoteInstance qi = qiRepository.findByName("server1");
    Assertions.assertEquals("server1", resource.findConfigured(qiRepository, qi.getId(), subscription).getName());
}
Also used : ProvQuoteInstance(org.ligoj.app.plugin.prov.model.ProvQuoteInstance) AbstractAppTest(org.ligoj.app.AbstractAppTest) Test(org.junit.jupiter.api.Test)

Example 8 with ProvQuoteInstance

use of org.ligoj.app.plugin.prov.model.ProvQuoteInstance in project plugin-prov by ligoj.

the class ProvQuoteUsageResourceTest method deleteUsedInQuote.

@Test
public void deleteUsedInQuote() {
    // Usage = 100% by default and 50% fixed for "server1"
    final ProvQuoteInstance server1 = qiRepository.findByName("server1");
    server1.setUsage(usageRepository.findByName("Dev"));
    em.persist(server1);
    checkCost(resource.refresh(subscription), 3019.0, 4883.0, false);
    // Usage = 50% by default and 50% fixed for "server1"
    final ProvQuote quote = repository.findByName("quote1");
    quote.setUsage(usageRepository.findByName("Dev"));
    em.persist(quote);
    checkCost(resource.refresh(subscription), 1743.865, 3607.865, false);
    // Delete the usage
    // Check the cost is now back at 100%
    checkUsage100AfterDelete(uResource.delete(subscription, "Dev"));
}
Also used : ProvQuoteInstance(org.ligoj.app.plugin.prov.model.ProvQuoteInstance) ProvQuote(org.ligoj.app.plugin.prov.model.ProvQuote) AbstractAppTest(org.ligoj.app.AbstractAppTest) Test(org.junit.jupiter.api.Test)

Example 9 with ProvQuoteInstance

use of org.ligoj.app.plugin.prov.model.ProvQuoteInstance in project plugin-prov by ligoj.

the class ProvQuoteStorageResource method lookup.

private List<QuoteStorageLoopup> lookup(final ProvQuote configuration, final int size, final Rate latency, final Integer instance, final ProvStorageOptimized optimized, final String location) {
    // Get the attached node and check the security on this subscription
    final String node = configuration.getSubscription().getNode().getRefined().getId();
    final ProvQuoteInstance qi = checkInstance(node, instance);
    // The the right location from instance first, then the request one
    String iLocation = Optional.ofNullable(qi).map(qiResource::getLocation).map(ProvLocation::getName).orElse(location);
    iLocation = ObjectUtils.defaultIfNull(iLocation, configuration.getLocation().getName());
    if (location != null && !location.equals(iLocation)) {
        // Not compatible locations
        return Collections.emptyList();
    }
    return spRepository.findLowestPrice(node, size, latency, instance, optimized, iLocation, PageRequest.of(0, 10)).stream().map(spx -> (ProvStoragePrice) spx[0]).map(sp -> newPrice(sp, size, getCost(sp, size))).collect(Collectors.toList());
}
Also used : ProvStorageType(org.ligoj.app.plugin.prov.model.ProvStorageType) PathParam(javax.ws.rs.PathParam) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Path(javax.ws.rs.Path) Autowired(org.springframework.beans.factory.annotation.Autowired) ProvQuoteStorage(org.ligoj.app.plugin.prov.model.ProvQuoteStorage) ProvQuoteInstance(org.ligoj.app.plugin.prov.model.ProvQuoteInstance) Function(java.util.function.Function) ValidationJsonException(org.ligoj.bootstrap.core.validation.ValidationJsonException) MediaType(javax.ws.rs.core.MediaType) QueryParam(javax.ws.rs.QueryParam) ServicePluginLocator(org.ligoj.app.resource.ServicePluginLocator) Consumes(javax.ws.rs.Consumes) ProvLocation(org.ligoj.app.plugin.prov.model.ProvLocation) ObjectUtils(org.apache.commons.lang3.ObjectUtils) DataTableAttributes(org.ligoj.bootstrap.core.json.datatable.DataTableAttributes) Service(org.springframework.stereotype.Service) ProvStorageTypeRepository(org.ligoj.app.plugin.prov.dao.ProvStorageTypeRepository) DefaultValue(javax.ws.rs.DefaultValue) Rate(org.ligoj.app.plugin.prov.model.Rate) DescribedBean(org.ligoj.bootstrap.core.DescribedBean) DELETE(javax.ws.rs.DELETE) INamableBean(org.ligoj.bootstrap.core.INamableBean) POST(javax.ws.rs.POST) Context(javax.ws.rs.core.Context) Transactional(javax.transaction.Transactional) ProvQuoteInstanceRepository(org.ligoj.app.plugin.prov.dao.ProvQuoteInstanceRepository) PageRequest(org.springframework.data.domain.PageRequest) Collectors(java.util.stream.Collectors) ProvInstancePrice(org.ligoj.app.plugin.prov.model.ProvInstancePrice) ProvQuoteStorageRepository(org.ligoj.app.plugin.prov.dao.ProvQuoteStorageRepository) List(java.util.List) Optional(java.util.Optional) PUT(javax.ws.rs.PUT) ProvQuote(org.ligoj.app.plugin.prov.model.ProvQuote) ProvStoragePrice(org.ligoj.app.plugin.prov.model.ProvStoragePrice) UriInfo(javax.ws.rs.core.UriInfo) TableItem(org.ligoj.bootstrap.core.json.TableItem) Collections(java.util.Collections) ProvStorageOptimized(org.ligoj.app.plugin.prov.model.ProvStorageOptimized) ProvStoragePriceRepository(org.ligoj.app.plugin.prov.dao.ProvStoragePriceRepository) ProvQuoteInstance(org.ligoj.app.plugin.prov.model.ProvQuoteInstance)

Example 10 with ProvQuoteInstance

use of org.ligoj.app.plugin.prov.model.ProvQuoteInstance 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)

Aggregations

ProvQuoteInstance (org.ligoj.app.plugin.prov.model.ProvQuoteInstance)19 Test (org.junit.jupiter.api.Test)15 AbstractAppTest (org.ligoj.app.AbstractAppTest)14 ProvInstancePrice (org.ligoj.app.plugin.prov.model.ProvInstancePrice)5 ProvQuote (org.ligoj.app.plugin.prov.model.ProvQuote)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ProvQuoteStorage (org.ligoj.app.plugin.prov.model.ProvQuoteStorage)4 ProvInstanceType (org.ligoj.app.plugin.prov.model.ProvInstanceType)3 ProvLocation (org.ligoj.app.plugin.prov.model.ProvLocation)3 ProvStoragePrice (org.ligoj.app.plugin.prov.model.ProvStoragePrice)3 Optional (java.util.Optional)2 Function (java.util.function.Function)2 Transactional (javax.transaction.Transactional)2 Consumes (javax.ws.rs.Consumes)2 DELETE (javax.ws.rs.DELETE)2 DefaultValue (javax.ws.rs.DefaultValue)2 GET (javax.ws.rs.GET)2 POST (javax.ws.rs.POST)2 PUT (javax.ws.rs.PUT)2 Path (javax.ws.rs.Path)2