Search in sources :

Example 36 with ConsumerDTO

use of org.candlepin.dto.api.v1.ConsumerDTO in project candlepin by candlepin.

the class ConsumerResource method checkForHypervisorIdUpdate.

private boolean checkForHypervisorIdUpdate(Consumer existing, ConsumerDTO incoming) {
    HypervisorIdDTO incomingId = incoming.getHypervisorId();
    if (incomingId != null) {
        HypervisorId existingId = existing.getHypervisorId();
        if (incomingId.getHypervisorId() == null || incomingId.getHypervisorId().isEmpty()) {
            // Allow hypervisorId to be removed
            existing.setHypervisorId(null);
        } else {
            if (existingId != null) {
                if (existingId.getHypervisorId() != null && !existingId.getHypervisorId().equals(incomingId.getHypervisorId())) {
                    existingId.setHypervisorId(incomingId.getHypervisorId());
                    Owner owner = ownerCurator.findOwnerById(existing.getOwnerId());
                    existingId.setOwner(owner);
                } else {
                    return false;
                }
            } else {
                // Safer to build a new clean HypervisorId object
                Owner owner = ownerCurator.findOwnerById(existing.getOwnerId());
                HypervisorId hypervisorId = new HypervisorId(incomingId.getHypervisorId());
                hypervisorId.setOwner(owner);
                existing.setHypervisorId(hypervisorId);
            }
        }
        return true;
    }
    return false;
}
Also used : HypervisorIdDTO(org.candlepin.dto.api.v1.HypervisorIdDTO) Owner(org.candlepin.model.Owner) HypervisorId(org.candlepin.model.HypervisorId)

Example 37 with ConsumerDTO

use of org.candlepin.dto.api.v1.ConsumerDTO in project candlepin by candlepin.

the class ConsumerResource method populateEntity.

/**
 * Populates the specified entity with data from the provided DTO, during consumer creation (not update).
 * This method will not set the ID, entitlementStatus, complianceStatusHash, idCert, entitlements,
 * keyPair and canActivate, because clients are not allowed to create or update those properties.
 *
 * while autoheal is populated, it is overridden in create method.
 *
 * owner is not populated because create populates it differently.
 *
 * @param entity
 *  The entity instance to populate
 *
 * @param dto
 *  The DTO containing the data with which to populate the entity
 *
 * @throws IllegalArgumentException
 *  if either entity or dto are null
 */
protected void populateEntity(Consumer entity, ConsumerDTO dto) {
    if (entity == null) {
        throw new IllegalArgumentException("the consumer model entity is null");
    }
    if (dto == null) {
        throw new IllegalArgumentException("the consumer dto is null");
    }
    if (dto.getCreated() != null) {
        entity.setCreated(dto.getCreated());
    }
    if (dto.getName() != null) {
        entity.setName(dto.getName());
    }
    if (dto.getUuid() != null) {
        entity.setUuid(dto.getUuid());
    }
    if (dto.getFacts() != null) {
        entity.setFacts(dto.getFacts());
    }
    if (dto.getUsername() != null) {
        entity.setUsername(dto.getUsername());
    }
    if (dto.getServiceLevel() != null) {
        entity.setServiceLevel(dto.getServiceLevel());
    }
    if (dto.getReleaseVersion() != null) {
        entity.setReleaseVer(new Release(dto.getReleaseVersion()));
    }
    if (dto.getEnvironment() != null) {
        Environment env = environmentCurator.find(dto.getEnvironment().getId());
        if (env == null) {
            throw new NotFoundException(i18n.tr("Environment \"{0}\" could not be found.", dto.getEnvironment().getId()));
        }
        entity.setEnvironment(env);
    }
    if (dto.getLastCheckin() != null) {
        entity.setLastCheckin(dto.getLastCheckin());
    }
    if (dto.getCapabilities() != null) {
        Set<ConsumerCapability> capabilities = populateCapabilities(entity, dto);
        entity.setCapabilities(capabilities);
    }
    if (dto.getGuestIds() != null) {
        List<GuestId> guestIds = new ArrayList<>();
        for (GuestIdDTO guestIdDTO : dto.getGuestIds()) {
            if (guestIdDTO != null) {
                guestIds.add(new GuestId(guestIdDTO.getGuestId(), entity, guestIdDTO.getAttributes()));
            }
        }
        entity.setGuestIds(guestIds);
    }
    if (dto.getHypervisorId() != null && entity.getOwnerId() != null) {
        HypervisorId hypervisorId = new HypervisorId(entity, ownerCurator.findOwnerById(entity.getOwnerId()), dto.getHypervisorId().getHypervisorId(), dto.getHypervisorId().getReporterId());
        entity.setHypervisorId(hypervisorId);
    }
    if (dto.getHypervisorId() == null && dto.getFact("system_uuid") != null && !"true".equals(dto.getFact("virt.is_guest")) && entity.getOwnerId() != null) {
        HypervisorId hypervisorId = new HypervisorId(entity, ownerCurator.findOwnerById(entity.getOwnerId()), dto.getFact("system_uuid"));
        entity.setHypervisorId(hypervisorId);
    }
    if (dto.getContentTags() != null) {
        entity.setContentTags(dto.getContentTags());
    }
    if (dto.getAutoheal() != null) {
        entity.setAutoheal(dto.getAutoheal());
    }
    if (dto.getContentAccessMode() != null) {
        entity.setContentAccessMode(dto.getContentAccessMode());
    }
    if (dto.getRecipientOwnerKey() != null) {
        entity.setRecipientOwnerKey(dto.getRecipientOwnerKey());
    }
    if (dto.getAnnotations() != null) {
        entity.setAnnotations(dto.getAnnotations());
    }
    if (dto.getInstalledProducts() != null) {
        Set<ConsumerInstalledProduct> installedProducts = populateInstalledProducts(entity, dto);
        entity.setInstalledProducts(installedProducts);
    }
}
Also used : GuestIdDTO(org.candlepin.dto.api.v1.GuestIdDTO) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) ArrayList(java.util.ArrayList) NotFoundException(org.candlepin.common.exceptions.NotFoundException) ConsumerCapability(org.candlepin.model.ConsumerCapability) GuestId(org.candlepin.model.GuestId) Environment(org.candlepin.model.Environment) HypervisorId(org.candlepin.model.HypervisorId) Release(org.candlepin.model.Release)

Example 38 with ConsumerDTO

use of org.candlepin.dto.api.v1.ConsumerDTO 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 39 with ConsumerDTO

use of org.candlepin.dto.api.v1.ConsumerDTO in project candlepin by candlepin.

the class ConsumerResourceTest method testIdCertDoesNotRegenerate.

@Test
public void testIdCertDoesNotRegenerate() throws Exception {
    SubscriptionServiceAdapter ssa = Mockito.mock(SubscriptionServiceAdapter.class);
    ComplianceRules rules = Mockito.mock(ComplianceRules.class);
    Consumer consumer = createConsumer(createOwner());
    ComplianceStatus status = new ComplianceStatus();
    when(rules.getStatus(any(Consumer.class), any(Date.class), anyBoolean())).thenReturn(status);
    consumer.setIdCert(createIdCert(TestUtil.createDate(2025, 6, 9)));
    BigInteger origserial = consumer.getIdCert().getSerial().getSerial();
    ConsumerResource cr = new ConsumerResource(mockConsumerCurator, mockConsumerTypeCurator, null, ssa, this.mockOwnerServiceAdapter, null, null, null, null, null, null, null, null, null, null, null, mockOwnerCurator, null, null, rules, null, null, null, this.config, null, null, null, consumerBindUtil, null, null, this.factValidator, null, consumerEnricher, migrationProvider, translator);
    ConsumerDTO c = cr.getConsumer(consumer.getUuid());
    assertEquals(origserial, c.getIdCert().getSerial().getSerial());
}
Also used : Consumer(org.candlepin.model.Consumer) ComplianceStatus(org.candlepin.policy.js.compliance.ComplianceStatus) ConsumerDTO(org.candlepin.dto.api.v1.ConsumerDTO) BigInteger(java.math.BigInteger) ComplianceRules(org.candlepin.policy.js.compliance.ComplianceRules) SubscriptionServiceAdapter(org.candlepin.service.SubscriptionServiceAdapter) Date(java.util.Date) Test(org.junit.Test)

Example 40 with ConsumerDTO

use of org.candlepin.dto.api.v1.ConsumerDTO in project candlepin by candlepin.

the class ConsumerResourceTest method testFetchAllConsumersForSomeUUIDs.

@Test
public void testFetchAllConsumersForSomeUUIDs() {
    ModelTranslator mockTranslator = mock(ModelTranslator.class);
    ConsumerResource cr = new ConsumerResource(mockConsumerCurator, mockConsumerTypeCurator, null, null, null, null, null, null, i18n, null, null, null, null, null, null, null, null, null, null, null, null, null, null, this.config, null, null, null, null, null, null, this.factValidator, new ConsumerTypeValidator(null, null), consumerEnricher, migrationProvider, mockTranslator);
    ArrayList<Consumer> consumers = new ArrayList<>();
    CandlepinQuery cqmock = mock(CandlepinQuery.class);
    when(cqmock.list()).thenReturn(consumers);
    when(cqmock.iterator()).thenReturn(consumers.iterator());
    when(mockConsumerCurator.searchOwnerConsumers(any(Owner.class), anyString(), (java.util.Collection<ConsumerType>) any(Collection.class), any(List.class), any(List.class), any(List.class), any(List.class), any(List.class), any(List.class))).thenReturn(cqmock);
    when(mockTranslator.translateQuery(eq(cqmock), eq(ConsumerDTO.class))).thenReturn(cqmock);
    List<String> uuids = new ArrayList<>();
    uuids.add("swiftuuid");
    List<ConsumerDTO> result = cr.list(null, null, null, uuids, null, null, null).list();
    assertEquals(consumers, result);
}
Also used : Owner(org.candlepin.model.Owner) ArrayList(java.util.ArrayList) CandlepinQuery(org.candlepin.model.CandlepinQuery) Matchers.anyString(org.mockito.Matchers.anyString) ConsumerTypeValidator(org.candlepin.resource.util.ConsumerTypeValidator) Consumer(org.candlepin.model.Consumer) ConsumerDTO(org.candlepin.dto.api.v1.ConsumerDTO) ArrayList(java.util.ArrayList) List(java.util.List) ModelTranslator(org.candlepin.dto.ModelTranslator) ConsumerType(org.candlepin.model.ConsumerType) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)70 ConsumerDTO (org.candlepin.dto.api.v1.ConsumerDTO)64 Consumer (org.candlepin.model.Consumer)37 Owner (org.candlepin.model.Owner)20 UserPrincipal (org.candlepin.auth.UserPrincipal)19 HashSet (java.util.HashSet)16 ConsumerType (org.candlepin.model.ConsumerType)16 ConsumerDTO (org.candlepin.dto.manifest.v1.ConsumerDTO)15 Principal (org.candlepin.auth.Principal)14 NoAuthPrincipal (org.candlepin.auth.NoAuthPrincipal)13 TrustedUserPrincipal (org.candlepin.auth.TrustedUserPrincipal)12 Set (java.util.Set)10 ArrayList (java.util.ArrayList)9 Date (java.util.Date)8 TestUtil.createConsumerDTO (org.candlepin.test.TestUtil.createConsumerDTO)8 ConsumerTypeDTO (org.candlepin.dto.api.v1.ConsumerTypeDTO)7 ConsumerTypeDTO (org.candlepin.dto.manifest.v1.ConsumerTypeDTO)7 OwnerDTO (org.candlepin.dto.manifest.v1.OwnerDTO)7 UpstreamConsumer (org.candlepin.model.UpstreamConsumer)7 GuestIdDTO (org.candlepin.dto.api.v1.GuestIdDTO)5