use of org.candlepin.resource.util.GuestMigration in project candlepin by candlepin.
the class GuestIdResourceTest method setUp.
@Before
public void setUp() {
testMigration = Mockito.spy(new GuestMigration(consumerCurator));
migrationProvider = Providers.of(testMigration);
this.modelTranslator = new StandardTranslator(this.consumerTypeCurator, this.environmentCurator, this.ownerCurator);
i18n = I18nFactory.getI18n(getClass(), Locale.US, I18nFactory.FALLBACK);
owner = new Owner("test-owner", "Test Owner");
ct = new ConsumerType(ConsumerTypeEnum.SYSTEM);
ct.setId("test-system-ctype");
consumer = new Consumer("consumer", "test", owner, ct);
guestIdResource = new GuestIdResource(guestIdCurator, consumerCurator, consumerTypeCurator, consumerResource, i18n, eventFactory, sink, migrationProvider, this.modelTranslator);
when(consumerCurator.findByUuid(consumer.getUuid())).thenReturn(consumer);
when(consumerCurator.verifyAndLookupConsumer(consumer.getUuid())).thenReturn(consumer);
}
use of org.candlepin.resource.util.GuestMigration in project candlepin by candlepin.
the class GuestIdResource method updateGuests.
@ApiOperation(notes = "Updates the List of Guests on a Consumer This method should work " + "just like updating the consumer, except that it only updates GuestIds. " + " Eventually we should move All the logic here, and depricate updating guests " + "through the consumer update.", value = "updateGuests")
@PUT
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public void updateGuests(@PathParam("consumer_uuid") @Verify(Consumer.class) String consumerUuid, @ApiParam(name = "guestIds", required = true) List<GuestIdDTO> guestIdDTOs) {
Consumer toUpdate = consumerCurator.findByUuid(consumerUuid);
// Create a skeleton consumer for consumerResource.performConsumerUpdates
ConsumerDTO consumer = new ConsumerDTO();
consumer.setGuestIds(guestIdDTOs);
Set<String> allGuestIds = new HashSet<>();
for (GuestIdDTO gid : consumer.getGuestIds()) {
allGuestIds.add(gid.getGuestId());
}
VirtConsumerMap guestConsumerMap = consumerCurator.getGuestConsumersMap(toUpdate.getOwnerId(), allGuestIds);
GuestMigration guestMigration = migrationProvider.get().buildMigrationManifest(consumer, toUpdate);
if (consumerResource.performConsumerUpdates(consumer, toUpdate, guestMigration)) {
if (guestMigration.isMigrationPending()) {
guestMigration.migrate();
} else {
consumerCurator.update(toUpdate);
}
}
}
use of org.candlepin.resource.util.GuestMigration in project candlepin by candlepin.
the class HypervisorResource method addGuestIds.
/*
* Add a list of guestIds to the given consumer,
* return whether or not there was any change
*/
private boolean addGuestIds(Consumer consumer, List<GuestId> guestIds) {
Consumer withIds = new Consumer();
withIds.setGuestIds(guestIds);
GuestMigration guestMigration = migrationProvider.get().buildMigrationManifest(withIds, consumer);
boolean guestIdsUpdated = consumerResource.performConsumerUpdates(this.translator.translate(withIds, ConsumerDTO.class), consumer, guestMigration);
if (guestIdsUpdated) {
if (guestMigration.isMigrationPending()) {
guestMigration.migrate();
} else {
consumerCurator.update(consumer);
}
}
return guestIdsUpdated;
}
use of org.candlepin.resource.util.GuestMigration in project candlepin by candlepin.
the class HypervisorResourceTest method setupTest.
@Before
public void setupTest() {
Configuration config = new CandlepinCommonTestConfig();
testMigration = new GuestMigration(consumerCurator);
migrationProvider = Providers.of(testMigration);
this.i18n = I18nFactory.getI18n(getClass(), Locale.US, I18nFactory.FALLBACK);
this.hypervisorType = new ConsumerType(ConsumerTypeEnum.HYPERVISOR);
this.hypervisorType.setId("test-hypervisor-ctype");
this.mockConsumerType(this.hypervisorType);
this.modelTranslator = new StandardTranslator(this.consumerTypeCurator, this.environmentCurator, this.ownerCurator);
this.consumerResource = new ConsumerResource(this.consumerCurator, this.consumerTypeCurator, null, this.subscriptionService, this.ownerService, null, this.idCertService, null, this.i18n, this.sink, this.eventFactory, null, null, this.userService, null, null, this.ownerCurator, this.activationKeyCurator, null, this.complianceRules, this.deletedConsumerCurator, null, null, config, null, null, null, this.consumerBindUtil, null, null, new FactValidator(config, this.i18n), null, consumerEnricher, migrationProvider, modelTranslator);
this.guestIdResource = new GuestIdResource(this.guestIdCurator, this.consumerCurator, this.consumerTypeCurator, this.consumerResource, this.i18n, this.eventFactory, this.sink, migrationProvider, modelTranslator);
this.hypervisorResource = new HypervisorResource(consumerResource, consumerCurator, consumerTypeCurator, i18n, ownerCurator, migrationProvider, modelTranslator, guestIdResource);
// Ensure that we get the consumer that was passed in back from the create call.
when(consumerCurator.create(any(Consumer.class))).thenAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return invocation.getArguments()[0];
}
});
when(consumerCurator.create(any(Consumer.class), any(Boolean.class))).thenAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return invocation.getArguments()[0];
}
});
when(complianceRules.getStatus(any(Consumer.class), any(Date.class), any(Boolean.class))).thenReturn(new ComplianceStatus(new Date()));
when(ownerCurator.lookupByKey(any(String.class))).thenReturn(new Owner());
when(eventFactory.getEventBuilder(any(Target.class), any(Type.class))).thenReturn(consumerEventBuilder);
when(consumerEventBuilder.setEventData(any(Consumer.class))).thenReturn(consumerEventBuilder);
}
use of org.candlepin.resource.util.GuestMigration in project candlepin by candlepin.
the class ConsumerResource method updateConsumer.
// While this is a PUT, we are treating it as a PATCH until this operation
// becomes more prevalent. We only update the portions of the consumer that appear
// to be set.
@ApiOperation(notes = "Updates a Consumer", value = "updateConsumer")
@ApiResponses({ @ApiResponse(code = 404, message = "") })
@PUT
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("{consumer_uuid}")
@Transactional
@UpdateConsumerCheckIn
public void updateConsumer(@PathParam("consumer_uuid") @Verify(Consumer.class) String uuid, @ApiParam(name = "consumer", required = true) ConsumerDTO dto, @Context Principal principal) {
Consumer toUpdate = consumerCurator.verifyAndLookupConsumer(uuid);
dto.setUuid(uuid);
// Sanitize the inbound facts before applying the update
this.sanitizeConsumerFacts(dto);
ConsumerType toUpdateType = this.consumerTypeCurator.getConsumerType(toUpdate);
if (toUpdateType.isType(ConsumerTypeEnum.SHARE)) {
validateShareConsumerUpdate(toUpdate, dto, principal);
}
GuestMigration guestMigration = migrationProvider.get();
guestMigration.buildMigrationManifest(dto, toUpdate);
if (performConsumerUpdates(dto, toUpdate, guestMigration)) {
try {
if (guestMigration.isMigrationPending()) {
guestMigration.migrate();
} else {
consumerCurator.update(toUpdate);
}
} catch (CandlepinException ce) {
// If it is one of ours, rethrow it.
throw ce;
} catch (Exception e) {
log.error("Problem updating unit:", e);
throw new BadRequestException(i18n.tr("Problem updating unit {0}", dto));
}
}
}
Aggregations