Ver Fonte

ZF-10610: fixed the endpoint of the Amazon Sqs region

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@24469 44c647ce-9c0f-0410-b52a-842ac1e357ba
ezimuel há 14 anos atrás
pai
commit
764a4be81f

+ 77 - 0
library/Zend/Service/Amazon/Sqs.php

@@ -67,9 +67,17 @@ class Zend_Service_Amazon_Sqs extends Zend_Service_Amazon_Abstract
      */
     protected $_sqsSignatureMethod = 'HmacSHA256';
 
+    protected $_sqsEndpoints = array('us-east-1' => 'sqs.us-east-1.amazonaws.com',
+                                     'us-west-1' => 'sqs.us-west-1.amazonaws.com',
+                                     'eu-west-1' => 'sqs.eu-west-1.amazonaws.com',
+                                     'ap-southeast-1' => 'sqs.ap-southeast-1.amazonaws.com',
+                                     'ap-northeast-1' => 'sqs.ap-northeast-1.amazonaws.com');
     /**
      * Constructor
      *
+     * The default region is us-east-1. Use the region to set it to one of the regions that is build-in into ZF.
+     * To add a new AWS region use the setEndpoint() method.
+     *
      * @param string $accessKey
      * @param string $secretKey
      * @param string $region
@@ -77,9 +85,78 @@ class Zend_Service_Amazon_Sqs extends Zend_Service_Amazon_Abstract
     public function __construct($accessKey = null, $secretKey = null, $region = null)
     {
         parent::__construct($accessKey, $secretKey, $region);
+        
+        if (null !== $region) {
+            $this->_setEndpoint($region);
+        }
     }
 
     /**
+     * Set SQS endpoint
+     *
+     * Checks and sets endpoint if region exists in $_sqsEndpoints. If a new SQS region is added by amazon,
+     * please use the setEndpoint function to set it.
+     *
+     * @param  string  $region region
+     * @throws Zend_Service_Amazon_Sqs_Exception
+     */
+    protected function _setEndpoint($region)
+    {
+        if (array_key_exists($region, $this->_sqsEndpoints)) {
+            $this->_sqsEndpoint = $this->_sqsEndpoints[$region];
+        } else {
+            throw new Zend_Service_Amazon_Sqs_Exception('Invalid SQS region specified.');
+        }
+    }
+    
+    /**
+     * Set SQS endpoint
+     *
+     * You can set SQS to on of the build-in regions. If the region does not exsist it will be added.
+     *
+     * @param  string  $region region
+     * @throws Zend_Service_Amazon_Sqs_Exception
+     */
+    public function setEndpoint($region)
+    {
+        if (!empty($region)) {
+            if (array_key_exists($region, $this->_sqsEndpoints)) {
+                $this->_sqsEndpoint = $this->_sqsEndpoints[$region];
+            } else {
+                $this->_sqsEndpoints[$region] = "sqs.$region.amazonaws.com";
+                $this->_sqsEndpoint = $this->_sqsEndpoints[$region];
+            }
+        } else {
+            throw new Zend_Service_Amazon_Sqs_Exception('Empty region specified.');
+        }
+    }
+
+    /**
+     * Get the SQS endpoint
+     * 
+     * @return string 
+     */
+    public function getEndpoint()
+    {
+        return $this->_sqsEndpoint;
+    }
+
+    /**
+     * Get possible SQS endpoints
+     *
+     * Since there is not an SQS webserive to get all possible endpoints, a hardcoded list is available.
+     * For the actual region list please check:
+     * http://docs.amazonwebservices.com/AWSSimpleQueueService/2009-02-01/APIReference/index.html?QueueServiceWsdlArticle.html
+     *
+     * @param  string  $region region
+     * @return array
+     */
+    public function getEndpoints()
+    {
+        return $this->_sqsEndpoints;
+    }
+    
+    /**
      * Create a new queue
      *
      * Visibility timeout is how long a message is left in the queue "invisible"

+ 55 - 2
tests/Zend/Service/Amazon/Sqs/OfflineTest.php

@@ -20,6 +20,10 @@
  * @version    $Id$
  */
 
+require_once 'Zend/Service/Amazon/Sqs.php';
+require_once 'Zend/Service/Amazon/Sqs/Exception.php';
+require_once 'Zend/Http/Client/Adapter/Test.php';
+
 /**
  * @category   Zend
  * @package    Zend_Service_Amazon
@@ -32,12 +36,61 @@
  */
 class Zend_Service_Amazon_Sqs_OfflineTest extends PHPUnit_Framework_TestCase
 {
+    /**
+     * Reference to Amazon service consumer object
+     *
+     * @var Zend_Service_Amazon_Sqs
+     */
+    protected $_amazon;
+    
+    /**
+     * Test based HTTP client adapter
+     *
+     * @var Zend_Http_Client_Adapter_Test
+     */
+    protected $_httpClientAdapterTest;
+    
     public function setUp()
     {
-        $this->markTestSkipped('No offline tests for Zend_Service_Amazon_Sqs');
+        //$this->markTestSkipped('No offline tests for Zend_Service_Amazon_Sqs');
+        
+        $this->_amazon= new Zend_Service_Amazon_Sqs('test','test');
+        
+        $this->_httpClientAdapterTest = new Zend_Http_Client_Adapter_Test();
+
+        $this->_amazon->getHttpClient()
+                      ->setAdapter($this->_httpClientAdapterTest);
     }
 
-    public function testNothing()
+    public function testSetRegion()
+    {
+        $this->_amazon->setEndpoint('eu-west-1');
+        $endPoints= $this->_amazon->getEndpoints();
+        $this->assertEquals($this->_amazon->getEndpoint(),$endPoints['eu-west-1']);
+    }
+    
+    public function testSetNewRegion()
+    {
+        $this->_amazon->setEndpoint('foo');
+        $this->assertEquals($this->_amazon->getEndpoint(),'sqs.foo.amazonaws.com');
+    }
+    
+    public function testSetEmptyRegion()
+    {
+         $this->setExpectedException(
+            'Zend_Service_Amazon_Sqs_Exception',
+            'Empty region specified.'
+        );
+        $this->_amazon->setEndpoint('');
+    }
+    
+    public function testGetRegions()
     {
+        $endPoints= array('us-east-1' => 'sqs.us-east-1.amazonaws.com',
+                                     'us-west-1' => 'sqs.us-west-1.amazonaws.com',
+                                     'eu-west-1' => 'sqs.eu-west-1.amazonaws.com',
+                                     'ap-southeast-1' => 'sqs.ap-southeast-1.amazonaws.com',
+                                     'ap-northeast-1' => 'sqs.ap-northeast-1.amazonaws.com');
+        $this->assertEquals($this->_amazon->getEndpoints(),$endPoints);
     }
 }