use of org.ligoj.app.plugin.prov.model.ProvInstancePriceTerm in project plugin-prov by ligoj.
the class ProvResourceTest method testToString.
@Test
public void testToString() {
final QuoteInstanceLookup computedInstancePrice = new QuoteInstanceLookup();
computedInstancePrice.setCost(1.23);
final ProvInstancePrice ip = new ProvInstancePrice();
final ProvInstancePriceTerm type = new ProvInstancePriceTerm();
type.setName("type1");
ip.setTerm(type);
final ProvInstanceType instance = new ProvInstanceType();
instance.setName("instance1");
ip.setType(instance);
computedInstancePrice.setPrice(ip);
Assertions.assertTrue(computedInstancePrice.toString().contains("cost=1.23"));
Assertions.assertTrue(computedInstancePrice.toString().contains("name=instance1"));
final QuoteStorageLoopup computedStoragePrice = new QuoteStorageLoopup();
computedStoragePrice.setCost(1.23);
final ProvStoragePrice sp = new ProvStoragePrice();
final ProvStorageType stype = new ProvStorageType();
stype.setName("type1");
sp.setType(stype);
computedStoragePrice.setPrice(sp);
Assertions.assertTrue(computedStoragePrice.toString().contains("cost=1.23"));
Assertions.assertTrue(computedStoragePrice.toString().contains("name=type1"));
}
use of org.ligoj.app.plugin.prov.model.ProvInstancePriceTerm in project plugin-prov-azure by ligoj.
the class ProvAzurePriceImportResourceTest method checkInstance.
private ProvQuoteInstance checkInstance(final ProvQuoteInstance instance, final double cost) {
Assertions.assertEquals(cost, instance.getCost(), DELTA);
final ProvInstancePrice price = instance.getPrice();
Assertions.assertNull(price.getInitialCost());
Assertions.assertEquals(VmOs.LINUX, price.getOs());
Assertions.assertEquals(ProvTenancy.SHARED, price.getTenancy());
Assertions.assertEquals(150.28, price.getCost(), DELTA);
Assertions.assertEquals(0.2053, price.getCostPeriod(), DELTA);
final ProvInstancePriceTerm priceType = price.getTerm();
Assertions.assertEquals("base-three-year", priceType.getName());
Assertions.assertFalse(priceType.isEphemeral());
Assertions.assertEquals(36, priceType.getPeriod());
Assertions.assertEquals("ds4v2", price.getType().getName());
return instance;
}
use of org.ligoj.app.plugin.prov.model.ProvInstancePriceTerm in project plugin-prov by ligoj.
the class ProvQuoteInstanceResourceTest method uploadFixedInstance.
@Test
public void uploadFixedInstance() throws IOException {
qiResource.upload(subscription, new ByteArrayInputStream("ANY;0.5;500;LINUX;instance10;true".getBytes("UTF-8")), new String[] { "name", "cpu", "ram", "os", "type", "ephemeral" }, false, "Full Time 12 month", 1, "UTF-8");
final QuoteVo configuration = resource.getConfiguration(subscription);
Assertions.assertEquals(8, configuration.getInstances().size());
final ProvInstancePriceTerm term = configuration.getInstances().get(7).getPrice().getTerm();
Assertions.assertEquals("on-demand1", term.getName());
Assertions.assertEquals("dynamic", configuration.getInstances().get(7).getPrice().getType().getName());
Assertions.assertEquals(4, configuration.getStorages().size());
checkCost(configuration.getCost(), 4950.846, 7400.446, false);
}
use of org.ligoj.app.plugin.prov.model.ProvInstancePriceTerm 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.ProvInstancePriceTerm 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