cloud-8.x-2.0-beta1/modules/cloud_service_providers/openstack/openstack.module

modules/cloud_service_providers/openstack/openstack.module
<?php

/**
 * @file
 * OpenStack Cloud module.
 *
 * This module handles UI interactions with the OpenStack cloud.
 */

use Drupal\cloud\Entity\CloudConfig;
use Drupal\Core\Url;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_cron().
 */
function openstack_cron() {
  $entities = \Drupal::service('plugin.manager.cloud_config_plugin')->loadConfigEntities('openstack');
  foreach ($entities as $entity) {
    openstack_update_resources($entity);
  }
}

/**
 * Update OpenStack resources.
 *
 * @param \Drupal\cloud\Entity\CloudConfig $cloud_config
 *   The cloud service provider (CloudConfig) object.
 */
function openstack_update_resources(CloudConfig $cloud_config) {
  /* @var \Drupal\openstack\Service\OpenstackEc2Service $openstack_ec2_service */
  $openstack_ec2_service = \Drupal::service('openstack.ec2');
  $openstack_ec2_service->setCloudContext($cloud_config->getCloudContext());
  $openstack_ec2_service->updateInstances();
}

/**
 * Implements hook_cloud_config_presave().
 */
function openstack_cloud_config_presave(CloudConfig $cloud_config) {
  if ($cloud_config->bundle() == 'openstack') {
    if ($cloud_config->isNew()) {
      // Auto generate the cloud_context.
      $cloud_context = aws_cloud_form_cloud_config_aws_ec2_add_form_create_cloud_context(
        $cloud_config->getName(),
        $cloud_config->get('field_os_region')->value
      );
      $cloud_config->set('cloud_context', $cloud_context);
    }
    else {
      $cloud_context = $cloud_config->get('cloud_context')->value;
    }

    if (isset($cloud_config->get('field_access_key')->value) && isset($cloud_config->get('field_secret_key')->value)) {
      // Write the access key ID and secret out to an ini file.
      $access_data = <<<EOF
[default]
aws_access_key_id = {$cloud_config->get('field_access_key')->value}
aws_secret_access_key = {$cloud_config->get('field_secret_key')->value}
EOF;
      $credential_file = aws_cloud_ini_file_path($cloud_context);
      $credential_directory = aws_cloud_ini_directory();
      if (\Drupal::service('file_system')->prepareDirectory($credential_directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS)) {
        \Drupal::service('file_system')->saveData($access_data, $credential_file, FileSystemInterface::EXISTS_REPLACE);
      }
      $cloud_config->set('field_access_key', NULL);
      $cloud_config->set('field_secret_key', NULL);
    }
  }
}

/**
 * Implements hook_cloud_config_delete().
 */
function openstack_cloud_config_delete(CloudConfig $cloud_config) {
  if ($cloud_config->bundle() == 'openstack') {
    // Delete the openstack_cloud_instances.
    $entity_type_manager = \Drupal::entityTypeManager();
    $entities = $entity_type_manager
      ->getStorage('openstack_cloud_instance')
      ->loadByProperties(
        [
          'cloud_context' => $cloud_config->getCloudContext(),
        ]);
    $entity_type_manager->getStorage('openstack_cloud_instance')->delete($entities);
    // Clean up credential files.
    $credential_file = aws_cloud_ini_file_path($cloud_config->get('cloud_context')->value);
    \Drupal::service('file_system')->delete($credential_file);
  }
}

/**
 * Implements hook_cloud_config_update().
 */
function openstack_cloud_config_update(CloudConfig $cloud_config) {
  if ($cloud_config->bundle() == 'openstack') {
    openstack_update_resources($cloud_config);
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function openstack_form_cloud_config_openstack_add_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  openstack_form_cloud_config_openstack_form_common_alter($form, $form_state, $form_id);

  // Location fieldset is unnecessary for add form.
  unset($form['location']);

  $form['field_location_country']['#access'] = FALSE;
  $form['field_location_city']['#access'] = FALSE;
  $form['field_location_longitude']['#access'] = FALSE;
  $form['field_location_latitude']['#access'] = FALSE;
  $form['cloud_provider']['cloud_context']['#access'] = FALSE;
  $form['actions']['submit']['#submit'][] = 'openstack_form_cloud_config_openstack_add_form_submit';
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function openstack_form_cloud_config_openstack_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  openstack_form_cloud_config_openstack_form_common_alter($form, $form_state, $form_id);
}

/**
 * Common alter function.
 */
function openstack_form_cloud_config_openstack_form_common_alter(&$form, FormStateInterface $form_state, $form_id) {
  openstack_cloud_config_fieldsets($form);

  $form['new_revision']['#access'] = FALSE;
  $form['revision_log_message']['#access'] = FALSE;
  $form['status']['#access'] = FALSE;
}

/**
 * Submit function to set the redirect to update openstack instances.
 *
 * @param array $form
 *   An associative array containing the structure of the form.
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 *   The current state of the form.
 */
function openstack_form_cloud_config_openstack_add_form_submit(array $form, FormStateInterface $form_state) {
  $cloud_config = $form_state->getFormObject()->getEntity();
  $form_state->setRedirect(
    'entity.openstack_cloud.update_all',
    [],
    [
      'query' =>
        [
          'regions' => $cloud_config->getCloudContext(),
        ],
    ]);
}

/**
 * Get fieldsets of cloud config page.
 *
 * @param array $fields
 *   Array of fields.
 */
function openstack_cloud_config_fieldsets(array &$fields) {
  $fieldset_defs = [
    [
      'name' => 'cloud_provider',
      'title' => t('Cloud Service Provider'),
      'open' => TRUE,
      'fields' => [
        'cloud_context',
        'name',
      ],
    ],
    [
      'name' => 'credentials',
      'title' => t('Credentials'),
      'open' => TRUE,
      'fields' => [
        'field_account_id',
        'field_api_endpoint',
        'field_os_region',
        'field_access_key',
        'field_secret_key',
      ],
    ],
    [
      'name' => 'location',
      'title' => t('Location'),
      'open' => TRUE,
      'fields' => [
        'cloud_config_location_map',
        'field_location_country',
        'field_location_city',
        'field_location_latitude',
        'field_location_longitude',
      ],
    ],
    [
      'name' => 'others',
      'title' => t('Others'),
      'open' => FALSE,
      'fields' => [
        'uid',
      ],
    ],
  ];

  cloud_form_reorder($fields, $fieldset_defs);
}

/**
 * Implements hook_ENTITY_TYPE_view_alter().
 */
function openstack_cloud_config_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
  if ($entity->bundle() == 'openstack') {
    $map_json_url = \Drupal::config('cloud.settings')->get('cloud_location_map_json_url');
    $url = Url::fromRoute('entity.cloud_config.location', ['cloud_config' => $entity->id()])->toString();

    $build['cloud_config_location_map'] = [
      '#markup' => '<div id="cloud_config_location"></div>',
      '#attached' => [
        'library' => [
          'cloud/cloud_config_location',
        ],
        'drupalSettings' => [
          'cloud' => [
            'cloud_location_map_json_url' => $map_json_url,
            'cloud_config_location_json_url' => $url,
          ],
        ],
      ],
    ];

    $build['field_location_country']['#access'] = FALSE;
    $build['field_location_city']['#access'] = FALSE;
    $build['field_location_longitude']['#access'] = FALSE;
    $build['field_location_latitude']['#access'] = FALSE;

    openstack_cloud_config_fieldsets($build);

  }
}

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

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