cloud-8.x-2.0-beta1/modules/cloud_service_providers/aws_cloud/src/Form/Ec2/SecurityGroupCreateForm.php
modules/cloud_service_providers/aws_cloud/src/Form/Ec2/SecurityGroupCreateForm.php
<?php
namespace Drupal\aws_cloud\Form\Ec2;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\aws_cloud\Entity\Ec2\SecurityGroup;
/**
* Form controller for the SecurityGroup entity create form.
*
* @ingroup aws_cloud
*/
class SecurityGroupCreateForm extends AwsCloudContentForm {
/**
* Overrides Drupal\Core\Entity\EntityFormController::buildForm().
*/
public function buildForm(array $form, FormStateInterface $form_state, $cloud_context = '') {
$this->ec2Service->setCloudContext($cloud_context);
/* @var $entity \Drupal\aws_cloud\Entity\Ec2\SecurityGroup */
$form = parent::buildForm($form, $form_state);
$entity = $this->entity;
$weight = -50;
$form['security_group'] = [
'#type' => 'details',
'#title' => $this->t('AWS Cloud Security Group'),
'#open' => TRUE,
'#weight' => $weight++,
];
$form['security_group']['group_name'] = [
'#type' => 'textfield',
'#title' => $this->t('Security Group Name'),
'#size' => 60,
'#default_value' => $entity->getGroupName(),
'#required' => TRUE,
];
$vpcs = $this->ec2Service->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'] = [
'#type' => 'select',
'#title' => $this->t('VPC CIDR (ID)'),
'#options' => $vpcs,
'#default_value' => $entity->getVpcId(),
'#required' => TRUE,
];
$form['security_group']['description'] = [
'#type' => 'textarea',
'#title' => $this->t('Description'),
'#cols' => 60,
'#rows' => 3,
'#default_value' => $entity->getDescription(),
'#required' => TRUE,
];
$this->addOthersFieldset($form, $weight++, $cloud_context);
// Unset these until and present them on the edit security group form.
unset($form['ip_permission']);
unset($form['outbound_permission']);
if (isset($form['actions'])) {
$form['actions']['submit']['#weight'] = $weight++;
}
return $form;
}
/**
* Overrides Drupal\Core\Entity\EntityFormController::save().
*/
public function save(array $form, FormStateInterface $form_state) {
$this->trimTextfields($form, $form_state);
$entity = $this->entity;
$result = $this->ec2Service->createSecurityGroup([
'GroupName' => $entity->getGroupName(),
'VpcId' => $entity->getVpcId(),
'Description' => $entity->getDescription(),
]);
if (isset($result['GroupId'])
&& ($entity->setGroupId($result['GroupId']))
&& ($entity->set('name', $entity->getGroupName()))
&& ($entity->save())) {
$this->setTagsInAws($entity->getGroupId(), [
SecurityGroup::TAG_CREATED_BY_UID => $entity->getOwner()->id(),
'Name' => $entity->getName(),
]);
$message = $this->t('The @label "@group_name" has been created. Please setup the IP permissions', [
'@label' => $entity->getEntityType()->getLabel(),
'@group_name' => $entity->getGroupName(),
]);
$form_state->setRedirect('entity.aws_cloud_security_group.edit_form', [
'cloud_context' => $entity->getCloudContext(),
'aws_cloud_security_group' => $entity->id(),
]);
$this->messenger->addMessage($message);
}
else {
$message = $this->t('The @label "@group_name" couldn\'t create.', [
'@label' => $entity->getEntityType()->getLabel(),
'@group_name' => $entity->getGroupName(),
]);
$this->messenger->addError($message);
}
}
}
