Search in sources :

Example 1 with ProvLocation

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

the class ProvResourceTest method updateLocation.

/**
 * Update the location of the quote, impact all instances, but no one use the default location. Cost still updated.
 */
@Test
public void updateLocation() {
    final ProvLocation location4 = locationRepository.findByName("region-4");
    // Make sure there is no more world wild prices
    em.createQuery("FROM ProvInstancePrice WHERE location IS NULL", ProvInstancePrice.class).getResultList().forEach(ip -> ip.setLocation(location4));
    em.flush();
    em.clear();
    final QuoteEditionVo quote = new QuoteEditionVo();
    quote.setName("name1");
    quote.setDescription("description1");
    quote.setLocation("region-1");
    final FloatingCost cost = resource.update(subscription, quote);
    checkCost(cost, 5799.465, 9669.918, false);
    ProvQuote quote2 = repository.findByNameExpected("name1");
    Assertions.assertEquals("description1", quote2.getDescription());
    // Check location
    final ProvLocation location = quote2.getLocation();
    Assertions.assertEquals("region-1", location.getName());
    Assertions.assertEquals("west", location.getPlacement());
    Assertions.assertEquals(840, location.getCountryM49().intValue());
    Assertions.assertEquals("US", location.getCountryA2());
    Assertions.assertEquals(21, location.getRegionM49().intValue());
    Assertions.assertEquals(19, location.getContinentM49().intValue());
    Assertions.assertEquals("Virginia", location.getSubRegion());
    // CHeck the association on the quote
    Assertions.assertEquals("region-1", resource.getConfiguration(subscription).getLocation().getName());
    // Check the "region-1" is the one related to our provider
    Assertions.assertEquals("service:prov:test", repository.findByName("name1").getLocation().getNode().getId());
}
Also used : ProvLocation(org.ligoj.app.plugin.prov.model.ProvLocation) ProvQuote(org.ligoj.app.plugin.prov.model.ProvQuote) AbstractAppTest(org.ligoj.app.AbstractAppTest) Test(org.junit.jupiter.api.Test)

Example 2 with ProvLocation

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

the class ProvQuoteInstanceResourceTest method lookupInstanceLocationNotFound.

/**
 * Search instance type within a region where minimal instance types are not available.
 */
@Test
public void lookupInstanceLocationNotFound() {
    Assertions.assertEquals("instance2", qiResource.lookup(subscription, 1, 2000, null, VmOs.LINUX, null, true, "region-xxx", "Full Time 12 month").getPrice().getType().getName());
    final ProvLocation location = locationRepository.findByName("region-1");
    // Add location constraint on the first matching instances to exclude
    // them
    ipRepository.findAllBy("type.name", "instance2").forEach(ip -> ip.setLocation(location));
    ipRepository.findAllBy("type.name", "dynamic").forEach(ip -> ip.setLocation(location));
    em.flush();
    em.clear();
    // Instance 2 is not available in this region
    Assertions.assertEquals("instance4", qiResource.lookup(subscription, 1, 2000, null, VmOs.LINUX, null, true, "region-xxx", "Full Time 12 month").getPrice().getType().getName());
}
Also used : ProvLocation(org.ligoj.app.plugin.prov.model.ProvLocation) AbstractAppTest(org.ligoj.app.AbstractAppTest) Test(org.junit.jupiter.api.Test)

Example 3 with ProvLocation

use of org.ligoj.app.plugin.prov.model.ProvLocation 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)

Example 4 with ProvLocation

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

the class ProvQuoteInstanceResource method saveOrUpdate.

/**
 * Save or update the given entity from the {@link QuoteInstanceEditionVo}. The computed cost are recursively
 * updated from the instance to the quote total cost.
 */
private UpdatedCost saveOrUpdate(final ProvQuoteInstance entity, final QuoteInstanceEditionVo vo) {
    // Compute the unbound cost delta
    final int deltaUnbound = BooleanUtils.toInteger(vo.getMaxQuantity() == null) - BooleanUtils.toInteger(entity.isUnboundCost());
    // Check the associations and copy attributes to the entity
    final ProvQuote configuration = getQuoteFromSubscription(vo.getSubscription());
    final Subscription subscription = configuration.getSubscription();
    final String providerId = subscription.getNode().getRefined().getId();
    DescribedBean.copy(vo, entity);
    entity.setConfiguration(configuration);
    final ProvLocation oldLocation = getLocation(entity);
    entity.setPrice(ipRepository.findOneExpected(vo.getPrice()));
    entity.setLocation(resource.findLocation(providerId, vo.getLocation()));
    entity.setUsage(Optional.ofNullable(vo.getUsage()).map(u -> resource.findConfiguredByName(usageRepository, u, subscription.getId())).orElse(null));
    entity.setOs(ObjectUtils.defaultIfNull(vo.getOs(), entity.getPrice().getOs()));
    entity.setRam(vo.getRam());
    entity.setCpu(vo.getCpu());
    entity.setConstant(vo.getConstant());
    entity.setEphemeral(vo.isEphemeral());
    entity.setInternet(vo.getInternet());
    entity.setMaxVariableCost(vo.getMaxVariableCost());
    entity.setMinQuantity(vo.getMinQuantity());
    entity.setMaxQuantity(vo.getMaxQuantity());
    resource.checkVisibility(entity.getPrice().getType(), providerId);
    checkConstraints(entity);
    checkOs(entity);
    // Update the unbound increment of the global quote
    configuration.setUnboundCostCounter(configuration.getUnboundCostCounter() + deltaUnbound);
    // Save and update the costs
    final UpdatedCost cost = newUpdateCost(entity);
    final Map<Integer, FloatingCost> storagesCosts = new HashMap<>();
    final boolean dirtyPrice = !oldLocation.equals(getLocation(entity));
    CollectionUtils.emptyIfNull(entity.getStorages()).stream().peek(s -> {
        if (dirtyPrice) {
            // Location has changed, the available storage price is invalidated
            storageResource.refresh(s);
            storageResource.refreshCost(s);
        }
    }).forEach(s -> storagesCosts.put(s.getId(), addCost(s, storageResource::updateCost)));
    cost.setRelatedCosts(storagesCosts);
    cost.setTotalCost(toFloatingCost(entity.getConfiguration()));
    return cost;
}
Also used : CsvForBean(org.ligoj.bootstrap.core.csv.CsvForBean) Produces(javax.ws.rs.Produces) ProvInstancePriceRepository(org.ligoj.app.plugin.prov.dao.ProvInstancePriceRepository) Path(javax.ws.rs.Path) ProvUsageRepository(org.ligoj.app.plugin.prov.dao.ProvUsageRepository) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) ValidationJsonException(org.ligoj.bootstrap.core.validation.ValidationJsonException) MediaType(javax.ws.rs.core.MediaType) QueryParam(javax.ws.rs.QueryParam) ByteArrayInputStream(java.io.ByteArrayInputStream) Consumes(javax.ws.rs.Consumes) ProvLocation(org.ligoj.app.plugin.prov.model.ProvLocation) ProvInstanceTypeRepository(org.ligoj.app.plugin.prov.dao.ProvInstanceTypeRepository) DataTableAttributes(org.ligoj.bootstrap.core.json.datatable.DataTableAttributes) Map(java.util.Map) Multipart(org.apache.cxf.jaxrs.ext.multipart.Multipart) DefaultValue(javax.ws.rs.DefaultValue) Subscription(org.ligoj.app.model.Subscription) DescribedBean(org.ligoj.bootstrap.core.DescribedBean) DELETE(javax.ws.rs.DELETE) Context(javax.ws.rs.core.Context) Transactional(javax.transaction.Transactional) PageRequest(org.springframework.data.domain.PageRequest) Reader(java.io.Reader) StandardCharsets(java.nio.charset.StandardCharsets) ProvInstancePrice(org.ligoj.app.plugin.prov.model.ProvInstancePrice) Objects(java.util.Objects) ProvQuoteStorageRepository(org.ligoj.app.plugin.prov.dao.ProvQuoteStorageRepository) IOUtils(org.apache.commons.io.IOUtils) Slf4j(lombok.extern.slf4j.Slf4j) Optional(java.util.Optional) UriInfo(javax.ws.rs.core.UriInfo) TableItem(org.ligoj.bootstrap.core.json.TableItem) PathParam(javax.ws.rs.PathParam) GET(javax.ws.rs.GET) HashMap(java.util.HashMap) ArrayUtils(org.apache.commons.lang3.ArrayUtils) BooleanUtils(org.apache.commons.lang3.BooleanUtils) ProvQuoteInstance(org.ligoj.app.plugin.prov.model.ProvQuoteInstance) Function(java.util.function.Function) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ObjectUtils(org.apache.commons.lang3.ObjectUtils) Service(org.springframework.stereotype.Service) ProvUsage(org.ligoj.app.plugin.prov.model.ProvUsage) POST(javax.ws.rs.POST) SequenceInputStream(java.io.SequenceInputStream) ProvQuoteInstanceRepository(org.ligoj.app.plugin.prov.dao.ProvQuoteInstanceRepository) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) ProvInstancePriceTermRepository(org.ligoj.app.plugin.prov.dao.ProvInstancePriceTermRepository) VmOs(org.ligoj.app.plugin.prov.model.VmOs) StringReader(java.io.StringReader) ProvInstancePriceTerm(org.ligoj.app.plugin.prov.model.ProvInstancePriceTerm) PUT(javax.ws.rs.PUT) ProvQuote(org.ligoj.app.plugin.prov.model.ProvQuote) BufferedReader(java.io.BufferedReader) ProvInstanceType(org.ligoj.app.plugin.prov.model.ProvInstanceType) InputStream(java.io.InputStream) HashMap(java.util.HashMap) ProvLocation(org.ligoj.app.plugin.prov.model.ProvLocation) ProvQuote(org.ligoj.app.plugin.prov.model.ProvQuote) Subscription(org.ligoj.app.model.Subscription)

Example 5 with ProvLocation

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

the class ProvAzurePriceImportResource method installRegion.

/**
 * Install a new region.<br/>
 * Also see CLI2 command <code>az account list-locations</code>
 */
private ProvLocation installRegion(final UpdateContext context, final NamedResource region) {
    final ProvLocation entity = context.getRegions().computeIfAbsent(region.getId(), r -> {
        final ProvLocation newRegion = new ProvLocation();
        newRegion.setNode(context.getNode());
        newRegion.setName(region.getId());
        return newRegion;
    });
    // Update the location details as needed
    if (context.getRegionsMerged().add(region.getId())) {
        final ProvLocation regionStats = mapRegionToName.getOrDefault(region.getId(), new ProvLocation());
        entity.setContinentM49(regionStats.getContinentM49());
        entity.setCountryM49(regionStats.getCountryM49());
        entity.setCountryA2(regionStats.getCountryA2());
        entity.setPlacement(regionStats.getPlacement());
        entity.setRegionM49(regionStats.getRegionM49());
        entity.setSubRegion(regionStats.getSubRegion());
        entity.setLatitude(regionStats.getLatitude());
        entity.setLongitude(regionStats.getLongitude());
        entity.setDescription(region.getName());
        locationRepository.saveAndFlush(entity);
    }
    return entity;
}
Also used : ProvLocation(org.ligoj.app.plugin.prov.model.ProvLocation)

Aggregations

ProvLocation (org.ligoj.app.plugin.prov.model.ProvLocation)8 ProvQuote (org.ligoj.app.plugin.prov.model.ProvQuote)5 ProvInstancePrice (org.ligoj.app.plugin.prov.model.ProvInstancePrice)4 Optional (java.util.Optional)3 Function (java.util.function.Function)3 Test (org.junit.jupiter.api.Test)3 AbstractAppTest (org.ligoj.app.AbstractAppTest)3 ProvQuoteInstance (org.ligoj.app.plugin.prov.model.ProvQuoteInstance)3 Service (org.springframework.stereotype.Service)3 IOException (java.io.IOException)2 Transactional (javax.transaction.Transactional)2 Consumes (javax.ws.rs.Consumes)2 DELETE (javax.ws.rs.DELETE)2 DefaultValue (javax.ws.rs.DefaultValue)2 GET (javax.ws.rs.GET)2 POST (javax.ws.rs.POST)2 PUT (javax.ws.rs.PUT)2 Path (javax.ws.rs.Path)2 PathParam (javax.ws.rs.PathParam)2 Produces (javax.ws.rs.Produces)2