Skip to content
Snippets Groups Projects
Commit d6d6f67a authored by xiaohua guan's avatar xiaohua guan Committed by Yas Naoi
Browse files

Issue #3038994 by Xiaohua Guan, yas, baldwinlouie, Masami: Add tags to ServerTemplate

parent b0edc457
No related branches found
No related tags found
No related merge requests found
Showing
with 187 additions and 51 deletions
<?php <?php
// Updated by yas 2016/06/23
// Updated by yas 2016/05/26
// Updated by yas 2016/05/25
// Updated by yas 2016/05/24
// Updated by yas 2016/05/23
// Ureated by yas 2015/06/08.
namespace Drupal\Tests\cloud_server_template\Functional; namespace Drupal\Tests\cloud_server_template\Functional;
use Drupal\Tests\aws_cloud\Functional\Ec2\AwsCloudTestCase; use Drupal\Tests\aws_cloud\Functional\Ec2\AwsCloudTestCase;
...@@ -107,6 +101,8 @@ class CloudServerTemplateTest extends AwsCloudTestCase { ...@@ -107,6 +101,8 @@ class CloudServerTemplateTest extends AwsCloudTestCase {
$this->assertText($vpc_name); $this->assertText($vpc_name);
$this->assertText($subnet_name); $this->assertText($subnet_name);
$this->assertText($iam_role_name); $this->assertText($iam_role_name);
$this->assertText($add[$i]['field_tags[0][tag_key]']);
$this->assertText($add[$i]['field_tags[0][tag_value]']);
// Make sure listing. // Make sure listing.
$this->drupalGet("/clouds/design/server_template/list/$cloud_context"); $this->drupalGet("/clouds/design/server_template/list/$cloud_context");
...@@ -337,6 +333,8 @@ class CloudServerTemplateTest extends AwsCloudTestCase { ...@@ -337,6 +333,8 @@ class CloudServerTemplateTest extends AwsCloudTestCase {
'field_ram[0][value]' => 'ari-' . $this->getRandomAwsId(), 'field_ram[0][value]' => 'ari-' . $this->getRandomAwsId(),
'field_security_group' => 1, 'field_security_group' => 1,
'field_ssh_key' => 1, 'field_ssh_key' => 1,
'field_tags[0][tag_key]' => 'key_' . $random->name(8, TRUE),
'field_tags[0][tag_value]' => 'value_' . $random->name(8, TRUE),
]; ];
} }
return $data; return $data;
......
...@@ -430,6 +430,13 @@ function aws_cloud_update_8127() { ...@@ -430,6 +430,13 @@ function aws_cloud_update_8127() {
cloud_update_yml_definitions($files, 'aws_cloud'); cloud_update_yml_definitions($files, 'aws_cloud');
} }
/**
* Add field_tags field to cloud server template of aws_cloud.
*/
function aws_cloud_update_8128() {
aws_cloud_add_fields('cloud_server_template', 'aws_cloud', 'field_tags');
}
/** /**
* Helper function to add fields to the entity type. * Helper function to add fields to the entity type.
* *
......
...@@ -850,6 +850,9 @@ function aws_cloud_form_cloud_server_template_aws_cloud_form_common_alter(&$form ...@@ -850,6 +850,9 @@ function aws_cloud_form_cloud_server_template_aws_cloud_form_common_alter(&$form
$form['#attached']['drupalSettings']['aws_cloud']['field_security_group_default_values'] $form['#attached']['drupalSettings']['aws_cloud']['field_security_group_default_values']
= $security_group_default_values; = $security_group_default_values;
// Hide labels of field_tags.
$form['tags']['field_tags']['widget']['#title'] = NULL;
} }
/** /**
...@@ -900,6 +903,14 @@ function aws_cloud_server_template_field_orders($include_name = TRUE) { ...@@ -900,6 +903,14 @@ function aws_cloud_server_template_field_orders($include_name = TRUE) {
'field_network', 'field_network',
], ],
], ],
[
'name' => 'tags',
'title' => t('Tags'),
'open' => TRUE,
'fields' => [
'field_tags',
],
],
[ [
'name' => 'options', 'name' => 'options',
'title' => t('Options'), 'title' => t('Options'),
......
uuid: 55ffa908-1861-41dc-af06-8ebaabcb1e23
langcode: en langcode: en
status: true status: true
dependencies: dependencies:
...@@ -24,6 +23,7 @@ dependencies: ...@@ -24,6 +23,7 @@ dependencies:
- field.field.cloud_server_template.aws_cloud.field_vpc - field.field.cloud_server_template.aws_cloud.field_vpc
- field.field.cloud_server_template.aws_cloud.field_termination_protection - field.field.cloud_server_template.aws_cloud.field_termination_protection
- field.field.cloud_server_template.aws_cloud.field_iam_role - field.field.cloud_server_template.aws_cloud.field_iam_role
- field.field.cloud_server_template.aws_cloud.field_tags
id: cloud_server_template.aws_cloud.default id: cloud_server_template.aws_cloud.default
targetEntityType: cloud_server_template targetEntityType: cloud_server_template
bundle: aws_cloud bundle: aws_cloud
...@@ -179,4 +179,10 @@ content: ...@@ -179,4 +179,10 @@ content:
placeholder: '' placeholder: ''
region: content region: content
third_party_settings: { } third_party_settings: { }
field_tags:
weight: 21
settings: { }
third_party_settings: { }
type: tag_item
region: content
hidden: { } hidden: { }
...@@ -23,6 +23,7 @@ dependencies: ...@@ -23,6 +23,7 @@ dependencies:
- field.field.cloud_server_template.aws_cloud.field_vpc - field.field.cloud_server_template.aws_cloud.field_vpc
- field.field.cloud_server_template.aws_cloud.field_termination_protection - field.field.cloud_server_template.aws_cloud.field_termination_protection
- field.field.cloud_server_template.aws_cloud.field_iam_role - field.field.cloud_server_template.aws_cloud.field_iam_role
- field.field.cloud_server_template.aws_cloud.field_tags
module: module:
- options - options
- user - user
...@@ -205,4 +206,11 @@ content: ...@@ -205,4 +206,11 @@ content:
region: content region: content
settings: { } settings: { }
third_party_settings: { } third_party_settings: { }
field_tags:
label: hidden
type: tag_formatter
weight: 0
region: content
settings: { }
third_party_settings: { }
hidden: { } hidden: { }
langcode: en
status: true
dependencies:
config:
- cloud_server_template.cloud_server_template_type.aws_cloud
- field.storage.cloud_server_template.field_tags
module:
- aws_cloud
id: cloud_server_template.aws_cloud.field_tags
field_name: field_tags
entity_type: cloud_server_template
bundle: aws_cloud
label: Tags
description: ''
required: false
translatable: false
default_value: { }
default_value_callback: ''
settings: { }
field_type: tag
langcode: en
status: true
dependencies:
module:
- aws_cloud
- cloud_server_template
id: cloud_server_template.field_tags
field_name: field_tags
entity_type: cloud_server_template
type: tag
settings:
max_length: 255
module: aws_cloud
locked: false
cardinality: -1
translatable: true
indexes: { }
persist_with_no_fields: false
custom_storage: false
...@@ -11,3 +11,10 @@ field.formatter.settings.entity_link: ...@@ -11,3 +11,10 @@ field.formatter.settings.entity_link:
comma_separated: comma_separated:
type: boolean type: boolean
label: 'Separated with comma' label: 'Separated with comma'
field.storage_settings.tag:
type: mapping
label: 'Aws Tag'
mapping:
max_length:
type: integer
label: 'Maximum length'
...@@ -5,7 +5,6 @@ namespace Drupal\aws_cloud\Form\Ec2; ...@@ -5,7 +5,6 @@ namespace Drupal\aws_cloud\Form\Ec2;
use Drupal\Core\Datetime\Element\Datetime; use Drupal\Core\Datetime\Element\Datetime;
use Drupal\Core\Datetime\Entity\DateFormat; use Drupal\Core\Datetime\Entity\DateFormat;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\Language;
use Drupal\Core\Link; use Drupal\Core\Link;
use Drupal\aws_cloud\Entity\Ec2\ElasticIp; use Drupal\aws_cloud\Entity\Ec2\ElasticIp;
...@@ -228,6 +227,7 @@ class InstanceEditForm extends AwsCloudContentForm { ...@@ -228,6 +227,7 @@ class InstanceEditForm extends AwsCloudContentForm {
'#open' => TRUE, '#open' => TRUE,
'#weight' => $weight++, '#weight' => $weight++,
]; ];
$form['fieldset_tags'][] = $form['tags']; $form['fieldset_tags'][] = $form['tags'];
unset($form['tags']); unset($form['tags']);
......
...@@ -151,7 +151,12 @@ class AwsCloudServerTemplatePlugin extends PluginBase implements CloudServerTemp ...@@ -151,7 +151,12 @@ class AwsCloudServerTemplatePlugin extends PluginBase implements CloudServerTemp
$this->awsEc2Service->setCloudContext($cloud_server_template->getCloudContext()); $this->awsEc2Service->setCloudContext($cloud_server_template->getCloudContext());
$tags = []; $tags_map = [];
// Add tags from the template.
foreach ($cloud_server_template->get('field_tags') as $tag_item) {
$tags_map[$tag_item->getTagKey()] = $tag_item->getTagValue();
}
if ($form_state->getValue('termination_protection')) { if ($form_state->getValue('termination_protection')) {
$params['DisableApiTermination'] = $form_state->getValue('termination_protection') == "0" ? FALSE : TRUE; $params['DisableApiTermination'] = $form_state->getValue('termination_protection') == "0" ? FALSE : TRUE;
...@@ -162,19 +167,22 @@ class AwsCloudServerTemplatePlugin extends PluginBase implements CloudServerTemp ...@@ -162,19 +167,22 @@ class AwsCloudServerTemplatePlugin extends PluginBase implements CloudServerTemp
if ($form_state->getValue('terminate')) { if ($form_state->getValue('terminate')) {
/* @var \Drupal\Core\Datetime\DrupalDateTime $timestamp */ /* @var \Drupal\Core\Datetime\DrupalDateTime $timestamp */
$timestamp = $form_state->getValue('termination_date'); $timestamp = $form_state->getValue('termination_date');
$tags[] = [ $tags_map['cloud_termination_timestamp'] = $timestamp->getTimeStamp();
'Key' => 'cloud_termination_timestamp',
'Value' => $timestamp->getTimeStamp(),
];
} }
} }
if (!empty($form_state->getValue('schedule'))) { if (!empty($form_state->getValue('schedule'))) {
// Send the schedule if scheduler is enabled. // Send the schedule if scheduler is enabled.
$config = \Drupal::config('aws_cloud.settings'); $config = \Drupal::config('aws_cloud.settings');
$tags_map[$config->get('aws_cloud_scheduler_tag')]
= $form_state->getValue('schedule');
}
$tags = [];
foreach ($tags_map as $tag_key => $tag_value) {
$tags[] = [ $tags[] = [
'Key' => $config->get('aws_cloud_scheduler_tag'), 'Key' => $tag_key,
'Value' => $form_state->getValue('schedule'), 'Value' => $tag_value,
]; ];
} }
......
...@@ -17,7 +17,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface; ...@@ -17,7 +17,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* id = "tag_formatter", * id = "tag_formatter",
* label = @Translation("Tag formatter"), * label = @Translation("Tag formatter"),
* field_types = { * field_types = {
* "tag_formatter" * "tag"
* } * }
* ) * )
*/ */
......
...@@ -16,6 +16,7 @@ use Drupal\aws_cloud\Aws\Ec2\ImageInterface; ...@@ -16,6 +16,7 @@ use Drupal\aws_cloud\Aws\Ec2\ImageInterface;
use Drupal\aws_cloud\Aws\Ec2\KeyPairInterface; use Drupal\aws_cloud\Aws\Ec2\KeyPairInterface;
use Drupal\aws_cloud\Aws\Ec2\SecurityGroupInterface; use Drupal\aws_cloud\Aws\Ec2\SecurityGroupInterface;
use Drupal\aws_cloud\Aws\Ec2\NetworkInterfaceInterface; use Drupal\aws_cloud\Aws\Ec2\NetworkInterfaceInterface;
use Drupal\aws_cloud\Plugin\Field\FieldType\Tag;
/** /**
* Tests AWS Cloud Template Plugin. * Tests AWS Cloud Template Plugin.
...@@ -30,7 +31,7 @@ class AwsCloudServerTemplatePluginTest extends UnitTestCase { ...@@ -30,7 +31,7 @@ class AwsCloudServerTemplatePluginTest extends UnitTestCase {
const MAX_SECURITY_GROUPS_COUNT = 500; const MAX_SECURITY_GROUPS_COUNT = 500;
/** /**
* Plugin * Plugin.
* *
* @var string * @var string
*/ */
...@@ -51,11 +52,10 @@ class AwsCloudServerTemplatePluginTest extends UnitTestCase { ...@@ -51,11 +52,10 @@ class AwsCloudServerTemplatePluginTest extends UnitTestCase {
private $random; private $random;
/** /**
* {@inheritDoc} * {@inheritdoc}
*/ */
protected function setUp() { protected function setUp() {
$this->mockAwsEc2Service = $this->getMockBuilder(AwsEc2ServiceInterface::class) $this->mockAwsEc2Service = $this->getMock(AwsEc2ServiceInterface::class);
->getMock();
$this->plugin = new AwsCloudServerTemplatePlugin( $this->plugin = new AwsCloudServerTemplatePlugin(
[], '', [], [], '', [],
...@@ -78,7 +78,7 @@ class AwsCloudServerTemplatePluginTest extends UnitTestCase { ...@@ -78,7 +78,7 @@ class AwsCloudServerTemplatePluginTest extends UnitTestCase {
// Mock object of Image. // Mock object of Image.
$mock_image = $this->getMock(ImageInterface::class); $mock_image = $this->getMock(ImageInterface::class);
$image_value_map = [ $image_value_map = [
['image_id', (object)['value' => 'ami-' . $this->getRandomAwsId()]], ['image_id', (object) ['value' => 'ami-' . $this->getRandomAwsId()]],
['root_device_type', 'ebs'], ['root_device_type', 'ebs'],
]; ];
$mock_image->expects($this->any()) $mock_image->expects($this->any())
...@@ -105,6 +105,9 @@ class AwsCloudServerTemplatePluginTest extends UnitTestCase { ...@@ -105,6 +105,9 @@ class AwsCloudServerTemplatePluginTest extends UnitTestCase {
$field_security_groups_test_cases = $this->createFieldSecurityGroupsTestCases($vpc_id); $field_security_groups_test_cases = $this->createFieldSecurityGroupsTestCases($vpc_id);
$subnet_id = 'subnet-' . $this->getRandomAwsId(); $subnet_id = 'subnet-' . $this->getRandomAwsId();
// Create random tags.
$mock_tags = $this->createRandomTags();
// Run test cases. // Run test cases.
foreach ($field_security_groups_test_cases as $field_security_groups_test_case) { foreach ($field_security_groups_test_cases as $field_security_groups_test_case) {
$mock_template = $this->getMock(CloudServerTemplateInterface::class); $mock_template = $this->getMock(CloudServerTemplateInterface::class);
...@@ -124,8 +127,9 @@ class AwsCloudServerTemplatePluginTest extends UnitTestCase { ...@@ -124,8 +127,9 @@ class AwsCloudServerTemplatePluginTest extends UnitTestCase {
['field_network', (object) ['entity' => $mock_network]], ['field_network', (object) ['entity' => $mock_network]],
['field_subnet', (object) ['value' => $subnet_id]], ['field_subnet', (object) ['value' => $subnet_id]],
['field_instance_shutdown_behavior', (object) ['value' => '1']], ['field_instance_shutdown_behavior', (object) ['value' => '1']],
['field_vpc', (object)['value' => $vpc_id]], ['field_vpc', (object) ['value' => $vpc_id]],
['field_iam_role', (object)['value' => '']], ['field_iam_role', (object) ['value' => '']],
['field_tags', $mock_tags],
]; ];
$mock_template->expects($this->any()) $mock_template->expects($this->any())
->method('get') ->method('get')
...@@ -140,13 +144,16 @@ class AwsCloudServerTemplatePluginTest extends UnitTestCase { ...@@ -140,13 +144,16 @@ class AwsCloudServerTemplatePluginTest extends UnitTestCase {
$this->mockAwsEc2Service $this->mockAwsEc2Service
->expects($this->any()) ->expects($this->any())
->method('runInstances') ->method('runInstances')
->with($this->logicalAnd( ->with(
$this->arrayHasKey('SecurityGroupIds'), $this->logicalAnd(
$this->logicalNot($this->arrayHasKey('SecurityGroups')), $this->arrayHasKey('SecurityGroupIds'),
$this->contains($this->extractArrayItem($field_security_groups_test_case, 1)), $this->logicalNot($this->arrayHasKey('SecurityGroups')),
$this->arrayHasKey('SubnetId'), $this->contains($this->extractArrayItem($field_security_groups_test_case, 1)),
$this->contains($subnet_id) $this->arrayHasKey('SubnetId'),
)) $this->contains($subnet_id)
),
$this->equalTo($this->convertToAwsTags($mock_tags))
)
->will($this->returnValue(TRUE)); ->will($this->returnValue(TRUE));
$return = $this->plugin->launch( $return = $this->plugin->launch(
...@@ -162,11 +169,12 @@ class AwsCloudServerTemplatePluginTest extends UnitTestCase { ...@@ -162,11 +169,12 @@ class AwsCloudServerTemplatePluginTest extends UnitTestCase {
* Extract array item. * Extract array item.
* *
* @param array $array * @param array $array
* array to extract. * Array to be extracted.
* @param int $index * @param int $index
* index. * Index.
*
* @return array * @return array
* extracted array. * Extracted array.
*/ */
private function extractArrayItem(array $array, $index) { private function extractArrayItem(array $array, $index) {
$extracted = []; $extracted = [];
...@@ -181,8 +189,10 @@ class AwsCloudServerTemplatePluginTest extends UnitTestCase { ...@@ -181,8 +189,10 @@ class AwsCloudServerTemplatePluginTest extends UnitTestCase {
* Create field security groups test cases. * Create field security groups test cases.
* *
* @param string $vpc_id * @param string $vpc_id
* vpc id. * The ID of vpc.
* @return mixed[][]|NULL[][]|unknown[][] *
* @return array
* The test cases of security group.
*/ */
private function createFieldSecurityGroupsTestCases($vpc_id) { private function createFieldSecurityGroupsTestCases($vpc_id) {
$random = $this->random; $random = $this->random;
...@@ -202,7 +212,7 @@ class AwsCloudServerTemplatePluginTest extends UnitTestCase { ...@@ -202,7 +212,7 @@ class AwsCloudServerTemplatePluginTest extends UnitTestCase {
$mock_security_group_and_ids[] = [ $mock_security_group_and_ids[] = [
(object) ['entity' => $mock_security_group], (object) ['entity' => $mock_security_group],
$group_id $group_id,
]; ];
} }
...@@ -255,11 +265,10 @@ class AwsCloudServerTemplatePluginTest extends UnitTestCase { ...@@ -255,11 +265,10 @@ class AwsCloudServerTemplatePluginTest extends UnitTestCase {
// Security Group M (* SELECTED). // Security Group M (* SELECTED).
// ... // ...
// Security Group N. // Security Group N.
$rand_index = array_rand($mock_security_group_and_ids_exclude_first_last);
$field_security_groups_test_cases[] = [ $field_security_groups_test_cases[] = [
// Pickup one index randomly (array index: 1 to the (last index -2)) // Pickup one index randomly (array index: 1 to the (last index -2))
$mock_security_group_and_ids_exclude_first_last[ $mock_security_group_and_ids_exclude_first_last[$rand_index],
array_rand($mock_security_group_and_ids_exclude_first_last)
]
]; ];
// The first item and one random one. // The first item and one random one.
...@@ -268,12 +277,11 @@ class AwsCloudServerTemplatePluginTest extends UnitTestCase { ...@@ -268,12 +277,11 @@ class AwsCloudServerTemplatePluginTest extends UnitTestCase {
// Security Group M (* SELECTED). // Security Group M (* SELECTED).
// ... // ...
// Security Group N. // Security Group N.
$rand_index = array_rand($mock_security_group_and_ids_exclude_first_last);
$field_security_groups_test_cases[] = [ $field_security_groups_test_cases[] = [
reset($mock_security_group_and_ids), reset($mock_security_group_and_ids),
// Pickup one index randomly (array index: 1 to the (last index -2)) // Pickup one index randomly (array index: 1 to the (last index -2))
$mock_security_group_and_ids_exclude_first_last[ $mock_security_group_and_ids_exclude_first_last[$rand_index],
array_rand($mock_security_group_and_ids_exclude_first_last)
],
]; ];
// One random item and the last one. // One random item and the last one.
...@@ -282,11 +290,10 @@ class AwsCloudServerTemplatePluginTest extends UnitTestCase { ...@@ -282,11 +290,10 @@ class AwsCloudServerTemplatePluginTest extends UnitTestCase {
// Security Group M (* SELECTED). // Security Group M (* SELECTED).
// ... // ...
// Security Group N (* SELECTED). // Security Group N (* SELECTED).
$rand_index = array_rand($mock_security_group_and_ids_exclude_first_last);
$field_security_groups_test_cases[] = [ $field_security_groups_test_cases[] = [
// Pickup one index randomly (array index: 1 to the (last index -2)) // Pickup one index randomly (array index: 1 to the (last index -2))
$mock_security_group_and_ids_exclude_first_last[ $mock_security_group_and_ids_exclude_first_last[$rand_index],
array_rand($mock_security_group_and_ids_exclude_first_last)
],
end($mock_security_group_and_ids), end($mock_security_group_and_ids),
]; ];
...@@ -302,12 +309,8 @@ class AwsCloudServerTemplatePluginTest extends UnitTestCase { ...@@ -302,12 +309,8 @@ class AwsCloudServerTemplatePluginTest extends UnitTestCase {
// Security Group N. // Security Group N.
$array_indice = array_rand($mock_security_group_and_ids_exclude_first_last, 2); $array_indice = array_rand($mock_security_group_and_ids_exclude_first_last, 2);
$field_security_groups_test_cases[] = [ $field_security_groups_test_cases[] = [
$mock_security_group_and_ids_exclude_first_last[ $mock_security_group_and_ids_exclude_first_last[$array_indice[0]],
$array_indice[0] $mock_security_group_and_ids_exclude_first_last[$array_indice[1]],
],
$mock_security_group_and_ids_exclude_first_last[
$array_indice[1]
],
]; ];
} }
} }
...@@ -316,4 +319,53 @@ class AwsCloudServerTemplatePluginTest extends UnitTestCase { ...@@ -316,4 +319,53 @@ class AwsCloudServerTemplatePluginTest extends UnitTestCase {
return $field_security_groups_test_cases; return $field_security_groups_test_cases;
} }
/**
* Create random tags.
*
* @return array
* Tag mock objects.
*/
private function createRandomTags() {
$mock_tags = [];
$count = rand(0, 5);
for ($i = 0; $i < $count; $i++) {
$mock_tag = $this->getMockBuilder(Tag::class)
->disableOriginalConstructor()
->getMock();
$mock_tag->expects($this->any())
->method('getTagKey')
->will($this->returnValue('key_' . $this->random->name(8, TRUE)));
$mock_tag->expects($this->any())
->method('getTagValue')
->will($this->returnValue('value_' . $this->random->name(8, TRUE)));
$mock_tags[] = $mock_tag;
}
return $mock_tags;
}
/**
* Convert mock tag objects to AWS tags.
*
* @param array $mock_tags
* Mock tag objects.
*
* @return array
* Aws tags.
*/
private function convertToAwsTags(array $mock_tags) {
$tags = [];
foreach ($mock_tags as $mock_tag) {
$tags[] = [
'Key' => $mock_tag->getTagKey(),
'Value' => $mock_tag->getTagValue(),
];
}
return $tags;
}
} }
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