Search in sources :

Example 1 with ProvInstancePriceTerm

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

Example 2 with ProvInstancePriceTerm

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

Example 3 with ProvInstancePriceTerm

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);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ProvInstancePriceTerm(org.ligoj.app.plugin.prov.model.ProvInstancePriceTerm) AbstractAppTest(org.ligoj.app.AbstractAppTest) Test(org.junit.jupiter.api.Test)

Example 4 with ProvInstancePriceTerm

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);
    });
}
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 5 with ProvInstancePriceTerm

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

ProvInstancePriceTerm (org.ligoj.app.plugin.prov.model.ProvInstancePriceTerm)6 ProvInstancePrice (org.ligoj.app.plugin.prov.model.ProvInstancePrice)5 Test (org.junit.jupiter.api.Test)3 ProvInstanceType (org.ligoj.app.plugin.prov.model.ProvInstanceType)3 AbstractAppTest (org.ligoj.app.AbstractAppTest)2 Node (org.ligoj.app.model.Node)2 ProvStoragePrice (org.ligoj.app.plugin.prov.model.ProvStoragePrice)2 ProvStorageType (org.ligoj.app.plugin.prov.model.ProvStorageType)2 CurlProcessor (org.ligoj.app.resource.plugin.CurlProcessor)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ByteArrayInputStream (java.io.ByteArrayInputStream)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