use of org.candlepin.model.HypervisorId in project candlepin by candlepin.
the class ConsumerResource method createConsumerFromDTO.
public Consumer createConsumerFromDTO(ConsumerDTO consumer, ConsumerType type, Principal principal, String userName, String ownerKey, String activationKeys, boolean identityCertCreation) throws BadRequestException {
// API:registerConsumer
Set<String> keyStrings = splitKeys(activationKeys);
// Only let NoAuth principals through if there are activation keys to consider:
if ((principal instanceof NoAuthPrincipal) && keyStrings.isEmpty()) {
throw new ForbiddenException(i18n.tr("Insufficient permissions"));
}
validateOnKeyStrings(keyStrings, ownerKey, userName);
Owner owner = setupOwner(principal, ownerKey);
// Raise an exception if none of the keys specified exist for this owner.
List<ActivationKey> keys = checkActivationKeys(principal, owner, keyStrings);
userName = setUserName(consumer, principal, userName);
checkConsumerName(consumer);
validateViaConsumerType(consumer, type, keys, owner, userName, principal);
if (type.isType(ConsumerTypeEnum.SHARE)) {
// Share consumers do not need identity certificates so refuse to create them.
identityCertCreation = false;
validateShareConsumer(consumer, principal, keys);
// if there exists a share consumer between the two orgs, return it.
Consumer existingShareConsumer = consumerCurator.getSharingConsumer(owner, consumer.getRecipientOwnerKey());
if (existingShareConsumer != null) {
return existingShareConsumer;
}
consumer.setAutoheal(false);
} else {
// this is the default
consumer.setAutoheal(true);
if (StringUtils.isNotEmpty(consumer.getRecipientOwnerKey())) {
throw new BadRequestException(i18n.tr("Only share consumers can specify recipient owners"));
}
}
if (consumer.getServiceLevel() == null) {
consumer.setServiceLevel("");
}
// Sanitize the inbound facts
this.sanitizeConsumerFacts(consumer);
// If no service level was specified, and the owner has a default set, use it:
if (consumer.getServiceLevel().equals("") && owner.getDefaultServiceLevel() != null && !type.isType(ConsumerTypeEnum.SHARE)) {
consumer.setServiceLevel(owner.getDefaultServiceLevel());
}
Consumer consumerToCreate = new Consumer();
consumerToCreate.setOwner(owner);
populateEntity(consumerToCreate, consumer);
consumerToCreate.setType(type);
if (!type.isType(ConsumerTypeEnum.SHARE)) {
consumerToCreate.setCanActivate(subAdapter.canActivateSubscription(consumerToCreate));
}
HypervisorId hvsrId = consumerToCreate.getHypervisorId();
if (hvsrId != null && hvsrId.getHypervisorId() != null && !hvsrId.getHypervisorId().isEmpty()) {
// If a hypervisorId is supplied, make sure the consumer and owner are correct
hvsrId.setConsumer(consumerToCreate);
hvsrId.setOwner(owner);
}
updateCapabilities(consumerToCreate, null);
logNewConsumerDebugInfo(consumerToCreate, keys, type);
validateContentAccessMode(consumerToCreate, owner);
consumerBindUtil.validateServiceLevel(owner.getId(), consumerToCreate.getServiceLevel());
try {
Date createdDate = consumerToCreate.getCreated();
Date lastCheckIn = consumerToCreate.getLastCheckin();
// create sets created to current time.
consumerToCreate = consumerCurator.create(consumerToCreate);
// If we sent in a created date, we want it persisted at the update below
if (createdDate != null) {
consumerToCreate.setCreated(createdDate);
}
if (lastCheckIn != null) {
log.info("Creating with specific last check-in time: {}", lastCheckIn);
consumerToCreate.setLastCheckin(lastCheckIn);
}
if (identityCertCreation) {
IdentityCertificate idCert = generateIdCert(consumerToCreate, false);
consumerToCreate.setIdCert(idCert);
}
sink.emitConsumerCreated(consumerToCreate);
if (keys.size() > 0) {
consumerBindUtil.handleActivationKeys(consumerToCreate, keys, owner.isAutobindDisabled());
}
// Don't allow complianceRules to update entitlementStatus, because we're about to perform
// an update unconditionally.
complianceRules.getStatus(consumerToCreate, null, false, false);
consumerCurator.update(consumerToCreate);
log.info("Consumer {} created in org {}", consumerToCreate.getUuid(), consumerToCreate.getOwnerId());
return consumerToCreate;
} catch (CandlepinException ce) {
// If it is one of ours, rethrow it.
throw ce;
} catch (Exception e) {
log.error("Problem creating unit:", e);
throw new BadRequestException(i18n.tr("Problem creating unit {0}", consumer));
}
}
use of org.candlepin.model.HypervisorId in project candlepin by candlepin.
the class HypervisorResource method createConsumerForHypervisorId.
/*
* Create a new hypervisor type consumer to represent the incoming hypervisorId
*/
private Consumer createConsumerForHypervisorId(String incHypervisorId, Owner owner, Principal principal) {
Consumer consumer = new Consumer();
consumer.setName(incHypervisorId);
consumer.setType(this.hypervisorType);
consumer.setFact("uname.machine", "x86_64");
consumer.setGuestIds(new ArrayList<>());
consumer.setOwner(owner);
// Create HypervisorId
HypervisorId hypervisorId = new HypervisorId(consumer, owner, incHypervisorId);
hypervisorId.setOwner(owner);
consumer.setHypervisorId(hypervisorId);
// Create Consumer
return consumerResource.createConsumerFromDTO(this.translator.translate(consumer, ConsumerDTO.class), this.hypervisorType, principal, null, owner.getKey(), null, false);
}
use of org.candlepin.model.HypervisorId in project candlepin by candlepin.
the class HypervisorUpdateJob method createConsumerForHypervisorId.
/*
* Create a new hypervisor type consumer to represent the incoming hypervisorId
*/
private Consumer createConsumerForHypervisorId(String incHypervisorId, String reporterId, Owner owner, Principal principal, Consumer incoming) {
Consumer consumer = new Consumer();
if (incoming.getName() != null) {
consumer.setName(incoming.getName());
} else {
consumer.setName(sanitizeHypervisorId(incHypervisorId));
}
consumer.setType(hypervisorType);
consumer.setFact("uname.machine", "x86_64");
consumer.setGuestIds(new ArrayList<>());
consumer.setLastCheckin(new Date());
consumer.setOwner(owner);
consumer.setAutoheal(true);
consumer.setCanActivate(subAdapter.canActivateSubscription(consumer));
if (owner.getDefaultServiceLevel() != null) {
consumer.setServiceLevel(owner.getDefaultServiceLevel());
} else {
consumer.setServiceLevel("");
}
if (principal.getUsername() != null) {
consumer.setUsername(principal.getUsername());
}
consumer.setEntitlementCount(0L);
// TODO: Refactor this to not call resource methods directly
consumerResource.sanitizeConsumerFacts(consumer);
// Create HypervisorId
HypervisorId hypervisorId = new HypervisorId(consumer, owner, incHypervisorId);
hypervisorId.setReporterId(reporterId);
consumer.setHypervisorId(hypervisorId);
// TODO: Refactor this to not call resource methods directly
consumerResource.checkForFactsUpdate(consumer, incoming);
return consumer;
}
use of org.candlepin.model.HypervisorId in project candlepin by candlepin.
the class HypervisorUpdateJobTest method reporterIdOnUpdateTest.
@Test
public void reporterIdOnUpdateTest() throws JobExecutionException {
when(ownerCurator.lookupByKey(eq("joe"))).thenReturn(owner);
Consumer hypervisor = new Consumer();
String hypervisorId = "uuid_999";
hypervisor.setHypervisorId(new HypervisorId(hypervisorId));
VirtConsumerMap vcm = new VirtConsumerMap();
vcm.add(hypervisorId, hypervisor);
when(consumerCurator.getHostConsumersMap(eq(owner), any(Set.class))).thenReturn(vcm);
JobDetail detail = HypervisorUpdateJob.forOwner(owner, hypervisorJson, true, principal, "updateReporterId");
JobExecutionContext ctx = mock(JobExecutionContext.class);
when(ctx.getMergedJobDataMap()).thenReturn(detail.getJobDataMap());
HypervisorUpdateJob job = new HypervisorUpdateJob(ownerCurator, consumerCurator, consumerTypeCurator, consumerResource, i18n, subAdapter, complianceRules);
injector.injectMembers(job);
job.execute(ctx);
assertEquals("updateReporterId", hypervisor.getHypervisorId().getReporterId());
}
Aggregations