cloud-8.x-2.0-beta1/modules/cloud_service_providers/aws_cloud/src/Controller/Ec2/ApiController.php

modules/cloud_service_providers/aws_cloud/src/Controller/Ec2/ApiController.php
<?php

namespace Drupal\aws_cloud\Controller\Ec2;

use Drupal\aws_cloud\Service\Ec2\Ec2ServiceInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Link;
use Drupal\Core\Messenger\Messenger;
use Drupal\Core\Render\RendererInterface;
use Drupal\views\Views;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;

/**
 * Controller responsible for "update" URLs.
 *
 * This class is mainly responsible for
 * updating the AWS entities from URLs.
 */
class ApiController extends ControllerBase implements ApiControllerInterface {

  /**
   * The AWS Cloud EC2 Service.
   *
   * @var \Drupal\aws_cloud\Service\Ec2\Ec2ServiceInterface
   */
  private $ec2Service;

  /**
   * The Messenger service.
   *
   * @var \Drupal\Core\Messenger\Messenger
   */
  protected $messenger;

  /**
   * Request stack.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  private $requestStack;

  /**
   * Renderer service.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  private $renderer;

  /**
   * ApiController constructor.
   *
   * @param \Drupal\aws_cloud\Service\Ec2\Ec2ServiceInterface $ec2_service
   *   Object for interfacing with AWS API.
   * @param \Drupal\Core\Messenger\Messenger $messenger
   *   Messenger Object.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   Request stack object.
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer service.
   */
  public function __construct(Ec2ServiceInterface $ec2_service, Messenger $messenger, RequestStack $request_stack, RendererInterface $renderer) {
    $this->ec2Service = $ec2_service;
    $this->messenger = $messenger;
    $this->requestStack = $request_stack;
    $this->renderer = $renderer;
  }

  /**
   * Dependency Injection.
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('aws_cloud.ec2'),
      $container->get('messenger'),
      $container->get('request_stack'),
      $container->get('renderer')
    );
  }

  /**
   * {@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 service provider was performed successfully.'));
      drupal_flush_all_caches();
    }
    return $this->redirect('entity.cloud_config.collection');
  }

  /**
   * {@inheritdoc}
   */
  public function updateInstanceList($cloud_context) {
    $this->ec2Service->setCloudContext($cloud_context);
    $updated = $this->ec2Service->updateInstances();

    if ($updated != FALSE) {
      $this->messageUser($this->t('Updated Instances.'));
    }
    else {
      $this->messageUser($this->t('Unable to update Instances.'), 'error');
    }

    return $this->redirect('view.aws_cloud_instance.list', [
      'cloud_context' => $cloud_context,
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function updateImageList($cloud_context) {
    $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 = $cloud_config->get('field_account_id')->value;
      // Use the switch role account_id if switching is enabled.
      $assume_role = $cloud_config->get('field_assume_role')->value;
      $switch_role = $cloud_config->get('field_switch_role')->value;
      if (isset($assume_role) && isset($switch_role) && $switch_role == TRUE && $assume_role == TRUE) {
        $account_id = trim($cloud_config->get('field_switch_role_account_id')->value);
      }
    }

    if ($account_id) {

      $this->ec2Service->setCloudContext($cloud_context);
      $updated = $this->ec2Service->updateImages([
        'Owners' => [
          $account_id,
        ],
      ], TRUE);

      if ($updated !== FALSE) {
        $this->messageUser($this->t('Updated Images.'));
      }
      else {
        $this->messageUser($this->t('Unable to update Images.'), 'error');
      }
    }
    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.list', [
      'cloud_context' => $cloud_context,
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function updateSecurityGroupList($cloud_context) {
    $this->ec2Service->setCloudContext($cloud_context);
    $updated = $this->ec2Service->updateSecurityGroups();

    if ($updated != FALSE) {
      $this->messageUser($this->t('Updated Security Groups.'));
    }
    else {
      $this->messageUser($this->t('Unable to update Security Groups.'), 'error');
    }
    return $this->redirect('view.aws_cloud_security_group.list', [
      'cloud_context' => $cloud_context,
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function updateNetworkInterfaceList($cloud_context) {
    $this->ec2Service->setCloudContext($cloud_context);
    $updated = $this->ec2Service->updateNetworkInterfaces();

    if ($updated != FALSE) {
      $this->messageUser($this->t('Updated Network Interfaces.'));
    }
    else {
      $this->messageUser($this->t('Unable to update Network Interfaces.'), 'error');
    }

    return $this->redirect('view.aws_cloud_network_interface.list', [
      'cloud_context' => $cloud_context,
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function updateElasticIpList($cloud_context) {

    $this->ec2Service->setCloudContext($cloud_context);
    $updated = $this->ec2Service->updateElasticIp();

    if ($updated != FALSE) {
      $this->messageUser($this->t('Updated Elastic IPs.'));
    }
    else {
      $this->messageUser($this->t('Unable to update Elastic IPs.'), 'error');
    }

    return $this->redirect('view.aws_cloud_elastic_ip.list', [
      'cloud_context' => $cloud_context,
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function updateKeyPairList($cloud_context) {

    $this->ec2Service->setCloudContext($cloud_context);
    $updated = $this->ec2Service->updateKeyPairs();

    if ($updated != FALSE) {
      $this->messageUser($this->t('Updated Key Pairs.'));
    }
    else {
      $this->messageUser($this->t('Unable to update Key Pairs.'), 'error');
    }

    return $this->redirect('view.aws_cloud_key_pair.list', [
      'cloud_context' => $cloud_context,
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function updateVolumeList($cloud_context) {

    $this->ec2Service->setCloudContext($cloud_context);
    $updated = $this->ec2Service->updateVolumes();

    if ($updated != FALSE) {
      $this->messageUser($this->t('Updated Volumes.'));
    }
    else {
      $this->messageUser($this->t('Unable to update Volumes.'), 'error');
    }

    return $this->redirect('view.aws_cloud_volume.list', [
      'cloud_context' => $cloud_context,
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function updateSnapshotList($cloud_context) {

    $this->ec2Service->setCloudContext($cloud_context);
    $updated = $this->ec2Service->updateSnapshots();

    if ($updated !== FALSE) {
      $this->messageUser($this->t('Updated Snapshots.'));
    }
    else {
      $this->messageUser($this->t('Unable to update Snapshots.'), 'error');
    }

    return $this->redirect('view.aws_cloud_snapshot.list', [
      'cloud_context' => $cloud_context,
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function updateVpcList($cloud_context) {

    $this->ec2Service->setCloudContext($cloud_context);
    $updated = $this->ec2Service->updateVpcs();

    if ($updated !== FALSE) {
      $this->messageUser($this->t('Updated VPCs.'));
    }
    else {
      $this->messageUser($this->t('Unable to update VPCs.'), 'error');
    }

    return $this->redirect('view.aws_cloud_vpc.list', [
      'cloud_context' => $cloud_context,
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function updateCloudServerTemplateList($cloud_context) {
    $this->ec2Service->setCloudContext($cloud_context);
    $updated = $this->ec2Service->updateCloudServerTemplates();

    if ($updated != FALSE) {
      $this->messageUser($this->t('Updated cloud server templates.'));
    }
    else {
      $this->messageUser($this->t('Unable to update cloud server templates.'), 'error');
    }

    return $this->redirect('entity.cloud_server_template.collection', [
      'cloud_context' => $cloud_context,
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function listInstanceCallback($cloud_context) {
    return $this->getViewResponse('aws_cloud_instance');
  }

  /**
   * {@inheritdoc}
   */
  public function listImageCallback($cloud_context) {
    return $this->getViewResponse('aws_cloud_image');
  }

  /**
   * {@inheritdoc}
   */
  public function listSnapshotCallback($cloud_context) {
    return $this->getViewResponse('aws_cloud_snapshot');
  }

  /**
   * {@inheritdoc}
   */
  public function listVolumeCallback($cloud_context) {
    return $this->getViewResponse('aws_cloud_volume');
  }

  /**
   * {@inheritdoc}
   */
  public function searchImages($cloud_context) {
    $this->ec2Service->setCloudContext($cloud_context);
    $name = $this->requestStack->getCurrentRequest()->query->get('q');
    $result = $this->ec2Service->describeImages([
      'Filters' => [
        [
          'Name' => 'name',
          'Values' => [$name],
        ],
        [
          'Name' => 'state',
          'Values' => ['available'],
        ],
      ],
    ]);

    $limit = 50;
    $count = 0;
    $images = [];
    foreach ($result['Images'] ?: [] as $image) {
      $images[] = [
        'name' => $image['Name'],
        'id' => $image['Name'],
        'created' => strtotime($image['CreationDate']),
      ];
      if ($count++ >= $limit) {
        break;
      }
    }

    // Sort by latest.
    usort($images, function ($a, $b) {
      return ($a['created'] < $b['created']) ? 1 : -1;
    });

    return new Response(json_encode($images));
  }

  /**
   * Helper method to get views output.
   *
   * @param string $view_id
   *   The ID of view list.
   *
   * @return \Symfony\Component\HttpFoundation\Response
   *   The response of view list.
   */
  private function getViewResponse($view_id) {
    $view = Views::getView($view_id);

    // Set the display machine ID.
    $view->setDisplay('list');

    // Render the view as html, and return it as a response object.
    $build = $view->executeDisplay();
    return new Response($this->renderer->render($build));
  }

  /**
   * Helper method to add messages for the end user.
   *
   * @param string $message
   *   The message.
   * @param string $type
   *   The message type: error or message.
   */
  private function messageUser($message, $type = 'message') {
    switch ($type) {
      case 'error':
        $this->messenger->addError($message);
        break;

      case 'message':
        $this->messenger->addMessage($message);
      default:
        break;
    }
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc