Search in sources :

Example 1 with ProvQuote

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

the class ProvResourceTest method updateLocation.

/**
 * Update the location of the quote, impact all instances, but no one use the default location. Cost still updated.
 */
@Test
public void updateLocation() {
    final ProvLocation location4 = locationRepository.findByName("region-4");
    // Make sure there is no more world wild prices
    em.createQuery("FROM ProvInstancePrice WHERE location IS NULL", ProvInstancePrice.class).getResultList().forEach(ip -> ip.setLocation(location4));
    em.flush();
    em.clear();
    final QuoteEditionVo quote = new QuoteEditionVo();
    quote.setName("name1");
    quote.setDescription("description1");
    quote.setLocation("region-1");
    final FloatingCost cost = resource.update(subscription, quote);
    checkCost(cost, 5799.465, 9669.918, false);
    ProvQuote quote2 = repository.findByNameExpected("name1");
    Assertions.assertEquals("description1", quote2.getDescription());
    // Check location
    final ProvLocation location = quote2.getLocation();
    Assertions.assertEquals("region-1", location.getName());
    Assertions.assertEquals("west", location.getPlacement());
    Assertions.assertEquals(840, location.getCountryM49().intValue());
    Assertions.assertEquals("US", location.getCountryA2());
    Assertions.assertEquals(21, location.getRegionM49().intValue());
    Assertions.assertEquals(19, location.getContinentM49().intValue());
    Assertions.assertEquals("Virginia", location.getSubRegion());
    // CHeck the association on the quote
    Assertions.assertEquals("region-1", resource.getConfiguration(subscription).getLocation().getName());
    // Check the "region-1" is the one related to our provider
    Assertions.assertEquals("service:prov:test", repository.findByName("name1").getLocation().getNode().getId());
}
Also used : ProvLocation(org.ligoj.app.plugin.prov.model.ProvLocation) ProvQuote(org.ligoj.app.plugin.prov.model.ProvQuote) AbstractAppTest(org.ligoj.app.AbstractAppTest) Test(org.junit.jupiter.api.Test)

Example 2 with ProvQuote

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

the class ProvResourceTest method checkCost.

private QuoteLigthVo checkCost(final int subscription, final double min, final double max, final boolean unbound) {
    final QuoteLigthVo status = resource.getSusbcriptionStatus(subscription);
    final ProvQuote quote = repository.findByNameExpected(status.getName());
    Assertions.assertSame(unbound, quote.isUnboundCost());
    Assertions.assertSame(quote, quote.getConfiguration());
    checkCost(status.getCost(), min, max, unbound);
    return status;
}
Also used : ProvQuote(org.ligoj.app.plugin.prov.model.ProvQuote)

Example 3 with ProvQuote

use of org.ligoj.app.plugin.prov.model.ProvQuote 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 4 with ProvQuote

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

the class ProvQuoteInstanceResource method lookup.

private QuoteInstanceLookup lookup(final ProvQuote configuration, final double cpu, final int ram, final Boolean constant, final VmOs osName, final String type, final boolean ephemeral, final String location, final String usageName) {
    final String node = configuration.getSubscription().getNode().getId();
    final int subscription = configuration.getSubscription().getId();
    // Resolve
    final VmOs os = Optional.ofNullable(osName).map(VmOs::toPricingOs).orElse(null);
    // Resolve the location to use
    final String locationR = location == null ? configuration.getLocation().getName() : location;
    // Compute the rate to use
    final ProvUsage usage = usageName == null ? ObjectUtils.defaultIfNull(configuration.getUsage(), USAGE_DEFAULT) : resource.findConfiguredByName(usageRepository, usageName, subscription);
    final double rate = usage.getRate() / 100d;
    final int duration = usage.getDuration();
    // Resolve the required instance type
    final Integer typeId = type == null ? null : assertFound(itRepository.findByName(subscription, type), type).getId();
    // Return only the first matching instance
    // Template instance
    final QuoteInstanceLookup template = ipRepository.findLowestPrice(node, cpu, ram, constant, os, typeId, ephemeral, locationR, rate, duration, PageRequest.of(0, 1)).stream().findFirst().map(ip -> newPrice((ProvInstancePrice) ip[0], (double) ip[2])).orElse(null);
    // Custom instance
    final QuoteInstanceLookup custom = ipRepository.findLowestCustomPrice(node, Math.ceil(cpu), Math.ceil(ram / 1024), constant, os, locationR, PageRequest.of(0, 1)).stream().findFirst().map(ip -> newPrice((ProvInstancePrice) ip[0], rate * (double) ip[1])).orElse(null);
    // Select the best price term
    if (template == null) {
        return custom;
    }
    if (custom == null) {
        return template;
    }
    return custom.getCost() < template.getCost() ? custom : template;
}
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) VmOs(org.ligoj.app.plugin.prov.model.VmOs) ProvUsage(org.ligoj.app.plugin.prov.model.ProvUsage)

Example 5 with ProvQuote

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

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