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