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());
}
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;
}
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"));
}
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;
}
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());
}
Aggregations