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;
}
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);
}
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;
}
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);
}
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;
}
Aggregations