use of org.ligoj.app.model.Subscription in project plugin-prov by ligoj.
the class TerraformResource method applyTerraform.
/**
* Apply (plan, apply, show) the Terraform configuration.
*
* @param subscription
* The related subscription.
*/
@POST
@Produces(MediaType.TEXT_HTML)
@Path("{subscription:\\d+}/terraform")
public void applyTerraform(@PathParam("subscription") final int subscription) {
final Subscription entity = subscriptionResource.checkVisibleSubscription(subscription);
final QuoteVo quote = resource.getConfiguration(entity);
// Check the provider support the Terraform generation
final Terraforming terra = getTerraform(entity.getNode());
log.info("Terraform request for {} ({})", subscription, entity);
final SecurityContext context = SecurityContextHolder.getContext();
// The Terraform execution will done into another thread
Executors.newSingleThreadExecutor().submit(() -> {
// Restore the context
log.info("Terraform start for {} ({})", entity.getId(), entity);
SecurityContextHolder.setContext(context);
try {
final File file = applyTerraform(entity, terra, quote);
log.info("Terraform succeed for {} ({})", entity.getId(), entity);
return file;
} catch (final Exception e) {
// The error is not put in the Terraform logger for security
log.error("Terraform failed for {}", entity, e);
}
return null;
});
}
use of org.ligoj.app.model.Subscription in project plugin-prov by ligoj.
the class TerraformResource method getTerraformLog.
/**
* Get the log of the current or last Terraform execution of a given subscription.
*
* @param subscription
* The related subscription.
* @return the streaming {@link Response} with output.
* @throws IOException
* When Terraform content cannot be written.
*/
@GET
@Produces(MediaType.TEXT_HTML)
@Path("{subscription:\\d+}/terraform.log")
public Response getTerraformLog(@PathParam("subscription") final int subscription) throws IOException {
final Subscription entity = subscriptionResource.checkVisibleSubscription(subscription);
final File log = toFile(entity, MAIN_LOG);
// Check there is a log file
if (log.exists()) {
final StreamingOutput so = o -> FileUtils.copyFile(toFile(entity, MAIN_LOG), o);
return Response.ok().entity(so).build();
}
// No log file for now
return Response.status(Status.NOT_FOUND).build();
}
use of org.ligoj.app.model.Subscription in project plugin-prov by ligoj.
the class TerraformResource method getTerraform.
/**
* Produce the Terraform configuration.
*
* @param subscription
* The related subscription.
* @param file
* The target file name.
* @return the {@link Response} ready to be consumed.
* @throws IOException
* When Terraform content cannot be written.
*/
@GET
@Produces(MediaType.APPLICATION_OCTET_STREAM)
@Path("{subscription:\\d+}/{file:.*.tf}")
public Response getTerraform(@PathParam("subscription") final int subscription, @PathParam("file") final String file) throws IOException {
final Terraforming terra = getTerraform(subscription);
final ByteArrayOutputStream output = new ByteArrayOutputStream();
terra.terraform(output, subscription, resource.getConfiguration(subscription));
return AbstractToolPluginResource.download(o -> o.write(output.toByteArray()), file).build();
}
use of org.ligoj.app.model.Subscription in project plugin-prov by ligoj.
the class ProvResourceTest method createNoCatalog.
@Test
public void createNoCatalog() {
final Subscription subscription = new Subscription();
subscription.setNode(em.find(Subscription.class, this.subscription).getNode());
subscription.setProject(em.find(Subscription.class, this.subscription).getProject());
em.persist(subscription);
qsRepository.deleteAll();
qiRepository.deleteAll();
ipRepository.deleteAll();
iptRepository.deleteAll();
itRepository.deleteAll();
spRepository.deleteAll();
stRepository.deleteAll();
icsRepository.deleteAll();
repository.deleteAll();
locationRepository.deleteAll();
em.flush();
em.clear();
Assertions.assertEquals("service:prov-no-catalog", Assertions.assertThrows(BusinessException.class, () -> {
resource.create(subscription.getId());
}).getMessage());
}
use of org.ligoj.app.model.Subscription in project plugin-prov by ligoj.
the class TerraformResourceTest method toFile.
@Test
public void toFile() throws IOException {
ThreadClassLoaderScope scope = null;
try {
final PluginsClassLoader classLoader = Mockito.mock(PluginsClassLoader.class);
scope = new ThreadClassLoaderScope(new URLClassLoader(new URL[0], classLoader));
final File file = new File("");
final Subscription entity = new Subscription();
entity.setId(15);
Mockito.when(classLoader.toFile(entity, "15", "some")).thenReturn(file);
Assertions.assertSame(file, resource.toFile(entity, "some"));
Assertions.assertNotNull(PluginsClassLoader.getInstance());
} finally {
IOUtils.closeQuietly(scope);
}
}
Aggregations