Search in sources :

Example 1 with QuoteVo

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

the class TerraformResourceTest method newResource.

private TerraformResource newResource(final Terraforming providerResource, final BiFunction<Subscription, String[], File> toFile, final boolean dryRun, final String... customArgs) {
    final TerraformResource resource = new TerraformResource() {

        @Override
        protected File toFile(final Subscription subscription, final String file) {
            return toFile.apply(subscription, new String[] { file });
        }

        /**
         * Prepare the Terraform environment to apply the new environment. Note there is no concurrency check.
         */
        @Override
        protected File applyTerraform(final Subscription entity, final Terraforming terra, final QuoteVo configuration) throws IOException, InterruptedException {
            if (dryRun) {
                // Ignore this call
                return null;
            }
            return super.applyTerraform(entity, terra, configuration);
        }
    };
    super.applicationContext.getAutowireCapableBeanFactory().autowireBean(resource);
    // Replace the plugin locator
    final ServicePluginLocator locator = Mockito.mock(ServicePluginLocator.class);
    resource.locator = locator;
    // Replace the runner
    resource.runner = new TerraformRunnerResource();
    super.applicationContext.getAutowireCapableBeanFactory().autowireBean(resource.runner);
    Mockito.when(locator.getResource("service:prov:test:account", Terraforming.class)).thenReturn(providerResource);
    final PluginsClassLoader classLoader = Mockito.mock(PluginsClassLoader.class);
    Mockito.when(classLoader.getHomeDirectory()).thenReturn(MOCK_PATH.toPath());
    // Replace the CLI runner
    resource.terraformUtils = new TerraformUtils() {

        @Override
        public ProcessBuilder newBuilder(String... args) {
            return new ProcessBuilder(ArrayUtils.addAll(new String[] { "java", "-cp", MOCK_PATH.getParent(), "org.ligoj.app.plugin.prov.terraform.Main" }, customArgs.length > 0 ? customArgs : args));
        }

        @Override
        protected boolean isInstalled() {
            return true;
        }

        @Override
        protected PluginsClassLoader getClassLoader() {
            return classLoader;
        }

        public String getLastestVersion() {
            return "2.0.0";
        }
    };
    return resource;
}
Also used : QuoteVo(org.ligoj.app.plugin.prov.QuoteVo) Subscription(org.ligoj.app.model.Subscription) PluginsClassLoader(org.ligoj.app.resource.plugin.PluginsClassLoader) ServicePluginLocator(org.ligoj.app.resource.ServicePluginLocator)

Example 2 with QuoteVo

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

the class ProvAzurePriceImportResourceTest method installOnLine.

@Test
public void installOnLine() throws Exception {
    configuration.delete(ProvAzurePriceImportResource.CONF_API_PRICES);
    configuration.saveOrUpdate(ProvAzurePriceImportResource.CONF_REGIONS, "europe-north");
    // Check the reserved
    final QuoteVo quote = installAndConfigure();
    Assertions.assertTrue(quote.getCost().getMin() > 150);
    // Check the spot
    final QuoteInstanceLookup lookup = qiResource.lookup(subscription, 8, 26000, true, VmOs.LINUX, "ds4v2", false, null, "36month");
    Assertions.assertTrue(lookup.getCost() > 100d);
    final ProvInstancePrice instance2 = lookup.getPrice();
    Assertions.assertEquals("base-three-year", instance2.getTerm().getName());
    Assertions.assertEquals("ds4v2", instance2.getType().getName());
}
Also used : QuoteVo(org.ligoj.app.plugin.prov.QuoteVo) ProvInstancePrice(org.ligoj.app.plugin.prov.model.ProvInstancePrice) QuoteInstanceLookup(org.ligoj.app.plugin.prov.QuoteInstanceLookup) Test(org.junit.jupiter.api.Test) AbstractServerTest(org.ligoj.app.AbstractServerTest)

Example 3 with QuoteVo

use of org.ligoj.app.plugin.prov.QuoteVo 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 4 with QuoteVo

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

the class TerraformResourceTest method applyTerraformError.

/**
 * IOException during the asynchronous execution
 */
@Test
public void applyTerraformError() {
    final TerraformResource resource = new TerraformResource() {

        @Override
        protected File applyTerraform(final Subscription entity, final Terraforming terra, final QuoteVo configuration) throws IOException {
            throw new IOException();
        }
    };
    super.applicationContext.getAutowireCapableBeanFactory().autowireBean(resource);
    // Mock to disable inner transactions for this test
    resource.resource = Mockito.mock(ProvResource.class);
    final ServicePluginLocator locator = Mockito.mock(ServicePluginLocator.class);
    // Replace the plugin locator
    resource.locator = locator;
    Mockito.when(locator.getResource("service:prov:test:account", Terraforming.class)).thenReturn(Mockito.mock(Terraforming.class));
    resource.applyTerraform(subscription);
}
Also used : ProvResource(org.ligoj.app.plugin.prov.ProvResource) QuoteVo(org.ligoj.app.plugin.prov.QuoteVo) IOException(java.io.IOException) Subscription(org.ligoj.app.model.Subscription) ServicePluginLocator(org.ligoj.app.resource.ServicePluginLocator) AbstractAppTest(org.ligoj.app.AbstractAppTest) Test(org.junit.jupiter.api.Test)

Example 5 with QuoteVo

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

the class ProvAzurePriceImportResourceTest method installOffLine.

@Test
public void installOffLine() throws Exception {
    // Install a new configuration
    final QuoteVo quote = install();
    // Check the whole quote
    final ProvQuoteInstance instance = check(quote, 157.096, 150.28d);
    // Check the spot
    final QuoteInstanceLookup lookup = qiResource.lookup(instance.getConfiguration().getSubscription().getId(), 2, 1741, true, VmOs.LINUX, null, true, null, null);
    Assertions.assertEquals(150.28, lookup.getCost(), DELTA);
    Assertions.assertEquals(150.28, lookup.getPrice().getCost(), DELTA);
    Assertions.assertEquals("base-three-year", lookup.getPrice().getTerm().getName());
    Assertions.assertFalse(lookup.getPrice().getTerm().isEphemeral());
    Assertions.assertEquals("ds4v2", lookup.getPrice().getType().getName());
    Assertions.assertEquals(10, ipRepository.countBy("term.name", "base-three-year"));
    Assertions.assertEquals("europe-north", lookup.getPrice().getLocation().getName());
    Assertions.assertEquals("North Europe", lookup.getPrice().getLocation().getDescription());
    checkImportStatus();
    // Install again to check the update without change
    resetImportTask();
    resource.install();
    provResource.updateCost(subscription);
    check(provResource.getConfiguration(subscription), 157.096d, 150.28d);
    checkImportStatus();
    // Now, change a price within the remote catalog
    // Point to another catalog with different prices
    configuration.saveOrUpdate(ProvAzurePriceImportResource.CONF_API_PRICES, "http://localhost:" + MOCK_PORT + "/v2");
    // Install the new catalog, update occurs
    resetImportTask();
    resource.install();
    provResource.updateCost(subscription);
    // Check the new price
    final QuoteVo newQuote = provResource.getConfiguration(subscription);
    Assertions.assertEquals(171.837d, newQuote.getCost().getMin(), DELTA);
    // Storage price is updated
    final ProvQuoteStorage storage = newQuote.getStorages().get(0);
    Assertions.assertEquals(1.537d, storage.getCost(), DELTA);
    Assertions.assertEquals(5, storage.getSize(), DELTA);
    // Compute price is updated
    final ProvQuoteInstance instance2 = newQuote.getInstances().get(0);
    Assertions.assertEquals(164.92d, instance2.getCost(), DELTA);
    ProvInstancePrice price = instance2.getPrice();
    Assertions.assertNull(price.getInitialCost());
    Assertions.assertEquals(VmOs.LINUX, price.getOs());
    Assertions.assertEquals(ProvTenancy.SHARED, price.getTenancy());
    Assertions.assertEquals(164.92d, price.getCost(), DELTA);
    final ProvInstancePriceTerm priceType = price.getTerm();
    Assertions.assertEquals("base-three-year", priceType.getName());
    Assertions.assertFalse(priceType.isEphemeral());
    Assertions.assertEquals(36, priceType.getPeriod());
    ProvInstanceType type = price.getType();
    Assertions.assertEquals("ds4v2", type.getName());
    Assertions.assertEquals("series:Dsv2, disk:56GiB", type.getDescription());
    // Check rating of "ds4v2"
    Assertions.assertEquals(Rate.GOOD, type.getRamRate());
    Assertions.assertEquals(Rate.GOOD, type.getCpuRate());
    Assertions.assertEquals(Rate.MEDIUM, type.getNetworkRate());
    Assertions.assertEquals(Rate.GOOD, type.getStorageRate());
    // Check rating of "f1"
    type = itRepository.findByName("f1");
    Assertions.assertEquals(Rate.GOOD, type.getRamRate());
    Assertions.assertEquals(Rate.MEDIUM, type.getCpuRate());
    Assertions.assertEquals(Rate.MEDIUM, type.getNetworkRate());
    Assertions.assertEquals(Rate.GOOD, type.getStorageRate());
    Assertions.assertEquals("series:F, disk:16GiB", type.getDescription());
    // Check rating of "ds15v2" (dedicated)
    price = iptRepository.findBy("type.name", "ds15v2");
    Assertions.assertEquals(ProvTenancy.DEDICATED, price.getTenancy());
    // Check status
    checkImportStatus();
    // Check some prices
    final ProvInstancePrice price2 = ipRepository.findBy("code", "europe-west-lowpriority-windows-a1-lowpriority");
    final ProvInstancePriceTerm term = price2.getTerm();
    Assertions.assertEquals("lowpriority", term.getName());
    Assertions.assertEquals(0, term.getPeriod());
    Assertions.assertEquals("europe-west", price2.getLocation().getName());
    Assertions.assertEquals(VmOs.WINDOWS, price2.getOs());
    Assertions.assertTrue(term.isEphemeral());
}
Also used : ProvInstanceType(org.ligoj.app.plugin.prov.model.ProvInstanceType) ProvQuoteStorage(org.ligoj.app.plugin.prov.model.ProvQuoteStorage) QuoteVo(org.ligoj.app.plugin.prov.QuoteVo) ProvQuoteInstance(org.ligoj.app.plugin.prov.model.ProvQuoteInstance) ProvInstancePrice(org.ligoj.app.plugin.prov.model.ProvInstancePrice) ProvInstancePriceTerm(org.ligoj.app.plugin.prov.model.ProvInstancePriceTerm) QuoteInstanceLookup(org.ligoj.app.plugin.prov.QuoteInstanceLookup) Test(org.junit.jupiter.api.Test) AbstractServerTest(org.ligoj.app.AbstractServerTest)

Aggregations

QuoteVo (org.ligoj.app.plugin.prov.QuoteVo)5 Test (org.junit.jupiter.api.Test)3 Subscription (org.ligoj.app.model.Subscription)3 IOException (java.io.IOException)2 AbstractServerTest (org.ligoj.app.AbstractServerTest)2 QuoteInstanceLookup (org.ligoj.app.plugin.prov.QuoteInstanceLookup)2 ProvInstancePrice (org.ligoj.app.plugin.prov.model.ProvInstancePrice)2 ServicePluginLocator (org.ligoj.app.resource.ServicePluginLocator)2 File (java.io.File)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 AbstractAppTest (org.ligoj.app.AbstractAppTest)1 ProvResource (org.ligoj.app.plugin.prov.ProvResource)1 ProvInstancePriceTerm (org.ligoj.app.plugin.prov.model.ProvInstancePriceTerm)1 ProvInstanceType (org.ligoj.app.plugin.prov.model.ProvInstanceType)1 ProvQuoteInstance (org.ligoj.app.plugin.prov.model.ProvQuoteInstance)1 ProvQuoteStorage (org.ligoj.app.plugin.prov.model.ProvQuoteStorage)1 PluginsClassLoader (org.ligoj.app.resource.plugin.PluginsClassLoader)1 BusinessException (org.ligoj.bootstrap.core.resource.BusinessException)1