Search in sources :

Example 1 with ProvStoragePrice

use of org.ligoj.app.plugin.prov.model.ProvStoragePrice in project plugin-prov by ligoj.

the class ProvResourceTest method getConfiguration.

@Test
public void getConfiguration() {
    QuoteVo vo = resource.getConfiguration(subscription);
    Assertions.assertEquals("quote1", vo.getName());
    Assertions.assertEquals("quoteD1", vo.getDescription());
    checkCost(vo.getCost(), 4704.758, 7154.358, false);
    checkCost(resource.updateCost(subscription), 4704.758, 7154.358, false);
    vo = resource.getConfiguration(subscription);
    checkCost(vo.getCost(), 4704.758, 7154.358, false);
    Assertions.assertNotNull(vo.getId());
    Assertions.assertNotNull(vo.getCreatedBy());
    Assertions.assertNotNull(vo.getCreatedDate());
    Assertions.assertNotNull(vo.getLastModifiedBy());
    Assertions.assertNotNull(vo.getLastModifiedDate());
    Assertions.assertEquals("region-1", vo.getLocation().getName());
    // Check compute
    final List<ProvQuoteInstance> instances = vo.getInstances();
    Assertions.assertEquals(7, instances.size());
    final ProvQuoteInstance quoteInstance = instances.get(0);
    Assertions.assertNotNull(quoteInstance.getId());
    Assertions.assertEquals("server1", quoteInstance.getName());
    Assertions.assertEquals("serverD1", quoteInstance.getDescription());
    Assertions.assertTrue(quoteInstance.getConstant());
    Assertions.assertEquals(InternetAccess.PUBLIC, quoteInstance.getInternet());
    Assertions.assertEquals(10.1, quoteInstance.getMaxVariableCost(), DELTA);
    Assertions.assertEquals(2, quoteInstance.getMinQuantity().intValue());
    Assertions.assertEquals(10, quoteInstance.getMaxQuantity().intValue());
    final ProvInstancePrice price = quoteInstance.getPrice();
    Assertions.assertEquals(146.4, price.getCost(), DELTA);
    Assertions.assertEquals(146.4, price.getCostPeriod(), DELTA);
    Assertions.assertEquals(VmOs.LINUX, price.getOs());
    Assertions.assertNotNull(price.getTerm().getId());
    Assertions.assertFalse(price.getTerm().isEphemeral());
    Assertions.assertFalse(price.getTerm().isVariable());
    Assertions.assertEquals(0, price.getTerm().getPeriod());
    Assertions.assertEquals("on-demand1", price.getTerm().getName());
    Assertions.assertEquals("15 minutes fragment", price.getTerm().getDescription());
    final ProvInstanceType instance = price.getType();
    Assertions.assertNotNull(instance.getId().intValue());
    Assertions.assertEquals("instance1", instance.getName());
    Assertions.assertEquals("instanceD1", instance.getDescription());
    Assertions.assertEquals(0.5, instance.getCpu(), 0.0001);
    Assertions.assertEquals(2000, instance.getRam().intValue());
    Assertions.assertTrue(instance.getConstant());
    // No minimal for this instance price
    Assertions.assertNull(instances.get(1).getMaxVariableCost());
    Assertions.assertEquals(1, instances.get(3).getMinQuantity().intValue());
    Assertions.assertEquals(1, instances.get(3).getMaxQuantity().intValue());
    // Check the constant CPU requirement
    Assertions.assertTrue(instances.get(0).getConstant());
    Assertions.assertNull(instances.get(1).getConstant());
    Assertions.assertFalse(instances.get(3).getConstant());
    // Check the network requirement
    Assertions.assertEquals(InternetAccess.PUBLIC, instances.get(0).getInternet());
    Assertions.assertEquals(InternetAccess.PRIVATE, instances.get(1).getInternet());
    Assertions.assertEquals(InternetAccess.PRIVATE_NAT, instances.get(2).getInternet());
    // Check storage
    final List<ProvQuoteStorage> storages = vo.getStorages();
    Assertions.assertEquals(4, storages.size());
    final ProvQuoteStorage quoteStorage = storages.get(0);
    Assertions.assertNotNull(quoteStorage.getId());
    Assertions.assertEquals("server1-root", quoteStorage.getName());
    Assertions.assertEquals("server1-rootD", quoteStorage.getDescription());
    Assertions.assertEquals(20, quoteStorage.getSize().intValue());
    Assertions.assertEquals(8.4, quoteStorage.getCost(), DELTA);
    // = 8.4 * 5
    Assertions.assertEquals(42, quoteStorage.getMaxCost(), DELTA);
    Assertions.assertNotNull(quoteStorage.getQuoteInstance());
    final ProvStoragePrice storage = quoteStorage.getPrice();
    final ProvStorageType storageType = storage.getType();
    Assertions.assertNotNull(storage.getId());
    Assertions.assertEquals(0.21, storage.getCostGb(), DELTA);
    Assertions.assertEquals(0, storage.getCost(), DELTA);
    Assertions.assertEquals("storage1", storageType.getName());
    Assertions.assertEquals("storageD1", storageType.getDescription());
    Assertions.assertEquals(200, storageType.getIops());
    Assertions.assertEquals(60, storageType.getThroughput());
    Assertions.assertEquals(0, storage.getCostTransaction(), DELTA);
    Assertions.assertEquals(Rate.GOOD, storageType.getLatency());
    Assertions.assertEquals(ProvStorageOptimized.IOPS, storageType.getOptimized());
    // Not attached storage
    Assertions.assertNull(storages.get(3).getQuoteInstance());
    // Check the small transactional cost
    Assertions.assertEquals(0.000000072, storages.get(1).getPrice().getCostTransaction(), 0.000000001);
    // Check the related instance and price for the next comparison
    Assertions.assertEquals("instance1", instances.get(0).getPrice().getType().getName());
    Assertions.assertEquals("instance1", instances.get(1).getPrice().getType().getName());
    Assertions.assertEquals("instance1", instances.get(2).getPrice().getType().getName());
    Assertions.assertEquals("instance3", instances.get(3).getPrice().getType().getName());
    Assertions.assertEquals("instance5", instances.get(4).getPrice().getType().getName());
    Assertions.assertEquals("instance10", instances.get(5).getPrice().getType().getName());
    Assertions.assertEquals("dynamic", instances.get(6).getPrice().getType().getName());
    Assertions.assertEquals("on-demand1", instances.get(0).getPrice().getTerm().getName());
    Assertions.assertEquals("on-demand2", instances.get(1).getPrice().getTerm().getName());
    Assertions.assertEquals("1y", instances.get(2).getPrice().getTerm().getName());
    Assertions.assertEquals("on-demand1", instances.get(3).getPrice().getTerm().getName());
    Assertions.assertEquals("on-demand2", instances.get(4).getPrice().getTerm().getName());
    Assertions.assertEquals("on-demand1", instances.get(5).getPrice().getTerm().getName());
    Assertions.assertEquals("on-demand1", instances.get(6).getPrice().getTerm().getName());
    // Optimize the configuration
    checkCost(resource.refresh(subscription), 3165.4, 5615.0, false);
    final QuoteVo vo2 = resource.getConfiguration(subscription);
    Assertions.assertEquals("quote1", vo2.getName());
    Assertions.assertEquals(vo2.getId(), vo.getId());
    // Check the new instances
    final List<ProvQuoteInstance> instances2 = vo2.getInstances();
    Assertions.assertEquals(7, instances2.size());
    // Same instance
    Assertions.assertEquals("instance1", instances2.get(0).getPrice().getType().getName());
    Assertions.assertEquals("dynamic", instances2.get(5).getPrice().getType().getName());
    Assertions.assertEquals("dynamic", instances2.get(4).getPrice().getType().getName());
    // Fixed instance types for the same constraints
    Assertions.assertEquals("instance2", instances2.get(1).getPrice().getType().getName());
    Assertions.assertEquals("instance2", instances2.get(2).getPrice().getType().getName());
    Assertions.assertEquals("instance2", instances2.get(3).getPrice().getType().getName());
    Assertions.assertEquals("dynamic", instances2.get(6).getPrice().getType().getName());
    // Check the contracts are the same but for 2
    Assertions.assertEquals("on-demand1", instances2.get(0).getPrice().getTerm().getName());
    Assertions.assertEquals("on-demand2", instances2.get(1).getPrice().getTerm().getName());
    // Updated
    Assertions.assertEquals("on-demand1", instances2.get(2).getPrice().getTerm().getName());
    Assertions.assertEquals("on-demand1", instances2.get(3).getPrice().getTerm().getName());
    // Updated
    Assertions.assertEquals("on-demand1", instances2.get(4).getPrice().getTerm().getName());
    Assertions.assertEquals("on-demand1", instances2.get(5).getPrice().getTerm().getName());
    Assertions.assertEquals("on-demand1", instances2.get(6).getPrice().getTerm().getName());
    // No associated usage for this use case
    Assertions.assertNull(vo.getUsage());
}
Also used : ProvStorageType(org.ligoj.app.plugin.prov.model.ProvStorageType) ProvInstanceType(org.ligoj.app.plugin.prov.model.ProvInstanceType) ProvQuoteStorage(org.ligoj.app.plugin.prov.model.ProvQuoteStorage) ProvQuoteInstance(org.ligoj.app.plugin.prov.model.ProvQuoteInstance) ProvInstancePrice(org.ligoj.app.plugin.prov.model.ProvInstancePrice) ProvStoragePrice(org.ligoj.app.plugin.prov.model.ProvStoragePrice) AbstractAppTest(org.ligoj.app.AbstractAppTest) Test(org.junit.jupiter.api.Test)

Example 2 with ProvStoragePrice

use of org.ligoj.app.plugin.prov.model.ProvStoragePrice in project plugin-prov by ligoj.

the class ProvQuoteStorageResourceTest method refresh.

@Test
public void refresh() {
    // Create with constraints
    final QuoteStorageEditionVo vo = new QuoteStorageEditionVo();
    vo.setSubscription(subscription);
    vo.setName("server-new");
    vo.setType("storage1");
    vo.setOptimized(null);
    vo.setInstanceCompatible(true);
    vo.setLatency(Rate.GOOD);
    vo.setQuoteInstance(qiRepository.findByNameExpected("server2").getId());
    vo.setSize(512);
    final UpdatedCost cost = qsResource.create(vo);
    checkCost(cost.getResourceCost(), 107.52, 107.52, false);
    // No change
    checkCost(qsResource.refresh(qsRepository.findOneExpected(cost.getId())), 107.52, 107.52, false);
    Assertions.assertEquals("storage1", qsRepository.findOneExpected(cost.getId()).getPrice().getType().getName());
    Assertions.assertEquals(true, qsRepository.findOneExpected(cost.getId()).getInstanceCompatible());
    // Change some constraints
    vo.setLatency(Rate.WORST);
    vo.setInstanceCompatible(false);
    vo.setQuoteInstance(null);
    vo.setId(cost.getId());
    // Cost is the same since the type still match the constraints
    checkCost(qsResource.update(vo).getResourceCost(), 107.52, 107.52, false);
    // The cost changed since a best type matches to the constraints
    checkCost(qsResource.refresh(qsRepository.findOneExpected(cost.getId())), 77.8, 77.8, false);
    Assertions.assertEquals("storage2", qsRepository.findOneExpected(cost.getId()).getPrice().getType().getName());
    // Change the price of "storage3" to make it as cheap as "storage2"
    final ProvStoragePrice price3 = spRepository.findBy("type.name", "storage3");
    price3.setCost(1);
    spRepository.saveAndFlush(price3);
    // Also, change the latency of "storage3" to "GOOD" class
    final ProvStorageType type3 = stRepository.findByName("storage3");
    type3.setLatency(Rate.GOOD);
    stRepository.saveAndFlush(type3);
    // Even if "storage2" and "storage3" have identical prices and match to
    // the
    // requirements, "storage3" offers a lowest latency.
    checkCost(qsResource.refresh(qsRepository.findOneExpected(cost.getId())), 77.8, 77.8, false);
    Assertions.assertEquals("storage3", qsRepository.findOneExpected(cost.getId()).getPrice().getType().getName());
}
Also used : ProvStorageType(org.ligoj.app.plugin.prov.model.ProvStorageType) ProvStoragePrice(org.ligoj.app.plugin.prov.model.ProvStoragePrice) AbstractAppTest(org.ligoj.app.AbstractAppTest) Test(org.junit.jupiter.api.Test)

Example 3 with ProvStoragePrice

use of org.ligoj.app.plugin.prov.model.ProvStoragePrice 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 4 with ProvStoragePrice

use of org.ligoj.app.plugin.prov.model.ProvStoragePrice in project plugin-prov by ligoj.

the class ProvQuoteStorageResourceTest method assertCSP.

private QuoteStorageLoopup assertCSP(final QuoteStorageLoopup price) {
    final ProvStoragePrice sp = price.getPrice();
    final ProvStorageType st = sp.getType();
    Assertions.assertNotNull(sp.getId());
    Assertions.assertNotNull(st.getId());
    Assertions.assertEquals("storage1", st.getName());
    return price;
}
Also used : ProvStorageType(org.ligoj.app.plugin.prov.model.ProvStorageType) ProvStoragePrice(org.ligoj.app.plugin.prov.model.ProvStoragePrice)

Example 5 with ProvStoragePrice

use of org.ligoj.app.plugin.prov.model.ProvStoragePrice in project plugin-prov by ligoj.

the class ProvQuoteStorageResource method lookup.

private List<QuoteStorageLoopup> lookup(final ProvQuote configuration, final int size, final Rate latency, final Integer instance, final ProvStorageOptimized optimized, final String location) {
    // Get the attached node and check the security on this subscription
    final String node = configuration.getSubscription().getNode().getRefined().getId();
    final ProvQuoteInstance qi = checkInstance(node, instance);
    // The the right location from instance first, then the request one
    String iLocation = Optional.ofNullable(qi).map(qiResource::getLocation).map(ProvLocation::getName).orElse(location);
    iLocation = ObjectUtils.defaultIfNull(iLocation, configuration.getLocation().getName());
    if (location != null && !location.equals(iLocation)) {
        // Not compatible locations
        return Collections.emptyList();
    }
    return spRepository.findLowestPrice(node, size, latency, instance, optimized, iLocation, PageRequest.of(0, 10)).stream().map(spx -> (ProvStoragePrice) spx[0]).map(sp -> newPrice(sp, size, getCost(sp, size))).collect(Collectors.toList());
}
Also used : ProvStorageType(org.ligoj.app.plugin.prov.model.ProvStorageType) PathParam(javax.ws.rs.PathParam) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Path(javax.ws.rs.Path) Autowired(org.springframework.beans.factory.annotation.Autowired) ProvQuoteStorage(org.ligoj.app.plugin.prov.model.ProvQuoteStorage) ProvQuoteInstance(org.ligoj.app.plugin.prov.model.ProvQuoteInstance) Function(java.util.function.Function) ValidationJsonException(org.ligoj.bootstrap.core.validation.ValidationJsonException) MediaType(javax.ws.rs.core.MediaType) QueryParam(javax.ws.rs.QueryParam) ServicePluginLocator(org.ligoj.app.resource.ServicePluginLocator) Consumes(javax.ws.rs.Consumes) ProvLocation(org.ligoj.app.plugin.prov.model.ProvLocation) ObjectUtils(org.apache.commons.lang3.ObjectUtils) DataTableAttributes(org.ligoj.bootstrap.core.json.datatable.DataTableAttributes) Service(org.springframework.stereotype.Service) ProvStorageTypeRepository(org.ligoj.app.plugin.prov.dao.ProvStorageTypeRepository) DefaultValue(javax.ws.rs.DefaultValue) Rate(org.ligoj.app.plugin.prov.model.Rate) DescribedBean(org.ligoj.bootstrap.core.DescribedBean) DELETE(javax.ws.rs.DELETE) INamableBean(org.ligoj.bootstrap.core.INamableBean) POST(javax.ws.rs.POST) Context(javax.ws.rs.core.Context) Transactional(javax.transaction.Transactional) ProvQuoteInstanceRepository(org.ligoj.app.plugin.prov.dao.ProvQuoteInstanceRepository) PageRequest(org.springframework.data.domain.PageRequest) Collectors(java.util.stream.Collectors) ProvInstancePrice(org.ligoj.app.plugin.prov.model.ProvInstancePrice) ProvQuoteStorageRepository(org.ligoj.app.plugin.prov.dao.ProvQuoteStorageRepository) List(java.util.List) Optional(java.util.Optional) PUT(javax.ws.rs.PUT) ProvQuote(org.ligoj.app.plugin.prov.model.ProvQuote) ProvStoragePrice(org.ligoj.app.plugin.prov.model.ProvStoragePrice) UriInfo(javax.ws.rs.core.UriInfo) TableItem(org.ligoj.bootstrap.core.json.TableItem) Collections(java.util.Collections) ProvStorageOptimized(org.ligoj.app.plugin.prov.model.ProvStorageOptimized) ProvStoragePriceRepository(org.ligoj.app.plugin.prov.dao.ProvStoragePriceRepository) ProvQuoteInstance(org.ligoj.app.plugin.prov.model.ProvQuoteInstance)

Aggregations

ProvStoragePrice (org.ligoj.app.plugin.prov.model.ProvStoragePrice)9 ProvStorageType (org.ligoj.app.plugin.prov.model.ProvStorageType)7 ProvInstancePrice (org.ligoj.app.plugin.prov.model.ProvInstancePrice)5 Test (org.junit.jupiter.api.Test)4 AbstractAppTest (org.ligoj.app.AbstractAppTest)4 ProvInstanceType (org.ligoj.app.plugin.prov.model.ProvInstanceType)3 ProvLocation (org.ligoj.app.plugin.prov.model.ProvLocation)3 ProvQuoteInstance (org.ligoj.app.plugin.prov.model.ProvQuoteInstance)3 List (java.util.List)2 Optional (java.util.Optional)2 Function (java.util.function.Function)2 Collectors (java.util.stream.Collectors)2 ProvInstancePriceTerm (org.ligoj.app.plugin.prov.model.ProvInstancePriceTerm)2 ProvQuote (org.ligoj.app.plugin.prov.model.ProvQuote)2 ProvQuoteStorage (org.ligoj.app.plugin.prov.model.ProvQuoteStorage)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 Collections (java.util.Collections)1