Commit 3cbb8c3a authored by Yas Naoi's avatar Yas Naoi
Browse files

Issue #3278112 by yas, Xiaohua Guan, baldwinlouie, kumikoono: Refactor...

Issue #3278112 by yas, Xiaohua Guan, baldwinlouie, kumikoono: Refactor Drupal\cloud\Traits\CloudContentEntityTrait to fix drupal-check errors
parent fd731a95
Loading
Loading
Loading
Loading
+6 −3
Original line number Diff line number Diff line
@@ -754,15 +754,17 @@ function aws_cloud_cloud_config_delete(CloudConfig $cloud_config): void {

  if ($cloud_config->bundle() === 'aws_cloud') {

    $cloud_context = $cloud_config->getCloudContext();

    /** @var \Drupal\aws_cloud\Service\Ec2\Ec2ServiceInterface $ec2_service */
    $ec2_service = \Drupal::service('aws_cloud.ec2');
    try {
      $ec2_service->setCloudContext($cloud_config->getCloudContext());
      $ec2_service->setCloudContext($cloud_context);
    }
    catch (\Exception $e) {
    }
    finally {
      $ec2_service->clearAllEntities();
      \Drupal::service('cloud')->clearAllEntities('aws_cloud', $cloud_context);

      // Clean up credential files.
      $credential_file = aws_cloud_ini_file_path($cloud_config->get('cloud_context')->value);
@@ -2134,7 +2136,8 @@ function aws_cloud_get_launch_template_data(CloudLaunchTemplate $server_template
    $tags_map[CloudLaunchTemplate::TAG_SUBNET] = $server_template->field_subnet->value;
    $key_pair_id = '';
    if (!empty($ec2Service) && !empty($server_template->field_ssh_key->entity)) {
      $key_pair_id = $ec2Service->getEntityId(
      $key_pair_id = \Drupal::service('cloud')->getEntityId(
        $server_template->getCloudContext(),
        'aws_cloud_key_pair',
        'key_pair_name',
        $server_template->field_ssh_key->entity->getKeyPairName()
+43 −36
Original line number Diff line number Diff line
@@ -19,12 +19,15 @@ use Drupal\aws_cloud\Entity\Vpc\TransitGatewayRoute;
use Drupal\aws_cloud\Entity\Vpc\Vpc;
use Drupal\aws_cloud\Entity\Vpc\VpcPeeringConnection;
use Drupal\cloud\Entity\CloudLaunchTemplate;
use Drupal\cloud\Traits\CloudContentEntityTrait;

/**
 * Entity update methods for Batch API processing.
 */
class Ec2BatchOperations {

  use CloudContentEntityTrait;

  /**
   * The finish callback function.
   *
@@ -52,7 +55,7 @@ class Ec2BatchOperations {
   * @param array $instance
   *   The instance array.
   */
  public static function updateInstance($cloud_context, array $instance): void {
  public static function updateInstance(string $cloud_context, array $instance): void {
    /** @var \Drupal\aws_cloud\Service\Ec2\Ec2ServiceInterface */
    $ec2Service = \Drupal::service('aws_cloud.ec2');
    $ec2Service->setCloudContext($cloud_context);
@@ -171,7 +174,7 @@ class Ec2BatchOperations {

    // Get instance types.
    $instance_types = aws_cloud_get_instance_types($cloud_context);
    $entity_id = $ec2Service->getEntityId('aws_cloud_instance', 'instance_id', $instance['InstanceId']);
    $entity_id = \Drupal::service('cloud')->getEntityId($cloud_context, 'aws_cloud_instance', 'instance_id', $instance['InstanceId']);
    $cost = $ec2Service->calculateInstanceCost($instance, $instance_types);
    $low_utilization = $low_utilization_checker->isLow($cloud_context, $instance['InstanceId']);
    // Skip if $entity already exists, by updating 'refreshed' time.
@@ -283,7 +286,7 @@ class Ec2BatchOperations {
   *
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  public static function updateImage($cloud_context, array $image): void {
  public static function updateImage(string $cloud_context, array $image): void {
    /** @var \Drupal\aws_cloud\Service\Ec2\Ec2ServiceInterface */
    $ec2Service = \Drupal::service('aws_cloud.ec2');
    $ec2Service->setCloudContext($cloud_context);
@@ -329,7 +332,7 @@ class Ec2BatchOperations {

    $name = $ec2Service->getTagName($image, $image['Name']);
    $uid = $ec2Service->getUidTagValue($image, 'aws_cloud_image');
    $entity_id = $ec2Service->getEntityId('aws_cloud_image', 'image_id', $image['ImageId']);
    $entity_id = \Drupal::service('cloud')->getEntityId($cloud_context, 'aws_cloud_image', 'image_id', $image['ImageId']);

    // Skip if $entity already exists, by updating 'refreshed' time.
    if (!empty($entity_id)) {
@@ -391,7 +394,7 @@ class Ec2BatchOperations {
   * @param array $security_group
   *   The security_group array.
   */
  public static function updateSecurityGroup($cloud_context, array $security_group): void {
  public static function updateSecurityGroup(string $cloud_context, array $security_group): void {
    /** @var \Drupal\aws_cloud\Service\Ec2\Ec2ServiceInterface */
    $ec2Service = \Drupal::service('aws_cloud.ec2');
    $ec2Service->setCloudContext($cloud_context);
@@ -399,7 +402,7 @@ class Ec2BatchOperations {
    $timestamp = time();
    $name = $ec2Service->getTagName($security_group, $security_group['GroupName']);
    $uid = $ec2Service->getUidTagValue($security_group, 'aws_cloud_security_group');
    $entity_id = $ec2Service->getEntityId('aws_cloud_security_group', 'group_id', $security_group['GroupId']);
    $entity_id = \Drupal::service('cloud')->getEntityId($cloud_context, 'aws_cloud_security_group', 'group_id', $security_group['GroupId']);

    // Skip if $entity already exists, by updating 'refreshed' time.
    if (!empty($entity_id)) {
@@ -462,7 +465,7 @@ class Ec2BatchOperations {
   * @param array $network_interface
   *   The network interface array.
   */
  public static function updateNetworkInterface($cloud_context, array $network_interface): void {
  public static function updateNetworkInterface(string $cloud_context, array $network_interface): void {
    /** @var \Drupal\aws_cloud\Service\Ec2\Ec2ServiceInterface */
    $ec2Service = \Drupal::service('aws_cloud.ec2');
    $ec2Service->setCloudContext($cloud_context);
@@ -516,7 +519,7 @@ class Ec2BatchOperations {

    $name = $ec2Service->getTagName($network_interface, $network_interface['NetworkInterfaceId']);
    $uid = $ec2Service->getUidTagValue($network_interface, 'aws_cloud_network_interface');
    $entity_id = $ec2Service->getEntityId('aws_cloud_network_interface', 'network_interface_id', $network_interface['NetworkInterfaceId']);
    $entity_id = \Drupal::service('cloud')->getEntityId($cloud_context, 'aws_cloud_network_interface', 'network_interface_id', $network_interface['NetworkInterfaceId']);
    $allocation_id = NULL;
    if (!empty($network_interface['Association'])) {
      $allocation_id = $network_interface['Association']['AllocationId'] ?? NULL;
@@ -619,7 +622,7 @@ class Ec2BatchOperations {
   * @param array $elastic_ip
   *   The Elastic IP array.
   */
  public static function updateElasticIp($cloud_context, array $elastic_ip): void {
  public static function updateElasticIp(string $cloud_context, array $elastic_ip): void {
    /** @var \Drupal\aws_cloud\Service\Ec2\Ec2ServiceInterface */
    $ec2Service = \Drupal::service('aws_cloud.ec2');
    $ec2Service->setCloudContext($cloud_context);
@@ -638,7 +641,7 @@ class Ec2BatchOperations {
      $elastic_ip_type = 'Carrier IP';
    }

    $entity_id = $ec2Service->getEntityId('aws_cloud_elastic_ip', 'public_ip', $public_ip);
    $entity_id = \Drupal::service('cloud')->getEntityId($cloud_context, 'aws_cloud_elastic_ip', 'public_ip', $public_ip);

    // Skip if $entity already exists, by updating 'refreshed' time.
    if (!empty($entity_id)) {
@@ -697,13 +700,13 @@ class Ec2BatchOperations {
   * @param array $key_pair
   *   The key_pair array.
   */
  public static function updateKeyPair($cloud_context, array $key_pair): void {
  public static function updateKeyPair(string $cloud_context, array $key_pair): void {
    /** @var \Drupal\aws_cloud\Service\Ec2\Ec2ServiceInterface */
    $ec2Service = \Drupal::service('aws_cloud.ec2');
    $ec2Service->setCloudContext($cloud_context);
    $timestamp = time();

    $entity_id = $ec2Service->getEntityId('aws_cloud_key_pair', 'key_pair_name', $key_pair['KeyName']);
    $entity_id = \Drupal::service('cloud')->getEntityId($cloud_context, 'aws_cloud_key_pair', 'key_pair_name', $key_pair['KeyName']);

    $uid = $ec2Service->getUidTagValue($key_pair, 'aws_cloud_key_pair');
    if (!empty($entity_id)) {
@@ -738,14 +741,14 @@ class Ec2BatchOperations {
   * @param array $snapshot
   *   The snapshot array.
   */
  public static function updateSnapshot($cloud_context, array $snapshot): void {
  public static function updateSnapshot(string $cloud_context, array $snapshot): void {
    /** @var \Drupal\aws_cloud\Service\Ec2\Ec2ServiceInterface */
    $ec2Service = \Drupal::service('aws_cloud.ec2');
    $ec2Service->setCloudContext($cloud_context);

    $timestamp = time();
    $name = $ec2Service->getTagName($snapshot, $snapshot['SnapshotId']);
    $entity_id = $ec2Service->getEntityId('aws_cloud_snapshot', 'snapshot_id', $snapshot['SnapshotId']);
    $entity_id = \Drupal::service('cloud')->getEntityId($cloud_context, 'aws_cloud_snapshot', 'snapshot_id', $snapshot['SnapshotId']);
    $uid = $ec2Service->getUidTagValue($snapshot, 'aws_cloud_snapshot');

    if (!empty($entity_id)) {
@@ -792,7 +795,7 @@ class Ec2BatchOperations {
   * @param array $vpc
   *   The VPC array.
   */
  public static function updateVpc($cloud_context, array $vpc): void {
  public static function updateVpc(string $cloud_context, array $vpc): void {
    /** @var \Drupal\aws_cloud\Service\Ec2\Ec2ServiceInterface */
    $ec2Service = \Drupal::service('aws_cloud.ec2');
    $ec2Service->setCloudContext($cloud_context);
@@ -801,7 +804,7 @@ class Ec2BatchOperations {

    $timestamp = time();
    $name = $ec2Service->getTagName($vpc, $vpc_id);
    $entity_id = $ec2Service->getEntityId('aws_cloud_vpc', 'vpc_id', $vpc_id);
    $entity_id = \Drupal::service('cloud')->getEntityId($cloud_context, 'aws_cloud_vpc', 'vpc_id', $vpc_id);
    $uid = $ec2Service->getUidTagValue($vpc, 'aws_cloud_vpc');

    // Tags.
@@ -916,7 +919,7 @@ class Ec2BatchOperations {
   * @param array $vpc_peering_connection
   *   The VPC peering connection array.
   */
  public static function updateVpcPeeringConnection($cloud_context, array $vpc_peering_connection): void {
  public static function updateVpcPeeringConnection(string $cloud_context, array $vpc_peering_connection): void {
    /** @var \Drupal\aws_cloud\Service\Ec2\Ec2ServiceInterface */
    $ec2Service = \Drupal::service('aws_cloud.ec2');
    $ec2Service->setCloudContext($cloud_context);
@@ -925,7 +928,7 @@ class Ec2BatchOperations {

    $timestamp = time();
    $name = $ec2Service->getTagName($vpc_peering_connection, $vpc_peering_connection_id);
    $entity_id = $ec2Service->getEntityId('aws_cloud_vpc_peering_connection', 'vpc_peering_connection_id', $vpc_peering_connection_id);
    $entity_id = \Drupal::service('cloud')->getEntityId($cloud_context, 'aws_cloud_vpc_peering_connection', 'vpc_peering_connection_id', $vpc_peering_connection_id);
    $uid = $ec2Service->getUidTagValue($vpc_peering_connection, 'aws_cloud_vpc_peering_connection');

    // Tags.
@@ -998,7 +1001,7 @@ class Ec2BatchOperations {
   * @param array $subnet
   *   The subnet array.
   */
  public static function updateSubnet($cloud_context, array $subnet): void {
  public static function updateSubnet(string $cloud_context, array $subnet): void {
    /** @var \Drupal\aws_cloud\Service\Ec2\Ec2ServiceInterface */
    $ec2_service = \Drupal::service('aws_cloud.ec2');
    $ec2_service->setCloudContext($cloud_context);
@@ -1006,7 +1009,7 @@ class Ec2BatchOperations {
    $subnet_id = $subnet['SubnetId'] ?? NULL;
    $timestamp = time();
    $name = $ec2_service->getTagName($subnet, $subnet_id);
    $entity_id = $ec2_service->getEntityId('aws_cloud_subnet', 'subnet_id', $subnet_id);
    $entity_id = \Drupal::service('cloud')->getEntityId($cloud_context, 'aws_cloud_subnet', 'subnet_id', $subnet_id);
    $uid = $ec2_service->getUidTagValue($subnet, 'aws_cloud_subnet');

    $subnet_vpc_id = $subnet['VpcId'] ?? NULL;
@@ -1095,7 +1098,7 @@ class Ec2BatchOperations {
   * @param array $internet_gateway
   *   The internet gateway array.
   */
  public static function updateInternetGateway($cloud_context, array $internet_gateway): void {
  public static function updateInternetGateway(string $cloud_context, array $internet_gateway): void {
    /** @var \Drupal\aws_cloud\Service\Ec2\Ec2ServiceInterface */
    $ec2Service = \Drupal::service('aws_cloud.ec2');
    $ec2Service->setCloudContext($cloud_context);
@@ -1104,7 +1107,7 @@ class Ec2BatchOperations {

    $timestamp = time();
    $name = $ec2Service->getTagName($internet_gateway, $internet_gateway_id);
    $entity_id = $ec2Service->getEntityId('aws_cloud_internet_gateway', 'internet_gateway_id', $internet_gateway_id);
    $entity_id = \Drupal::service('cloud')->getEntityId($cloud_context, 'aws_cloud_internet_gateway', 'internet_gateway_id', $internet_gateway_id);
    $uid = $ec2Service->getUidTagValue($internet_gateway, 'aws_cloud_internet_gateway');

    // Tags.
@@ -1175,7 +1178,7 @@ class Ec2BatchOperations {
   * @param array $carrier_gateway
   *   The carrier gateway array.
   */
  public static function updateCarrierGateway($cloud_context, array $carrier_gateway): void {
  public static function updateCarrierGateway(string $cloud_context, array $carrier_gateway): void {
    /** @var \Drupal\aws_cloud\Service\Ec2\Ec2ServiceInterface */
    $ec2Service = \Drupal::service('aws_cloud.ec2');
    $ec2Service->setCloudContext($cloud_context);
@@ -1184,7 +1187,7 @@ class Ec2BatchOperations {

    $timestamp = time();
    $name = $ec2Service->getTagName($carrier_gateway, $carrier_gateway_id);
    $entity_id = $ec2Service->getEntityId('aws_cloud_carrier_gateway', 'carrier_gateway_id', $carrier_gateway_id);
    $entity_id = \Drupal::service('cloud')->getEntityId($cloud_context, 'aws_cloud_carrier_gateway', 'carrier_gateway_id', $carrier_gateway_id);
    $uid = $ec2Service->getUidTagValue($carrier_gateway, 'aws_cloud_carrier_gateway');

    // Tags.
@@ -1255,7 +1258,7 @@ class Ec2BatchOperations {

    $timestamp = time();
    $name = $ec2Service->getTagName($transit_gateway, $transit_gateway_id);
    $entity_id = $ec2Service->getEntityId('aws_cloud_transit_gateway', 'transit_gateway_id', $transit_gateway_id);
    $entity_id = \Drupal::service('cloud')->getEntityId($cloud_context, 'aws_cloud_transit_gateway', 'transit_gateway_id', $transit_gateway_id);
    $uid = $ec2Service->getUidTagValue($transit_gateway, 'aws_cloud_transit_gateway');

    // Tags.
@@ -1328,7 +1331,7 @@ class Ec2BatchOperations {
   * @param array $transit_gateway_route_table
   *   The transit gateway array.
   */
  public static function updateTransitGatewayRoute($cloud_context, array $transit_gateway_route_table): void {
  public static function updateTransitGatewayRoute(string $cloud_context, array $transit_gateway_route_table): void {
    /** @var \Drupal\aws_cloud\Service\Ec2\Ec2ServiceInterface */
    $ec2Service = \Drupal::service('aws_cloud.ec2');
    $ec2Service->setCloudContext($cloud_context);
@@ -1339,7 +1342,7 @@ class Ec2BatchOperations {

    $timestamp = time();
    $name = $ec2Service->getTagName($transit_gateway_route_table, $transit_gateway_route_table_id);
    $entity_id = $ec2Service->getEntityId('aws_cloud_transit_gateway_route', 'transit_gateway_route_table_id', $transit_gateway_route_table_id);
    $entity_id = \Drupal::service('cloud')->getEntityId($cloud_context, 'aws_cloud_transit_gateway_route', 'transit_gateway_route_table_id', $transit_gateway_route_table_id);
    $uid = $ec2Service->getUidTagValue($transit_gateway_route_table, 'aws_cloud_transit_gateway_route');

    if (!empty($entity_id)) {
@@ -1380,7 +1383,7 @@ class Ec2BatchOperations {
   * @param array $snapshot_id_name_map
   *   The snapshot map.
   */
  public static function updateVolume($cloud_context, array $volume, array $snapshot_id_name_map): void {
  public static function updateVolume(string $cloud_context, array $volume, array $snapshot_id_name_map): void {
    /** @var \Drupal\aws_cloud\Service\Ec2\Ec2ServiceInterface */
    $ec2Service = \Drupal::service('aws_cloud.ec2');
    $ec2Service->setCloudContext($cloud_context);
@@ -1393,7 +1396,7 @@ class Ec2BatchOperations {
    }

    $name = $ec2Service->getTagName($volume, $volume['VolumeId']);
    $entity_id = $ec2Service->getEntityId('aws_cloud_volume', 'volume_id', $volume['VolumeId']);
    $entity_id = \Drupal::service('cloud')->getEntityId($cloud_context, 'aws_cloud_volume', 'volume_id', $volume['VolumeId']);
    $uid = $ec2Service->getUidTagValue($volume, 'aws_cloud_volume');

    if ($uid === 0) {
@@ -1458,7 +1461,7 @@ class Ec2BatchOperations {
   * @param array $availability_zone
   *   The Availability Zone array.
   */
  public static function updateAvailabilityZone($cloud_context, array $availability_zone): void {
  public static function updateAvailabilityZone(string $cloud_context, array $availability_zone): void {
    /** @var \Drupal\aws_cloud\Service\Ec2\Ec2ServiceInterface */
    $ec2Service = \Drupal::service('aws_cloud.ec2');
    $ec2Service->setCloudContext($cloud_context);
@@ -1467,7 +1470,7 @@ class Ec2BatchOperations {

    $timestamp = time();
    $name = $ec2Service->getTagName($availability_zone, $zone_id);
    $entity_id = $ec2Service->getEntityId('aws_cloud_availability_zone', 'zone_id', $zone_id);
    $entity_id = \Drupal::service('cloud')->getEntityId($cloud_context, 'aws_cloud_availability_zone', 'zone_id', $zone_id);
    $uid = $ec2Service->getUidTagValue($availability_zone, 'aws_cloud_availability_zone');

    if (!empty($entity_id)) {
@@ -1509,13 +1512,14 @@ class Ec2BatchOperations {
   * @param array $template
   *   The launch template array.
   */
  public static function updateCloudLaunchTemplate($cloud_context, array $template): void {
  public static function updateCloudLaunchTemplate(string $cloud_context, array $template): void {
    /** @var \Drupal\aws_cloud\Service\Ec2\Ec2ServiceInterface */
    $ec2_service = \Drupal::service('aws_cloud.ec2');
    $ec2_service->setCloudContext($cloud_context);
    $timestamp = time();

    $entity_id = $ec2_service->getEntityId(
    $entity_id = \Drupal::service('cloud')->getEntityId(
      $cloud_context,
      'cloud_launch_template',
      'name',
      $template['LaunchTemplateName'],
@@ -1615,7 +1619,8 @@ class Ec2BatchOperations {
    $security_group_entity_ids = [];
    if (!empty($template_data['SecurityGroupIds'])) {
      foreach ($template_data['SecurityGroupIds'] ?: [] as $group_id) {
        $entity_id = $ec2_service->getEntityId(
        $entity_id = \Drupal::service('cloud')->getEntityId(
          $revision->getCloudContext(),
          'aws_cloud_security_group',
          'group_id',
          $group_id
@@ -1631,7 +1636,8 @@ class Ec2BatchOperations {
    // Key pair.
    $key_pair_id = NULL;
    if (isset($template_data['KeyName'])) {
      $key_pair_id = $ec2_service->getEntityId(
      $key_pair_id = \Drupal::service('cloud')->getEntityId(
        $revision->getCloudContext(),
        'aws_cloud_key_pair',
        'key_pair_name',
        $template_data['KeyName']
@@ -1646,7 +1652,8 @@ class Ec2BatchOperations {
      $network_interface = array_shift($network_interfaces);

      if (!empty($network_interface['NetworkInterfaceId'])) {
        $network_interface_id = $ec2_service->getEntityId(
        $network_interface_id = \Drupal::service('cloud')->getEntityId(
          $revision->getCloudContext(),
          'aws_cloud_network_interface',
          'network_interface_id',
          $network_interface['NetworkInterfaceId']
+23 −46

File changed.

Preview size limit exceeded, changes collapsed.

+0 −5
Original line number Diff line number Diff line
@@ -1641,11 +1641,6 @@ interface Ec2ServiceInterface {
   */
  public function getVpcs();

  /**
   * Method to clear all entities out of the system.
   */
  public function clearAllEntities();

  /**
   * Stops ec2 instance given an instance array.
   *
+5 −2
Original line number Diff line number Diff line
@@ -102,10 +102,13 @@ function k8s_cloud_config_predelete(CloudConfig $cloud_config): void {
  // cloud service providers.
  // See https://www.drupal.org/project/cloud/issues/3186140
  if ($cloud_config->bundle() === 'k8s') {

    $cloud_context = $cloud_config->getCloudContext();

    /** @var \Drupal\k8s\Service\K8sServiceInterface $k8s_service */
    $k8s_service = \Drupal::service('k8s');
    $k8s_service->setCloudContext($cloud_config->getCloudContext());
    $k8s_service->clearAllEntities();
    $k8s_service->setCloudContext($cloud_context);
    \Drupal::service('cloud')->clearAllEntities('k8s', $cloud_context);

    CloudContentEntityTrait::deleteQueue(CloudContentEntityTrait::getResourceQueueName($cloud_config));
  }
Loading