cloud-8.x-2.0-beta1/modules/cloud_service_providers/aws_cloud/src/Form/Ec2/ElasticIpDisassociateForm.php

modules/cloud_service_providers/aws_cloud/src/Form/Ec2/ElasticIpDisassociateForm.php
<?php

namespace Drupal\aws_cloud\Form\Ec2;

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Markup;

/**
 * Disassociate Elastic IP address form.
 */
class ElasticIpDisassociateForm extends AwsDeleteForm {

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
    /* @var \Drupal\aws_cloud\Entity\Ec2\ElasticIp $entity */
    $entity = $this->entity;
    return $this->t('Are you sure you want to disassociate this Elastic IP address (@ip_address)', [
      '@ip_address' => $entity->getPublicIp(),
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function getDescription() {
    $entity = $this->entity;
    $instance_id = $entity->getInstanceId();
    $network_interface_id = $entity->getNetworkInterfaceId();
    $instance = $this->getInstanceById($instance_id);

    $instance_link = $this->entityLinkRenderer->renderViewElement(
      $instance_id,
      'aws_cloud_instance',
      'instance_id',
      [],
      $instance->getName() != $instance->getInstanceId() ? $this->t('@instance_name (@instance_id)', [
        '@instance_name' => $instance->getName(),
        '@instance_id' => $instance_id,
      ]) : $instance_id
    );

    $msg = '<h2>Elastic IP Information:</h2>';
    $msg .= $this->t('<ul><li>Instance ID: @instance_id</li><li>Network ID: @network_id</li></ul>',
      [
        '@instance_id' => Markup::create($instance_link['#markup']),
        '@network_id' => $network_interface_id,
      ]
    );

    return $msg;
  }

  /**
   * {@inheritdoc}
   */
  public function getConfirmText() {
    return $this->t('Disassociate Address');
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildForm($form, $form_state);
    if ($this->entity->getAssociationId() == NULL) {
      $form['error'] = [
        '#markup' => '<div>' . $this->t('Elastic IP is already disassociated') . '</div>',
      ];
      unset($form['description']);
      unset($form['actions']['submit']);
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->ec2Service->setCloudContext($this->entity->getCloudContext());
    $entity = $this->entity;

    $result = $this->ec2Service->disassociateAddress([
      'AssociationId' => $this->entity->getAssociationId(),
    ]);
    if ($result != NULL) {
      $instance_id = $entity->getInstanceId();
      $network_interface_id = $entity->getNetworkInterfaceId();

      $this->messenger->addMessage($this->t('Elastic IP disassociated from instance: @instance_id and network: @network_id', [
        '@instance_id' => $instance_id,
        '@network_id' => $network_interface_id,
      ]));

      $this->ec2Service->updateElasticIp();
      $this->ec2Service->updateInstances();
      $this->ec2Service->updateNetworkInterfaces();

      $this->clearCacheValues();
    }
    else {
      $this->messenger->addError($this->t('Unable to disassociate Elastic IP.'));
    }
    $form_state->setRedirect('view.aws_cloud_elastic_ip.list', ['cloud_context' => $entity->getCloudContext()]);
  }

  /**
   * Helper method to load instance by ID.
   *
   * @param string $instance_id
   *   Instance ID to load.
   *
   * @return \Drupal\aws_cloud\Entity\Ec2\Instance
   *   The Instance entity.
   */
  private function getInstanceById($instance_id) {
    $instances = $this->entityTypeManager->getStorage('aws_cloud_instance')
      ->loadByProperties([
        'instance_id' => $instance_id,
      ]);
    return array_shift($instances);
  }

}

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

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