use of org.candlepin.model.GuestId in project candlepin by candlepin.
the class InstalledProductStatusCalculatorTest method validRangeWhenGuestLimitOverridden.
@Test
public void validRangeWhenGuestLimitOverridden() {
Date now = new Date();
Owner owner = TestUtil.createOwner();
Product product = TestUtil.createProduct("p1", "product1");
product.setAttribute(Product.Attributes.GUEST_LIMIT, "2");
Product product2 = TestUtil.createProduct("p2", "product2");
product2.setAttribute(Product.Attributes.GUEST_LIMIT, "-1");
Product product3 = TestUtil.createProduct("p3", "product3");
Consumer consumer = this.mockConsumer(owner, product);
for (int i = 0; i < 5; i++) {
consumer.addGuestId(new GuestId(String.valueOf(i), consumer, this.getActiveGuestAttrs()));
}
DateRange range1 = this.rangeRelativeToDate(now, -4, 4);
DateRange range2 = this.rangeRelativeToDate(now, -2, 2);
consumer.addEntitlement(this.mockStackedEntitlement(owner, consumer, "stack_id_1", product, 10, range1, product));
consumer.addEntitlement(this.mockStackedEntitlement(owner, consumer, "stack_id_2", product2, 10, range2, product3));
this.mockConsumerEntitlements(consumer, consumer.getEntitlements());
this.mockOwnerProducts(owner, Arrays.asList(product, product2, product3));
this.consumerEnricher.enrich(consumer);
ConsumerInstalledProduct cip = this.getInstalledProduct(consumer, product);
assertEquals(range2.getStartDate(), cip.getStartDate());
assertEquals(range2.getEndDate(), cip.getEndDate());
}
use of org.candlepin.model.GuestId in project candlepin by candlepin.
the class EventFactoryTest method testGuestIdCreation.
@Test
public void testGuestIdCreation() throws Exception {
// this test is testing bz 786730, to ensure
// the virt-who error does not occur
Consumer consumer = mock(Consumer.class);
GuestId guestId = mock(GuestId.class);
when(guestId.getConsumer()).thenReturn(consumer);
when(guestId.getGuestId()).thenReturn("guest-id");
when(guestId.getId()).thenReturn("test");
when(consumer.getOwnerId()).thenReturn("owner-id");
when(consumer.getId()).thenReturn("consumer-id");
Event event = eventFactory.guestIdCreated(guestId);
assertNotNull(event.getEntityId());
}
use of org.candlepin.model.GuestId in project candlepin by candlepin.
the class ConsumerTranslatorTest method initSourceObject.
@Override
protected Consumer initSourceObject() {
ConsumerType ctype = this.consumerTypeTranslatorTest.initSourceObject();
Environment environment = this.environmentTranslatorTest.initSourceObject();
Owner owner = this.ownerTranslatorTest.initSourceObject();
when(mockOwnerCurator.findOwnerById(eq(owner.getId()))).thenReturn(owner);
Consumer consumer = new Consumer();
consumer.setId("consumer_id");
consumer.setUuid("consumer_uuid");
consumer.setName("consumer_name");
consumer.setUsername("consumer_user_name");
consumer.setEntitlementStatus("consumer_ent_status");
consumer.setServiceLevel("consumer_service_level");
consumer.setReleaseVer(new Release("releaseVer"));
consumer.setOwner(owner);
consumer.setEnvironment(environment);
consumer.setEntitlementCount(0L);
consumer.setLastCheckin(new Date());
consumer.setCanActivate(Boolean.TRUE);
consumer.setHypervisorId(hypervisorIdTranslatorTest.initSourceObject());
consumer.setAutoheal(Boolean.TRUE);
consumer.setRecipientOwnerKey("test_recipient_owner_key");
consumer.setAnnotations("test_annotations");
consumer.setContentAccessMode("test_content_access_mode");
consumer.setIdCert((IdentityCertificate) this.certificateTranslatorTest.initSourceObject());
consumer.setType(ctype);
Map<String, String> facts = new HashMap<>();
for (int i = 0; i < 5; ++i) {
facts.put("fact-" + i, "value-" + i);
}
consumer.setFacts(facts);
Set<ConsumerInstalledProduct> installedProducts = new HashSet<>();
for (int i = 0; i < 5; ++i) {
ConsumerInstalledProduct installedProduct = cipTranslatorTest.initSourceObject();
installedProduct.setId("installedProduct-" + i);
installedProducts.add(installedProduct);
}
consumer.setInstalledProducts(installedProducts);
Set<ConsumerCapability> capabilities = new HashSet<>();
for (int i = 0; i < 5; ++i) {
ConsumerCapability capability = capabilityTranslatorTest.initSourceObject();
capability.setName("capability-" + i);
capabilities.add(capability);
}
consumer.setCapabilities(capabilities);
Set<String> contentTags = new HashSet<>();
for (int i = 0; i < 5; ++i) {
contentTags.add("contentTag-" + i);
}
consumer.setContentTags(contentTags);
List<GuestId> guestIds = new LinkedList<>();
for (int i = 0; i < 5; ++i) {
GuestId guestId = guestIdTranslatorTest.initSourceObject();
guestId.setId("guestId-" + i);
guestIds.add(guestId);
}
consumer.setGuestIds(guestIds);
when(mockConsumerTypeCurator.find(eq(ctype.getId()))).thenReturn(ctype);
when(mockConsumerTypeCurator.getConsumerType(eq(consumer))).thenReturn(ctype);
when(mockEnvironmentCurator.find(eq(environment.getId()))).thenReturn(environment);
when(mockEnvironmentCurator.getConsumerEnvironment(eq(consumer))).thenReturn(environment);
return consumer;
}
use of org.candlepin.model.GuestId in project candlepin by candlepin.
the class GuestIdResource method getGuestId.
@ApiOperation(notes = "Retrieves a single Guest By its consumer and the guest UUID", value = "getGuestId")
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/{guest_id}")
public GuestIdDTO getGuestId(@PathParam("consumer_uuid") @Verify(Consumer.class) String consumerUuid, @PathParam("guest_id") String guestId) {
Consumer consumer = consumerCurator.findByUuid(consumerUuid);
GuestId result = validateGuestId(guestIdCurator.findByConsumerAndId(consumer, guestId), guestId);
return translator.translate(result, GuestIdDTO.class);
}
use of org.candlepin.model.GuestId in project candlepin by candlepin.
the class HypervisorResource method hypervisorUpdate.
/**
* @deprecated Use the asynchronous method
* @return HypervisorCheckInResult
*/
@ApiOperation(notes = "Updates the list of Hypervisor Guests Allows agents such as " + "virt-who to update its host list and associate the guests for each host. This is " + "typically used when a host is unable to register to candlepin via subscription" + " manager. In situations where consumers already exist it is probably best not " + "to allow creation of new hypervisor consumers. Most consumers do not have a" + " hypervisorId attribute, so that should be added manually when necessary by the " + "management environment. @deprecated Use the asynchronous method", value = "hypervisorUpdate")
@ApiResponses({ @ApiResponse(code = 202, message = "") })
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Deprecated
@Transactional
@UpdateConsumerCheckIn
@SuppressWarnings("checkstyle:indentation")
public HypervisorCheckInResult hypervisorUpdate(Map<String, List<GuestIdDTO>> hostGuestDTOMap, @Context Principal principal, @QueryParam("owner") @Verify(value = Owner.class, require = Access.READ_ONLY, subResource = SubResource.HYPERVISOR) String ownerKey, @ApiParam("specify whether or not to create missing hypervisors." + "Default is true. If false is specified, hypervisorIds that are not found" + "will result in failed entries in the resulting HypervisorCheckInResult") @QueryParam("create_missing") @DefaultValue("true") boolean createMissing) {
log.debug("Hypervisor check-in by principal: {}", principal);
if (hostGuestDTOMap == null) {
log.debug("Host/Guest mapping provided during hypervisor checkin was null.");
throw new BadRequestException(i18n.tr("Host to guest mapping was not provided for hypervisor check-in."));
}
Owner owner = this.getOwner(ownerKey);
if (owner.isAutobindDisabled()) {
log.debug("Could not update host/guest mapping. Auto-attach is disabled for owner {}", owner.getKey());
throw new BadRequestException(i18n.tr("Could not update host/guest mapping. Auto-attach is disabled for owner {0}.", owner.getKey()));
}
if (hostGuestDTOMap.remove("") != null) {
log.warn("Ignoring empty hypervisor id");
}
// Maps virt hypervisor ID to registered consumer for that hypervisor, should one exist:
VirtConsumerMap hypervisorConsumersMap = consumerCurator.getHostConsumersMap(owner, hostGuestDTOMap.keySet());
int emptyGuestIdCount = 0;
Set<String> allGuestIds = new HashSet<>();
Collection<List<GuestIdDTO>> idsLists = hostGuestDTOMap.values();
for (List<GuestIdDTO> guestIds : idsLists) {
// See bzs 1332637, 1332635
if (guestIds == null) {
continue;
}
for (Iterator<GuestIdDTO> guestIdsItr = guestIds.iterator(); guestIdsItr.hasNext(); ) {
String id = guestIdsItr.next().getGuestId();
if (StringUtils.isEmpty(id)) {
emptyGuestIdCount++;
guestIdsItr.remove();
} else {
allGuestIds.add(id);
}
}
}
if (emptyGuestIdCount > 0) {
log.warn("Ignoring {} empty/null guest id(s).", emptyGuestIdCount);
}
HypervisorCheckInResult result = new HypervisorCheckInResult();
for (Entry<String, List<GuestIdDTO>> hostEntry : hostGuestDTOMap.entrySet()) {
String hypervisorId = hostEntry.getKey();
// See bzs 1332637, 1332635
if (hostEntry.getValue() == null) {
hostEntry.setValue(new ArrayList<>());
}
try {
log.debug("Syncing virt host: {} ({} guest IDs)", hypervisorId, hostEntry.getValue().size());
boolean hostConsumerCreated = false;
boolean updatedType = false;
// Attempt to find a consumer for the given hypervisorId
Consumer consumer = null;
if (hypervisorConsumersMap.get(hypervisorId) == null) {
if (!createMissing) {
log.info("Unable to find hypervisor with id {} in org {}", hypervisorId, ownerKey);
result.failed(hypervisorId, i18n.tr("Unable to find hypervisor in org \"{0}\"", ownerKey));
continue;
}
log.debug("Registering new host consumer for hypervisor ID: {}", hypervisorId);
consumer = createConsumerForHypervisorId(hypervisorId, owner, principal);
hostConsumerCreated = true;
} else {
consumer = hypervisorConsumersMap.get(hypervisorId);
if (!hypervisorType.getId().equals(consumer.getTypeId())) {
consumer.setType(hypervisorType);
updatedType = true;
}
}
List<GuestId> guestIds = new ArrayList<>();
guestIdResource.populateEntities(guestIds, hostEntry.getValue());
boolean guestIdsUpdated = addGuestIds(consumer, guestIds);
Date now = new Date();
consumerCurator.updateLastCheckin(consumer, now);
consumer.setLastCheckin(now);
// Populate the result with the processed consumer.
if (hostConsumerCreated) {
result.created(consumer);
} else if (guestIdsUpdated || updatedType) {
result.updated(consumer);
} else {
result.unchanged(consumer);
}
} catch (Exception e) {
log.error("Hypervisor checkin failed", e);
result.failed(hypervisorId, e.getMessage());
}
}
log.info("Summary of hypervisor checkin by principal \"{}\": {}", principal, result);
return result;
}
Aggregations