Search in sources :

Example 51 with ConsumerDTO

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

the class Importer method importConsumer.

protected ConsumerDTO importConsumer(Owner owner, File consumerFile, File[] upstreamConsumer, ConflictOverrides forcedConflicts, Meta meta) throws IOException, SyncDataFormatException {
    IdentityCertificate idcert = null;
    for (File uc : upstreamConsumer) {
        if (uc.getName().endsWith(".json")) {
            log.debug("Import upstream consumeridentity certificate: {}", uc.getName());
            try (Reader reader = new FileReader(uc)) {
                CertificateDTO dtoCert = mapper.readValue(reader, CertificateDTO.class);
                idcert = new IdentityCertificate();
                populateEntity(idcert, dtoCert);
            }
        } else {
            log.warn("Extra file found in upstream_consumer directory: {}", uc.getName());
        }
    }
    ConsumerImporter importer = new ConsumerImporter(ownerCurator, idCertCurator, i18n, csCurator);
    Reader reader = null;
    ConsumerDTO consumer = null;
    try {
        reader = new FileReader(consumerFile);
        consumer = importer.createObject(mapper, reader);
        // we can not rely on the actual ConsumerType in the ConsumerDto
        // because it could have an id not in our database. We need to
        // stick with the label. Hence we need to lookup the ACTUAL type
        // by label here before attempting to store the UpstreamConsumer
        ConsumerType type = consumerTypeCurator.lookupByLabel(consumer.getType().getLabel());
        consumer.setType(this.translator.translate(type, ConsumerTypeDTO.class));
        // the metadata
        if (StringUtils.isEmpty(consumer.getUrlWeb())) {
            consumer.setUrlWeb(meta.getWebAppPrefix());
        }
        importer.store(owner, consumer, forcedConflicts, idcert);
    } finally {
        if (reader != null) {
            reader.close();
        }
    }
    return consumer;
}
Also used : CertificateDTO(org.candlepin.dto.manifest.v1.CertificateDTO) ConsumerDTO(org.candlepin.dto.manifest.v1.ConsumerDTO) Reader(java.io.Reader) FileReader(java.io.FileReader) FileReader(java.io.FileReader) ConsumerType(org.candlepin.model.ConsumerType) ManifestFile(org.candlepin.sync.file.ManifestFile) File(java.io.File) ConsumerTypeDTO(org.candlepin.dto.manifest.v1.ConsumerTypeDTO) IdentityCertificate(org.candlepin.model.IdentityCertificate)

Example 52 with ConsumerDTO

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

the class ConsumerExporter method export.

void export(ObjectMapper mapper, Writer writer, Consumer consumer, String weburl, String apiurl) throws IOException {
    ConsumerDTO consumerDTO = this.translator.translate(consumer, ConsumerDTO.class);
    consumerDTO.setUrlApi(apiurl);
    consumerDTO.setUrlWeb(weburl);
    mapper.writeValue(writer, consumerDTO);
}
Also used : ConsumerDTO(org.candlepin.dto.manifest.v1.ConsumerDTO)

Example 53 with ConsumerDTO

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

the class ConsumerResource method populateInstalledProducts.

/**
 * Utility method that translates a ConsumerDTO's installed products to
 * Consumer model entity installed products.
 *
 * @param entity the Consumer model entity which the installed products are to reference
 *
 * @param dto the ConsumerDTO whose installed products we want to translate
 *
 * @return the model entity ConsumerInstalledProduct set that was created
 */
private Set<ConsumerInstalledProduct> populateInstalledProducts(Consumer entity, ConsumerDTO dto) {
    Set<ConsumerInstalledProduct> installedProducts = new HashSet<>();
    for (ConsumerInstalledProductDTO installedProductDTO : dto.getInstalledProducts()) {
        if (installedProductDTO != null) {
            ConsumerInstalledProduct installedProduct = new ConsumerInstalledProduct(installedProductDTO.getProductId(), installedProductDTO.getProductName(), entity, installedProductDTO.getVersion(), installedProductDTO.getArch(), installedProductDTO.getStatus(), installedProductDTO.getStartDate(), installedProductDTO.getEndDate());
            installedProducts.add(installedProduct);
        }
    }
    return installedProducts;
}
Also used : ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) ConsumerInstalledProductDTO(org.candlepin.dto.api.v1.ConsumerInstalledProductDTO) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet)

Example 54 with ConsumerDTO

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

the class EnvironmentResource method create.

@ApiOperation(notes = "Creates an Environment", value = "create")
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@SecurityHole(noAuth = true)
@Path("/{env_id}/consumers")
public ConsumerDTO create(@PathParam("env_id") String envId, @ApiParam(name = "consumer", required = true) ConsumerDTO consumer, @Context Principal principal, @QueryParam("username") String userName, @QueryParam("owner") String ownerKey, @QueryParam("activation_keys") String activationKeys) throws BadRequestException {
    Environment e = lookupEnvironment(envId);
    consumer.setEnvironment(translator.translate(e, EnvironmentDTO.class));
    return this.consumerResource.create(consumer, principal, userName, e.getOwner().getKey(), activationKeys, true);
}
Also used : EnvironmentDTO(org.candlepin.dto.api.v1.EnvironmentDTO) Environment(org.candlepin.model.Environment) Path(javax.ws.rs.Path) SecurityHole(org.candlepin.common.auth.SecurityHole) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation)

Example 55 with ConsumerDTO

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

the class GuestMigration method buildMigrationManifest.

/**
 * Build a manifest detailing any guest migrations occurring due to a host consumer update.
 *
 * If a consumer's guest was already reported by another host consumer, record that host in the
 * manifest as well so that the migration can be made atomically.
 *
 * @param incoming incoming consumer in DTO form
 * @param existing existing consumer in model form
 * @return guestMigration object that contains all information related to the migration
 */
public GuestMigration buildMigrationManifest(ConsumerDTO incoming, Consumer existing) {
    if (incoming.getGuestIds() == null) {
        log.debug("Guests not included in this consumer update, skipping update.");
        migrationPending = false;
        return this;
    }
    manifest = new MigrationManifest(existing);
    log.debug("Updating {} guest IDs.", incoming.getGuestIds().size());
    List<GuestId> existingGuests = existing.getGuestIds();
    // Transform incoming GuestIdDTOs to GuestIds
    List<GuestId> incomingGuestIds = incoming.getGuestIds().stream().filter(Objects::nonNull).map(guestIdDTO -> new GuestId(guestIdDTO.getGuestId(), existing, guestIdDTO.getAttributes())).collect(Collectors.toList());
    List<GuestId> removedGuests = getRemovedGuestIds(existing, incomingGuestIds);
    List<GuestId> addedGuests = getAddedGuestIds(existing, incomingGuestIds);
    // remove guests that are missing.
    if (existingGuests != null) {
        for (GuestId guestId : removedGuests) {
            existingGuests.remove(guestId);
            log.debug("Guest ID removed: {}", guestId);
        }
    }
    // Check guests that are existing/added.
    for (GuestId guestId : incomingGuestIds) {
        if (addedGuests.contains(guestId)) {
            manifest.addGuestId(guestId);
            log.debug("New guest ID added: {}", guestId);
        }
    }
    migrationPending = removedGuests.size() != 0 || addedGuests.size() != 0;
    return this;
}
Also used : ConsumerDTO(org.candlepin.dto.api.v1.ConsumerDTO) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Objects(java.util.Objects) Inject(javax.inject.Inject) ConsumerCurator(org.candlepin.model.ConsumerCurator) List(java.util.List) Map(java.util.Map) Consumer(org.candlepin.model.Consumer) GuestId(org.candlepin.model.GuestId) GuestId(org.candlepin.model.GuestId) Objects(java.util.Objects)

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