Search in sources :

Example 6 with Environment

use of org.candlepin.model.Environment in project candlepin by candlepin.

the class DatabaseTestFixture method createEnvironment.

protected Environment createEnvironment(Owner owner, String id, String name, String description, Collection<Consumer> consumers, Collection<Content> content) {
    Environment environment = new Environment(id, name, owner);
    environment.setDescription(description);
    if (content != null) {
        for (Content elem : content) {
            EnvironmentContent envContent = new EnvironmentContent(environment, elem, true);
            // Impl note:
            // At the time of writing, this line is redundant. But if we ever fix environment,
            // this will be good to have as a backup.
            environment.getEnvironmentContent().add(envContent);
        }
    }
    environment = this.environmentCurator.create(environment);
    // Update consumers to point to the new environment
    if (consumers != null) {
        for (Consumer consumer : consumers) {
            consumer.setEnvironmentId(environment.getId());
            this.consumerCurator.merge(consumer);
        }
    }
    return environment;
}
Also used : Consumer(org.candlepin.model.Consumer) Content(org.candlepin.model.Content) EnvironmentContent(org.candlepin.model.EnvironmentContent) Environment(org.candlepin.model.Environment) EnvironmentContent(org.candlepin.model.EnvironmentContent)

Example 7 with Environment

use of org.candlepin.model.Environment in project candlepin by candlepin.

the class ConsumerTranslatorTest method verifyOutput.

@Override
protected void verifyOutput(Consumer source, ConsumerDTO dest, boolean childrenGenerated) {
    if (source != null) {
        assertEquals(source.getId(), dest.getId());
        assertEquals(source.getUuid(), dest.getUuid());
        assertEquals(source.getName(), dest.getName());
        assertEquals(source.getUsername(), dest.getUsername());
        assertEquals(source.getEntitlementStatus(), dest.getEntitlementStatus());
        assertEquals(source.getServiceLevel(), dest.getServiceLevel());
        assertEquals(source.getEntitlementCount(), (long) dest.getEntitlementCount());
        assertEquals(source.getFacts(), dest.getFacts());
        assertEquals(source.getLastCheckin(), dest.getLastCheckin());
        assertEquals(source.isCanActivate(), dest.isCanActivate());
        assertEquals(source.getContentTags(), dest.getContentTags());
        assertEquals(source.isAutoheal(), dest.getAutoheal());
        assertEquals(source.getRecipientOwnerKey(), dest.getRecipientOwnerKey());
        assertEquals(source.getAnnotations(), dest.getAnnotations());
        assertEquals(source.getContentAccessMode(), dest.getContentAccessMode());
        if (childrenGenerated) {
            ConsumerType ctype = this.mockConsumerTypeCurator.getConsumerType(source);
            this.consumerTypeTranslatorTest.verifyOutput(ctype, dest.getType(), true);
            Environment environment = this.mockEnvironmentCurator.getConsumerEnvironment(source);
            this.environmentTranslatorTest.verifyOutput(environment, dest.getEnvironment(), true);
            assertEquals(source.getReleaseVer().getReleaseVer(), dest.getReleaseVersion());
            String destOwnerId = null;
            if (dest.getOwner() != null) {
                destOwnerId = dest.getOwner().getId();
            }
            assertEquals(source.getOwnerId(), destOwnerId);
            this.hypervisorIdTranslatorTest.verifyOutput(source.getHypervisorId(), dest.getHypervisorId(), childrenGenerated);
            this.certificateTranslatorTest.verifyOutput(source.getIdCert(), dest.getIdCert(), true);
            if (source.getInstalledProducts() != null) {
                for (ConsumerInstalledProduct cip : source.getInstalledProducts()) {
                    for (ConsumerInstalledProductDTO cipDTO : dest.getInstalledProducts()) {
                        assertNotNull(cip);
                        assertNotNull(cipDTO);
                        this.cipTranslatorTest.verifyOutput(cip, cipDTO, childrenGenerated);
                    }
                }
            } else {
                assertNull(dest.getInstalledProducts());
            }
            if (source.getCapabilities() != null) {
                for (ConsumerCapability cc : source.getCapabilities()) {
                    boolean verified = false;
                    for (CapabilityDTO ccDTO : dest.getCapabilities()) {
                        assertNotNull(cc);
                        assertNotNull(ccDTO);
                        if (cc.getName().contentEquals(ccDTO.getName())) {
                            this.capabilityTranslatorTest.verifyOutput(cc, ccDTO, childrenGenerated);
                            verified = true;
                        }
                    }
                    assertTrue(verified);
                }
            } else {
                assertNull(dest.getCapabilities());
            }
            assertEquals(0, dest.getGuestIds().size());
        } else {
            assertNull(dest.getReleaseVersion());
            assertNull(dest.getOwner());
            assertNull(dest.getEnvironment());
            assertNull(dest.getHypervisorId());
            assertNull(dest.getType());
            assertNull(dest.getIdCert());
            assertNull(dest.getInstalledProducts());
            assertNull(dest.getCapabilities());
            assertNull(dest.getGuestIds());
        }
    } else {
        assertNull(dest);
    }
}
Also used : ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) Environment(org.candlepin.model.Environment) ConsumerCapability(org.candlepin.model.ConsumerCapability) ConsumerType(org.candlepin.model.ConsumerType)

Example 8 with Environment

use of org.candlepin.model.Environment in project candlepin by candlepin.

the class OwnerContentResourceTest method deleteLockedContent.

@Test(expected = ForbiddenException.class)
public void deleteLockedContent() {
    Owner owner = this.createOwner("test_owner");
    Content content = this.createContent("test_content", "test_content", owner);
    content.setLocked(true);
    this.contentCurator.merge(content);
    Environment environment = this.createEnvironment(owner, "test_env", "test_env", null, null, Arrays.asList(content));
    assertNotNull(this.ownerContentCurator.getContentById(owner, content.getId()));
    try {
        this.ownerContentResource.remove(owner.getKey(), content.getId());
    } catch (ForbiddenException e) {
        assertNotNull(this.ownerContentCurator.getContentById(owner, content.getId()));
        this.environmentCurator.evict(environment);
        environment = this.environmentCurator.find(environment.getId());
        assertEquals(1, environment.getEnvironmentContent().size());
        throw e;
    }
}
Also used : Owner(org.candlepin.model.Owner) ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) Content(org.candlepin.model.Content) Environment(org.candlepin.model.Environment) Test(org.junit.Test)

Example 9 with Environment

use of org.candlepin.model.Environment in project candlepin by candlepin.

the class OwnerContentResourceTest method deleteContent.

@Test
public void deleteContent() {
    Owner owner = this.createOwner("test_owner");
    Content content = this.createContent("test_content", "test_content", owner);
    Environment environment = this.createEnvironment(owner, "test_env", "test_env", null, null, Arrays.asList(content));
    assertNotNull(this.ownerContentCurator.getContentById(owner, content.getId()));
    this.ownerContentResource.remove(owner.getKey(), content.getId());
    assertNull(this.ownerContentCurator.getContentById(owner, content.getId()));
    this.environmentCurator.evict(environment);
    environment = this.environmentCurator.find(environment.getId());
    assertEquals(0, environment.getEnvironmentContent().size());
}
Also used : Owner(org.candlepin.model.Owner) Content(org.candlepin.model.Content) Environment(org.candlepin.model.Environment) Test(org.junit.Test)

Example 10 with Environment

use of org.candlepin.model.Environment in project candlepin by candlepin.

the class ConsumerResource method performConsumerUpdates.

@Transactional
public boolean performConsumerUpdates(ConsumerDTO updated, Consumer toUpdate, GuestMigration guestMigration, boolean isIdCert) {
    log.debug("Updating consumer: {}", toUpdate.getUuid());
    // We need a representation of the consumer before making any modifications.
    // If nothing changes we won't send.  The new entity needs to be correct though,
    // so we should get a Jsonstring now, and finish it off if we're going to send
    EventBuilder eventBuilder = eventFactory.getEventBuilder(Target.CONSUMER, Type.MODIFIED).setEventData(toUpdate);
    // version changed on non-checked in consumer, or list of capabilities
    // changed on checked in consumer
    boolean changesMade = updateCapabilities(toUpdate, updated);
    changesMade = checkForFactsUpdate(toUpdate, updated) || changesMade;
    changesMade = checkForInstalledProductsUpdate(toUpdate, updated) || changesMade;
    changesMade = checkForHypervisorIdUpdate(toUpdate, updated) || changesMade;
    changesMade = guestMigration.isMigrationPending() || changesMade;
    if (updated.getContentTags() != null && !updated.getContentTags().equals(toUpdate.getContentTags())) {
        log.info("   Updating content tags.");
        toUpdate.setContentTags(updated.getContentTags());
        changesMade = true;
    }
    // Allow optional setting of the autoheal attribute:
    if (updated.getAutoheal() != null && !updated.getAutoheal().equals(toUpdate.isAutoheal())) {
        log.info("   Updating consumer autoheal setting.");
        toUpdate.setAutoheal(updated.getAutoheal());
        changesMade = true;
    }
    if (updated.getReleaseVersion() != null && !updated.getReleaseVersion().equals(toUpdate.getReleaseVer() == null ? null : toUpdate.getReleaseVer().getReleaseVer())) {
        log.info("   Updating consumer releaseVer setting.");
        toUpdate.setReleaseVer(new Release(updated.getReleaseVersion()));
        changesMade = true;
    }
    // Allow optional setting of the service level attribute:
    String level = updated.getServiceLevel();
    if (level != null && !level.equals(toUpdate.getServiceLevel())) {
        log.info("   Updating consumer service level setting.");
        consumerBindUtil.validateServiceLevel(toUpdate.getOwnerId(), level);
        toUpdate.setServiceLevel(level);
        changesMade = true;
    }
    String environmentId = updated.getEnvironment() == null ? null : updated.getEnvironment().getId();
    if (environmentId != null && (toUpdate.getEnvironmentId() == null || !toUpdate.getEnvironmentId().equals(environmentId))) {
        Environment e = environmentCurator.find(environmentId);
        if (e == null) {
            throw new NotFoundException(i18n.tr("Environment with ID \"{0}\" could not be found.", environmentId));
        }
        log.info("Updating environment to: {}", environmentId);
        toUpdate.setEnvironment(e);
        // lazily regenerate certs, so the client can still work
        poolManager.regenerateCertificatesOf(toUpdate, true);
        changesMade = true;
    }
    // it should remain the same
    if (updated.getName() != null && !toUpdate.getName().equals(updated.getName())) {
        checkConsumerName(updated);
        log.info("Updating consumer name: {} -> {}", toUpdate.getName(), updated.getName());
        toUpdate.setName(updated.getName());
        changesMade = true;
        // get the new name into the id cert if we are using the cert
        if (isIdCert) {
            IdentityCertificate ic = generateIdCert(toUpdate, true);
            toUpdate.setIdCert(ic);
        }
    }
    ConsumerType ctype = this.consumerTypeCurator.getConsumerType(toUpdate);
    if (updated.getContentAccessMode() != null && !updated.getContentAccessMode().equals(toUpdate.getContentAccessMode()) && ctype.isManifest()) {
        Owner toUpdateOwner = ownerCurator.findOwnerById(toUpdate.getOwnerId());
        if (!toUpdateOwner.isAllowedContentAccessMode(updated.getContentAccessMode())) {
            throw new BadRequestException(i18n.tr("The consumer cannot use the supplied content access mode."));
        }
        toUpdate.setContentAccessMode(updated.getContentAccessMode());
        changesMade = true;
    }
    if (!StringUtils.isEmpty(updated.getContentAccessMode()) && !ctype.isManifest()) {
        throw new BadRequestException(i18n.tr("The consumer cannot be assigned a content access mode."));
    }
    if (updated.getLastCheckin() != null) {
        log.info("Updating to specific last checkin time: {}", updated.getLastCheckin());
        toUpdate.setLastCheckin(updated.getLastCheckin());
        changesMade = true;
    }
    if (changesMade) {
        log.debug("Consumer {} updated.", toUpdate.getUuid());
        // Set the updated date here b/c @PreUpdate will not get fired
        // since only the facts table will receive the update.
        toUpdate.setUpdated(new Date());
        // this should update compliance on toUpdate, but not call the curator
        complianceRules.getStatus(toUpdate, null, false, false);
        Event event = eventBuilder.setEventData(toUpdate).buildEvent();
        sink.queueEvent(event);
    }
    return changesMade;
}
Also used : Owner(org.candlepin.model.Owner) EventBuilder(org.candlepin.audit.EventBuilder) Environment(org.candlepin.model.Environment) NotFoundException(org.candlepin.common.exceptions.NotFoundException) BadRequestException(org.candlepin.common.exceptions.BadRequestException) Event(org.candlepin.audit.Event) ConsumerType(org.candlepin.model.ConsumerType) Release(org.candlepin.model.Release) Date(java.util.Date) IdentityCertificate(org.candlepin.model.IdentityCertificate) Transactional(com.google.inject.persist.Transactional)

Aggregations

Environment (org.candlepin.model.Environment)29 EnvironmentContent (org.candlepin.model.EnvironmentContent)10 Test (org.junit.Test)9 HashSet (java.util.HashSet)8 Owner (org.candlepin.model.Owner)7 ApiOperation (io.swagger.annotations.ApiOperation)6 HashMap (java.util.HashMap)6 Path (javax.ws.rs.Path)6 Produces (javax.ws.rs.Produces)6 Content (org.candlepin.model.Content)6 ApiResponses (io.swagger.annotations.ApiResponses)5 Date (java.util.Date)5 Consumer (org.candlepin.model.Consumer)5 NotFoundException (org.candlepin.common.exceptions.NotFoundException)4 ConsumerCapability (org.candlepin.model.ConsumerCapability)4 ConsumerInstalledProduct (org.candlepin.model.ConsumerInstalledProduct)4 ConsumerType (org.candlepin.model.ConsumerType)4 Release (org.candlepin.model.Release)4 Matchers.anyString (org.mockito.Matchers.anyString)4 KeyPair (java.security.KeyPair)3