Search in sources :

Example 71 with ConsumerType

use of org.candlepin.model.ConsumerType 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 72 with ConsumerType

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

the class PersonConsumerResourceCreationTest method initConsumerType.

public ConsumerType initConsumerType() {
    ConsumerType ctype = new ConsumerType(ConsumerType.ConsumerTypeEnum.PERSON);
    this.mockConsumerType(ctype);
    // create an owner, a ownerperm, and roles for the user we prodive
    // as coming from userService
    owner = new Owner("test_owner");
    PermissionBlueprint p = new PermissionBlueprint(PermissionType.OWNER, owner, Access.ALL);
    User user = new User("anyuser", "");
    role = new Role();
    role.addPermission(p);
    role.addUser(user);
    when(userService.findByLogin("anyuser")).thenReturn(user);
    return ctype;
}
Also used : Role(org.candlepin.model.Role) Owner(org.candlepin.model.Owner) User(org.candlepin.model.User) PermissionBlueprint(org.candlepin.model.PermissionBlueprint) ConsumerType(org.candlepin.model.ConsumerType)

Example 73 with ConsumerType

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

the class AutobindRulesTest method createEnforcer.

@Before
public void createEnforcer() throws Exception {
    MockitoAnnotations.initMocks(this);
    when(config.getInt(eq(ConfigProperties.PRODUCT_CACHE_MAX))).thenReturn(100);
    InputStream is = this.getClass().getResourceAsStream(RulesCurator.DEFAULT_RULES_FILE);
    Rules rules = new Rules(Util.readFile(is));
    when(rulesCurator.getRules()).thenReturn(rules);
    when(rulesCurator.getUpdated()).thenReturn(TestDateUtil.date(2010, 1, 1));
    when(cacheProvider.get()).thenReturn(cache);
    JsRunner jsRules = new JsRunnerProvider(rulesCurator, cacheProvider).get();
    translator = new StandardTranslator(consumerTypeCurator, environmentCurator, mockOwnerCurator);
    autobindRules = new AutobindRules(jsRules, mockProductCurator, consumerTypeCurator, mockOwnerCurator, new RulesObjectMapper(new ProductCachedSerializationModule(mockProductCurator)), translator);
    owner = new Owner();
    owner.setId(TestUtil.randomString());
    when(mockOwnerCurator.findOwnerById(eq(owner.getId()))).thenReturn(owner);
    ConsumerType ctype = new ConsumerType(ConsumerTypeEnum.SYSTEM);
    ctype.setId("test-ctype");
    consumer = new Consumer("test consumer", "test user", owner, ctype);
    when(consumerTypeCurator.find(eq(ctype.getId()))).thenReturn(ctype);
    when(consumerTypeCurator.lookupByLabel(eq(ctype.getLabel()))).thenReturn(ctype);
    when(consumerTypeCurator.getConsumerType(eq(consumer))).thenReturn(ctype);
    compliance = new ComplianceStatus();
    activeGuestAttrs = new HashMap<>();
    activeGuestAttrs.put("virtWhoType", "libvirt");
    activeGuestAttrs.put("active", "1");
}
Also used : Owner(org.candlepin.model.Owner) AutobindRules(org.candlepin.policy.js.autobind.AutobindRules) ComplianceStatus(org.candlepin.policy.js.compliance.ComplianceStatus) InputStream(java.io.InputStream) RulesObjectMapper(org.candlepin.policy.js.RulesObjectMapper) JsRunner(org.candlepin.policy.js.JsRunner) Rules(org.candlepin.model.Rules) AutobindRules(org.candlepin.policy.js.autobind.AutobindRules) StandardTranslator(org.candlepin.dto.StandardTranslator) ProductCachedSerializationModule(org.candlepin.jackson.ProductCachedSerializationModule) Consumer(org.candlepin.model.Consumer) JsRunnerProvider(org.candlepin.policy.js.JsRunnerProvider) ConsumerType(org.candlepin.model.ConsumerType) Before(org.junit.Before)

Example 74 with ConsumerType

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

the class OwnerResource method listConsumers.

/**
 * Retrieve a list of Consumers for the Owner
 *
 * @param ownerKey id of the owner whose consumers are sought.
 * @return a list of Consumer objects
 * @httpcode 400
 * @httpcode 404
 * @httpcode 200
 */
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{owner_key}/consumers")
@SuppressWarnings("checkstyle:indentation")
@ApiOperation(notes = "Retrieve a list of Consumers for the Owner", value = "List Consumers", response = Consumer.class, responseContainer = "list")
@ApiResponses({ @ApiResponse(code = 404, message = "Owner not found"), @ApiResponse(code = 400, message = "Invalid request") })
public CandlepinQuery<ConsumerDTO> listConsumers(@PathParam("owner_key") @Verify(value = Owner.class, subResource = SubResource.CONSUMERS) String ownerKey, @QueryParam("username") String userName, @QueryParam("type") Set<String> typeLabels, @QueryParam("uuid") @Verify(value = Consumer.class, nullable = true) List<String> uuids, @QueryParam("hypervisor_id") List<String> hypervisorIds, @QueryParam("fact") @CandlepinParam(type = KeyValueParameter.class) List<KeyValueParameter> attrFilters, @QueryParam("sku") List<String> skus, @QueryParam("subscription_id") List<String> subscriptionIds, @QueryParam("contract") List<String> contracts, @Context PageRequest pageRequest) {
    Owner owner = findOwnerByKey(ownerKey);
    List<ConsumerType> types = consumerTypeValidator.findAndValidateTypeLabels(typeLabels);
    CandlepinQuery<Consumer> query = this.consumerCurator.searchOwnerConsumers(owner, userName, types, uuids, hypervisorIds, attrFilters, skus, subscriptionIds, contracts);
    return translator.translateQuery(query, ConsumerDTO.class);
}
Also used : Owner(org.candlepin.model.Owner) UpstreamConsumer(org.candlepin.model.UpstreamConsumer) Consumer(org.candlepin.model.Consumer) ConsumerType(org.candlepin.model.ConsumerType) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 75 with ConsumerType

use of org.candlepin.model.ConsumerType 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

ConsumerType (org.candlepin.model.ConsumerType)169 Consumer (org.candlepin.model.Consumer)92 Test (org.junit.Test)71 Owner (org.candlepin.model.Owner)53 Pool (org.candlepin.model.Pool)47 Entitlement (org.candlepin.model.Entitlement)33 ArrayList (java.util.ArrayList)29 Date (java.util.Date)27 HashMap (java.util.HashMap)24 HashSet (java.util.HashSet)24 ValidationResult (org.candlepin.policy.ValidationResult)22 ApiOperation (io.swagger.annotations.ApiOperation)21 Produces (javax.ws.rs.Produces)21 Before (org.junit.Before)20 ApiResponses (io.swagger.annotations.ApiResponses)19 Path (javax.ws.rs.Path)18 LinkedList (java.util.LinkedList)16 BadRequestException (org.candlepin.common.exceptions.BadRequestException)16 DeletedConsumer (org.candlepin.model.DeletedConsumer)16 Matchers.anyString (org.mockito.Matchers.anyString)16