cloud-8.x-2.0-beta1/modules/cloud_service_providers/openstack/src/Service/OpenStackEc2Service.php
modules/cloud_service_providers/openstack/src/Service/OpenStackEc2Service.php
<?php
namespace Drupal\openstack\Service;
use Aws\Credentials\CredentialProvider;
use Aws\Ec2\Ec2Client;
use Drupal\aws_cloud\Service\Ec2\Ec2Service;
/**
* Interacts with OpenStack using the Amazon EC2 API.
*/
class OpenstackEc2Service extends Ec2Service {
/**
* {@inheritdoc}
*/
protected function getEc2Client() {
$credentials = $this->cloudConfigPluginManager->loadCredentials();
try {
$ec2_params = [
'region' => $credentials['region'],
'version' => $credentials['version'],
'endpoint' => $credentials['endpoint'],
];
$provider = CredentialProvider::ini('default', $credentials['ini_file']);
$provider = CredentialProvider::memoize($provider);
$ec2_params['credentials'] = $provider;
$ec2_client = new Ec2Client($ec2_params);
}
catch (\Exception $e) {
$ec2_client = NULL;
$this->logger->error($e->getMessage());
}
return $ec2_client;
}
/**
* {@inheritdoc}
*/
public function updateInstances(array $params = [], $clear = TRUE) {
$updated = FALSE;
$entity_type = 'openstack_cloud_instance';
$lock_name = $this->getLockKey($entity_type);
if (!$this->lock->acquire($lock_name)) {
return FALSE;
}
// Call the api and get all instances.
$result = $this->describeInstances($params);
if ($result != NULL) {
$all_instances = $this->loadAllEntities($entity_type);
$stale = [];
// Make it easier to lookup the instances by setting up
// the array with the instance_id.
foreach ($all_instances as $instance) {
$stale[$instance->getInstanceId()] = $instance;
}
/* @var \Drupal\Core\Batch\BatchBuilder $batch_builder */
$batch_builder = $this->initBatch('Instance Update');
// Loop through the reservations and store each one as an Instance entity.
foreach ($result['Reservations'] as $reservation) {
foreach ($reservation['Instances'] as $instance) {
// Keep track of instances that do not exist anymore
// delete them after saving the rest of the instances.
if (isset($stale[$instance['InstanceId']])) {
unset($stale[$instance['InstanceId']]);
}
// Store the Reservation OwnerId in instance so batch
// callback has access.
$instance['reservation_ownerid'] = $reservation['OwnerId'];
$instance['reservation_id'] = $reservation['ReservationId'];
$batch_builder->addOperation([
'\Drupal\openstack\Service\OpenStackBatchOperations',
'updateInstance',
], [$this->cloudContext, $instance]);
}
}
$batch_builder->addOperation([
'\Drupal\openstack\Service\OpenStackBatchOperations',
'finished',
], [$entity_type, $stale, $clear]);
$this->runBatch($batch_builder);
$updated = TRUE;
}
return $updated;
}
/**
* {@inheritdoc}
*/
public function updateInstancesWithoutBatch(array $params = [], $clear = TRUE) {
$updated = FALSE;
$entity_type = 'openstack_cloud_instance';
$lock_name = $this->getLockKey($entity_type);
if (!$this->lock->acquire($lock_name)) {
return FALSE;
}
// Override to handle OpenStack specific instances.
$results = $this->describeInstances($params);
if ($results != NULL) {
$all_instances = $this->loadAllEntities($entity_type);
$stale = [];
// Make it easier to lookup the images by setting up
// the array with the image_id.
foreach ($all_instances as $instance) {
$stale[$instance->getInstanceId()] = $instance;
}
foreach ($results['Reservations'] as $reservation) {
foreach ($reservation['Instances'] as $instance) {
if (isset($stale[$instance['InstanceId']])) {
unset($stale[$instance['InstanceId']]);
}
// Store the Reservation OwnerId in instance so batch
// callback has access.
$instance['reservation_ownerid'] = $reservation['OwnerId'];
$instance['reservation_id'] = $reservation['ReservationId'];
OpenStackBatchOperations::updateInstance($this->cloudContext, $instance);
}
}
if (count($stale) && $clear == TRUE) {
$this->entityTypeManager->getStorage($entity_type)->delete($stale);
}
$updated = TRUE;
}
$this->lock->release($lock_name);
return $updated;
}
/**
* {@inheritdoc}
*/
protected function getDefaultParameters() {
return [];
}
}
