use of org.ligoj.app.plugin.prov.model.ProvQuoteInstance in project plugin-prov by ligoj.
the class ProvQuoteStorageResourceTest method createStorageUnboundInstance.
@Test
public void createStorageUnboundInstance() {
final ProvQuoteInstance quoteInstance = setUnboundInstance("server1");
// Attach the new storage to this unbound instance
final QuoteStorageEditionVo vo = new QuoteStorageEditionVo();
vo.setSubscription(subscription);
vo.setName("server1-root-bis");
vo.setType("storage1");
vo.setQuoteInstance(quoteInstance.getId());
vo.setSize(512);
final UpdatedCost cost = qsResource.create(vo);
checkCost(cost.getTotalCost(), 4919.798, 4919.798, true);
checkCost(cost.getResourceCost(), 215.04, 215.04, true);
Assertions.assertEquals(1, cost.getRelatedCosts().size());
checkCost(cost.getRelatedCosts().get(vo.getQuoteInstance()), 292.8, 292.8, true);
final int id = cost.getId();
em.flush();
em.clear();
// Check the exact new cost
checkCost(subscription, 4919.798, 4919.798, true);
final ProvQuoteStorage storage = qsRepository.findOneExpected(id);
Assertions.assertEquals("server1-root-bis", storage.getName());
Assertions.assertEquals(512, storage.getSize().intValue());
Assertions.assertEquals(vo.getType(), storage.getPrice().getType().getName());
Assertions.assertEquals(215.04, storage.getCost(), DELTA);
Assertions.assertTrue(storage.isUnboundCost());
}
use of org.ligoj.app.plugin.prov.model.ProvQuoteInstance in project plugin-prov by ligoj.
the class ProvResourceTest method findConfigured.
@Test
public void findConfigured() {
final ProvQuoteInstance qi = qiRepository.findByName("server1");
Assertions.assertEquals("server1", resource.findConfigured(qiRepository, qi.getId(), subscription).getName());
}
use of org.ligoj.app.plugin.prov.model.ProvQuoteInstance in project plugin-prov by ligoj.
the class ProvQuoteUsageResourceTest method deleteUsedInQuote.
@Test
public void deleteUsedInQuote() {
// Usage = 100% by default and 50% fixed for "server1"
final ProvQuoteInstance server1 = qiRepository.findByName("server1");
server1.setUsage(usageRepository.findByName("Dev"));
em.persist(server1);
checkCost(resource.refresh(subscription), 3019.0, 4883.0, false);
// Usage = 50% by default and 50% fixed for "server1"
final ProvQuote quote = repository.findByName("quote1");
quote.setUsage(usageRepository.findByName("Dev"));
em.persist(quote);
checkCost(resource.refresh(subscription), 1743.865, 3607.865, false);
// Delete the usage
// Check the cost is now back at 100%
checkUsage100AfterDelete(uResource.delete(subscription, "Dev"));
}
use of org.ligoj.app.plugin.prov.model.ProvQuoteInstance 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.ProvQuoteInstance 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;
}
Aggregations