Jelajahi Sumber

ZF-7832: Fix to have the security group describe return all the ip's related to a port in said security group.

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@18095 44c647ce-9c0f-0410-b52a-842ac1e357ba
sidhighwind 16 tahun lalu
induk
melakukan
3816ee99a8

+ 11 - 1
library/Zend/Service/Amazon/Ec2/Securitygroups.php

@@ -109,7 +109,17 @@ class Zend_Service_Amazon_Ec2_Securitygroups extends Zend_Service_Amazon_Ec2_Abs
                 $sItem['ipProtocol'] = $xpath->evaluate('string(ec2:ipProtocol/text())', $ip_node);
                 $sItem['fromPort'] = $xpath->evaluate('string(ec2:fromPort/text())', $ip_node);
                 $sItem['toPort'] = $xpath->evaluate('string(ec2:toPort/text())', $ip_node);
-                $sItem['ipRanges'] = $xpath->evaluate('string(ec2:ipRanges/ec2:item/ec2:cidrIp/text())', $ip_node);
+
+                $ips = $xpath->query('ec2:ipRanges/ec2:item', $ip_node);
+
+                $sItem['ipRanges'] = array();
+                foreach($ips as $ip) {
+                    $sItem['ipRanges'][] = $xpath->evaluate('string(ec2:cidrIp/text())', $ip);
+                }
+
+                if(count($sItem['ipRanges']) == 1) {
+                    $sItem['ipRanges'] = $sItem['ipRanges'][0];
+                }
 
                 $item['ipPermissions'][] = $sItem;
                 unset($ip_node, $sItem);

+ 72 - 9
tests/Zend/Service/Amazon/Ec2/SecuritygroupsTest.php

@@ -305,15 +305,15 @@ class Zend_Service_Amazon_Ec2_SecuritygroupsTest extends PHPUnit_Framework_TestC
                     . "      <groupDescription>Web</groupDescription>\r\n"
                     . "      <ipPermissions>\r\n"
                     . "        <item>\r\n"
-                    . "       <ipProtocol>tcp</ipProtocol>\r\n"
-                    . "   <fromPort>80</fromPort>\r\n"
-                    . "   <toPort>80</toPort>\r\n"
-                    . "   <groups/>\r\n"
-                    . "   <ipRanges>\r\n"
-                    . "     <item>\r\n"
-                    . "       <cidrIp>0.0.0.0/0</cidrIp>\r\n"
-                    . "     </item>\r\n"
-                    . "   </ipRanges>\r\n"
+                    . "         <ipProtocol>tcp</ipProtocol>\r\n"
+                    . "          <fromPort>80</fromPort>\r\n"
+                    . "          <toPort>80</toPort>\r\n"
+                    . "          <groups/>\r\n"
+                    . "          <ipRanges>\r\n"
+                    . "            <item>\r\n"
+                    . "              <cidrIp>0.0.0.0/0</cidrIp>\r\n"
+                    . "            </item>\r\n"
+                    . "          </ipRanges>\r\n"
                     . "         </item>\r\n"
                     . "      </ipPermissions>\r\n"
                     . "    </item>\r\n"
@@ -343,6 +343,69 @@ class Zend_Service_Amazon_Ec2_SecuritygroupsTest extends PHPUnit_Framework_TestC
         }
     }
 
+    public function testDescribeSingleSecruityGroupWithMultipleIpsSamePort()
+    {
+        $rawHttpResponse = "HTTP/1.1 200 OK\r\n"
+                    . "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
+                    . "Server: hi\r\n"
+                    . "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
+                    . "Status: 200 OK\r\n"
+                    . "Content-type: application/xml; charset=utf-8\r\n"
+                    . "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
+                    . "Connection: close\r\n"
+                    . "\r\n"
+                    . "<DescribeSecurityGroupsResponse xmlns=\"http://ec2.amazonaws.com/doc/2009-04-04/\">\r\n"
+                    . "  <securityGroupInfo>\r\n"
+                    . "    <item>\r\n"
+                    . "      <ownerId>UYY3TLBUXIEON5NQVUUX6OMPWBZIQNFM</ownerId>\r\n"
+                    . "      <groupName>WebServers</groupName>\r\n"
+                    . "      <groupDescription>Web</groupDescription>\r\n"
+                    . "      <ipPermissions>\r\n"
+                    . "        <item>\r\n"
+                    . "         <ipProtocol>tcp</ipProtocol>\r\n"
+                    . "          <fromPort>80</fromPort>\r\n"
+                    . "          <toPort>80</toPort>\r\n"
+                    . "          <groups/>\r\n"
+                    . "          <ipRanges>\r\n"
+                    . "            <item>\r\n"
+                    . "              <cidrIp>0.0.0.0/0</cidrIp>\r\n"
+                    . "            </item>\r\n"
+                    . "            <item>\r\n"
+                    . "              <cidrIp>1.1.1.1/0</cidrIp>\r\n"
+                    . "            </item>\r\n"
+                    . "          </ipRanges>\r\n"
+                    . "         </item>\r\n"
+                    . "      </ipPermissions>\r\n"
+                    . "    </item>\r\n"
+                    . "  </securityGroupInfo>\r\n"
+                    . "</DescribeSecurityGroupsResponse>\r\n";
+        $this->adapter->setResponse($rawHttpResponse);
+
+        $return = $this->Zend_Service_Amazon_Ec2_Securitygroups->describe('WebServers');
+
+        $this->assertEquals(1, count($return));
+
+        $arrGroups = array(
+                array(
+                    'ownerId'   => 'UYY3TLBUXIEON5NQVUUX6OMPWBZIQNFM',
+                    'groupName' => 'WebServers',
+                    'groupDescription' => 'Web',
+                    'ipPermissions' => array(0 => array(
+                        'ipProtocol' => 'tcp',
+                        'fromPort'  => '80',
+                        'toPort'    => '80',
+                        'ipRanges'  => array(
+                        	'0.0.0.0/0',
+                            '1.1.1.1/0'
+                            )
+                    ))
+                )
+            );
+        foreach($return as $k => $r) {
+            $this->assertSame($arrGroups[$k], $r);
+        }
+    }
+
     /**
      * Tests Zend_Service_Amazon_Ec2_Securitygroups->revoke()
      */