Skip to content
Snippets Groups Projects
Commit 5a2281cc authored by baldwinlouie's avatar baldwinlouie Committed by Yas Naoi
Browse files

Issue #3051125 by baldwinlouie: Implement batch API processing for AwsEc2Service::update*

parent 84cc512f
No related branches found
No related tags found
No related merge requests found
Showing
with 1102 additions and 697 deletions
...@@ -26,6 +26,7 @@ use Drupal\Core\Routing\RouteMatchInterface; ...@@ -26,6 +26,7 @@ use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StreamWrapper\PrivateStream; use Drupal\Core\StreamWrapper\PrivateStream;
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\field\Entity\FieldStorageConfig; use Drupal\field\Entity\FieldStorageConfig;
use Symfony\Component\HttpFoundation\RedirectResponse;
// 1 day in seconds. // 1 day in seconds.
const AWS_CLOUD_REIMPORT_INTERVAL = 86400; const AWS_CLOUD_REIMPORT_INTERVAL = 86400;
...@@ -1444,8 +1445,7 @@ function aws_cloud_form_cloud_config_aws_ec2_add_form_submit_batch( ...@@ -1444,8 +1445,7 @@ function aws_cloud_form_cloud_config_aws_ec2_add_form_submit_batch(
$entity->set('field_image_upload_url', [$api_endpoint_uri]); $entity->set('field_image_upload_url', [$api_endpoint_uri]);
$entity->save(); $entity->save();
$context['results']['regions'][] = $entity->getCloudContext();
aws_cloud_update_ec2_resources($entity);
} }
} }
...@@ -1491,10 +1491,14 @@ function aws_cloud_form_cloud_config_aws_ec2_add_form_submit_finished( ...@@ -1491,10 +1491,14 @@ function aws_cloud_form_cloud_config_aws_ec2_add_form_submit_finished(
array $results, array $results,
array $operations) { array $operations) {
drupal_flush_all_caches(); if ($success) {
$message = $success ? t('Creating Cloud config was performed successfully.') // Send to update_all url to update all the regions.
: t('Creating Cloud config has NOT been finished successfully.'); return new RedirectResponse('/clouds/aws_cloud/update_all?regions=' . implode(',', $results['regions']));
\Drupal::messenger()->addMessage($message); }
else {
$message = t('Creating Cloud config has NOT been finished successfully.');
\Drupal::messenger()->addMessage($message);
}
} }
/** /**
......
...@@ -86,6 +86,13 @@ entity.aws_cloud_snapshot.list_update: ...@@ -86,6 +86,13 @@ entity.aws_cloud_snapshot.list_update:
options: options:
perm: 'edit any aws cloud snapshot+edit own aws cloud snapshot' perm: 'edit any aws cloud snapshot+edit own aws cloud snapshot'
entity.aws_cloud.update_all:
path: '/clouds/aws_cloud/update_all'
defaults:
_controller: '\Drupal\aws_cloud\Controller\Ec2\ApiController::updateAll'
requirements:
_permission: 'add cloud config entities'
# AWS Cloud Admin Settings # AWS Cloud Admin Settings
aws_cloud.settings: aws_cloud.settings:
path: '/admin/config/services/cloud/aws_cloud' path: '/admin/config/services/cloud/aws_cloud'
......
services: services:
aws_cloud.ec2: aws_cloud.ec2:
class: Drupal\aws_cloud\Service\AwsEc2Service class: Drupal\aws_cloud\Service\AwsEc2Service
arguments: ['@entity_type.manager', '@logger.factory', '@config.factory', '@messenger', '@string_translation', '@current_user', '@plugin.manager.cloud_config_plugin', '@plugin.manager.field.field_type', '@entity_field.manager'] arguments: ['@entity_type.manager', '@logger.factory', '@config.factory', '@messenger', '@string_translation', '@current_user', '@plugin.manager.cloud_config_plugin', '@plugin.manager.field.field_type', '@entity_field.manager', '@lock']
aws_cloud.iam: aws_cloud.iam:
class: Drupal\aws_cloud\Service\AwsIamService class: Drupal\aws_cloud\Service\AwsIamService
......
...@@ -10,6 +10,7 @@ use Drupal\views\Views; ...@@ -10,6 +10,7 @@ use Drupal\views\Views;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Drupal\Core\Link; use Drupal\Core\Link;
use Symfony\Component\HttpFoundation\RequestStack;
/** /**
* Controller responsible for "update" urls. * Controller responsible for "update" urls.
...@@ -33,6 +34,13 @@ class ApiController extends ControllerBase implements ApiControllerInterface { ...@@ -33,6 +34,13 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
*/ */
protected $messenger; protected $messenger;
/**
* Request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
private $requestStack;
/** /**
* ApiController constructor. * ApiController constructor.
* *
...@@ -40,10 +48,13 @@ class ApiController extends ControllerBase implements ApiControllerInterface { ...@@ -40,10 +48,13 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
* Object for interfacing with AWS Api. * Object for interfacing with AWS Api.
* @param \Drupal\Core\Messenger\Messenger $messenger * @param \Drupal\Core\Messenger\Messenger $messenger
* Messanger Object. * Messanger Object.
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* Request stack object.
*/ */
public function __construct(AwsEc2ServiceInterface $aws_ec2_service, Messenger $messenger) { public function __construct(AwsEc2ServiceInterface $aws_ec2_service, Messenger $messenger, RequestStack $request_stack) {
$this->awsEc2Service = $aws_ec2_service; $this->awsEc2Service = $aws_ec2_service;
$this->messenger = $messenger; $this->messenger = $messenger;
$this->requestStack = $request_stack;
} }
/** /**
...@@ -52,10 +63,39 @@ class ApiController extends ControllerBase implements ApiControllerInterface { ...@@ -52,10 +63,39 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
public static function create(ContainerInterface $container) { public static function create(ContainerInterface $container) {
return new static( return new static(
$container->get('aws_cloud.ec2'), $container->get('aws_cloud.ec2'),
$container->get('messenger') $container->get('messenger'),
$container->get('request_stack')
); );
} }
/**
* {@inheritdoc}
*/
public function updateAll() {
$regions = $this->requestStack->getCurrentRequest()->query->get('regions');
if ($regions == NULL) {
$this->messageUser($this->t('No region specified'), 'error');
}
else {
$regions_array = explode(',', $regions);
foreach ($regions_array as $region) {
$entity = $this->entityTypeManager()->getStorage('cloud_config')
->loadByProperties(
[
'cloud_context' => $region,
]);
if ($entity) {
aws_cloud_update_ec2_resources(array_shift($entity));
}
}
$this->messageUser($this->t('Creating Cloud config was performed successfully.'));
drupal_flush_all_caches();
}
return $this->redirect('entity.cloud_config.collection');
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
...@@ -105,7 +145,7 @@ class ApiController extends ControllerBase implements ApiControllerInterface { ...@@ -105,7 +145,7 @@ class ApiController extends ControllerBase implements ApiControllerInterface {
} }
} }
else { else {
$message = t('AWS User ID is not specified.'); $message = $this->t('AWS User ID is not specified.');
$account = $this->currentUser(); $account = $this->currentUser();
if ($account->hasPermission('edit cloud config entities')) { if ($account->hasPermission('edit cloud config entities')) {
$message = Link::createFromRoute($message, 'entity.cloud_config.edit_form', ['cloud_config' => $cloud_config->id()])->toString(); $message = Link::createFromRoute($message, 'entity.cloud_config.edit_form', ['cloud_config' => $cloud_config->id()])->toString();
......
...@@ -95,4 +95,12 @@ interface ApiControllerInterface { ...@@ -95,4 +95,12 @@ interface ApiControllerInterface {
*/ */
public function updateSnapshotList($cloud_context); public function updateSnapshotList($cloud_context);
/**
* Update all entities in a given region.
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
* A redirect response object that may be returned by the controller.
*/
public function updateAll();
} }
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
namespace Drupal\aws_cloud\Service; namespace Drupal\aws_cloud\Service;
use Drupal\aws_cloud\Entity\Ec2\SecurityGroup;
/** /**
* Interface AwsEc2ServiceInterface. * Interface AwsEc2ServiceInterface.
*/ */
...@@ -778,4 +780,106 @@ interface AwsEc2ServiceInterface { ...@@ -778,4 +780,106 @@ interface AwsEc2ServiceInterface {
*/ */
public function getSupportedPlatforms(); public function getSupportedPlatforms();
/**
* Helper method to get the name of aws object.
*
* @param array $aws_obj
* Array of aws pbject.
* @param string $default_value
* Default value of tag name.
*
* @return string
* Tag name.
*/
public function getTagName(array $aws_obj, $default_value);
/**
* Helper method to get the map of snapshot ID and name.
*
* @param array $volumes
* Array of volumes.
*
* @return array
* Map of snapshots.
*/
public function getSnapshotIdNameMap(array $volumes);
/**
* Helper method to load an entity using parameters.
*
* @param string $entity_type
* Entity Type.
* @param string $id_field
* Entity id field.
* @param string $id_value
* Entity id value.
*
* @return int
* Entity id.
*/
public function getEntityId($entity_type, $id_field, $id_value);
/**
* Helper function to parse drupal uid value out of the tags array.
*
* @param array $tags_array
* The tags array.
* @param string $key
* The uid key.
*
* @return int
* Drupal uid.
*/
public function getUidTagValue(array $tags_array, $key);
/**
* Helper function to get an instance's uid.
*
* @param string $instance_id
* The instance_id to load.
*
* @return int
* The uid of the instance.
*/
public function getInstanceUid($instance_id);
/**
* Helper function to loop the network interfaces.
*
* Also creates a comma delimited string of private ips. Function returns
* false if no private ips found.
*
* @param array $network_interfaces
* Array of network interfaces from the EC2 DescribeInstance api.
*
* @return string|false
* Imploded string or FALSE if no private ips round.
*/
public function getPrivateIps(array $network_interfaces);
/**
* Setup the ipermission field given the inbound security group array.
*
* The array comes from DescribeSecurityGroup EC2 api call.
*
* @param array $ec2_permission
* An array object of Ec2 permission.
*
* @return array
* An array of \Drupal\Core\Field\FieldItemInterface.
*/
public function setupIpPermissionObject(array $ec2_permission);
/**
* Setup IP Permisssions.
*
* @param \Drupal\aws_cloud\Entity\Ec2\SecurityGroup $security_group
* The security group entity.
* @param string $field
* Field to used for lookup.
* @param array $ec2_permissions
* Permissions array from Ec2.
*/
public function setupIpPermissions(SecurityGroup &$security_group, $field, array $ec2_permissions);
} }
...@@ -12,7 +12,7 @@ use Drupal\Tests\UnitTestCase; ...@@ -12,7 +12,7 @@ use Drupal\Tests\UnitTestCase;
class AwsEc2ServiceTest extends UnitTestCase { class AwsEc2ServiceTest extends UnitTestCase {
/** /**
* {@inheritDoc} * {@inheritdoc}
*/ */
protected function setUp() { protected function setUp() {
$this->service = new AwsEc2ServiceMock( $this->service = new AwsEc2ServiceMock(
...@@ -28,7 +28,8 @@ class AwsEc2ServiceTest extends UnitTestCase { ...@@ -28,7 +28,8 @@ class AwsEc2ServiceTest extends UnitTestCase {
$this->getMock('Drupal\Core\Session\AccountInterface'), $this->getMock('Drupal\Core\Session\AccountInterface'),
$this->getMock('Drupal\cloud\Plugin\CloudConfigPluginManagerInterface'), $this->getMock('Drupal\cloud\Plugin\CloudConfigPluginManagerInterface'),
$this->getMock('Drupal\Core\Field\FieldTypePluginManagerInterface'), $this->getMock('Drupal\Core\Field\FieldTypePluginManagerInterface'),
$this->getMock('Drupal\Core\Entity\EntityFieldManagerInterface') $this->getMock('Drupal\Core\Entity\EntityFieldManagerInterface'),
$this->getMock('Drupal\Core\Lock\LockBackendInterface')
); );
} }
......
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