Search in sources :

Example 1 with ExportResult

use of org.candlepin.sync.ExportResult in project candlepin by candlepin.

the class ManifestManager method generateAndStoreManifest.

/**
 * Generates a manifest for the specifed consumer and stores the resulting file via the
 * {@link ManifestFileService}.
 *
 * @param consumerUuid the target consumer's UUID.
 * @param cdnLabel the CDN label to store in the meta file.
 * @param webUrl the URL pointing to the manifest's originating web application.
 * @param apiUrl the API URL pointing to the manifest's originating candlepin API.
 * @param extensionData data to be passed to the {@link ExportExtensionAdapter} when creating
 *                      a new export of the target consumer.
 * @return an {@link ExportResult} containing the details of the stored file.
 * @throws ExportCreationException if there are any issues generating the manifest.
 */
public ExportResult generateAndStoreManifest(String consumerUuid, String cdnLabel, String webUrl, String apiUrl, Map<String, String> extensionData) throws ExportCreationException {
    Consumer consumer = validateConsumerForExport(consumerUuid, cdnLabel);
    File export = null;
    try {
        poolManager.regenerateDirtyEntitlements(entitlementCurator.listByConsumer(consumer));
        export = exporter.getFullExport(consumer, cdnLabel, webUrl, apiUrl, extensionData);
        ManifestFile manifestFile = storeExport(export, consumer);
        sink.queueEvent(eventFactory.exportCreated(consumer));
        return new ExportResult(consumer.getUuid(), manifestFile.getId());
    } catch (ManifestFileServiceException e) {
        throw new ExportCreationException("Unable to create export archive", e);
    } finally {
        // We no longer need the export work directory since the archive has been saved in the DB.
        if (export != null) {
            File workDir = export.getParentFile();
            try {
                FileUtils.deleteDirectory(workDir);
            } catch (IOException ioe) {
            // It'll get cleaned up by the ManifestCleanerJob if it couldn't
            // be deleted for some reason.
            }
        }
    }
}
Also used : Consumer(org.candlepin.model.Consumer) ExportCreationException(org.candlepin.sync.ExportCreationException) IOException(java.io.IOException) ManifestFile(org.candlepin.sync.file.ManifestFile) File(java.io.File) ManifestFile(org.candlepin.sync.file.ManifestFile) ManifestFileServiceException(org.candlepin.sync.file.ManifestFileServiceException) ExportResult(org.candlepin.sync.ExportResult)

Example 2 with ExportResult

use of org.candlepin.sync.ExportResult in project candlepin by candlepin.

the class ManifestManagerTest method testGenerateAndStoreManifest.

@Test
public void testGenerateAndStoreManifest() throws Exception {
    Consumer consumer = this.createMockConsumer(true);
    Cdn cdn = new Cdn("test-cdn", "Test CDN", "");
    String webAppPrefix = "webapp-prefix";
    String apiUrl = "api-url";
    Map<String, String> extData = new HashMap<>();
    Event event = mock(Event.class);
    when(eventFactory.exportCreated(eq(consumer))).thenReturn(event);
    UserPrincipal principal = TestUtil.createOwnerPrincipal();
    when(principalProvider.get()).thenReturn(principal);
    String exportId = "export-id";
    ManifestFile manifest = mock(ManifestFile.class);
    when(manifest.getId()).thenReturn(exportId);
    when(fileService.store(eq(ManifestFileType.EXPORT), any(File.class), eq(principal.getName()), any(String.class))).thenReturn(manifest);
    when(consumerCurator.verifyAndLookupConsumer(eq(consumer.getUuid()))).thenReturn(consumer);
    when(cdnCurator.lookupByLabel(eq(cdn.getLabel()))).thenReturn(cdn);
    List<Entitlement> ents = new ArrayList<>();
    when(entitlementCurator.listByConsumer(eq(consumer))).thenReturn(ents);
    ExportResult result = manager.generateAndStoreManifest(consumer.getUuid(), cdn.getLabel(), webAppPrefix, apiUrl, extData);
    assertEquals(consumer.getUuid(), result.getExportedConsumer());
    assertEquals(exportId, result.getExportId());
    verify(entitlementCurator).listByConsumer(eq(consumer));
    verify(exporter).getFullExport(eq(consumer), eq(cdn.getLabel()), eq(webAppPrefix), eq(apiUrl), eq(extData));
    verify(eventFactory).exportCreated(eq(consumer));
    verify(eventSink).queueEvent(eq(event));
    verify(fileService).delete(eq(ManifestFileType.EXPORT), eq(consumer.getUuid()));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Cdn(org.candlepin.model.Cdn) UserPrincipal(org.candlepin.auth.UserPrincipal) ManifestFile(org.candlepin.sync.file.ManifestFile) Consumer(org.candlepin.model.Consumer) Event(org.candlepin.audit.Event) Entitlement(org.candlepin.model.Entitlement) ManifestFile(org.candlepin.sync.file.ManifestFile) File(java.io.File) ExportResult(org.candlepin.sync.ExportResult) Test(org.junit.Test)

Example 3 with ExportResult

use of org.candlepin.sync.ExportResult in project candlepin by candlepin.

the class ExportJob method toExecute.

@Override
public void toExecute(JobExecutionContext context) throws JobExecutionException {
    JobDataMap map = context.getMergedJobDataMap();
    String consumerUuid = map.getString(JobStatus.TARGET_ID);
    String cdnLabel = map.getString(CDN_LABEL);
    String webAppPrefix = map.getString(WEBAPP_PREFIX);
    String apiUrl = map.getString(API_URL);
    Map<String, String> extensionData = (Map<String, String>) map.get(EXTENSION_DATA);
    log.info("Starting async export for {}", consumerUuid);
    try {
        ExportResult result = manifestManager.generateAndStoreManifest(consumerUuid, cdnLabel, webAppPrefix, apiUrl, extensionData);
        context.setResult(result);
        log.info("Async export complete.");
    } catch (Exception e) {
        throw new JobExecutionException(e.getMessage(), e, false);
    }
}
Also used : JobDataMap(org.quartz.JobDataMap) JobExecutionException(org.quartz.JobExecutionException) JobDataMap(org.quartz.JobDataMap) Map(java.util.Map) JobExecutionException(org.quartz.JobExecutionException) ExportResult(org.candlepin.sync.ExportResult)

Example 4 with ExportResult

use of org.candlepin.sync.ExportResult in project candlepin by candlepin.

the class ExportJobTest method ensureJobSuccess.

@Test
public void ensureJobSuccess() throws Exception {
    Owner owner = TestUtil.createOwner();
    owner.setId(TestUtil.randomString());
    Consumer distributor = TestUtil.createDistributor(owner);
    String cdnLabel = "cdn-label";
    String webappPrefix = "webapp-prefix";
    String apiUrl = "url";
    String manifestId = "1234";
    Map<String, String> extData = new HashMap<>();
    ExportResult result = new ExportResult(distributor.getUuid(), manifestId);
    when(manifestManager.generateAndStoreManifest(eq(distributor.getUuid()), eq(cdnLabel), eq(webappPrefix), eq(apiUrl), eq(extData))).thenReturn(result);
    JobDetail detail = job.scheduleExport(distributor, owner.getKey(), cdnLabel, webappPrefix, apiUrl, extData);
    when(ctx.getMergedJobDataMap()).thenReturn(detail.getJobDataMap());
    job.execute(ctx);
    verify(manifestManager).generateAndStoreManifest(eq(distributor.getUuid()), eq(cdnLabel), eq(webappPrefix), eq(apiUrl), eq(extData));
    verify(ctx).setResult(eq(result));
}
Also used : Owner(org.candlepin.model.Owner) JobDetail(org.quartz.JobDetail) Consumer(org.candlepin.model.Consumer) HashMap(java.util.HashMap) ExportResult(org.candlepin.sync.ExportResult) Test(org.junit.Test)

Aggregations

ExportResult (org.candlepin.sync.ExportResult)4 Consumer (org.candlepin.model.Consumer)3 File (java.io.File)2 HashMap (java.util.HashMap)2 ManifestFile (org.candlepin.sync.file.ManifestFile)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Event (org.candlepin.audit.Event)1 UserPrincipal (org.candlepin.auth.UserPrincipal)1 Cdn (org.candlepin.model.Cdn)1 Entitlement (org.candlepin.model.Entitlement)1 Owner (org.candlepin.model.Owner)1 ExportCreationException (org.candlepin.sync.ExportCreationException)1 ManifestFileServiceException (org.candlepin.sync.file.ManifestFileServiceException)1 JobDataMap (org.quartz.JobDataMap)1 JobDetail (org.quartz.JobDetail)1 JobExecutionException (org.quartz.JobExecutionException)1