Search in sources :

Example 11 with ProvInstancePrice

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());
}
Also used : ObjectMapperTrim(org.ligoj.bootstrap.core.json.ObjectMapperTrim) ProvInstancePrice(org.ligoj.app.plugin.prov.model.ProvInstancePrice) AbstractAppTest(org.ligoj.app.AbstractAppTest) Test(org.junit.jupiter.api.Test)

Example 12 with ProvInstancePrice

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);
    }
}
Also used : ProvInstancePrice(org.ligoj.app.plugin.prov.model.ProvInstancePrice) ValidationJsonException(org.ligoj.bootstrap.core.validation.ValidationJsonException)

Example 13 with ProvInstancePrice

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());
}
Also used : ProvLocation(org.ligoj.app.plugin.prov.model.ProvLocation) ProvInstancePrice(org.ligoj.app.plugin.prov.model.ProvInstancePrice) ProvQuoteInstance(org.ligoj.app.plugin.prov.model.ProvQuoteInstance) ProvQuote(org.ligoj.app.plugin.prov.model.ProvQuote) ProvStoragePrice(org.ligoj.app.plugin.prov.model.ProvStoragePrice) AbstractAppTest(org.ligoj.app.AbstractAppTest) Test(org.junit.jupiter.api.Test)

Example 14 with ProvInstancePrice

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);
    });
}
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) VmOs(org.ligoj.app.plugin.prov.model.VmOs) ProvInstanceType(org.ligoj.app.plugin.prov.model.ProvInstanceType) ProvInstancePrice(org.ligoj.app.plugin.prov.model.ProvInstancePrice) ProvInstancePriceTerm(org.ligoj.app.plugin.prov.model.ProvInstancePriceTerm)

Example 15 with ProvInstancePrice

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));
}
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)

Aggregations

ProvInstancePrice (org.ligoj.app.plugin.prov.model.ProvInstancePrice)16 Test (org.junit.jupiter.api.Test)7 ProvInstancePriceTerm (org.ligoj.app.plugin.prov.model.ProvInstancePriceTerm)6 AbstractAppTest (org.ligoj.app.AbstractAppTest)5 ProvInstanceType (org.ligoj.app.plugin.prov.model.ProvInstanceType)5 ProvQuoteInstance (org.ligoj.app.plugin.prov.model.ProvQuoteInstance)4 ProvStoragePrice (org.ligoj.app.plugin.prov.model.ProvStoragePrice)4 ProvStorageType (org.ligoj.app.plugin.prov.model.ProvStorageType)3 IOException (java.io.IOException)2 StandardCharsets (java.nio.charset.StandardCharsets)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Function (java.util.function.Function)2 Slf4j (lombok.extern.slf4j.Slf4j)2 IOUtils (org.apache.commons.io.IOUtils)2 Node (org.ligoj.app.model.Node)2 ProvLocation (org.ligoj.app.plugin.prov.model.ProvLocation)2 CurlProcessor (org.ligoj.app.resource.plugin.CurlProcessor)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1