Skip to content
Snippets Groups Projects
Commit e69d2c8f authored by Masami  Suzuki's avatar Masami Suzuki Committed by Yas Naoi
Browse files

Issue #3067470 by Masami, baldwinlouie, yas: Add a refresh link to Instance Type Prices page

parent 0eb4e160
No related branches found
No related tags found
No related merge requests found
......@@ -486,6 +486,13 @@ aws_cloud.instance_type_prices:
attributes:
target: _blank
aws_cloud.instance_type_prices.refresh:
route_name: aws_cloud.instance_type_prices.list_update
title: 'Refresh'
weight: 20
appears_on:
- aws_cloud.instance_type_prices
entity.cloud_server_template.refresh:
route_name: entity.cloud_server_template.list_update
title: 'Refresh'
......
......@@ -3089,7 +3089,7 @@ function aws_cloud_update_instance_types(CloudConfig $cloud_config, $force = FAL
if ($next_import < time() || $cache == FALSE || $force == TRUE) {
aws_cloud_import_instance_types($cloud_config);
// Cache the instance types for 30 days.
// Cache the instance types for 1 day.
\Drupal::state()->set($state_key, time() + aws_cloud_get_reimport_interval());
}
}
......
......@@ -156,6 +156,16 @@ aws_cloud.instance_type_prices:
options:
perm: 'view aws cloud instance type prices'
aws_cloud.instance_type_prices.list_update:
path: '/clouds/aws_cloud/{cloud_context}/instance_type_price/update'
defaults:
_controller: '\Drupal\aws_cloud\Controller\Ec2\InstanceTypePriceController::update'
requirements:
# Use custom access that will check for cloud_context and the desired permission.
# Desired permission is passed as an option in the "perm" variable
_custom_access: '\Drupal\cloud\Controller\CloudConfigController::access'
options:
perm: 'view aws cloud instance type prices'
# AWS Cloud Instances Routes
entity.aws_cloud_instance.canonical:
......
......@@ -6,20 +6,42 @@ use Drupal\Core\Controller\ControllerBase;
use Drupal\aws_cloud\Service\InstanceTypePriceTableRenderer;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\cloud\Plugin\CloudConfigPluginManagerInterface;
use Drupal\Core\Routing\RouteBuilderInterface;
/**
* Controller responsible to show price list.
*/
class InstanceTypePriceController extends ControllerBase implements InstanceTypePriceControllerInterface {
/**
* The cloud config plugin.
*
* @var \Drupal\cloud\Plugin\CloudConfigPluginManagerInterface
*/
protected $cloudConfigPluginManager;
/**
* The route builder.
*
* @var \Drupal\Core\Routing\RouteBuilderInterface
*/
protected $routeBuilder;
/**
* InstanceTypePriceController constructor.
*
* @param \Drupal\aws_cloud\Service\InstanceTypePriceTableRenderer $price_table_renderer
* AWS Pricing service.
* @param \Drupal\cloud\Plugin\CloudConfigPluginManagerInterface $cloud_config_plugin_manager
* The cloud config plugin manager.
* @param \Drupal\Core\Routing\RouteBuilderInterface $route_builder
* The route builder.
*/
public function __construct(InstanceTypePriceTableRenderer $price_table_renderer) {
public function __construct(InstanceTypePriceTableRenderer $price_table_renderer, CloudConfigPluginManagerInterface $cloud_config_plugin_manager, RouteBuilderInterface $route_builder) {
$this->priceTableRenderer = $price_table_renderer;
$this->cloudConfigPluginManager = $cloud_config_plugin_manager;
$this->routeBuilder = $route_builder;
}
/**
......@@ -27,7 +49,9 @@ class InstanceTypePriceController extends ControllerBase implements InstanceType
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('aws_cloud.instance_type_price_table_renderer')
$container->get('aws_cloud.instance_type_price_table_renderer'),
$container->get('plugin.manager.cloud_config_plugin'),
$container->get('router.builder')
);
}
......@@ -42,4 +66,26 @@ class InstanceTypePriceController extends ControllerBase implements InstanceType
return $build;
}
/**
* {@inheritdoc}
*/
public function update($cloud_context) {
// Load the coud config.
$this->cloudConfigPluginManager->setCloudContext($cloud_context);
$cloud_config = $this->cloudConfigPluginManager->loadConfigEntity();
// Update instance types.
aws_cloud_update_instance_types($cloud_config, TRUE);
// Rebuild the route.
$this->routeBuilder->rebuild();
$this->messenger()->addMessage($this->t('Updated Instance Type Prices.'));
// Redirect to the price list.
return $this->redirect('aws_cloud.instance_type_prices', [
'cloud_context' => $cloud_context,
]);
}
}
......@@ -8,7 +8,7 @@ namespace Drupal\aws_cloud\Controller\Ec2;
interface InstanceTypePriceControllerInterface {
/**
* Update all instances in particular cloud region.
* Show instance type prices list.
*
* @param string $cloud_context
* Cloud context string.
......@@ -18,4 +18,15 @@ interface InstanceTypePriceControllerInterface {
*/
public function show($cloud_context);
/**
* Update instance type prices list.
*
* @param string $cloud_context
* Cloud context string.
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
* A redirect response object that may be returned by the controller.
*/
public function update($cloud_context);
}
......@@ -49,4 +49,49 @@ class InstanceTypePriceControllerTest extends AwsCloudTestCase {
}
}
/**
* Tests displaying prices.
*/
public function testUpdatePrice() {
$cloud_context = $this->cloudContext;
$instance_types = aws_cloud_get_instance_types($cloud_context);
$this->drupalGet("/clouds/aws_cloud/$cloud_context/instance_type_price");
$this->assertResponse(200);
$this->assertText(t('AWS Cloud Instance Type Prices'));
foreach ($instance_types as $instance_type_data) {
$parts = explode(':', $instance_type_data);
$this->assertText($parts[0]);
}
// Reset the cache.
$cloud_config_plugin = \Drupal::service('plugin.manager.cloud_config_plugin');
$cloud_config_plugin->setCloudContext($cloud_context);
$cloud_config = $cloud_config_plugin->loadConfigEntity();
$cache_key = _aws_cloud_get_instance_type_cache_key_by_region($cloud_config->get('field_region')->value);
\Drupal::cache()->set($cache_key, []);
\Drupal::service('router.builder')->rebuild();
$this->drupalGet("/clouds/aws_cloud/$cloud_context/instance_type_price");
$this->assertResponse(200);
$this->assertText(t('AWS Cloud Instance Type Prices'));
foreach ($instance_types as $instance_type_data) {
$parts = explode(':', $instance_type_data);
$this->assertNoText($parts[0]);
}
// Click 'Refresh'.
$this->clickLink(t('Refresh'));
$this->assertText(t('Updated Instance Type Prices.'));
$this->assertResponse(200);
$this->assertText(t('AWS Cloud Instance Type Prices'));
$new_instance_types = aws_cloud_get_instance_types($cloud_context);
foreach ($new_instance_types as $instance_type_data) {
$parts = explode(':', $instance_type_data);
$this->assertText($parts[0]);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment