use of org.candlepin.policy.ValidationError in project candlepin by candlepin.
the class EntitlerJob method toExecute.
@Override
public void toExecute(JobExecutionContext ctx) throws JobExecutionException {
try {
JobDataMap map = ctx.getMergedJobDataMap();
String uuid = (String) map.get(JobStatus.TARGET_ID);
PoolIdAndQuantity[] poolQuantities = (PoolIdAndQuantity[]) map.get("pool_and_quantities");
Map<String, Integer> poolMap = new HashMap<>();
for (PoolIdAndQuantity poolIdAndQuantity : poolQuantities) {
poolMap.put(poolIdAndQuantity.getPoolId(), poolIdAndQuantity.getQuantity());
}
List<Entitlement> ents = entitler.bindByPoolQuantities(uuid, poolMap);
entitler.sendEvents(ents);
PoolIdAndQuantity[] consumed = new PoolIdAndQuantity[ents.size()];
for (int i = 0; i < ents.size(); i++) {
consumed[i] = new PoolIdAndQuantity(ents.get(i).getPool().getId(), ents.get(i).getQuantity());
}
ctx.setResult(Arrays.asList(consumed));
poolCurator.clear();
} catch (EntitlementRefusedException e) {
log.error("EntitlerJob encountered a problem, translating errors", e);
Map<String, ValidationResult> validationResults = e.getResults();
EntitlementRulesTranslator translator = new EntitlementRulesTranslator(i18n);
List<PoolIdAndErrors> poolErrors = new ArrayList<>();
for (Pool pool : poolCurator.listAllByIds(validationResults.keySet())) {
List<String> errorMessages = new ArrayList<>();
for (ValidationError error : validationResults.get(pool.getId()).getErrors()) {
errorMessages.add(translator.poolErrorToMessage(pool, error));
}
poolErrors.add(new PoolIdAndErrors(pool.getId(), errorMessages));
}
ctx.setResult(poolErrors);
}// so that the job will be properly cleaned up on failure.
catch (Exception e) {
log.error("EntitlerJob encountered a problem.", e);
throw new JobExecutionException(e.getMessage(), e, false);
}
}
use of org.candlepin.policy.ValidationError in project candlepin by candlepin.
the class Entitler method getDryRun.
/**
* Entitles the given Consumer to the given Product. Will seek out pools
* which provide access to this product, either directly or as a child, and
* select the best one based on a call to the rules engine.
*
* @param consumer The consumer being entitled.
* @return List of Entitlements
*/
public List<PoolQuantity> getDryRun(Consumer consumer, Owner owner, String serviceLevelOverride) {
List<PoolQuantity> result = new ArrayList<>();
try {
if (consumer.isDev()) {
if (config.getBoolean(ConfigProperties.STANDALONE) || !poolCurator.hasActiveEntitlementPools(consumer.getOwnerId(), null)) {
throw new ForbiddenException(i18n.tr("Development units may only be used on hosted servers" + " and with orgs that have active subscriptions."));
}
// Look up the dev pool for this consumer, and if not found
// create one. If a dev pool already exists, remove it and
// create a new one.
String sku = consumer.getFact("dev_sku");
Pool devPool = poolCurator.findDevPool(consumer);
if (devPool != null) {
poolManager.deletePool(devPool);
}
devPool = poolManager.createPool(assembleDevPool(consumer, owner, sku));
result.add(new PoolQuantity(devPool, 1));
} else {
result = poolManager.getBestPools(consumer, null, null, owner.getId(), serviceLevelOverride, null);
}
log.debug("Created Pool Quantity list: {}", result);
} catch (EntitlementRefusedException e) {
// to the process
if (log.isDebugEnabled()) {
log.debug("consumer {} dry-run errors:", consumer.getUuid());
for (Entry<String, ValidationResult> entry : e.getResults().entrySet()) {
log.debug("errors for pool id: {}", entry.getKey());
for (ValidationError error : entry.getValue().getErrors()) {
log.debug(error.getResourceKey());
}
}
}
}
return result;
}
use of org.candlepin.policy.ValidationError in project candlepin by candlepin.
the class ManifestEntitlementRulesTest method preEntitlementShouldNotAllowListOfDerivedPools.
@Test
public void preEntitlementShouldNotAllowListOfDerivedPools() {
Consumer c = this.createMockConsumer(true);
Product prod = TestUtil.createProduct();
Pool p = TestUtil.createPool(prod);
p.setAttribute(Product.Attributes.VIRT_ONLY, "true");
p.setAttribute(Pool.Attributes.DERIVED_POOL, "true");
p.setId("poolId");
ValidationResult results = enforcer.preEntitlement(c, p, 1, CallerType.LIST_POOLS);
assertNotNull(results);
assertEquals(1, results.getErrors().size());
ValidationError error = results.getErrors().get(0);
assertEquals("pool.not.available.to.manifest.consumers", error.getResourceKey());
}
use of org.candlepin.policy.ValidationError in project candlepin by candlepin.
the class ManifestEntitlementRulesTest method preEntitlementNoInstanceCapableBindError.
@Test
public void preEntitlementNoInstanceCapableBindError() {
// Test with sockets to make sure that they are skipped.
Consumer c = this.createMockConsumer(true);
Set<ConsumerCapability> caps = new HashSet<>();
c.setCapabilities(caps);
Product prod = TestUtil.createProduct();
prod.setAttribute(Product.Attributes.INSTANCE_MULTIPLIER, "2");
Pool p = TestUtil.createPool(prod);
p.setId("poolId");
ValidationResult results = enforcer.preEntitlement(c, p, 1, CallerType.BIND);
assertNotNull(results);
assertEquals(0, results.getWarnings().size());
ValidationError error = results.getErrors().get(0);
assertEquals("rulefailed.instance.unsupported.by.consumer", error.getResourceKey());
}
use of org.candlepin.policy.ValidationError in project candlepin by candlepin.
the class ManifestEntitlementRulesTest method preEntitlementNoDerivedProductCapabilityProducesErrorOnBind.
@Test
public void preEntitlementNoDerivedProductCapabilityProducesErrorOnBind() {
Consumer c = this.createMockConsumer(true);
c.setCapabilities(new HashSet<>());
Product prod = TestUtil.createProduct();
Product derived = TestUtil.createProduct("sub-prod-id");
Pool p = TestUtil.createPool(prod);
p.setDerivedProduct(derived);
p.setId("poolId");
ValidationResult results = enforcer.preEntitlement(c, p, 1, CallerType.BIND);
assertNotNull(results);
assertEquals(1, results.getErrors().size());
assertTrue(results.getWarnings().isEmpty());
ValidationError error = results.getErrors().get(0);
assertEquals("rulefailed.derivedproduct.unsupported.by.consumer", error.getResourceKey());
}
Aggregations