Search in sources :

Example 6 with GuestMigration

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);
}
Also used : GuestMigration(org.candlepin.resource.util.GuestMigration) Owner(org.candlepin.model.Owner) Consumer(org.candlepin.model.Consumer) ConsumerType(org.candlepin.model.ConsumerType) StandardTranslator(org.candlepin.dto.StandardTranslator) Before(org.junit.Before)

Example 7 with GuestMigration

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);
        }
    }
}
Also used : GuestIdDTO(org.candlepin.dto.api.v1.GuestIdDTO) GuestMigration(org.candlepin.resource.util.GuestMigration) Consumer(org.candlepin.model.Consumer) VirtConsumerMap(org.candlepin.model.VirtConsumerMap) ConsumerDTO(org.candlepin.dto.api.v1.ConsumerDTO) HashSet(java.util.HashSet) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation) PUT(javax.ws.rs.PUT)

Example 8 with GuestMigration

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;
}
Also used : GuestMigration(org.candlepin.resource.util.GuestMigration) Consumer(org.candlepin.model.Consumer) ConsumerDTO(org.candlepin.dto.api.v1.ConsumerDTO)

Example 9 with GuestMigration

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);
}
Also used : FactValidator(org.candlepin.util.FactValidator) Owner(org.candlepin.model.Owner) Configuration(org.candlepin.common.config.Configuration) ComplianceStatus(org.candlepin.policy.js.compliance.ComplianceStatus) StandardTranslator(org.candlepin.dto.StandardTranslator) Date(java.util.Date) GuestMigration(org.candlepin.resource.util.GuestMigration) Target(org.candlepin.audit.Event.Target) Type(org.candlepin.audit.Event.Type) ConsumerType(org.candlepin.model.ConsumerType) Consumer(org.candlepin.model.Consumer) CandlepinCommonTestConfig(org.candlepin.config.CandlepinCommonTestConfig) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ConsumerType(org.candlepin.model.ConsumerType) Before(org.junit.Before)

Example 10 with GuestMigration

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));
        }
    }
}
Also used : GuestMigration(org.candlepin.resource.util.GuestMigration) CandlepinException(org.candlepin.common.exceptions.CandlepinException) DeletedConsumer(org.candlepin.model.DeletedConsumer) Consumer(org.candlepin.model.Consumer) BadRequestException(org.candlepin.common.exceptions.BadRequestException) ConsumerType(org.candlepin.model.ConsumerType) GeneralSecurityException(java.security.GeneralSecurityException) ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) IseException(org.candlepin.common.exceptions.IseException) AutobindDisabledForOwnerException(org.candlepin.controller.AutobindDisabledForOwnerException) CandlepinException(org.candlepin.common.exceptions.CandlepinException) IOException(java.io.IOException) NotFoundException(org.candlepin.common.exceptions.NotFoundException) ExportCreationException(org.candlepin.sync.ExportCreationException) BadRequestException(org.candlepin.common.exceptions.BadRequestException) PropertyValidationException(org.candlepin.util.PropertyValidationException) Path(javax.ws.rs.Path) UpdateConsumerCheckIn(org.candlepin.auth.UpdateConsumerCheckIn) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses) PUT(javax.ws.rs.PUT) Transactional(com.google.inject.persist.Transactional)

Aggregations

GuestMigration (org.candlepin.resource.util.GuestMigration)12 Consumer (org.candlepin.model.Consumer)10 ConsumerType (org.candlepin.model.ConsumerType)6 Before (org.junit.Before)6 StandardTranslator (org.candlepin.dto.StandardTranslator)5 Owner (org.candlepin.model.Owner)5 Date (java.util.Date)4 CandlepinCommonTestConfig (org.candlepin.config.CandlepinCommonTestConfig)4 FactValidator (org.candlepin.util.FactValidator)4 HashSet (java.util.HashSet)3 Target (org.candlepin.audit.Event.Target)3 Type (org.candlepin.audit.Event.Type)3 ComplianceStatus (org.candlepin.policy.js.compliance.ComplianceStatus)3 Transactional (com.google.inject.persist.Transactional)2 ApiOperation (io.swagger.annotations.ApiOperation)2 IOException (java.io.IOException)2 Consumes (javax.ws.rs.Consumes)2 PUT (javax.ws.rs.PUT)2 Produces (javax.ws.rs.Produces)2 Principal (org.candlepin.auth.Principal)2