Skip to content
Snippets Groups Projects
Commit 98ae189a authored by Masami  Suzuki's avatar Masami Suzuki Committed by Yas Naoi
Browse files

Issue #3070772 by Masami, yas: Make vpc id field required on security group

parent a69a2a83
No related branches found
No related tags found
No related merge requests found
......@@ -2,6 +2,7 @@
namespace Drupal\aws_cloud\Form\Ec2;
use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;
/**
......@@ -37,14 +38,19 @@ class SecurityGroupCopyForm extends SecurityGroupEditForm {
'#weight' => 0,
];
$vpcs = array_merge(['' => 'N/A'], $this->awsEc2Service->getVpcs());
$vpcs = $this->awsEc2Service->getVpcs();
if (count($vpcs) == 0) {
$this->messenger->addWarning(
$this->t('You do not have any VPCs. You need a VPC in order to create a security group. You can <a href=":create_vpc_link">create a VPC</a>.',
[':create_vpc_link' => Url::fromRoute('entity.aws_cloud_vpc.add_form', ['cloud_context' => $cloud_context])->toString()]));
}
ksort($vpcs);
$form['security_group']['vpc_id'] = [
'#type' => 'select',
'#title' => $this->t('VPC CIDR (ID)'),
'#options' => $vpcs,
'#default_value' => $entity->getVpcId(),
'#required' => FALSE,
'#required' => TRUE,
'#weight' => 1,
];
......
......@@ -3,6 +3,7 @@
namespace Drupal\aws_cloud\Form\Ec2;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
/**
* Form controller for the SecurityGroup entity create form.
......@@ -41,6 +42,11 @@ class SecurityGroupCreateForm extends AwsCloudContentForm {
];
$vpcs = $this->awsEc2Service->getVpcs();
if (count($vpcs) == 0) {
$this->messenger->addWarning(
$this->t('You do not have any VPCs. You need a VPC in order to create a security group. You can <a href=":create_vpc_link">create a VPC</a>.',
[':create_vpc_link' => Url::fromRoute('entity.aws_cloud_vpc.add_form', ['cloud_context' => $cloud_context])->toString()]));
}
$vpcs[$entity->getVpcId()] = 'N/A';
ksort($vpcs);
$form['security_group']['vpc_id'] = [
......@@ -48,7 +54,7 @@ class SecurityGroupCreateForm extends AwsCloudContentForm {
'#title' => $this->t('VPC CIDR (ID)'),
'#options' => $vpcs,
'#default_value' => $entity->getVpcId(),
'#required' => FALSE,
'#required' => TRUE,
];
$form['security_group']['description'] = [
......
......@@ -53,6 +53,8 @@ class SecurityGroupTest extends AwsCloudTestCase {
public function testSecurityGroup() {
$cloud_context = $this->cloudContext;
$this->deleteVpcMockData(0);
// List Security Group for Amazon EC2.
$this->drupalGet("/clouds/aws_cloud/$cloud_context/security_group");
$this->assertResponse(200, t('List | HTTP 200: Security Group'));
......@@ -60,14 +62,29 @@ class SecurityGroupTest extends AwsCloudTestCase {
$this->assertNoText(t('warning'), t('List | Make sure w/o Warnings'));
$this->drupalGet("/clouds/aws_cloud/$cloud_context/security_group/add");
$this->assertText(t('You do not have any VPCs. You need a VPC in order to create a security group. You can create a VPC.'));
// Add a new Security Group.
$add = $this->createSecurityGroupTestFormData(self::AWS_CLOUD_SECURITY_GROUP_REPEAT_COUNT);
$addVpc = $this->createVpcTestFormData(self::AWS_CLOUD_SECURITY_GROUP_REPEAT_COUNT);
for ($i = 0; $i < self::AWS_CLOUD_SECURITY_GROUP_REPEAT_COUNT; $i++) {
$this->reloadMockData();
$num = $i + 1;
$this->drupalPostForm("/clouds/aws_cloud/$cloud_context/security_group/add",
$add[$i],
t('Save'));
$this->assertText(t('VPC CIDR (ID) field is required.'), t('VPC CIDR is required test.'));
$defaults = $this->latestTemplateVars;
$add[$i]['vpc_id'] = $defaults['vpc_id'];
// Create VPC.
$this->addVpcMockData($addVpc[$i], $add[$i]['vpc_id']);
$this->drupalPostForm("/clouds/aws_cloud/$cloud_context/security_group/add",
$add[$i],
t('Save'));
......@@ -486,36 +503,6 @@ class SecurityGroupTest extends AwsCloudTestCase {
$this->assertText(t('From Port is greater than To Port.'), t('From port greater than to port test'));
}
// Validate errors related to non-vpc.
$add = $this->createSecurityGroupTestFormData(self::AWS_CLOUD_SECURITY_GROUP_REPEAT_COUNT);
for ($i = 0; $i < $max_test_repeat_count; $i++) {
$num = $i + 1 + $max_test_repeat_count;
$this->reloadMockData();
$defaults = $this->latestTemplateVars;
$defaults['group_name'] = $add[$i]['group_name'];
$add[$i]['vpc_id'] = '';
$this->drupalGet("/clouds/aws_cloud/$cloud_context/security_group/add");
$this->drupalPostForm("/clouds/aws_cloud/$cloud_context/security_group/add",
$add[$i],
t('Save'));
// After save, assert the save is successful.
$this->assertResponse(200, t('Add | HTTP 200: A New AWS Cloud Security Group', ['@num' => $num]));
$this->assertNoText(t('Notice'), t('Add | Make sure w/o Notice'));
$this->assertNoText(t('warning'), t('Add | Make sure w/o Warnings'));
$this->assertText($add[$i]['group_name'], t('Add | Key Pair: @group_name', ['@group_name' => $add[$i]['group_name']]));
$this->assertText(
t('The AWS Cloud Security Group "@group_name" has been created.', [
'@group_name' => $add[$i]['group_name'],
]),
t('Confirm Message: Add | The AWS Cloud Security Group "@group_name" has been created.', [
'@group_name' => $add[$i]['group_name'],
]));
}
}
/**
......@@ -859,6 +846,34 @@ class SecurityGroupTest extends AwsCloudTestCase {
}
/**
* Test for copying secuirty groups without vpc.
*/
public function testSecurityGroupCopyNoVpc() {
$cloud_context = $this->cloudContext;
// Delete init mock data.
$this->deleteFirstSecurityGroupMockData();
$this->deleteVpcMockData(0);
$add = $this->createSecurityGroupTestFormData(self::AWS_CLOUD_SECURITY_GROUP_REPEAT_COUNT);
for ($i = 0; $i < self::AWS_CLOUD_SECURITY_GROUP_REPEAT_COUNT; $i++) {
$num = $i + 1;
// Add mock data.
$add[$i]['group_id'] = $this->addSecurityGroupMockData($add[$i]['group_name'], $add[$i]['description']);
// Create entity.
$security_group = $this->createSecurityGroupTestEntity($i, $add[$i]['group_id'], $add[$i]['group_name'], '', $cloud_context);
$security_group->set('description', $add[$i]['description']);
$security_group->save();
$this->drupalGet("/clouds/aws_cloud/$cloud_context/security_group/$num/copy");
$this->assertText(t('You do not have any VPCs. You need a VPC in order to create a security group. You can create a VPC.'));
}
}
/**
* Create rule parameters.
*
......
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