Commit 76d37268 authored by xiaohua guan's avatar xiaohua guan Committed by Yas Naoi
Browse files

Issue #3273644 by Xiaohua Guan, yas: Fix the error "Invalid value for port 0"...

Issue #3273644 by Xiaohua Guan, yas: Fix the error "Invalid value for port 0" when updating OpenStack security group
parent af8793dd
Loading
Loading
Loading
Loading
+34 −1
Original line number Diff line number Diff line
@@ -26,6 +26,10 @@ class IpPermissionItem extends WidgetBase {
   * {@inheritdoc}
   */
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state): array {
    if (empty($items[$delta])) {
      return $element;
    }

    /** @var \Drupal\Core\Entity\Plugin\DataType\EntityAdapter $parent */
    $parent = $items->getParent();
    $security_group = $parent->getValue();
@@ -78,8 +82,9 @@ class IpPermissionItem extends WidgetBase {
      '#size' => 5,
      '#default_value' => $items[$delta]->from_port ?? NULL,
      '#maxlength' => 5,
      '#placeholder' => 0,
      '#placeholder' => $this->getSetting('from_port_placeholder'),
    ];

    $element['to_port'] = [
      '#type' => 'textfield',
      '#title' => $this->t('To Port'),
@@ -88,6 +93,7 @@ class IpPermissionItem extends WidgetBase {
      '#maxlength' => 5,
      '#placeholder' => 65535,
    ];

    $element['source'] = [
      '#type' => 'select',
      '#title' => $this->t('Source'),
@@ -100,6 +106,7 @@ class IpPermissionItem extends WidgetBase {
      ],
      '#default_value' => $items[$delta]->source ?? NULL,
    ];

    $element['cidr_ip'] = [
      '#type' => 'textfield',
      '#title' => $this->t('CIDR IP'),
@@ -113,6 +120,7 @@ class IpPermissionItem extends WidgetBase {
        ],
      ],
    ];

    $element['cidr_ip_v6'] = [
      '#type' => 'textfield',
      '#title' => $this->t('CIDR IPv6'),
@@ -139,6 +147,7 @@ class IpPermissionItem extends WidgetBase {
        ],
      ];
    }

    $element['group_id'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Group ID'),
@@ -151,6 +160,7 @@ class IpPermissionItem extends WidgetBase {
        ],
      ],
    ];

    if (isset($items[$delta]->group_name)) {
      $element['group_name'] = [
        '#type' => 'textfield',
@@ -166,6 +176,7 @@ class IpPermissionItem extends WidgetBase {
        ],
      ];
    }

    $element['peering_status'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Peering Status'),
@@ -227,4 +238,26 @@ class IpPermissionItem extends WidgetBase {
    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public static function defaultSettings(): array {
    return [
      'from_port_placeholder' => '0',
    ] + parent::defaultSettings();
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state): array {
    $element['from_port_placeholder'] = [
      '#title' => $this->t('From Port placeholder'),
      '#type' => 'textfield',
      '#default_value' => $this->getSetting('from_port_placeholder'),
    ];

    return $element;
  }

}
+24 −0
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ namespace Drupal\openstack\Entity;

use Drupal\aws_cloud\Entity\Ec2\SecurityGroup;
use Drupal\aws_cloud\Entity\Ec2\SecurityGroupInterface;
use Drupal\Core\Entity\EntityTypeInterface;

/**
 * Defines the OpenStack security group entity.
@@ -56,4 +57,27 @@ use Drupal\aws_cloud\Entity\Ec2\SecurityGroupInterface;
 */
class OpenStackSecurityGroup extends SecurityGroup implements SecurityGroupInterface {

  /**
   * {@inheritdoc}
   */
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type): array {
    $fields = parent::baseFieldDefinitions($entity_type);

    $fields['ip_permission']
      ->setDisplayOptions('form', [
        'settings' => [
          'from_port_placeholder' => '1',
        ],
      ]);

    $fields['outbound_permission']
      ->setDisplayOptions('form', [
        'settings' => [
          'from_port_placeholder' => '1',
        ],
      ]);

    return $fields;
  }

}
+16 −11
Original line number Diff line number Diff line
@@ -1785,7 +1785,12 @@ class OpenStackService extends CloudServiceBase implements OpenStackServiceInter

      $ip_permissions = $ip_permissions_egress = [];

      foreach ($value['security_group_rules'] ?: [] as $value_rules) {
      $security_group_rules = [];
      if (!empty($value['security_group_rules'])) {
        $security_group_rules = $value['security_group_rules'];
      }

      foreach ($security_group_rules ?: [] as $value_rules) {
        if (!empty($value_rules['direction']) && $value_rules['direction'] === 'egress') {

          $ip_permissions_egress[$ip_permission_egress_cnt] = [
@@ -1812,8 +1817,8 @@ class OpenStackService extends CloudServiceBase implements OpenStackServiceInter
            $ip_permissions_egress[$ip_permission_egress_cnt] += [
              'Ipv6Ranges' => [
                [
                  'CidrIpv6'    => $value_rules['remote_ip_prefix'] ?? NULL,
                  'Description' => $value_rules['description'] ?? NULL,
                  'CidrIpv6'    => !empty($value_rules['remote_ip_prefix']) ? $value_rules['remote_ip_prefix'] : '::/0',
                  'Description' => !empty($value_rules['description']) ? $value_rules['description'] : '',
                ],
              ],
            ];
@@ -1825,8 +1830,8 @@ class OpenStackService extends CloudServiceBase implements OpenStackServiceInter
            $ip_permissions_egress[$ip_permission_egress_cnt] += [
              'IpRanges' => [
                [
                  'CidrIp'      => $value_rules['remote_ip_prefix'] ?? NULL,
                  'Description' => $value_rules['description'] ?? NULL,
                  'CidrIp'      => !empty($value_rules['remote_ip_prefix']) ? $value_rules['remote_ip_prefix'] : '0.0.0.0/0',
                  'Description' => !empty($value_rules['description']) ? $value_rules['description'] : '',
                ],
              ],
            ];
@@ -1860,8 +1865,8 @@ class OpenStackService extends CloudServiceBase implements OpenStackServiceInter
            $ip_permissions[$ip_permission_cnt] += [
              'Ipv6Ranges' => [
                [
                  'CidrIpv6'    => $value_rules['remote_ip_prefix'] ?? NULL,
                  'Description' => $value_rules['description'] ?? NULL,
                  'CidrIpv6'    => !empty($value_rules['remote_ip_prefix']) ? $value_rules['remote_ip_prefix'] : '::/0',
                  'Description' => !empty($value_rules['description']) ? $value_rules['description'] : '',
                ],
              ],
            ];
@@ -1872,8 +1877,8 @@ class OpenStackService extends CloudServiceBase implements OpenStackServiceInter
            $ip_permissions[$ip_permission_cnt] += [
              'IpRanges' => [
                [
                  'CidrIp'      => $value_rules['remote_ip_prefix'] ?? NULL,
                  'Description' => $value_rules['description'] ?? NULL,
                  'CidrIp'      => !empty($value_rules['remote_ip_prefix']) ? $value_rules['remote_ip_prefix'] : '0.0.0.0/0',
                  'Description' => !empty($value_rules['description']) ? $value_rules['description'] : '',
                ],
              ],
            ];
@@ -3785,9 +3790,9 @@ class OpenStackService extends CloudServiceBase implements OpenStackServiceInter
    $ip_protocol = $ec2_permission['IpProtocol'];

    // To keep things consistent, if ip_protocol is -1,
    // set from_port and to_port as 0-65535.
    // set from_port and to_port as 1-65535.
    if ($ip_protocol === '-1') {
      $from_port = '0';
      $from_port = '1';
      $to_port = '65535';
    }