use of org.ligoj.app.plugin.prov.model.ProvInstancePrice in project plugin-prov by ligoj.
the class ProvQuoteInstanceResourceTest method lookupInstanceHighContraints.
/**
* Advanced case, all requirements.
*/
@Test
public void lookupInstanceHighContraints() throws IOException {
final QuoteInstanceLookup lookup = new ObjectMapperTrim().readValue(new ObjectMapperTrim().writeValueAsString(qiResource.lookup(subscription, 3, 9, true, VmOs.WINDOWS, null, false, null, "Full Time 12 month")), QuoteInstanceLookup.class);
final ProvInstancePrice pi = lookup.getPrice();
Assertions.assertNotNull(pi.getId());
Assertions.assertEquals("instance9", pi.getType().getName());
Assertions.assertEquals(4, pi.getType().getCpu().intValue());
Assertions.assertEquals(16000, pi.getType().getRam().intValue());
Assertions.assertTrue(pi.getType().getConstant());
Assertions.assertEquals(2928.0, pi.getCost(), DELTA);
Assertions.assertEquals(VmOs.WINDOWS, pi.getOs());
Assertions.assertEquals("1y", pi.getTerm().getName());
Assertions.assertFalse(pi.getTerm().isEphemeral());
Assertions.assertFalse(pi.getType().isCustom());
// Not serialized
Assertions.assertNull(pi.getType().getNode());
Assertions.assertNull(pi.getTerm().getNode());
}
use of org.ligoj.app.plugin.prov.model.ProvInstancePrice in project plugin-prov by ligoj.
the class ProvQuoteInstanceResource method persist.
private void persist(final InstanceUpload upload, final int subscription, String usage, final Integer ramMultiplier) {
final QuoteInstanceEditionVo vo = new QuoteInstanceEditionVo();
vo.setCpu(round(ObjectUtils.defaultIfNull(upload.getCpu(), 0d)));
vo.setEphemeral(upload.isEphemeral());
vo.setInternet(upload.getInternet());
vo.setMaxVariableCost(upload.getMaxVariableCost());
vo.setMaxQuantity(Optional.ofNullable(upload.getMaxQuantity()).map(q -> q <= 0 ? null : q).orElse(null));
vo.setMinQuantity(upload.getMinQuantity());
vo.setName(upload.getName());
vo.setLocation(upload.getLocation());
vo.setUsage(Optional.ofNullable(upload.getUsage()).map(u -> resource.findConfiguredByName(usageRepository, u, subscription).getName()).orElse(null));
vo.setRam(ObjectUtils.defaultIfNull(ramMultiplier, 1) * ObjectUtils.defaultIfNull(upload.getRam(), 0).intValue());
vo.setSubscription(subscription);
// Find the lowest price
final ProvInstancePrice instancePrice = validateLookup("instance", lookup(subscription, vo.getCpu(), vo.getRam(), upload.getConstant(), upload.getOs(), upload.getType(), upload.isEphemeral(), upload.getLocation(), upload.getUsage()), upload.getName());
vo.setPrice(instancePrice.getId());
final UpdatedCost newInstance = create(vo);
final int qi = newInstance.getId();
// Storage part
final Integer size = Optional.ofNullable(upload.getDisk()).map(Double::intValue).orElse(0);
if (size > 0) {
// Size is provided, propagate the upload properties
final QuoteStorageEditionVo svo = new QuoteStorageEditionVo();
svo.setName(vo.getName());
svo.setQuoteInstance(qi);
svo.setSize(size);
svo.setLatency(upload.getLatency());
svo.setInstanceCompatible(true);
svo.setOptimized(upload.getOptimized());
svo.setLocation(upload.getLocation());
// Find the nicest storage
svo.setType(storageResource.lookup(subscription, size, upload.getLatency(), qi, upload.getOptimized(), upload.getLocation()).stream().findFirst().orElseThrow(() -> new ValidationJsonException("storage", "NotNull")).getPrice().getType().getName());
// Default the storage name to the instance name
svo.setSubscription(subscription);
storageResource.create(svo);
}
}
use of org.ligoj.app.plugin.prov.model.ProvInstancePrice 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());
}
use of org.ligoj.app.plugin.prov.model.ProvInstancePrice in project plugin-prov-azure by ligoj.
the class ProvAzurePriceImportResource method installInstancesTerm.
private void installInstancesTerm(final UpdateContext context, final ProvInstancePriceTerm term, final ProvInstancePriceTerm termLow, Entry<String, AzureVmPrice> azPrice) {
final String[] parts = StringUtils.split(azPrice.getKey(), '-');
final VmOs os = VmOs.valueOf(parts[0].replace("redhat", "RHEL").replace("sles", "SUSE").toUpperCase());
// Basic, Low Priority, Standard
final String tier = parts[2];
final boolean isBasic = "basic".equals(tier);
final AzureVmPrice azType = azPrice.getValue();
// Get the right term : "lowpriority" within "PayGo" or the current term
final ProvInstancePriceTerm termU = tier.equals("lowpriority") ? termLow : term;
final String globalCode = termU.getName() + "-" + azPrice.getKey();
final ProvInstanceType type = installInstancePriceType(context, parts, isBasic, azType);
// Iterate over regions enabling this instance type
azType.getPrices().entrySet().stream().filter(pl -> isEnabledRegion(pl.getKey())).forEach(pl -> {
final ProvInstancePrice price = installInstancePrice(context, termU, os, globalCode, type, pl.getKey());
// Update the cost
price.setCost(round3Decimals(pl.getValue().getValue() * 24 * 30.5));
price.setCostPeriod(pl.getValue().getValue());
ipRepository.save(price);
});
}
use of org.ligoj.app.plugin.prov.model.ProvInstancePrice in project plugin-prov-azure by ligoj.
the class ProvAzurePriceImportResource method installComputePrices.
private void installComputePrices(final UpdateContext context, final String termName, final int period) throws IOException {
final Node node = context.getNode();
nextStep(node, "compute-" + termName + "-initialize", 1);
// Get or create the term
List<ProvInstancePriceTerm> terms = iptRepository.findAllBy(BY_NODE, node.getId());
final ProvInstancePriceTerm term = terms.stream().filter(p -> p.getName().equals(termName)).findAny().orElseGet(() -> {
final ProvInstancePriceTerm newTerm = new ProvInstancePriceTerm();
newTerm.setName(termName);
newTerm.setNode(node);
newTerm.setPeriod(period);
newTerm.setCode(termName);
iptRepository.saveAndFlush(newTerm);
return newTerm;
});
// Special "LOW PRIORITY" sub term of Pay As you Go
final ProvInstancePriceTerm termLow = terms.stream().filter(p -> p.getName().equals("lowpriority")).findAny().orElseGet(() -> {
final ProvInstancePriceTerm newTerm = new ProvInstancePriceTerm();
newTerm.setName("lowpriority");
newTerm.setNode(node);
newTerm.setEphemeral(true);
newTerm.setPeriod(0);
newTerm.setCode("lowpriority");
iptRepository.saveAndFlush(newTerm);
return newTerm;
});
// Get previous prices
context.setPrevious(ipRepository.findAllBy("term.id", term.getId()).stream().collect(Collectors.toMap(ProvInstancePrice::getCode, Function.identity())));
if (context.getPreviousLowPriority() == null) {
context.setPreviousLowPriority(ipRepository.findAllBy("term.id", termLow.getId()).stream().collect(Collectors.toMap(ProvInstancePrice::getCode, Function.identity())));
}
// Fetch the remote prices stream and build the prices object
nextStep(node, "compute-" + termName + "-retrieve-catalog", 1);
final String rawJson = StringUtils.defaultString(new CurlProcessor().get(getVmApi(termName)), "{}");
final ComputePrices prices = objectMapper.readValue(rawJson, ComputePrices.class);
nextStep(node, "compute-" + termName + "-update", 1);
prices.getOffers().entrySet().stream().forEach(e -> installInstancesTerm(context, term, termLow, e));
}
Aggregations