Search in sources :

Example 16 with ProvQuote

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

the class ProvResource method create.

@Override
public void create(final int subscription) throws Exception {
    // Add an empty quote
    final ProvQuote quote = new ProvQuote();
    quote.setSubscription(subscriptionRepository.findOne(subscription));
    // Associate a default name and description
    quote.setName(quote.getSubscription().getProject().getName());
    final Node provider = quote.getSubscription().getNode().getRefined();
    final List<ProvLocation> locations = locationRepository.findAllBy("node.id", provider.getId());
    if (locations.isEmpty()) {
        // No available location, need a catalog to continue
        throw new BusinessException(SERVICE_KEY + "-no-catalog", provider.getId(), provider.getName());
    }
    quote.setLocation(locations.get(0));
    quote.setDescription(quote.getSubscription().getProject().getPkey() + "-> " + provider.getName());
    repository.saveAndFlush(quote);
}
Also used : BusinessException(org.ligoj.bootstrap.core.resource.BusinessException) ProvLocation(org.ligoj.app.plugin.prov.model.ProvLocation) ProvQuote(org.ligoj.app.plugin.prov.model.ProvQuote) Node(org.ligoj.app.model.Node)

Example 17 with ProvQuote

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

the class ProvResource method getConfiguration.

/**
 * Return the quote configuration from a validated subscription.
 *
 * @param subscription
 *            A visible subscription for the current principal.
 * @return The configuration with computed data.
 */
public QuoteVo getConfiguration(final Subscription subscription) {
    final QuoteVo vo = new QuoteVo();
    final ProvQuote entity = repository.getCompute(subscription.getId());
    DescribedBean.copy(entity, vo);
    vo.copyAuditData(entity, toUser());
    vo.setLocation(entity.getLocation());
    vo.setInstances(entity.getInstances());
    vo.setStorages(repository.getStorage(subscription.getId()));
    vo.setUsage(entity.getUsage());
    // Also copy the pre-computed cost
    vo.setCost(toFloatingCost(entity));
    return vo;
}
Also used : ProvQuote(org.ligoj.app.plugin.prov.model.ProvQuote)

Example 18 with ProvQuote

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

the class ProvResource method getSusbcriptionStatus.

/**
 * Return the quote status linked to given subscription.
 *
 * @param subscription
 *            The parent subscription identifier.
 * @return The quote status (summary only) linked to given subscription.
 */
public QuoteLigthVo getSusbcriptionStatus(final int subscription) {
    final QuoteLigthVo vo = new QuoteLigthVo();
    final Object[] compute = repository.getComputeSummary(subscription).get(0);
    final Object[] storage = repository.getStorageSummary(subscription).get(0);
    final ProvQuote entity = (ProvQuote) compute[0];
    DescribedBean.copy(entity, vo);
    vo.setCost(toFloatingCost(entity));
    vo.setNbInstances(((Long) compute[1]).intValue());
    vo.setTotalCpu((Double) compute[2]);
    vo.setTotalRam(((Long) compute[3]).intValue());
    vo.setNbPublicAccess(((Long) compute[4]).intValue());
    vo.setNbStorages(((Long) storage[1]).intValue());
    vo.setTotalStorage(((Long) storage[2]).intValue());
    vo.setLocation(entity.getLocation());
    return vo;
}
Also used : ProvQuote(org.ligoj.app.plugin.prov.model.ProvQuote)

Example 19 with ProvQuote

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

the class ProvResourceTest method updateLocationDifferentQILocation.

/**
 * Update the location of the quote, impact all instances, but no one use the default location. Cost still updated.
 */
@Test
public void updateLocationDifferentQILocation() {
    final ProvLocation location = locationRepository.findByName("region-1");
    final ProvLocation location4 = locationRepository.findByName("region-4");
    // Change the required location of all quote instance
    qiRepository.findAll().forEach(ip -> ip.setLocation(location));
    // Make sure there is no more world wild prices
    em.createQuery("FROM ProvInstancePrice WHERE location.name=:location", ProvInstancePrice.class).setParameter("location", "region-1").getResultList().forEach(ip -> ip.setLocation(location4));
    em.createQuery("FROM ProvStoragePrice WHERE location.name=:location", ProvStoragePrice.class).setParameter("location", "region-1").getResultList().forEach(ip -> ip.setLocation(location4));
    em.createQuery("FROM ProvQuoteInstance WHERE location.name=:location", ProvQuoteInstance.class).setParameter("location", "region-1").getResultList().forEach(ip -> ip.setLocation(location4));
    em.flush();
    em.clear();
    // New cost based on region-4
    final QuoteEditionVo quote = new QuoteEditionVo();
    quote.setName("name1");
    quote.setDescription("description1");
    quote.setLocation("region-4");
    final FloatingCost cost = resource.update(subscription, quote);
    checkCost(cost, 3165.4, 5615.0, false);
    final ProvQuote quote2 = repository.findByNameExpected("name1");
    Assertions.assertEquals("description1", quote2.getDescription());
    Assertions.assertEquals("region-4", quote2.getLocation().getName());
}
Also used : ProvLocation(org.ligoj.app.plugin.prov.model.ProvLocation) ProvInstancePrice(org.ligoj.app.plugin.prov.model.ProvInstancePrice) ProvQuoteInstance(org.ligoj.app.plugin.prov.model.ProvQuoteInstance) ProvQuote(org.ligoj.app.plugin.prov.model.ProvQuote) ProvStoragePrice(org.ligoj.app.plugin.prov.model.ProvStoragePrice) AbstractAppTest(org.ligoj.app.AbstractAppTest) Test(org.junit.jupiter.api.Test)

Example 20 with ProvQuote

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

the class ProvQuoteUsageResourceTest method deleteUnused.

@Test
public void deleteUnused() {
    final ProvQuote quote = repository.findByName("quote1");
    quote.setUsage(usageRepository.findByName("Full Time"));
    em.persist(quote);
    em.flush();
    em.clear();
    checkCost(resource.refresh(subscription), 3165.4, 5615.0, false);
    Assertions.assertNotNull(usageRepository.findByName(subscription, "Dev"));
    Assertions.assertEquals(2, usageRepository.findAllBy("name", "Dev").size());
    // Delete the usage
    // Check the cost is at 100%
    checkUsage100AfterDelete(uResource.delete(subscription, "Dev"));
}
Also used : ProvQuote(org.ligoj.app.plugin.prov.model.ProvQuote) AbstractAppTest(org.ligoj.app.AbstractAppTest) Test(org.junit.jupiter.api.Test)

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