use of org.ligoj.app.resource.plugin.CurlProcessor 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));
}
use of org.ligoj.app.resource.plugin.CurlProcessor in project plugin-prov-azure by ligoj.
the class ProvAzurePriceImportResource method installStoragePrices.
/**
* Install storage prices from the JSON file provided by AWS.
*
* @param context
* The update context.
*/
private void installStoragePrices(final UpdateContext context) throws IOException {
final Node node = context.getNode();
log.info("Azure managed-disk prices...");
nextStep(node, "managed-disk-initialize", 1);
// The previously installed storage types cache. Key is the storage type
// name
context.setStorageTypes(stRepository.findAllBy(BY_NODE, node.getId()).stream().collect(Collectors.toMap(INamableBean::getName, Function.identity())));
context.setPreviousStorages(new HashMap<>());
spRepository.findAllBy("type.node.id", node.getId()).forEach(p -> {
context.getPreviousStorages().computeIfAbsent(p.getType(), t -> new HashMap<>()).put(p.getLocation(), p);
});
// Fetch the remote prices stream
nextStep(node, "managed-disk-retrieve-catalog", 1);
final String rawJson = StringUtils.defaultString(new CurlProcessor().get(getManagedDiskApi()), "{}");
final ManagedDisks prices = objectMapper.readValue(rawJson, ManagedDisks.class);
// Add region as needed
nextStep(node, "managed-disk-update-catalog", 1);
prices.getRegions().stream().filter(this::isEnabledRegion).forEach(r -> installRegion(context, r));
// Update or install storage price
final Map<String, ManagedDisk> offers = prices.getOffers();
context.setTransactions(offers.getOrDefault("transactions", new ManagedDisk()).getPrices());
offers.entrySet().stream().filter(p -> !"transactions".equals(p.getKey())).forEach(o -> installStoragePrice(context, o));
}
Aggregations