Skip to content
Snippets Groups Projects
Commit 3549b64d authored by Yas Naoi's avatar Yas Naoi
Browse files

Issue #2761217: [Porting to D8] aws_cloud - implement update operations

parent a6a44d23
No related branches found
No related tags found
No related merge requests found
......@@ -5,6 +5,7 @@
* Contains \Drupal\aws_cloud\Aws\Ec2\ElasticIpInterface.
*/
// Updated by yas 2016/07/05
// Updated by yas 2016/06/03
// Updated by yas 2016/05/31
// Updated by yas 2016/05/28
......@@ -64,11 +65,6 @@ interface ElasticIpInterface extends ContentEntityInterface, EntityOwnerInterfac
*/
public function scope();
/**
* {@inheritdoc}
*/
public function public_dns();
/**
* {@inheritdoc}
*/
......@@ -89,6 +85,11 @@ interface ElasticIpInterface extends ContentEntityInterface, EntityOwnerInterfac
*/
public function allocation_id();
/**
* {@inheritdoc}
*/
public function association_id();
/**
* {@inheritdoc}
*/
......
<?php
// Updated by yas 2016/07/05
// Updated by yas 2016/07/03
// Updated by yas 2016/06/23
// Updated by yas 2016/06/13
......@@ -510,6 +511,17 @@ exit;
public function updateInstanceList(ConfigInterface $cloud_context) {
$entity_type = 'aws_cloud_instance';
// 1. Clean-up outdated objects.
$entity_ids = $this->entity_query->get($entity_type)
->condition('refreshed', $this->now, '<')
->execute();
foreach ($entity_ids as $entity_id) {
$entity = Instance::load($entity_id);
$entity->delete();
}
// 2. Fetch objets.
$operation = 'DescribeInstances';
$params = array(
'DryRun' => $this->is_dryrun, // true || false,
......@@ -534,6 +546,7 @@ exit;
}
// 3. Add objects.
$reservations = $result['Reservations'];
foreach ($reservations as $reservation) {
$instances = $reservation['Instances'];
......@@ -555,7 +568,8 @@ exit;
->condition('instance_id', $instance['InstanceId'])
->execute());
if ($entity_id) {
// Skip if $entity already exists, by updating 'refreshed' time.
if (isset($entity_id)) {
$entity = Instance::load($entity_id);
$entity->setInstanceState($instance['State']['Name']);
$entity->setElasticIp($instance['elastic_ip']);
......@@ -618,16 +632,7 @@ exit;
}
}
// Clean-up outdated objects.
$entity_ids = $this->entity_query->get($entity_type)
->condition('refreshed', "< $this->now")
->execute();
foreach ($entity_ids as $entity_id) {
$entity = Instance::load($entity_id);
$entity->delete();
}
// Redirect to list objects.
// 4. Redirect to list objects.
return $this->redirect('entity.aws_cloud_instance.collection', array(
'cloud_context' => $cloud_context->id(),
));
......@@ -639,6 +644,18 @@ exit;
public function updateImageList(ConfigInterface $cloud_context) {
$entity_type = 'aws_cloud_image';
// 1. Clean-up outdated objects.
$entity_ids = $this->entity_query->get($entity_type)
->condition('refreshed', $this->now, '<')
->execute();
foreach ($entity_ids as $entity_id) {
Image::load($entity_id);
$entity->delete();
}
// 2. Fetch objects.
$operation = 'DescribeImages';
$params = array(
'DryRun' => $this->is_dryrun, // true || false,
......@@ -663,6 +680,7 @@ exit;
}
// 3. Add objects.
$images = $result['Images'];
foreach ($images as $image) {
$block_devices = array();
......@@ -670,11 +688,12 @@ exit;
$block_devices[] = $block_device['DeviceName'];
}
$entity_id = array_shift($this->entity_query->get('aws_cloud_instance')
$entity_id = array_shift($this->entity_query->get($entity_type)
->condition('image_id', $image['ImageId'])
->execute());
if ($entity_id) {
// Skip if $entity already exists, by updating 'refreshed' time.
if (isset($entity_id)) {
$entity = Image::load($entity_id);
$entity->setRefreshed($this->now);
......@@ -708,17 +727,7 @@ exit;
$entity->save();
}
// Clean-up outdated objects.
$entity_ids = $this->entity_query->get($entity_type)
->condition('refreshed', "< $this->now")
->execute();
foreach ($entity_ids as $entity_id) {
Image::load($entity_id);
$entity->delete();
}
// Redirect to list objects.
// 4. Redirect to list objects.
return $this->redirect('entity.aws_cloud_image.collection', array(
'cloud_context' => $cloud_context->id(),
));
......@@ -730,6 +739,18 @@ exit;
public function updateSecurityGroupList(ConfigInterface $cloud_context) {
$entity_type = 'aws_cloud_security_group';
// 1. Clean-up outdated objects.
$entity_ids = $this->entity_query->get($entity_type)
->condition('refreshed', $this->now, '<')
->execute();
foreach ($entity_ids as $entity_id) {
$entity = SecurityGroup::load($entity_id);
$entity->delete();
}
// 2, Fetch objects.
$operation = 'DescribeSecurityGroups';
$params = array(
'DryRun' => $this->is_dryrun, // true || false,
......@@ -753,46 +774,39 @@ exit;
}
// 3. Add objects
$security_groups = $result['SecurityGroups'];
foreach ($security_groups as $security_group) {
$entity = $this->entity_query->get($entity_type)
$entity_id = array_shift($this->entity_query->get($entity_type)
->condition('group_id', $security_group['GroupId'])
->execute();
->execute());
if ($entity) {
// Skip if $entity already exists.
continue;
// Skip if $entity already exists, by updating 'refreshed' time.
if (isset($entity_id)) {
$entity = SecurityGroup::load($entity_id);
$entity->setRefreshed($this->now);
continue;
}
$entity = SecurityGroup::create(array(
// $cloud_context,.
'cloud_context' => $cloud_context->id(),
'name' => $security_group['GroupId'],
'name' => isset($security_group['GroupName']) ? $security_group['GroupName'] : $security_group['GroupId'],
'group_id' => $security_group['GroupId'],
'group_name' => $security_group['GroupName'],
'group_description' => $security_group['Description'],
'vpc_id' => $security_group['VpcId'],
'owner_id' => $security_group['OwnerId'],
'created' => strtotime(time()),
'created' => $this->now,
'changed' => $this->now,
'refreshed' => $this->now,
));
$entity->save();
}
// Clean-up outdated objects.
$entity_ids = $this->entity_query->get($entity_type)
->condition('refreshed', "< $this->now")
->execute();
foreach ($entity_ids as $entity_id) {
SecurityGroup::load($entity_id);
$entity->delete();
}
// Redirect to list objects.
// 4. Redirect to list objects.
return $this->redirect('entity.aws_cloud_security_group.collection', array(
'cloud_context' => $cloud_context->id(),
));
......@@ -804,6 +818,18 @@ exit;
public function updateNetworkInterfaceList(ConfigInterface $cloud_context) {
$entity_type = 'aws_cloud_network_interface';
// 1. Clean-up outdated objects.
$entity_ids = $this->entity_query->get('aws_cloud_network_interface')
->condition('refreshed', $this->now, '<')
->execute();
foreach ($entity_ids as $entity_id) {
$entity = NetworkInterface::load($entity_id);
$entity->delete();
}
// 2. Fetch objects.
$operation = 'DescribeNetworkInterfaces';
$params = array(
'DryRun' => $this->is_dryrun, // true || false,
......@@ -826,6 +852,7 @@ exit;
}
// 3. Add objects.
$network_interfaces = $result['NetworkInterfaces'];
foreach ($network_interfaces as $network_interface) {
$private_ip_addresses = array();
......@@ -843,7 +870,8 @@ exit;
->condition('network_interface_id', $network_interface['NetworkInterfaceId'])
->execute());
if ($entity_id) {
// Skip if $entity already exists, by updating 'refreshed' time.
if (isset($entity_id)) {
$entity = NetworkInterface::load($entity_id);
$entity->setRefreshed($this->now);
......@@ -883,17 +911,7 @@ exit;
$entity->save();
}
// Clean-up outdated objects.
$entity_ids = $this->entity_query->get('aws_cloud_network_interface')
->condition('refreshed', "< $this->now")
->execute();
foreach ($entity_ids as $entity_id) {
$entity = NetworkInterface::load($entity_id);
$entity->delete();
}
// Redirect to list objects.
// 4. Redirect to list objects.
return $this->redirect('entity.aws_cloud_network_interface.collection', array(
'cloud_context' => $cloud_context->id(),
));
......@@ -904,7 +922,19 @@ exit;
*/
public function updateElasticIpList(ConfigInterface $cloud_context) {
$entity_type = $entity_type;
$entity_type = 'aws_cloud_elastic_ip';
// 1. Clean-up outdated objects.
$entity_ids = $this->entity_query->get($entity_type)
->condition('refreshed', $this->now, '<')
->execute();
foreach ($entity_ids as $entity_id) {
$entity = ElasticIp::load($entity_id);
$entity->delete();
}
// 2. Fetch objects.
$operation = 'DescribeAddresses';
$params = array(
'DryRun' => $this->is_dryrun, // true || false,
......@@ -928,15 +958,17 @@ exit;
}
$elastic_ips = $result['ElasticIps'];
// 3. Add objects.
$elastic_ips = $result['Addresses'];
foreach ($elastic_ips as $elastic_ip) {
$entity_id = array_shift(
$this->entity_query->get($entity_type)
->condition('elastic_ip', $elastic_ip['ElasticIpId'])
->condition('public_ip', $elastic_ip['PublicIp'])
->execute());
if ($entity_id) {
// Skip if $entity already exists, by updating 'refreshed' time.
if (isset($entity_id)) {
$entity = ElasticIp::load($entity_id);
$entity->setRefreshed($this->now);
......@@ -945,33 +977,25 @@ exit;
$entity = ElasticIp::create(array(
// $cloud_context,.
'cloud_context' => $cloud_context->id(),
'elastic_ip' => $elastic_ip['PublicIp'],
'instance_id' => $elastic_ip['InstanceId'],
'public_dns' => $elastic_ip['PublicIp'],
'network_interface_id' => $elastic_ip['NetworkInterfaceId'],
'private_ip_address' => $elastic_ip['PrivateIpAddress'],
'network_interface_owner' => $elastic_ip['NetworkInterfaceOwnerId'],
'allocation_id' => $elastic_ip['AllocationId'],
'created' => strtotime($elastic_ip['CreationDate']),
'changed' => $this->now,
'refreshed' => $this->now,
'cloud_context' => $cloud_context->id(),
'name' => $elastic_ip['PublicIp'],
'public_ip' => $elastic_ip['PublicIp'],
'instance_id' => isset($elastic_ip['InstanceId']) ? $elastic_ip['InstanceId'] : '',
'network_interface_id' => isset($elastic_ip['NetworkInterfaceId']) ? $elastic_ip['NetworkInterfaceId'] : '',
'private_ip_address' => isset($elastic_ip['PrivateIpAddress']) ? $elastic_ip['PrivateIpAddress'] : '',
'network_interface_owner' => isset($elastic_ip['NetworkInterfaceOwnerId']) ? $elastic_ip['NetworkInterfaceOwnerId'] : '',
'allocation_id' => isset($elastic_ip['AllocationId']) ? $elastic_ip['AllocationId'] : '',
'association_id' => isset($elastic_ip['AssociationId']) ? $elastic_ip['AssociationId'] : '',
'domain' => isset($elastic_ip['Domain']) ? $elastic_ip['Domain'] : '',
'created' => $this->now,
'changed' => $this->now,
'refreshed' => $this->now,
));
$entity->save();
}
// Clean-up outdated objects.
$entity_ids = array_shift(
$this->entity_query->get($entity_type)
->condition('refreshed', "< $this->now")
->execute());
foreach ($entity_ids as $entity_id) {
$entity = ElasticIp::load($entity_id);
$entity->delete();
}
// Redirect to list objects.
// 4. Redirect to list objects.
return $this->redirect('entity.aws_cloud_elastic_ip.collection', array(
'cloud_context' => $cloud_context->id(),
));
......@@ -983,6 +1007,18 @@ exit;
public function updateKeyPairList(ConfigInterface $cloud_context) {
$entity_type = 'aws_cloud_key_pair';
// 1. Clean-up outdated objects.
$entity_ids = $this->entity_query->get($entity_type)
->condition('refreshed', $this->now, '<')
->execute();
foreach ($entity_ids as $entity_id) {
$entity = KeyPair::load($entity_id);
$entity->delete();
}
// 2. Fetch objects.
$operation = 'DescribeKeyPairs';
$params = array(
'DryRun' => $this->is_dryrun, // true || false,
......@@ -1004,6 +1040,7 @@ exit;
}
// 3. Add objects.
$key_pairs = $result['KeyPairs'];
foreach ($key_pairs as $key_pair) {
foreach ($key_pair['BlockDeviceMappings'] as $block_device) {
......@@ -1014,7 +1051,8 @@ exit;
->condition('key_pair_name', $key_pair['KeyName'])
->execute());
if ($entity_id) {
// Skip if $entity already exists, by updating 'refreshed' time.
if (isset($entity_id)) {
$entity = KeyPair::load($entity_id);
$entity->setRefreshed($this->now);
......@@ -1033,17 +1071,7 @@ exit;
$entity->save();
}
// Clean-up outdated objects.
$entity_ids = array_shift($this->entity_query->get($entity_type)
->condition('refreshed', "< $this->now")
->execute());
foreach ($entity_ids as $entity_id) {
$entity = KeyPair::load($entity_id);
$entity->delete();
}
// Redirect to list objects.
// 4. Redirect to list objects.
return $this->redirect('entity.aws_cloud_key_pair.collection', array(
'cloud_context' => $cloud_context->id(),
));
......@@ -1055,6 +1083,18 @@ exit;
public function updateVolumeList(ConfigInterface $cloud_context) {
$entity_type = 'aws_cloud_volume';
// 1. Clean-up outdated objects.
$entity_ids = $this->entity_query->get($entity_type)
->condition('refreshed', $this->now, '<')
->execute();
foreach ($entity_ids as $entity_id) {
$entity = Volume::load($entity_id);
$entity->delete();
}
// 2. Fetch objects.
$operation = 'DescribeVolumes';
$params = array(
'DryRun' => $this->is_dryrun, // true || false,
......@@ -1079,6 +1119,7 @@ exit;
}
// 3. Add objects.
$volumes = $result['Volumes'];
foreach ($volumes as $volume) {
$attachments = array();
......@@ -1090,7 +1131,8 @@ exit;
->condition('volume_id', $volume['VolumeId'])
->execute());
if ($entity_id) {
// Skip if $entity already exists, by updating 'refreshed' time.
if (isset($entity_id)) {
$entity = Volume::load($entity_id);
$entity->setRefreshed($this->now);
......@@ -1119,17 +1161,7 @@ exit;
$entity->save();
}
// Clean-up outdated objects.
$entity_ids = $this->entity_query->get($entity_type)
->condition('refreshed', "< $this->now")
->execute();
foreach ($entity_ids as $entity_id) {
$entity = Volume::load($entity_id);
$entity->delete();
}
// Redirect to list objects.
// 4. Redirect to list objects.
return $this->redirect('entity.aws_cloud_volume.collection', array(
'cloud_context' => $cloud_context->id(),
));
......@@ -1141,6 +1173,18 @@ exit;
public function updateSnapshotList(ConfigInterface $cloud_context) {
$entity_type = 'aws_cloud_snapshot';
// 1. Clean-up outdated objects.
$entity_ids = $this->entity_query->get($entity_type)
->condition('refreshed', $this->now, '<')
->execute();
foreach ($entity_ids as $entity) {
$entity = Snapshot::load($entity_id);
$entity->delete();
}
// 2. Fetch objects.
$operation = 'DescribeSnapshots';
$params = array(
'DryRun' => $this->is_dryrun, // true || false
......@@ -1167,7 +1211,7 @@ exit;
}
if(empty($result)) return;
// 3. Add objects.
$snapshots = $result['Snapshots'];
foreach ($snapshots as $snapshot) {
......@@ -1175,7 +1219,8 @@ exit;
->condition('snapshot_id', $snapshot['SnapshotId'])
->execute());
if ($entity_id) {
// Skip if $entity already exists, by updating 'refreshed' time.
if (isset($entity_id)) {
$entity = Snapshot::load($entity_id);
$entity->setRefreshed($this->now);
......@@ -1203,17 +1248,7 @@ exit;
$entity->save();
}
// Clean-up outdated objects.
$entity_ids = $this->entity_query->get($entity_type)
->condition('refreshed', "< $this->now")
->execute();
foreach ($entity_ids as $entity) {
$entity = Snapshot::load($entity_id);
$entity->delete();
}
// Redirect to list objects.
// 4. Redirect to list objects.
return $this->redirect('entity.aws_cloud_snapshot.collection', array(
'cloud_context' => $cloud_context->id(),
));
......
<?php
// Udpated by yas 2016/07/05
// Udpated by yas 2016/06/03
// Udpated by yas 2016/05/25
// Updated by yas 2016/05/23
......@@ -35,7 +36,7 @@ class ElasticIpListBuilder extends CloudContentListBuilder {
array('data' => t('Instance'), 'specifier' => 'instance_id'),
array('data' => t('Private IP Address'), 'specifier' => 'private_ip_address'),
array('data' => t('Scope'), 'specifier' => 'scope'),
array('data' => t('Public DNS'), 'specifier' => 'public_dns'),
// array('data' => t('Public DNS'), 'specifier' => 'public_dns'), // Need to get from Network Interface
);
return $header + parent::buildHeader();
......@@ -59,7 +60,7 @@ class ElasticIpListBuilder extends CloudContentListBuilder {
$row['instance_id'] = $entity->instance_id();
$row['private_ip_address'] = $entity->private_ip_address();
$row['scope'] = $entity->scope();
$row['public_dns'] = $entity->public_dns();
// $row['public_dns'] = $entity->public_dns(); // Need to get from Network Interface
return $row + parent::buildRow($entity);
}
......
<?php
// Updated by yas 2016/07/05
// Updated by yas 2016/06/03
// Updated by yas 2016/05/30
// Updated by yas 2016/05/28
......@@ -98,6 +99,13 @@ class ElasticIp extends ContentEntityBase implements ElasticIpInterface {
return $this->get('domain')->value;
}
/**
* {@inheritdoc}
*/
public function association_id() {
return $this->get('association_id')->value;
}
/**
* {@inheritdoc}
*/
......@@ -126,13 +134,6 @@ class ElasticIp extends ContentEntityBase implements ElasticIpInterface {
return $this->get('scope')->value;
}
/**
* {@inheritdoc}
*/
public function public_dns() {
return $this->get('public_dns')->value;
}
/**
* {@inheritdoc}
*/
......@@ -285,11 +286,6 @@ class ElasticIp extends ContentEntityBase implements ElasticIpInterface {
->setDescription(t('Indicates if the Elastic IP address is for use in EC2-Classic (standard) or in a VPC (vpc).'))
->setReadOnly(TRUE);
$fields['public_dns'] = BaseFieldDefinition::create('string')
->setLabel(t('Public DNS'))
->setDescription(t('The public hostname of the instance to which the Elastic IP address is associated.'))
->setReadOnly(TRUE);
$fields['network_interface_id'] = BaseFieldDefinition::create('string')
->setLabel(t('Network Interface ID'))
->setDescription(t('For instances in a VPC, indicates the ID of the network interface to which the Elastic IP is associated.'))
......@@ -309,6 +305,10 @@ class ElasticIp extends ContentEntityBase implements ElasticIpInterface {
->setLabel(t('Allocation ID'))
->setDescription(t('The allocation ID of the Elastic IP address. Only applicable to Elastic IP addresses used in a VPC.'));
$fields['association_id'] = BaseFieldDefinition::create('string')
->setLabel(t('Association ID'))
->setDescription(t('The ID representing the association of the address with an instance in a VPC.'));
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time that the entity was created.'));
......
<?php
// Updated by yas 2016/07/05
// Updated by yas 2016/07/03
// Updated by yas 2016/06/24
// Updated by yas 2016/06/23
......@@ -138,7 +139,7 @@ class ConfigEditForm extends CloudConfigForm {
'#description' => $this->t('e.g. 123456789012'),
'#default_value' => $entity->user_id(),
'#maxlength' => 32,
'#required' => TRUE,
'#required' => FALSE,
);
$form['image_upload_url'] = array(
......
......@@ -55,11 +55,12 @@ class ElasticIpCreateForm extends CloudContentForm {
);
$form['domain'] = array(
'#type' => 'textfield',
'#type' => 'select',
'#options' => array('standard' => 'standard',
'vpc' => 'vpc',
),
'#title' => $this->t('Domain (standard | vpc)'),
'#maxlength' => 15,
'#size' => 60,
'#default_value' => $entity->domain(),
'#default_value' => 'standard',
'#required' => TRUE,
'#weight' => -5,
);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment