Search in sources :

Example 31 with Subscription

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;
    });
}
Also used : QuoteVo(org.ligoj.app.plugin.prov.QuoteVo) SecurityContext(org.springframework.security.core.context.SecurityContext) Subscription(org.ligoj.app.model.Subscription) File(java.io.File) IOException(java.io.IOException) BusinessException(org.ligoj.bootstrap.core.resource.BusinessException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Example 32 with Subscription

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();
}
Also used : PathParam(javax.ws.rs.PathParam) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Produces(javax.ws.rs.Produces) CacheRemoveAll(javax.cache.annotation.CacheRemoveAll) GET(javax.ws.rs.GET) Path(javax.ws.rs.Path) Autowired(org.springframework.beans.factory.annotation.Autowired) ArrayUtils(org.apache.commons.lang3.ArrayUtils) AbstractToolPluginResource(org.ligoj.app.resource.plugin.AbstractToolPluginResource) MediaType(javax.ws.rs.core.MediaType) Matcher(java.util.regex.Matcher) ServicePluginLocator(org.ligoj.app.resource.ServicePluginLocator) NodeResource(org.ligoj.app.resource.node.NodeResource) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Service(org.springframework.stereotype.Service) Subscription(org.ligoj.app.model.Subscription) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) Status(javax.ws.rs.core.Response.Status) PrintWriter(java.io.PrintWriter) POST(javax.ws.rs.POST) Node(org.ligoj.app.model.Node) Transactional(javax.transaction.Transactional) QuoteVo(org.ligoj.app.plugin.prov.QuoteVo) FileWriter(java.io.FileWriter) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) StreamingOutput(javax.ws.rs.core.StreamingOutput) FileUtils(org.apache.commons.io.FileUtils) BusinessException(org.ligoj.bootstrap.core.resource.BusinessException) SubscriptionResource(org.ligoj.app.resource.subscription.SubscriptionResource) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) CacheResult(javax.cache.annotation.CacheResult) IOUtils(org.apache.commons.io.IOUtils) Slf4j(lombok.extern.slf4j.Slf4j) Response(javax.ws.rs.core.Response) SecurityContext(org.springframework.security.core.context.SecurityContext) Writer(java.io.Writer) Optional(java.util.Optional) ProvResource(org.ligoj.app.plugin.prov.ProvResource) Pattern(java.util.regex.Pattern) PluginsClassLoader(org.ligoj.app.resource.plugin.PluginsClassLoader) StreamingOutput(javax.ws.rs.core.StreamingOutput) Subscription(org.ligoj.app.model.Subscription) File(java.io.File) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 33 with Subscription

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();
}
Also used : PathParam(javax.ws.rs.PathParam) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Produces(javax.ws.rs.Produces) CacheRemoveAll(javax.cache.annotation.CacheRemoveAll) GET(javax.ws.rs.GET) Path(javax.ws.rs.Path) Autowired(org.springframework.beans.factory.annotation.Autowired) ArrayUtils(org.apache.commons.lang3.ArrayUtils) AbstractToolPluginResource(org.ligoj.app.resource.plugin.AbstractToolPluginResource) MediaType(javax.ws.rs.core.MediaType) Matcher(java.util.regex.Matcher) ServicePluginLocator(org.ligoj.app.resource.ServicePluginLocator) NodeResource(org.ligoj.app.resource.node.NodeResource) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Service(org.springframework.stereotype.Service) Subscription(org.ligoj.app.model.Subscription) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) Status(javax.ws.rs.core.Response.Status) PrintWriter(java.io.PrintWriter) POST(javax.ws.rs.POST) Node(org.ligoj.app.model.Node) Transactional(javax.transaction.Transactional) QuoteVo(org.ligoj.app.plugin.prov.QuoteVo) FileWriter(java.io.FileWriter) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) StreamingOutput(javax.ws.rs.core.StreamingOutput) FileUtils(org.apache.commons.io.FileUtils) BusinessException(org.ligoj.bootstrap.core.resource.BusinessException) SubscriptionResource(org.ligoj.app.resource.subscription.SubscriptionResource) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) CacheResult(javax.cache.annotation.CacheResult) IOUtils(org.apache.commons.io.IOUtils) Slf4j(lombok.extern.slf4j.Slf4j) Response(javax.ws.rs.core.Response) SecurityContext(org.springframework.security.core.context.SecurityContext) Writer(java.io.Writer) Optional(java.util.Optional) ProvResource(org.ligoj.app.plugin.prov.ProvResource) Pattern(java.util.regex.Pattern) PluginsClassLoader(org.ligoj.app.resource.plugin.PluginsClassLoader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 34 with Subscription

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());
}
Also used : Subscription(org.ligoj.app.model.Subscription) AbstractAppTest(org.ligoj.app.AbstractAppTest) Test(org.junit.jupiter.api.Test)

Example 35 with Subscription

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);
    }
}
Also used : URLClassLoader(java.net.URLClassLoader) ThreadClassLoaderScope(org.eclipse.jetty.util.thread.ThreadClassLoaderScope) Subscription(org.ligoj.app.model.Subscription) PluginsClassLoader(org.ligoj.app.resource.plugin.PluginsClassLoader) File(java.io.File) AbstractAppTest(org.ligoj.app.AbstractAppTest) Test(org.junit.jupiter.api.Test)

Aggregations

Subscription (org.ligoj.app.model.Subscription)43 Test (org.junit.jupiter.api.Test)22 Path (javax.ws.rs.Path)10 Node (org.ligoj.app.model.Node)10 AbstractServerTest (org.ligoj.app.AbstractServerTest)9 IOException (java.io.IOException)7 GET (javax.ws.rs.GET)7 POST (javax.ws.rs.POST)7 HashMap (java.util.HashMap)6 Transactional (javax.transaction.Transactional)6 Produces (javax.ws.rs.Produces)6 Map (java.util.Map)5 Optional (java.util.Optional)5 PathParam (javax.ws.rs.PathParam)5 MediaType (javax.ws.rs.core.MediaType)5 Slf4j (lombok.extern.slf4j.Slf4j)5 File (java.io.File)4 Date (java.util.Date)4 Function (java.util.function.Function)4 DELETE (javax.ws.rs.DELETE)4