Search in sources :

Example 1 with CurlProcessor

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));
}
Also used : CurlProcessor(org.ligoj.app.resource.plugin.CurlProcessor) Node(org.ligoj.app.model.Node) ProvInstancePrice(org.ligoj.app.plugin.prov.model.ProvInstancePrice) ProvInstancePriceTerm(org.ligoj.app.plugin.prov.model.ProvInstancePriceTerm)

Example 2 with CurlProcessor

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));
}
Also used : ProvStorageType(org.ligoj.app.plugin.prov.model.ProvStorageType) Arrays(java.util.Arrays) ClassPathResource(org.springframework.core.io.ClassPathResource) HashMap(java.util.HashMap) Function(java.util.function.Function) StringUtils(org.apache.commons.lang3.StringUtils) HashSet(java.util.HashSet) ProvTenancy(org.ligoj.app.plugin.prov.model.ProvTenancy) ProvLocation(org.ligoj.app.plugin.prov.model.ProvLocation) Service(org.springframework.stereotype.Service) Map(java.util.Map) Rate(org.ligoj.app.plugin.prov.model.Rate) CurlProcessor(org.ligoj.app.resource.plugin.CurlProcessor) TypeReference(com.fasterxml.jackson.core.type.TypeReference) INamableBean(org.ligoj.bootstrap.core.INamableBean) Node(org.ligoj.app.model.Node) Set(java.util.Set) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) ProvInstancePrice(org.ligoj.app.plugin.prov.model.ProvInstancePrice) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) VmOs(org.ligoj.app.plugin.prov.model.VmOs) Slf4j(lombok.extern.slf4j.Slf4j) Entry(java.util.Map.Entry) PostConstruct(javax.annotation.PostConstruct) ProvInstancePriceTerm(org.ligoj.app.plugin.prov.model.ProvInstancePriceTerm) Optional(java.util.Optional) ProvAzurePluginResource(org.ligoj.app.plugin.prov.azure.ProvAzurePluginResource) ProvStoragePrice(org.ligoj.app.plugin.prov.model.ProvStoragePrice) AbstractImportCatalogResource(org.ligoj.app.plugin.prov.in.AbstractImportCatalogResource) ProvInstanceType(org.ligoj.app.plugin.prov.model.ProvInstanceType) ProvStorageOptimized(org.ligoj.app.plugin.prov.model.ProvStorageOptimized) HashMap(java.util.HashMap) CurlProcessor(org.ligoj.app.resource.plugin.CurlProcessor) Node(org.ligoj.app.model.Node) INamableBean(org.ligoj.bootstrap.core.INamableBean)

Aggregations

Node (org.ligoj.app.model.Node)2 ProvInstancePrice (org.ligoj.app.plugin.prov.model.ProvInstancePrice)2 ProvInstancePriceTerm (org.ligoj.app.plugin.prov.model.ProvInstancePriceTerm)2 CurlProcessor (org.ligoj.app.resource.plugin.CurlProcessor)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 IOException (java.io.IOException)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 PostConstruct (javax.annotation.PostConstruct)1 Slf4j (lombok.extern.slf4j.Slf4j)1 IOUtils (org.apache.commons.io.IOUtils)1