Search in sources :

Example 6 with ImportCatalogStatus

use of org.ligoj.app.plugin.prov.model.ImportCatalogStatus in project plugin-prov by ligoj.

the class ImportCatalogResource method updateCatalog.

/**
 * Update the catalog prices of related provider. Asynchronous operation.
 *
 * @param node
 *            The node (provider) to update.
 * @return The catalog status.
 */
@POST
@Path("catalog/{node:service:prov:.+}")
public ImportCatalogStatus updateCatalog(@PathParam("node") final String node) {
    final Node entity = nodeResource.checkWritableNode(node).getTool();
    final ImportCatalogService catalogService = locator.getResource(entity.getId(), ImportCatalogService.class);
    final ImportCatalogStatus task = startTask(entity.getId(), t -> {
        t.setLocation(null);
        t.setNbInstancePrices(null);
        t.setNbInstanceTypes(null);
        t.setNbStorageTypes(null);
        t.setWorkload(0);
        t.setDone(0);
        t.setPhase(null);
    });
    final String user = securityHelper.getLogin();
    // The import execution will done into another thread
    Executors.newSingleThreadExecutor().submit(() -> {
        Thread.sleep(50);
        securityHelper.setUserName(user);
        updateCatalog(catalogService, entity.getId());
        return null;
    });
    return task;
}
Also used : LongTaskRunnerNode(org.ligoj.app.resource.node.LongTaskRunnerNode) Node(org.ligoj.app.model.Node) ImportCatalogStatus(org.ligoj.app.plugin.prov.model.ImportCatalogStatus) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 7 with ImportCatalogStatus

use of org.ligoj.app.plugin.prov.model.ImportCatalogStatus in project plugin-prov by ligoj.

the class ImportCatalogResourceTest method assertFailed.

private void assertFailed(final ImportCatalogService service) throws Exception {
    final ImportCatalogStatus status = repository.findBy("locked.id", "service:prov:test");
    Assertions.assertEquals(DEFAULT_USER, status.getAuthor());
    Assertions.assertNotNull(status.getEnd());
    Assertions.assertNull(status.getLocation());
    Assertions.assertEquals("service:prov:test", status.getLocked().getId());
    Assertions.assertNotNull(status.getStart());
    Assertions.assertNull(status.getLocation());
    Assertions.assertEquals(0, status.getDone());
    Assertions.assertEquals(0, status.getWorkload());
    Assertions.assertTrue(status.isFinished());
    Assertions.assertTrue(status.isFailed());
    Assertions.assertEquals(0, status.getLastSuccess().getTime());
    Assertions.assertEquals(-1, status.getNbInstancePrices().intValue());
    Assertions.assertEquals(-1, status.getNbInstanceTypes().intValue());
    Assertions.assertEquals(-1, status.getNbLocations().intValue());
    Assertions.assertEquals(-1, status.getNbStorageTypes().intValue());
    Mockito.verify(service).updateCatalog("service:prov:test");
}
Also used : ImportCatalogStatus(org.ligoj.app.plugin.prov.model.ImportCatalogStatus)

Example 8 with ImportCatalogStatus

use of org.ligoj.app.plugin.prov.model.ImportCatalogStatus in project plugin-prov by ligoj.

the class ImportCatalogResourceTest method updateCatalogFailed.

@Test
public void updateCatalogFailed() throws Exception {
    final ImportCatalogResource resource = new ImportCatalogResource() {

        @Override
        public ImportCatalogStatus nextStep(final String node, final Consumer<ImportCatalogStatus> stepper) {
            return super.nextStep(node, stepper);
        }
    };
    applicationContext.getAutowireCapableBeanFactory().autowireBean(resource);
    // Replace the locator for the custom provider
    resource.locator = Mockito.mock(ServicePluginLocator.class);
    final ImportCatalogService service = Mockito.mock(ImportCatalogService.class);
    Mockito.when(resource.locator.getResource("service:prov:test", ImportCatalogService.class)).thenReturn(service);
    Mockito.doThrow(new IOException()).when(service).updateCatalog("service:prov:test");
    final ImportCatalogStatus status = resource.updateCatalog("service:prov:test:account");
    Assertions.assertEquals(DEFAULT_USER, status.getAuthor());
    Assertions.assertNull(status.getEnd());
    Assertions.assertNull(status.getLocation());
    Assertions.assertEquals("service:prov:test", status.getLocked().getId());
    Assertions.assertNotNull(status.getStart());
    Assertions.assertEquals(0, status.getDone());
    Assertions.assertEquals(0, status.getWorkload());
}
Also used : Consumer(java.util.function.Consumer) IOException(java.io.IOException) ImportCatalogStatus(org.ligoj.app.plugin.prov.model.ImportCatalogStatus) ServicePluginLocator(org.ligoj.app.resource.ServicePluginLocator) AbstractAppTest(org.ligoj.app.AbstractAppTest) Test(org.junit.jupiter.api.Test)

Example 9 with ImportCatalogStatus

use of org.ligoj.app.plugin.prov.model.ImportCatalogStatus in project plugin-prov by ligoj.

the class ImportCatalogResourceTest method newStatus.

private ImportCatalogStatus newStatus() {
    ImportCatalogStatus status = new ImportCatalogStatus();
    status.setLastSuccess(new Date(0));
    status.setAuthor(DEFAULT_USER);
    status.setNbInstancePrices(-1);
    status.setNbInstanceTypes(-1);
    status.setNbLocations(-1);
    status.setNbStorageTypes(-1);
    status.setStart(new Date());
    status.setLocked(nodeRepository.findOne("service:prov:test"));
    repository.saveAndFlush(status);
    return status;
}
Also used : ImportCatalogStatus(org.ligoj.app.plugin.prov.model.ImportCatalogStatus) Date(java.util.Date)

Example 10 with ImportCatalogStatus

use of org.ligoj.app.plugin.prov.model.ImportCatalogStatus in project plugin-prov-azure by ligoj.

the class ProvAzurePriceImportResourceTest method checkImportStatus.

private void checkImportStatus() {
    final ImportCatalogStatus status = this.resource.getImportCatalogResource().getTask("service:prov:azure");
    Assertions.assertEquals(14, status.getDone());
    Assertions.assertEquals(14, status.getWorkload());
    Assertions.assertEquals("finalize", status.getPhase());
    Assertions.assertEquals(DEFAULT_USER, status.getAuthor());
    Assertions.assertTrue(status.getNbInstancePrices().intValue() >= 46);
    Assertions.assertEquals(5, status.getNbInstanceTypes().intValue());
    Assertions.assertEquals(2, status.getNbLocations().intValue());
    Assertions.assertEquals(5, status.getNbStorageTypes().intValue());
}
Also used : ImportCatalogStatus(org.ligoj.app.plugin.prov.model.ImportCatalogStatus)

Aggregations

ImportCatalogStatus (org.ligoj.app.plugin.prov.model.ImportCatalogStatus)10 Test (org.junit.jupiter.api.Test)5 AbstractAppTest (org.ligoj.app.AbstractAppTest)5 ServicePluginLocator (org.ligoj.app.resource.ServicePluginLocator)4 Node (org.ligoj.app.model.Node)3 Date (java.util.Date)2 POST (javax.ws.rs.POST)2 Path (javax.ws.rs.Path)2 PathParam (javax.ws.rs.PathParam)2 LongTaskRunnerNode (org.ligoj.app.resource.node.LongTaskRunnerNode)2 IOException (java.io.IOException)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Executors (java.util.concurrent.Executors)1 Consumer (java.util.function.Consumer)1 Function (java.util.function.Function)1 Supplier (java.util.function.Supplier)1 Collectors (java.util.stream.Collectors)1 Transactional (javax.transaction.Transactional)1