diff -u b/modules/cloud_service_providers/aws_cloud/src/Controller/Ec2/ApiController.php b/modules/cloud_service_providers/aws_cloud/src/Controller/Ec2/ApiController.php --- b/modules/cloud_service_providers/aws_cloud/src/Controller/Ec2/ApiController.php +++ b/modules/cloud_service_providers/aws_cloud/src/Controller/Ec2/ApiController.php @@ -163,7 +163,7 @@ $this->ec2Service->setCloudContext($cloud_context); $updated = $this->ec2Service->updateInstances(); - if ($updated !== FALSE) { + if ($updated === TRUE) { $this->messageUser($this->t('Updated Instances.')); Ec2Service::clearCacheValue(); } @@ -178,44 +178,9 @@ /** * Update all instances of all cloud region. - * - * @return \Symfony\Component\HttpFoundation\RedirectResponse - * Redirect to all instance list. */ public function updateAllInstanceList() { - $updated_instances = $this->ec2Service->updateAllInstances(); - - foreach ($updated_instances ?: [] as $cloud_context => $updated) { - $this->cloudConfigPluginManager->setCloudContext($cloud_context); - $cloud_config = $this->cloudConfigPluginManager->loadConfigEntity(); - $this->ec2Service->updateEntityMessage('aws_cloud_instance', $cloud_context, $cloud_config, $updated); - } - - return $this->redirect('view.aws_cloud_instance.all'); - } - - /** - * Get account id. - * - * @param array $cloud_config_entities - * Cloud config entities array. - * - * @return string - * The Account id. - */ - public function getAccountId(array $cloud_config_entities) { - if (!empty($cloud_config_entities)) { - $cloud_config = reset($cloud_config_entities); - $account_id = $cloud_config->get('field_account_id')->value; - // Use the switch role account_id if switching is enabled. - $use_assume_role = $cloud_config->get('field_use_assume_role')->value ?? FALSE; - $use_switch_role = $cloud_config->get('field_use_switch_role')->value ?? FALSE; - if (!empty($use_assume_role) && !empty($use_switch_role)) { - $account_id = trim($cloud_config->get('field_switch_role_account_id')->value); - } - } - - return $account_id; + return $this->ec2Service->updateAllResourceList('aws_cloud_instance'); } /** @@ -236,7 +201,7 @@ $cloud_config = reset($cloud_config_entities); } - $account_id = $this->getAccountId($cloud_config_entities); + $account_id = $this->ec2Service->getAccountId($cloud_config_entities); if ($account_id) { $this->ec2Service->setCloudContext($cloud_context); @@ -246,7 +211,7 @@ ], ], TRUE); - if ($updated !== FALSE) { + if ($updated === TRUE) { $this->messageUser($this->t('Updated Images.')); Ec2Service::clearCacheValue(); } @@ -275,46 +240,7 @@ * Redirect to all image list. */ public function updateAllImageList() { - $cloud_configs = $this->entityTypeManager() - ->getStorage('cloud_config') - ->loadByProperties([ - 'type' => 'aws_cloud', - ]); - - foreach ($cloud_configs ?: [] as $cloud_config_entity) { - $cloud_context = $cloud_config_entity->getCloudContext(); - - $cloud_config_entities = $this->entityTypeManager()->getStorage('cloud_config')->loadByProperties( - ['cloud_context' => [$cloud_context]] - ); - - if (!empty($cloud_config_entities)) { - $cloud_config = reset($cloud_config_entities); - } - - $account_id = $this->getAccountId($cloud_config_entities); - - if ($account_id) { - - $this->ec2Service->setCloudContext($cloud_context); - $updated = $this->ec2Service->updateAllImages([ - 'Owners' => [ - $account_id, - ], - ], TRUE); - - $this->ec2Service->updateEntityMessage('aws_cloud_image', $cloud_context, $cloud_config, $updated); - } - else { - $message = $this->t('AWS User ID is not specified.'); - $account = $this->currentUser(); - if ($account->hasPermission('edit cloud service providers')) { - $message = Link::createFromRoute($message, 'entity.cloud_config.edit_form', ['cloud_config' => $cloud_config->id()])->toString(); - } - $this->messageUser($message, 'error'); - } - } - return $this->redirect('view.aws_cloud_image.all'); + return $this->ec2Service->updateAllResourceList('aws_cloud_image'); } /** @@ -330,7 +256,7 @@ $this->ec2Service->setCloudContext($cloud_context); $updated = $this->ec2Service->updateSecurityGroups(); - if ($updated !== FALSE) { + if ($updated === TRUE) { $this->messageUser($this->t('Updated Security Groups.')); Ec2Service::clearCacheValue(); } @@ -345,20 +271,9 @@ /** * Update all security groups of all cloud region. - * - * @return \Symfony\Component\HttpFoundation\RedirectResponse - * Redirect to all security group list. */ public function updateAllSecurityGroupList() { - $updated_security_groups = $this->ec2Service->updateAllSecurityGroups(); - - foreach ($updated_security_groups ?: [] as $cloud_context => $updated) { - $this->cloudConfigPluginManager->setCloudContext($cloud_context); - $cloud_config = $this->cloudConfigPluginManager->loadConfigEntity(); - $this->ec2Service->updateEntityMessage('aws_cloud_security_group', $cloud_context, $cloud_config, $updated); - } - - return $this->redirect('view.aws_cloud_security_group.all'); + return $this->ec2Service->updateAllResourceList('aws_cloud_security_group'); } /** @@ -374,7 +289,7 @@ $this->ec2Service->setCloudContext($cloud_context); $updated = $this->ec2Service->updateNetworkInterfaces(); - if ($updated !== FALSE) { + if ($updated === TRUE) { $this->messageUser($this->t('Updated Network Interfaces.')); Ec2Service::clearCacheValue(); } @@ -389,20 +304,9 @@ /** * Update all network interfaces of all cloud region. - * - * @return \Symfony\Component\HttpFoundation\RedirectResponse - * Redirect to all network interface list. */ public function updateAllNetworkInterfaceList() { - $updated_network_interfaces = $this->ec2Service->updateAllNetworkInterfaces(); - - foreach ($updated_network_interfaces ?: [] as $cloud_context => $updated) { - $this->cloudConfigPluginManager->setCloudContext($cloud_context); - $cloud_config = $this->cloudConfigPluginManager->loadConfigEntity(); - $this->ec2Service->updateEntityMessage('aws_cloud_network_interface', $cloud_context, $cloud_config, $updated); - } - - return $this->redirect('view.aws_cloud_network_interface.all'); + return $this->ec2Service->updateAllResourceList('aws_cloud_network_interface'); } /** @@ -419,7 +323,7 @@ $this->ec2Service->setCloudContext($cloud_context); $updated = $this->ec2Service->updateElasticIp(); - if ($updated !== FALSE) { + if ($updated === TRUE) { // Also update Network Interfaces. $updated = $this->ec2Service->updateNetworkInterfaces(); if ($updated !== FALSE) { @@ -443,20 +347,9 @@ /** * Update all elastic ips of all cloud region. - * - * @return \Symfony\Component\HttpFoundation\RedirectResponse - * Redirect to all instance list. */ public function updateAllElasticIpList() { - $updated_elastic_ips = $this->ec2Service->updateAllElasticIp(); - - foreach ($updated_elastic_ips ?: [] as $cloud_context => $updated) { - $this->cloudConfigPluginManager->setCloudContext($cloud_context); - $cloud_config = $this->cloudConfigPluginManager->loadConfigEntity(); - $this->ec2Service->updateEntityMessage('aws_cloud_elastic_ip', $cloud_context, $cloud_config, $updated); - } - - return $this->redirect('view.aws_cloud_elastic_ip.all'); + return $this->ec2Service->updateAllResourceList('aws_cloud_elastic_ip'); } /** @@ -473,7 +366,7 @@ $this->ec2Service->setCloudContext($cloud_context); $updated = $this->ec2Service->updateKeyPairs(); - if ($updated !== FALSE) { + if ($updated === TRUE) { $this->messageUser($this->t('Updated Key Pairs.')); Ec2Service::clearCacheValue(); } @@ -493,15 +386,7 @@ * Redirect to all instance list. */ public function updateAllKeyPairList() { - $updated_key_pairs = $this->ec2Service->updateAllKeyPairs(); - - foreach ($updated_key_pairs ?: [] as $cloud_context => $updated) { - $this->cloudConfigPluginManager->setCloudContext($cloud_context); - $cloud_config = $this->cloudConfigPluginManager->loadConfigEntity(); - $this->ec2Service->updateEntityMessage('aws_cloud_key_pair', $cloud_context, $cloud_config, $updated); - } - - return $this->redirect('view.aws_cloud_key_pair.all'); + return $this->ec2Service->updateAllResourceList('aws_cloud_key_pair'); } /** @@ -517,7 +402,7 @@ $this->ec2Service->setCloudContext($cloud_context); $updated = $this->ec2Service->updateVolumes(); - if ($updated !== FALSE) { + if ($updated === TRUE) { $this->messageUser($this->t('Updated Volumes.')); Ec2Service::clearCacheValue(); } @@ -532,20 +417,9 @@ /** * Update all volumes of all cloud region. - * - * @return \Symfony\Component\HttpFoundation\RedirectResponse - * Redirect to all volume list. */ public function updateAllVolumeList() { - $updated_volumes = $this->ec2Service->updateAllVolumes(); - - foreach ($updated_volumes ?: [] as $cloud_context => $updated) { - $this->cloudConfigPluginManager->setCloudContext($cloud_context); - $cloud_config = $this->cloudConfigPluginManager->loadConfigEntity(); - $this->ec2Service->updateEntityMessage('aws_cloud_volume', $cloud_context, $cloud_config, $updated); - } - - return $this->redirect('view.aws_cloud_volume.all'); + return $this->ec2Service->updateAllResourceList('aws_cloud_volume'); } /** @@ -562,7 +436,7 @@ $this->ec2Service->setCloudContext($cloud_context); $updated = $this->ec2Service->updateSnapshots(); - if ($updated !== FALSE) { + if ($updated === TRUE) { $this->messageUser($this->t('Updated Snapshots.')); Ec2Service::clearCacheValue(); } @@ -577,20 +451,9 @@ /** * Update all snapshots of all cloud region. - * - * @return \Symfony\Component\HttpFoundation\RedirectResponse - * Redirect to all snapshot list. */ public function updateAllSnapshotList() { - $updated_snapshots = $this->ec2Service->updateAllSnapshots(); - - foreach ($updated_snapshots ?: [] as $cloud_context => $updated) { - $this->cloudConfigPluginManager->setCloudContext($cloud_context); - $cloud_config = $this->cloudConfigPluginManager->loadConfigEntity(); - $this->ec2Service->updateEntityMessage('aws_cloud_snapshot', $cloud_context, $cloud_config, $updated); - } - - return $this->redirect('view.aws_cloud_snapshot.all'); + return $this->ec2Service->updateAllResourceList('aws_cloud_snapshot'); } /** diff -u b/modules/cloud_service_providers/aws_cloud/src/Controller/Vpc/ApiController.php b/modules/cloud_service_providers/aws_cloud/src/Controller/Vpc/ApiController.php --- b/modules/cloud_service_providers/aws_cloud/src/Controller/Vpc/ApiController.php +++ b/modules/cloud_service_providers/aws_cloud/src/Controller/Vpc/ApiController.php @@ -115,7 +115,7 @@ $this->ec2Service->setCloudContext($cloud_context); $updated = $this->ec2Service->updateVpcs(); - if ($updated !== FALSE) { + if ($updated === TRUE) { $this->messenger->addStatus($this->t('Updated VPCs.')); Ec2Service::clearCacheValue(); } @@ -130,20 +130,9 @@ /** * Update all vpcs of all cloud region. - * - * @return \Symfony\Component\HttpFoundation\RedirectResponse - * Redirect to all vpc list. */ public function updateAllVpcList() { - $updated_vpcs = $this->ec2Service->updateAllVpcs(); - - foreach ($updated_vpcs ?: [] as $cloud_context => $updated) { - $this->cloudConfigPluginManager->setCloudContext($cloud_context); - $cloud_config = $this->cloudConfigPluginManager->loadConfigEntity(); - $this->ec2Service->updateEntityMessage('aws_cloud_vpc', $cloud_context, $cloud_config, $updated); - } - - return $this->redirect('view.aws_cloud_vpc.all'); + return $this->ec2Service->updateAllResourceList('aws_cloud_vpc'); } /** @@ -160,7 +149,7 @@ $this->ec2Service->setCloudContext($cloud_context); $updated = $this->ec2Service->updateVpcPeeringConnections(); - if ($updated !== FALSE) { + if ($updated === TRUE) { $this->messenger->addStatus($this->t('Updated VPC Peering Connections.')); Ec2Service::clearCacheValue(); } @@ -175,20 +164,9 @@ /** * Update all vpcpeeringconnections of all cloud region. - * - * @return \Symfony\Component\HttpFoundation\RedirectResponse - * Redirect to all vpcpeeringconnection list. */ public function updateAllVpcPeeringConnectionList() { - $updated_vpc_peering_connections = $this->ec2Service->updateAllVpcPeeringConnections(); - - foreach ($updated_vpc_peering_connections ?: [] as $cloud_context => $updated) { - $this->cloudConfigPluginManager->setCloudContext($cloud_context); - $cloud_config = $this->cloudConfigPluginManager->loadConfigEntity(); - $this->ec2Service->updateEntityMessage('aws_cloud_vpc_peering_connection', $cloud_context, $cloud_config, $updated); - } - - return $this->redirect('view.aws_cloud_vpc_peering_connection.all'); + return $this->ec2Service->updateAllResourceList('aws_cloud_vpc_peering_connection'); } /** @@ -205,7 +183,7 @@ $this->ec2Service->setCloudContext($cloud_context); $updated = $this->ec2Service->updateSubnets(); - if ($updated !== FALSE) { + if ($updated === TRUE) { $this->messenger->addStatus($this->t('Updated Subnets.')); Ec2Service::clearCacheValue(); } @@ -220,20 +198,9 @@ /** * Update all subnets of all cloud region. - * - * @return \Symfony\Component\HttpFoundation\RedirectResponse - * Redirect to all subnet list. */ public function updateAllSubnetList() { - $updated_subnets = $this->ec2Service->updateAllSubnets(); - - foreach ($updated_subnets ?: [] as $cloud_context => $updated) { - $this->cloudConfigPluginManager->setCloudContext($cloud_context); - $cloud_config = $this->cloudConfigPluginManager->loadConfigEntity(); - $this->ec2Service->updateEntityMessage('aws_cloud_subnet', $cloud_context, $cloud_config, $updated); - } - - return $this->redirect('view.aws_cloud_subnet.all'); + return $this->ec2Service->updateAllResourceList('aws_cloud_subnet'); } } diff -u b/modules/cloud_service_providers/aws_cloud/src/Service/Ec2/Ec2Service.php b/modules/cloud_service_providers/aws_cloud/src/Service/Ec2/Ec2Service.php --- b/modules/cloud_service_providers/aws_cloud/src/Service/Ec2/Ec2Service.php +++ b/modules/cloud_service_providers/aws_cloud/src/Service/Ec2/Ec2Service.php @@ -25,6 +25,7 @@ use Drupal\Core\Queue\QueueFactory; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Url; +use Symfony\Component\HttpFoundation\RedirectResponse; /** * Ec2Service interacts with the Amazon EC2 API. @@ -1232,20 +1233,7 @@ return FALSE; } - $cloud_configs = $this->entityTypeManager - ->getStorage('cloud_config') - ->loadByProperties([ - 'type' => 'aws_cloud', - ]); - - $updated = []; - foreach ($cloud_configs ?: [] as $cloud_config) { - $cloud_context = $cloud_config->getCloudContext(); - $this->setCloudContext($cloud_context); - - $updated[$cloud_context] = $this->updateInstanceEntities($params, $clear, $entity_type, $cloud_context); - } - + $updated = $this->updateInstanceEntities($params, $clear, $entity_type, $this->cloudContext); $this->lock->release($lock_name); return $updated; } @@ -1332,7 +1320,7 @@ 'finished', ], [$entity_type, $stale, $clear]); $this->runBatch($batch_builder); - $updated = count($result['Images']); + $updated = TRUE; } return $updated; @@ -1429,20 +1417,7 @@ return FALSE; } - $cloud_configs = $this->entityTypeManager - ->getStorage('cloud_config') - ->loadByProperties([ - 'type' => 'aws_cloud', - ]); - - $updated = []; - foreach ($cloud_configs ?: [] as $cloud_config) { - $cloud_context = $cloud_config->getCloudContext(); - $this->setCloudContext($cloud_context); - - $updated[$cloud_context] = $this->updateImageEntities($params, $clear, $entity_type, $cloud_context); - } - + $updated = $this->updateImageEntities($params, $clear, $entity_type, $this->cloudContext); $this->lock->release($lock_name); return $updated; } @@ -1547,20 +1522,7 @@ return FALSE; } - $cloud_configs = $this->entityTypeManager - ->getStorage('cloud_config') - ->loadByProperties([ - 'type' => 'aws_cloud', - ]); - - $updated = []; - foreach ($cloud_configs ?: [] as $cloud_config) { - $cloud_context = $cloud_config->getCloudContext(); - $this->setCloudContext($cloud_context); - - $updated[$cloud_context] = $this->updateSecurityGroupEntities($params, $clear, $entity_type, $cloud_context); - } - + $updated = $this->updateSecurityGroupEntities($params, $clear, $entity_type, $this->cloudContext); $this->lock->release($lock_name); return $updated; } @@ -1894,20 +1856,7 @@ return FALSE; } - $cloud_configs = $this->entityTypeManager - ->getStorage('cloud_config') - ->loadByProperties([ - 'type' => 'aws_cloud', - ]); - - $updated = []; - foreach ($cloud_configs ?: [] as $cloud_config) { - $cloud_context = $cloud_config->getCloudContext(); - $this->setCloudContext($cloud_context); - - $updated[$cloud_context] = $this->updateNetworkInterfaceEntities($params, $clear, $entity_type, $cloud_context); - } - + $updated = $this->updateNetworkInterfaceEntities($params, $clear, $entity_type, $this->cloudContext); $this->lock->release($lock_name); return $updated; } @@ -1989,7 +1938,7 @@ * @return bool * indicates success so failure. */ - public function updateAllElasticIp() { + public function updateAllElasticIps() { $entity_type = 'aws_cloud_elastic_ip'; $lock_name = $this->getLockKey($entity_type); @@ -1997,20 +1946,7 @@ return FALSE; } - $cloud_configs = $this->entityTypeManager - ->getStorage('cloud_config') - ->loadByProperties([ - 'type' => 'aws_cloud', - ]); - - $updated = []; - foreach ($cloud_configs ?: [] as $cloud_config) { - $cloud_context = $cloud_config->getCloudContext(); - $this->setCloudContext($cloud_context); - - $updated[$cloud_context] = $this->updateElasticIpEntities($entity_type, $cloud_context); - } - + $updated = $this->updateElasticIpEntities($entity_type, $this->cloudContext); $this->lock->release($lock_name); return $updated; } @@ -2099,20 +2035,7 @@ return FALSE; } - $cloud_configs = $this->entityTypeManager - ->getStorage('cloud_config') - ->loadByProperties([ - 'type' => 'aws_cloud', - ]); - - $updated = []; - foreach ($cloud_configs ?: [] as $cloud_config) { - $cloud_context = $cloud_config->getCloudContext(); - $this->setCloudContext($cloud_context); - - $updated[$cloud_context] = $this->updateKeyPairEntities($entity_type, $cloud_context); - } - + $updated = $this->updateKeyPairEntities($entity_type, $this->cloudContext); $this->lock->release($lock_name); return $updated; } @@ -2203,20 +2126,7 @@ return FALSE; } - $cloud_configs = $this->entityTypeManager - ->getStorage('cloud_config') - ->loadByProperties([ - 'type' => 'aws_cloud', - ]); - - $updated = []; - foreach ($cloud_configs ?: [] as $cloud_config) { - $cloud_context = $cloud_config->getCloudContext(); - $this->setCloudContext($cloud_context); - - $updated[$cloud_context] = $this->updateVolumeEntities($entity_type, $cloud_context); - } - + $updated = $this->updateVolumeEntities($entity_type, $this->cloudContext); $this->lock->release($lock_name); return $updated; } @@ -2306,20 +2216,7 @@ return FALSE; } - $cloud_configs = $this->entityTypeManager - ->getStorage('cloud_config') - ->loadByProperties([ - 'type' => 'aws_cloud', - ]); - - $updated = []; - foreach ($cloud_configs ?: [] as $cloud_config) { - $cloud_context = $cloud_config->getCloudContext(); - $this->setCloudContext($cloud_context); - - $updated[$cloud_context] = $this->updateSnapshotEntities($entity_type, $cloud_context); - } - + $updated = $this->updateSnapshotEntities($entity_type, $this->cloudContext); $this->lock->release($lock_name); return $updated; } @@ -2468,20 +2365,7 @@ return FALSE; } - $cloud_configs = $this->entityTypeManager - ->getStorage('cloud_config') - ->loadByProperties([ - 'type' => 'aws_cloud', - ]); - - $updated = []; - foreach ($cloud_configs ?: [] as $cloud_config) { - $cloud_context = $cloud_config->getCloudContext(); - $this->setCloudContext($cloud_context); - - $updated[$cloud_context] = $this->updateVpcEntities($params, $clear, $entity_type, $cloud_context); - } - + $updated = $this->updateVpcEntities($params, $clear, $entity_type, $this->cloudContext); $this->lock->release($lock_name); return $updated; } @@ -2586,20 +2470,7 @@ return FALSE; } - $cloud_configs = $this->entityTypeManager - ->getStorage('cloud_config') - ->loadByProperties([ - 'type' => 'aws_cloud', - ]); - - $updated = []; - foreach ($cloud_configs ?: [] as $cloud_config) { - $cloud_context = $cloud_config->getCloudContext(); - $this->setCloudContext($cloud_context); - - $updated[$cloud_context] = $this->updateVpcPeeringConnectionEntities($params, $clear, $entity_type, $cloud_context); - } - + $updated = $this->updateVpcPeeringConnectionEntities($params, $clear, $entity_type, $this->cloudContext); $this->lock->release($lock_name); return $updated; } @@ -2794,20 +2665,7 @@ return FALSE; } - $cloud_configs = $this->entityTypeManager - ->getStorage('cloud_config') - ->loadByProperties([ - 'type' => 'aws_cloud', - ]); - - $updated = []; - foreach ($cloud_configs ?: [] as $cloud_config) { - $cloud_context = $cloud_config->getCloudContext(); - $this->setCloudContext($cloud_context); - - $updated[$cloud_context] = $this->updateSubnetEntities($params, $clear, $entity_type, $cloud_context); - } - + $updated = $this->updateSubnetEntities($params, $clear, $entity_type, $this->cloudContext); $this->lock->release($lock_name); return $updated; } @@ -3200,7 +3058,7 @@ 'updateAllImages', 'updateAllSecurityGroups', 'updateAllNetworkInterfaces', - 'updateAllElasticIp', + 'updateAllElasticIps', 'updateAllKeyPairs', 'updateAllVolumes', 'updateAllSnapshots', @@ -3238,20 +3096,42 @@ } /** + * Get account id. + * + * @param array $cloud_config_entities + * Cloud config entities array. + * + * @return string + * The Account id. + */ + public function getAccountId(array $cloud_config_entities) { + if (!empty($cloud_config_entities)) { + $cloud_config = reset($cloud_config_entities); + $account_id = $cloud_config->get('field_account_id')->value; + // Use the switch role account_id if switching is enabled. + $use_assume_role = $cloud_config->get('field_use_assume_role')->value ?? FALSE; + $use_switch_role = $cloud_config->get('field_use_switch_role')->value ?? FALSE; + if (!empty($use_assume_role) && !empty($use_switch_role)) { + $account_id = trim($cloud_config->get('field_switch_role_account_id')->value); + } + } + + return $account_id; + } + + /** * Update entity message. * * @param string $entity_type * The entity type. - * @param string $cloud_context - * The cloud context. * @param object $cloud_config * The cloud config object. * @param bool $updated * TRUE if entity is updated. */ - public function updateEntityMessage($entity_type, $cloud_context, $cloud_config, $updated) { + private function updateEntityMessage($entity_type, $cloud_config, $updated) { $labels = $this->getDisplayLabels($entity_type); - $resource_link = $cloud_config->getResourceLink($labels['plural'], $entity_type, $cloud_context); + $resource_link = $cloud_config->getResourceLink($labels['plural'], $entity_type); $cloud_config_link = $cloud_config->getCloudConfigLink(); if ($entity_type === 'aws_cloud_elastic_ip') { @@ -3260,27 +3140,29 @@ $network_interface_label['plural'], Url::fromRoute( "entity.aws_cloud_network_interface.collection", - ['cloud_context' => $cloud_context] + ['cloud_context' => $cloud_config->getCloudContext()] ) )->toString(); - if ($updated !== FALSE) { + if ($updated === TRUE) { // Also update Network Interfaces. $updated = $this->updateAllNetworkInterfaces(); - if ($updated !== FALSE) { - $this->messageUser($this->t('Updated %resources and %other_resource of %cloud_config cloud service provider.', [ - '%resources' => $resource_link ?? $labels['plural'], - '%other_resource' => $network_interface_link ?? $network_interface_label['plural'], - '%cloud_config' => $cloud_config_link ?? $cloud_config->getName(), - ])); - } - else { - $this->messageUser($this->t('Unable to update %other_resource while updating %resources of %cloud_config cloud service provider.', [ - '%resources' => $resource_link ?? $labels['plural'], - '%other_resource' => $network_interface_link ?? $network_interface_label['plural'], - '%cloud_config' => $cloud_config_link ?? $cloud_config->getName(), - ]), 'error'); - } + $success_message = $this->t('Updated %resources and %other_resource of %cloud_config cloud service provider.', [ + '%resources' => $resource_link ?? $labels['plural'], + '%other_resource' => $network_interface_link ?? $network_interface_label['plural'], + '%cloud_config' => $cloud_config_link ?? $cloud_config->getName(), + ]); + + $error_message = $this->t('Unable to update %other_resource while updating %resources of %cloud_config cloud service provider.', [ + '%resources' => $resource_link ?? $labels['plural'], + '%other_resource' => $network_interface_link ?? $network_interface_label['plural'], + '%cloud_config' => $cloud_config_link ?? $cloud_config->getName(), + ]); + + $updated === TRUE + ? $this->messageUser($success_message) + : $this->messageUser($error_message, 'error'); + $this->clearCacheValue(); } else { @@ -3292,20 +3174,87 @@ } } else { - if ($updated !== FALSE) { - $this->messageUser($this->t('Updated %resources of %cloud_config cloud service provider.', [ - '%resources' => $resource_link ?? $labels['plural'], - '%cloud_config' => $cloud_config_link ?? $cloud_config->getName(), - ])); - $this->clearCacheValue(); + $success_message = $this->t('Updated %resources of %cloud_config cloud service provider.', [ + '%resources' => $resource_link ?? $labels['plural'], + '%cloud_config' => $cloud_config_link ?? $cloud_config->getName(), + ]); + + $error_message = $this->t('Unable to update %resources of %cloud_config cloud service provider.', [ + '%resources' => $resource_link ?? $labels['plural'], + '%cloud_config' => $cloud_config_link ?? $cloud_config->getName(), + ]); + + $updated === TRUE + ? $this->messageUser($success_message) + : $this->messageUser($error_message, 'error'); + + $this->clearCacheValue(); + } + } + + /** + * Helper method to update all resource list. + * + * @param string $entity_type_name + * The entity type name. + * + * @return \Symfony\Component\HttpFoundation\RedirectResponse + * An associative array with a redirect route and any parameters to build + * the route. + */ + public function updateAllResourceList($entity_type_name) { + $cloud_configs = $this->entityTypeManager + ->getStorage('cloud_config') + ->loadByProperties([ + 'type' => 'aws_cloud', + ]); + + $labels = $this->getDisplayLabels($entity_type_name); + $update_method_name = 'updateAll' . str_replace(' ', '', ucwords($labels['plural'], ' ')); + + foreach ($cloud_configs ?: [] as $cloud_config) { + $cloud_context = $cloud_config->getCloudContext(); + + if ($entity_type_name === 'aws_cloud_image') { + $cloud_config_entities = $this->entityTypeManager->getStorage('cloud_config')->loadByProperties( + ['cloud_context' => [$cloud_context]] + ); + + $cloud_config = $cloud_config_entities ? reset($cloud_config_entities) : []; + $account_id = $this->getAccountId($cloud_config_entities); + + if ($account_id) { + $this->setCloudContext($cloud_context); + $updated = $this->updateAllImages([ + 'Owners' => [ + $account_id, + ], + ], TRUE); + + $this->updateEntityMessage('aws_cloud_image', $cloud_config, $updated); + } + else { + $message = $this->t('AWS User ID is not specified.'); + $account = $this->currentUser(); + $account->hasPermission('edit cloud service providers') + ? $message = Link::createFromRoute($message, 'entity.cloud_config.edit_form', ['cloud_config' => $cloud_config->id()])->toString() + : ''; + $this->messageUser($message, 'error'); + } } else { - $this->messageUser($this->t('Unable to update %resources of %cloud_config cloud service provider.', [ - '%resources' => $resource_link ?? $labels['plural'], - '%cloud_config' => $cloud_config_link ?? $cloud_config->getName(), - ]), 'error'); + $this->setCloudContext($cloud_context); + $updated = $this->$update_method_name(); + $this->updateEntityMessage($entity_type_name, $cloud_config, $updated); } } + + $redirect_url = Url::fromRoute("view.$entity_type_name.all"); + + $redirect_response = new RedirectResponse($redirect_url->toString()); + $redirect_response->send(); + + return $redirect_response; } } reverted: --- b/modules/cloud_service_providers/aws_cloud/src/Service/Ec2/Ec2ServiceInterface.php +++ a/modules/cloud_service_providers/aws_cloud/src/Service/Ec2/Ec2ServiceInterface.php @@ -1375,18 +1375,4 @@ */ public function createResourceQueueItems(); - /** - * Update entity message. - * - * @param string $entity_type - * The entity type. - * @param string $cloud_context - * The cloud context. - * @param object $cloud_config - * The cloud config object. - * @param bool $updated - * TRUE if entity is updated. - */ - public function updateEntityMessage($entity_type, $cloud_context, $cloud_config, $updated); - } diff -u b/src/Entity/CloudConfig.php b/src/Entity/CloudConfig.php --- b/src/Entity/CloudConfig.php +++ b/src/Entity/CloudConfig.php @@ -347,18 +347,16 @@ * The plural label. * @param string $entity_type * The entity type. - * @param string $cloud_context - * The cloud context. * * @return array * The Resource Link Array. */ - public function getResourceLink($plural_label, $entity_type, $cloud_context) { + public function getResourceLink($plural_label, $entity_type) { return Link::fromTextAndUrl( $plural_label, Url::fromRoute( "entity.{$entity_type}.collection", - ['cloud_context' => $cloud_context] + ['cloud_context' => $this->getCloudContext()] ) )->toString(); }