cloud-8.x-2.0-beta1/modules/cloud_service_providers/openstack/src/Service/OpenStackBatchOperations.php
modules/cloud_service_providers/openstack/src/Service/OpenStackBatchOperations.php
<?php
namespace Drupal\openstack\Service;
use Drupal\openstack\Entity\Instance;
/**
* Entity update methods for Batch API processing.
*/
class OpenStackBatchOperations {
/**
* The finish callback function.
*
* Deletes stale entities from the database.
*
* @param string $entity_type
* The entity type.
* @param array $stale
* The stale entities to delete.
* @param bool $clear
* TRUE to clear entities, FALSE keep them.
*/
public static function finished($entity_type, array $stale, $clear = TRUE) {
$entity_type_manager = \Drupal::entityTypeManager();
if (count($stale) && $clear == TRUE) {
$entity_type_manager->getStorage($entity_type)->delete($stale);
}
}
/**
* Update or create an instance entity.
*
* @param string $cloud_context
* The cloud context.
* @param array $instance
* The instance array.
*/
public static function updateInstance($cloud_context, array $instance) {
/* @var \Drupal\openstack\Service\OpenstackEc2Service $openStackEc2Service */
$openStackEc2Service = \Drupal::service('openstack.ec2');
$openStackEc2Service->setCloudContext($cloud_context);
$uid = 0;
$instance_name = '';
$security_groups = [];
$timestamp = time();
foreach ($instance['SecurityGroups'] as $security_group) {
$security_groups[] = $security_group['GroupName'];
}
$entity_id = $openStackEc2Service->getEntityId('openstack_cloud_instance', 'instance_id', $instance['InstanceId']);
if (!empty($entity_id)) {
/* @var \Drupal\openstack\Entity\Instance $entity */
$entity = Instance::load($entity_id);
if (!empty($instance_name)) {
$entity->setName($instance['InstanceId']);
}
$entity->setAccountId($instance['reservation_ownerid']);
$entity->setSecurityGroups($security_groups);
$entity->setInstanceId($instance['InstanceId']);
$entity->setInstanceType($instance['InstanceType']);
$entity->setAvailabilityZone($instance['Placement']['AvailabilityZone']);
$entity->setInstanceState($instance['State']['Name']);
$entity->setPublicDns($instance['PublicDnsName']);
$entity->setPrivateDns($instance['PrivateDnsName']);
$entity->setPrivateIps($instance['PrivateIpAddress']);
$entity->setRootDevice($instance['RootDeviceName']);
$entity->setRootDeviceType($instance['RootDeviceType']);
$entity->setImageId($instance['ImageId']);
$entity->setAmiLaunchIndex($instance['AmiLaunchIndex']);
$entity->setReservation($instance['reservation_id']);
$entity->setKeyPairName($instance['KeyName']);
$entity->setLaunchTime(strtotime($instance['LaunchTime']->__toString()));
if ($uid != 0) {
$entity->setOwnerById($uid);
}
}
else {
$entity = Instance::create(
[
'cloud_context' => $cloud_context,
'name' => !empty($instance_name) ? $instance_name : $instance['InstanceId'],
'account_id' => $instance['reservation_ownerid'],
'security_groups' => $security_groups,
'instance_id' => $instance['InstanceId'],
'instance_type' => $instance['InstanceType'],
'availability_zone' => $instance['Placement']['AvailabilityZone'],
'instance_state' => $instance['State']['Name'],
'public_dns' => $instance['PublicDnsName'],
'private_dns' => $instance['PrivateDnsName'],
'private_ips' => $instance['PrivateIpAddress'],
'root_device_type' => $instance['RootDeviceType'],
'root_device' => $instance['RootDeviceName'],
'image_id' => $instance['ImageId'],
'ami_launch_index' => $instance['AmiLaunchIndex'],
'reservation' => $instance['reservation_id'],
'key_pair_name' => $instance['KeyName'],
'launch_time' => strtotime($instance['LaunchTime']->__toString()),
'changed' => $timestamp,
'refreshed' => $timestamp,
'uid' => $uid,
]
);
}
// Store the image name.
$images = $openStackEc2Service->describeImages(['ImageIds' => [$instance['ImageId']]]);
foreach ($images['Images'] as $image) {
if ($image['ImageId'] == $instance['ImageId']) {
$entity->setImageName($image['Name']);
}
}
$entity->save();
}
}
