PageRenderTime 10ms CodeModel.GetById 80ms app.highlight 113ms RepoModel.GetById 1ms app.codeStats 1ms

/Zend/Service/WindowsAzure/Management/Client.php

http://grupal.googlecode.com/
PHP | 2423 lines | 1465 code | 263 blank | 695 comment | 492 complexity | 77d2037f8277216f53ee3c19ec793e90 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1<?php
   2/**
   3 * Zend Framework
   4 *
   5 * LICENSE
   6 *
   7 * This source file is subject to the new BSD license that is bundled
   8 * with this package in the file LICENSE.txt.
   9 * It is also available through the world-wide-web at this URL:
  10 * http://framework.zend.com/license/new-bsd
  11 * If you did not receive a copy of the license and are unable to
  12 * obtain it through the world-wide-web, please send an email
  13 * to license@zend.com so we can send you a copy immediately.
  14 *
  15 * @category   Zend
  16 * @package    Zend_Service_WindowsAzure
  17 * @subpackage Management
  18 * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19 * @license    http://framework.zend.com/license/new-bsd     New BSD License
  20 * @version    $Id$
  21 */
  22
  23/**
  24 * @see Zend_Service_WindowsAzure_Management_OperationStatusInstance
  25 */
  26require_once 'Zend/Service/WindowsAzure/Management/OperationStatusInstance.php';
  27
  28/**
  29 * @see Zend_Service_WindowsAzure_Management_SubscriptionOperationInstance
  30 */
  31require_once 'Zend/Service/WindowsAzure/Management/SubscriptionOperationInstance.php';
  32
  33/**
  34 * @see Zend_Service_WindowsAzure_Management_DeploymentInstance
  35 */
  36require_once 'Zend/Service/WindowsAzure/Management/DeploymentInstance.php';
  37
  38/**
  39 * @see Zend_Service_WindowsAzure_Storage_Blob
  40 */
  41require_once 'Zend/Service/WindowsAzure/Storage/Blob.php';
  42
  43/**
  44 * @see Zend_Service_WindowsAzure_Storage_Table
  45 */
  46require_once 'Zend/Service/WindowsAzure/Storage/Table.php';
  47
  48/**
  49 * @see Zend_Service_WindowsAzure_Management_HostedServiceInstance
  50 */
  51require_once 'Zend/Service/WindowsAzure/Management/HostedServiceInstance.php';
  52
  53/**
  54 * @see Zend_Service_WindowsAzure_Management_CertificateInstance
  55 */
  56require_once 'Zend/Service/WindowsAzure/Management/CertificateInstance.php';
  57
  58/**
  59 * @see Zend_Service_WindowsAzure_Management_AffinityGroupInstance
  60 */
  61require_once 'Zend/Service/WindowsAzure/Management/AffinityGroupInstance.php';
  62
  63/**
  64 * @see Zend_Service_WindowsAzure_Management_LocationInstance
  65 */
  66require_once 'Zend/Service/WindowsAzure/Management/LocationInstance.php';
  67
  68/**
  69 * @see Zend_Service_WindowsAzure_Management_OperatingSystemInstance
  70 */
  71require_once 'Zend/Service/WindowsAzure/Management/OperatingSystemInstance.php';
  72
  73/**
  74 * @see Zend_Service_WindowsAzure_Management_OperatingSystemFamilyInstance
  75 */
  76require_once 'Zend/Service/WindowsAzure/Management/OperatingSystemFamilyInstance.php';
  77
  78/**
  79 * @category   Zend
  80 * @package    Zend_Service_WindowsAzure
  81 * @subpackage Management
  82 * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  83 * @license    http://framework.zend.com/license/new-bsd     New BSD License
  84 */
  85class Zend_Service_WindowsAzure_Management_Client
  86{
  87	/**
  88	 * Management service URL
  89	 */
  90	const URL_MANAGEMENT        = "https://management.core.windows.net";
  91	
  92	/**
  93	 * Operations
  94	 */
  95	const OP_OPERATIONS                = "operations";
  96	const OP_STORAGE_ACCOUNTS          = "services/storageservices";
  97	const OP_HOSTED_SERVICES           = "services/hostedservices";
  98	const OP_AFFINITYGROUPS            = "affinitygroups";
  99	const OP_LOCATIONS                 = "locations";
 100	const OP_OPERATINGSYSTEMS          = "operatingsystems";
 101	const OP_OPERATINGSYSTEMFAMILIES   = "operatingsystemfamilies";
 102
 103	/**
 104	 * Current API version
 105	 * 
 106	 * @var string
 107	 */
 108	protected $_apiVersion = '2011-02-25';
 109	
 110	/**
 111	 * Subscription ID
 112	 *
 113	 * @var string
 114	 */
 115	protected $_subscriptionId = '';
 116	
 117	/**
 118	 * Management certificate path (.PEM)
 119	 *
 120	 * @var string
 121	 */
 122	protected $_certificatePath = '';
 123	
 124	/**
 125	 * Management certificate passphrase
 126	 *
 127	 * @var string
 128	 */
 129	protected $_certificatePassphrase = '';
 130	
 131	/**
 132	 * Zend_Http_Client channel used for communication with REST services
 133	 * 
 134	 * @var Zend_Http_Client
 135	 */
 136	protected $_httpClientChannel = null;	
 137
 138	/**
 139	 * Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract instance
 140	 * 
 141	 * @var Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract
 142	 */
 143	protected $_retryPolicy = null;
 144	
 145	/**
 146	 * Returns the last request ID
 147	 * 
 148	 * @var string
 149	 */
 150	protected $_lastRequestId = null;
 151	
 152	/**
 153	 * Creates a new Zend_Service_WindowsAzure_Management instance
 154	 * 
 155	 * @param string $subscriptionId Subscription ID
 156	 * @param string $certificatePath Management certificate path (.PEM)
 157	 * @param string $certificatePassphrase Management certificate passphrase
 158     * @param Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy Retry policy to use when making requests
 159	 */
 160	public function __construct(
 161		$subscriptionId,
 162		$certificatePath,
 163		$certificatePassphrase,
 164		Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy = null
 165	) {
 166		$this->_subscriptionId = $subscriptionId;
 167		$this->_certificatePath = $certificatePath;
 168		$this->_certificatePassphrase = $certificatePassphrase;
 169		
 170		$this->_retryPolicy = $retryPolicy;
 171		if (is_null($this->_retryPolicy)) {
 172		    $this->_retryPolicy = Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract::noRetry();
 173		}
 174		
 175		// Setup default Zend_Http_Client channel
 176		$options = array(
 177		    'adapter'       => 'Zend_Http_Client_Adapter_Socket',
 178		    'ssltransport'  => 'ssl',
 179			'sslcert'       => $this->_certificatePath,
 180			'sslpassphrase' => $this->_certificatePassphrase,
 181			'sslusecontext' => true,
 182		);
 183		if (function_exists('curl_init')) {
 184			// Set cURL options if cURL is used afterwards
 185			$options['curloptions'] = array(
 186					CURLOPT_FOLLOWLOCATION => true,
 187					CURLOPT_TIMEOUT => 120,
 188			);
 189		}
 190		$this->_httpClientChannel = new Zend_Http_Client(null, $options);
 191	}
 192	
 193	/**
 194	 * Set the HTTP client channel to use
 195	 * 
 196	 * @param Zend_Http_Client_Adapter_Interface|string $adapterInstance Adapter instance or adapter class name.
 197	 */
 198	public function setHttpClientChannel($adapterInstance = 'Zend_Http_Client_Adapter_Socket')
 199	{
 200		$this->_httpClientChannel->setAdapter($adapterInstance);
 201	}
 202	
 203    /**
 204     * Retrieve HTTP client channel
 205     * 
 206     * @return Zend_Http_Client_Adapter_Interface
 207     */
 208    public function getHttpClientChannel()
 209    {
 210        return $this->_httpClientChannel;
 211    }
 212	
 213	/**
 214	 * Returns the Windows Azure subscription ID
 215	 * 
 216	 * @return string
 217	 */
 218	public function getSubscriptionId()
 219	{
 220		return $this->_subscriptionId;
 221	}
 222	
 223	/**
 224	 * Returns the last request ID.
 225	 * 
 226	 * @return string
 227	 */
 228	public function getLastRequestId()
 229	{
 230		return $this->_lastRequestId;
 231	}
 232	
 233	/**
 234	 * Get base URL for creating requests
 235	 *
 236	 * @return string
 237	 */
 238	public function getBaseUrl()
 239	{
 240		return self::URL_MANAGEMENT . '/' . $this->_subscriptionId;
 241	}
 242	
 243	/**
 244	 * Perform request using Zend_Http_Client channel
 245	 *
 246	 * @param string $path Path
 247	 * @param string $queryString Query string
 248	 * @param string $httpVerb HTTP verb the request will use
 249	 * @param array $headers x-ms headers to add
 250	 * @param mixed $rawData Optional RAW HTTP data to be sent over the wire
 251	 * @return Zend_Http_Response
 252	 */
 253	protected function _performRequest(
 254		$path = '/',
 255		$queryString = '',
 256		$httpVerb = Zend_Http_Client::GET,
 257		$headers = array(),
 258		$rawData = null
 259	) {
 260	    // Clean path
 261		if (strpos($path, '/') !== 0) {
 262			$path = '/' . $path;
 263		}
 264			
 265		// Clean headers
 266		if (is_null($headers)) {
 267		    $headers = array();
 268		}
 269		
 270		// Ensure cUrl will also work correctly:
 271		//  - disable Content-Type if required
 272		//  - disable Expect: 100 Continue
 273		if (!isset($headers["Content-Type"])) {
 274			$headers["Content-Type"] = '';
 275		}
 276		//$headers["Expect"] = '';
 277
 278		// Add version header
 279		$headers['x-ms-version'] = $this->_apiVersion;
 280		    
 281		// URL encoding
 282		$path           = self::urlencode($path);
 283		$queryString    = self::urlencode($queryString);
 284
 285		// Generate URL and sign request
 286		$requestUrl     = $this->getBaseUrl() . $path . $queryString;
 287		$requestHeaders = $headers;
 288
 289		// Prepare request 
 290		$this->_httpClientChannel->resetParameters(true);
 291		$this->_httpClientChannel->setUri($requestUrl);
 292		$this->_httpClientChannel->setHeaders($requestHeaders);
 293		$this->_httpClientChannel->setRawData($rawData);
 294
 295		// Execute request
 296		$response = $this->_retryPolicy->execute(
 297		    array($this->_httpClientChannel, 'request'),
 298		    array($httpVerb)
 299		);
 300		
 301		// Store request id
 302		$this->_lastRequestId = $response->getHeader('x-ms-request-id');
 303		
 304		return $response;
 305	}
 306	
 307	/** 
 308	 * Parse result from Zend_Http_Response
 309	 *
 310	 * @param Zend_Http_Response $response Response from HTTP call
 311	 * @return object
 312	 * @throws Zend_Service_WindowsAzure_Exception
 313	 */
 314	protected function _parseResponse(Zend_Http_Response $response = null)
 315	{
 316		if (is_null($response)) {
 317			require_once 'Zend/Service/WindowsAzure/Exception.php';
 318			throw new Zend_Service_WindowsAzure_Exception('Response should not be null.');
 319		}
 320		
 321        $xml = @simplexml_load_string($response->getBody());
 322        
 323        if ($xml !== false) {
 324            // Fetch all namespaces 
 325            $namespaces = array_merge($xml->getNamespaces(true), $xml->getDocNamespaces(true)); 
 326            
 327            // Register all namespace prefixes
 328            foreach ($namespaces as $prefix => $ns) { 
 329                if ($prefix != '') {
 330                    $xml->registerXPathNamespace($prefix, $ns);
 331                } 
 332            } 
 333        }
 334        
 335        return $xml;
 336	}
 337	
 338	/**
 339	 * URL encode function
 340	 * 
 341	 * @param  string $value Value to encode
 342	 * @return string        Encoded value
 343	 */
 344	public static function urlencode($value)
 345	{
 346	    return str_replace(' ', '%20', $value);
 347	}
 348	
 349    /**
 350     * Builds a query string from an array of elements
 351     * 
 352     * @param array     Array of elements
 353     * @return string   Assembled query string
 354     */
 355    public static function createQueryStringFromArray($queryString)
 356    {
 357    	return count($queryString) > 0 ? '?' . implode('&', $queryString) : '';
 358    }
 359    
 360	/**
 361	 * Get error message from Zend_Http_Response
 362	 *
 363	 * @param Zend_Http_Response $response Repsonse
 364	 * @param string $alternativeError Alternative error message
 365	 * @return string
 366	 */
 367	protected function _getErrorMessage(Zend_Http_Response $response, $alternativeError = 'Unknown error.')
 368	{
 369		$response = $this->_parseResponse($response);
 370		if ($response && $response->Message) {
 371			return (string)$response->Message;
 372		} else {
 373			return $alternativeError;
 374		}
 375	}
 376    
 377    /**
 378     * The Get Operation Status operation returns the status of the specified operation.
 379     * After calling an asynchronous operation, you can call Get Operation Status to
 380     * determine whether the operation has succeed, failed, or is still in progress.
 381     *
 382     * @param string $requestId The request ID. If omitted, the last request ID will be used.
 383     * @return Zend_Service_WindowsAzure_Management_OperationStatusInstance
 384     * @throws Zend_Service_WindowsAzure_Management_Exception
 385     */
 386    public function getOperationStatus($requestId = '')
 387    {
 388    	if ($requestId == '') {
 389    		$requestId = $this->getLastRequestId();
 390    	}
 391    	
 392    	$response = $this->_performRequest(self::OP_OPERATIONS . '/' . $requestId);
 393
 394    	if ($response->isSuccessful()) {
 395			$result = $this->_parseResponse($response);
 396
 397			if (!is_null($result)) {
 398				return new Zend_Service_WindowsAzure_Management_OperationStatusInstance(
 399					(string)$result->ID,
 400					(string)$result->Status,
 401					($result->Error ? (string)$result->Error->Code : ''),
 402					($result->Error ? (string)$result->Error->Message : '')
 403				);
 404			}
 405			return null;
 406		} else {
 407			require_once 'Zend/Service/Management/Exception.php';
 408			throw new Zend_Service_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
 409		}
 410    }
 411    
 412
 413    
 414    /**
 415     * The List Subscription Operations operation returns a list of create, update,
 416     * and delete operations that were performed on a subscription during the specified timeframe.
 417     * Documentation on the parameters can be found at http://msdn.microsoft.com/en-us/library/gg715318.aspx.
 418     *
 419     * @param string $startTime The start of the timeframe to begin listing subscription operations in UTC format. This parameter and the $endTime parameter indicate the timeframe to retrieve subscription operations. This parameter cannot indicate a start date of more than 90 days in the past.
 420     * @param string $endTime The end of the timeframe to begin listing subscription operations in UTC format. This parameter and the $startTime parameter indicate the timeframe to retrieve subscription operations. 
 421     * @param string $objectIdFilter Returns subscription operations only for the specified object type and object ID. 
 422     * @param string $operationResultFilter Returns subscription operations only for the specified result status, either Succeeded, Failed, or InProgress.
 423     * @param string $continuationToken Internal usage.
 424     * @return array Array of Zend_Service_WindowsAzure_Management_SubscriptionOperationInstance
 425     * @throws Zend_Service_WindowsAzure_Management_Exception
 426     */
 427    public function listSubscriptionOperations($startTime, $endTime, $objectIdFilter = null, $operationResultFilter = null, $continuationToken = null)
 428    {
 429    	if ($startTime == '' || is_null($startTime)) {
 430			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
 431    		throw new Zend_Service_WindowsAzure_Management_Exception('Start time should be specified.');
 432    	}
 433    	if ($endTime == '' || is_null($endTime)) {
 434			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
 435    		throw new Zend_Service_WindowsAzure_Management_Exception('End time should be specified.');
 436    	}
 437    	if ($operationResultFilter != '' && !is_null($operationResultFilter)) {
 438	        $operationResultFilter = strtolower($operationResultFilter);
 439	    	if ($operationResultFilter != 'succeeded' && $operationResultFilter != 'failed' && $operationResultFilter != 'inprogress') {
 440				require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
 441	    		throw new Zend_Service_WindowsAzure_Management_Exception('OperationResultFilter should be succeeded|failed|inprogress.');
 442	    	}
 443    	}
 444    	
 445    	$parameters = array();
 446    	$parameters[] = 'StartTime=' . $startTime;
 447    	$parameters[] = 'EndTime=' . $endTime;
 448    	if ($objectIdFilter != '' && !is_null($objectIdFilter)) {
 449    		$parameters[] = 'ObjectIdFilter=' . $objectIdFilter;
 450    	}
 451    	if ($operationResultFilter != '' && !is_null($operationResultFilter)) {
 452    		$parameters[] = 'OperationResultFilter=' . ucfirst($operationResultFilter);
 453    	}
 454    	if ($continuationToken != '' && !is_null($continuationToken)) {
 455    		$parameters[] = 'ContinuationToken=' . $continuationToken;
 456    	}
 457    	
 458    	$response = $this->_performRequest(self::OP_OPERATIONS, '?' . implode('&', $parameters));
 459
 460    	if ($response->isSuccessful()) {
 461			$result = $this->_parseResponse($response);
 462			$namespaces = $result->getDocNamespaces(); 
 463    		$result->registerXPathNamespace('__empty_ns', $namespaces['']);
 464 
 465			$xmlOperations = $result->xpath('//__empty_ns:SubscriptionOperation');
 466			
 467		    // Create return value
 468		    $returnValue = array();		    
 469		    foreach ($xmlOperations as $xmlOperation) {
 470		    	// Create operation instance
 471		    	$operation = new Zend_Service_WindowsAzure_Management_SubscriptionOperationInstance(
 472		    		$xmlOperation->OperationId,
 473		    		$xmlOperation->OperationObjectId,
 474		    		$xmlOperation->OperationName,
 475		    		array(),
 476		    		(array)$xmlOperation->OperationCaller,
 477		    		(array)$xmlOperation->OperationStatus
 478		    	);
 479		    	
 480		    	// Parse parameters
 481		    	$xmlOperation->registerXPathNamespace('__empty_ns', $namespaces['']); 
 482		    	$xmlParameters = $xmlOperation->xpath('.//__empty_ns:OperationParameter');
 483		    	foreach ($xmlParameters as $xmlParameter) {
 484		    		$xmlParameterDetails = $xmlParameter->children('http://schemas.datacontract.org/2004/07/Microsoft.Samples.WindowsAzure.ServiceManagement');
 485		    		$operation->addOperationParameter((string)$xmlParameterDetails->Name, (string)$xmlParameterDetails->Value);
 486		    	}
 487		    	
 488    		    // Add to result
 489    		    $returnValue[] = $operation;
 490		    }
 491		    
 492			// More data?
 493		    if (!is_null($result->ContinuationToken) && $result->ContinuationToken != '') {
 494		    	$returnValue = array_merge($returnValue, $this->listSubscriptionOperations($startTime, $endTime, $objectIdFilter, $operationResultFilter, (string)$result->ContinuationToken));
 495		    }
 496		    
 497		    // Return
 498		    return $returnValue;
 499		} else {
 500			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
 501			throw new Zend_Service_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
 502		}
 503    }
 504    
 505    /**
 506     * Wait for an operation to complete
 507     * 
 508     * @param string $requestId The request ID. If omitted, the last request ID will be used.
 509     * @param int $sleepInterval Sleep interval in milliseconds.
 510     * @return Zend_Service_WindowsAzure_Management_OperationStatusInstance
 511     * @throws Zend_Service_WindowsAzure_Management_Exception
 512     */
 513    public function waitForOperation($requestId = '', $sleepInterval = 250)
 514    {
 515    	if ($requestId == '') {
 516    		$requestId = $this->getLastRequestId();
 517    	}
 518    	if ($requestId == '' || is_null($requestId)) {
 519    		return null;
 520    	}
 521
 522		$status = $this->getOperationStatus($requestId);
 523		while ($status->Status == 'InProgress') {
 524		  $status = $this->getOperationStatus($requestId);
 525		  usleep($sleepInterval);
 526		}
 527		
 528		return $status;
 529    }
 530    
 531	/**
 532	 * Creates a new Zend_Service_WindowsAzure_Storage_Blob instance for the current account
 533	 *
 534	 * @param string $serviceName the service name to create a storage client for.
 535	 * @param Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy Retry policy to use when making requests
 536	 * @return Zend_Service_WindowsAzure_Storage_Blob
 537	 */
 538	public function createBlobClientForService($serviceName, Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy = null)
 539	{
 540		if ($serviceName == '' || is_null($serviceName)) {
 541    		throw new Zend_Service_WindowsAzure_Management_Exception('Service name should be specified.');
 542    	}
 543    	
 544    	$storageKeys = $this->getStorageAccountKeys($serviceName);
 545    	
 546		
 547		
 548		return new Zend_Service_WindowsAzure_Storage_Blob(
 549			Zend_Service_WindowsAzure_Storage::URL_CLOUD_BLOB,
 550			$serviceName,
 551			$storageKeys[0],
 552			false,
 553			$retryPolicy
 554		);
 555	}
 556	
 557	/**
 558	 * Creates a new Zend_Service_WindowsAzure_Storage_Table instance for the current account
 559	 *
 560	 * @param string $serviceName the service name to create a storage client for.
 561	 * @param Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy Retry policy to use when making requests
 562	 * @return Zend_Service_WindowsAzure_Storage_Table
 563	 */
 564	public function createTableClientForService($serviceName, Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy = null)
 565	{
 566		if ($serviceName == '' || is_null($serviceName)) {
 567			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
 568    		throw new Zend_Service_WindowsAzure_Management_Exception('Service name should be specified.');
 569    	}
 570    	
 571    	$storageKeys = $this->getStorageAccountKeys($serviceName);
 572		
 573		return new Zend_Service_WindowsAzure_Storage_Table(
 574			Zend_Service_WindowsAzure_Storage::URL_CLOUD_TABLE,
 575			$serviceName,
 576			$storageKeys[0],
 577			false,
 578			$retryPolicy
 579		);
 580	}
 581	
 582	/**
 583	 * Creates a new Zend_Service_WindowsAzure_Storage_Queue instance for the current account
 584	 *
 585	 * @param string $serviceName the service name to create a storage client for.
 586	 * @param Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy Retry policy to use when making requests
 587	 * @return Zend_Service_WindowsAzure_Storage_Queue
 588	 */
 589	public function createQueueClientForService($serviceName, Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy = null)
 590	{
 591		if ($serviceName == '' || is_null($serviceName)) {
 592			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
 593    		throw new Zend_Service_WindowsAzure_Management_Exception('Service name should be specified.');
 594    	}
 595    	
 596    	$storageKeys = $this->getStorageAccountKeys($serviceName);
 597    	
 598		require_once 'Zend/Service/WindowsAzure/Storage/Queue.php';
 599		
 600		return new Zend_Service_WindowsAzure_Storage_Queue(
 601			Zend_Service_WindowsAzure_Storage::URL_CLOUD_QUEUE,
 602			$serviceName,
 603			$storageKeys[0],
 604			false,
 605			$retryPolicy
 606		);
 607	}
 608    
 609    /**
 610     * The List Storage Accounts operation lists the storage accounts available under
 611     * the current subscription.
 612     *
 613     * @return array An array of Zend_Service_WindowsAzure_Management_StorageServiceInstance
 614     */
 615    public function listStorageAccounts()
 616    {
 617    	$response = $this->_performRequest(self::OP_STORAGE_ACCOUNTS);
 618
 619    	if ($response->isSuccessful()) {
 620			$result = $this->_parseResponse($response);
 621			
 622    		if (!$result->StorageService) {
 623				return array();
 624			}
 625		    if (count($result->StorageService) > 1) {
 626    		    $xmlServices = $result->StorageService;
 627    		} else {
 628    		    $xmlServices = array($result->StorageService);
 629    		}
 630    		
 631			$services = array();
 632			if (!is_null($xmlServices)) {				
 633				for ($i = 0; $i < count($xmlServices); $i++) {
 634					$services[] = new Zend_Service_WindowsAzure_Management_StorageServiceInstance(
 635					    (string)$xmlServices[$i]->Url,
 636					    (string)$xmlServices[$i]->ServiceName
 637					);
 638				}
 639			}
 640			return $services;
 641		} else {
 642			throw new Zend_Service_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
 643		}
 644    }
 645    
 646    /**
 647     * The Get Storage Account Properties operation returns the system properties for the
 648     * specified storage account. These properties include: the address, description, and 
 649     * label of the storage account; and the name of the affinity group to which the service
 650     * belongs, or its geo-location if it is not part of an affinity group.
 651     *
 652     * @param string $serviceName The name of your service.
 653     * @return Zend_Service_WindowsAzure_Management_StorageServiceInstance
 654     * @throws Zend_Service_WindowsAzure_Management_Exception
 655     */
 656    public function getStorageAccountProperties($serviceName)
 657    {
 658    	if ($serviceName == '' || is_null($serviceName)) {
 659    		throw new Zend_Service_WindowsAzure_Management_Exception('Service name should be specified.');
 660    	}
 661    	
 662    	$response = $this->_performRequest(self::OP_STORAGE_ACCOUNTS . '/' . $serviceName);
 663
 664    	if ($response->isSuccessful()) {
 665			$xmlService = $this->_parseResponse($response);
 666
 667			if (!is_null($xmlService)) {
 668				require_once 'Zend/Service/WindowsAzure/Management/StorageServiceInstance.php';
 669				
 670				return new Zend_Service_WindowsAzure_Management_StorageServiceInstance(
 671					(string)$xmlService->Url,
 672					(string)$xmlService->ServiceName,
 673					(string)$xmlService->StorageServiceProperties->Description,
 674					(string)$xmlService->StorageServiceProperties->AffinityGroup,
 675					(string)$xmlService->StorageServiceProperties->Location,
 676					(string)$xmlService->StorageServiceProperties->Label
 677				);
 678			}
 679			return null;
 680		} else {
 681			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
 682			throw new Zend_Service_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
 683		}
 684    }
 685    
 686    /**
 687     * The Get Storage Keys operation returns the primary
 688     * and secondary access keys for the specified storage account.
 689     *
 690     * @param string $serviceName The name of your service.
 691     * @return array An array of strings
 692     * @throws Zend_Service_WindowsAzure_Management_Exception
 693     */
 694    public function getStorageAccountKeys($serviceName)
 695    {
 696    	if ($serviceName == '' || is_null($serviceName)) {
 697			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
 698    		throw new Zend_Service_WindowsAzure_Management_Exception('Service name should be specified.');
 699    	}
 700    	
 701    	$response = $this->_performRequest(self::OP_STORAGE_ACCOUNTS . '/' . $serviceName . '/keys');
 702
 703    	if ($response->isSuccessful()) {
 704			$xmlService = $this->_parseResponse($response);
 705
 706			if (!is_null($xmlService)) {
 707				return array(
 708					(string)$xmlService->StorageServiceKeys->Primary,
 709					(string)$xmlService->StorageServiceKeys->Secondary
 710				);
 711			}
 712			return array();
 713		} else {
 714			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
 715			throw new Zend_Service_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
 716		}
 717    }
 718    
 719    /**
 720     * The Regenerate Keys operation regenerates the primary
 721     * or secondary access key for the specified storage account.
 722     *
 723     * @param string $serviceName The name of your service.
 724     * @param string $key		  The key to regenerate (primary or secondary)
 725     * @return array An array of strings
 726     * @throws Zend_Service_WindowsAzure_Management_Exception
 727     */
 728    public function regenerateStorageAccountKey($serviceName, $key = 'primary')
 729    {
 730    	if ($serviceName == '' || is_null($serviceName)) {
 731			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
 732    		throw new Zend_Service_WindowsAzure_Management_Exception('Service name should be specified.');
 733    	}
 734    	$key = strtolower($key);
 735    	if ($key != 'primary' && $key != 'secondary') {
 736			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
 737    		throw new Zend_Service_WindowsAzure_Management_Exception('Key identifier should be primary|secondary.');
 738    	}
 739    	
 740    	$response = $this->_performRequest(
 741    		self::OP_STORAGE_ACCOUNTS . '/' . $serviceName . '/keys', '?action=regenerate',
 742    		Zend_Http_Client::POST,
 743    		array('Content-Type' => 'application/xml'),
 744    		'<?xml version="1.0" encoding="utf-8"?>
 745             <RegenerateKeys xmlns="http://schemas.microsoft.com/windowsazure">
 746               <KeyType>' . ucfirst($key) . '</KeyType>
 747             </RegenerateKeys>');
 748
 749    	if ($response->isSuccessful()) {
 750			$xmlService = $this->_parseResponse($response);
 751
 752			if (!is_null($xmlService)) {
 753				return array(
 754					(string)$xmlService->StorageServiceKeys->Primary,
 755					(string)$xmlService->StorageServiceKeys->Secondary
 756				);
 757			}
 758			return array();
 759		} else {
 760			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
 761			throw new Zend_Service_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
 762		}
 763    }
 764    
 765    /**
 766     * The List Hosted Services operation lists the hosted services available
 767     * under the current subscription.
 768     *
 769     * @return array An array of Zend_Service_WindowsAzure_Management_HostedServiceInstance
 770     * @throws Zend_Service_WindowsAzure_Management_Exception
 771     */
 772    public function listHostedServices()
 773    {
 774    	$response = $this->_performRequest(self::OP_HOSTED_SERVICES);
 775
 776    	if ($response->isSuccessful()) {
 777			$result = $this->_parseResponse($response);
 778			
 779    		if (!$result->HostedService) {
 780				return array();
 781			}
 782		    if (count($result->HostedService) > 1) {
 783    		    $xmlServices = $result->HostedService;
 784    		} else {
 785    		    $xmlServices = array($result->HostedService);
 786    		}
 787    		
 788			$services = array();
 789			if (!is_null($xmlServices)) {	
 790				
 791				for ($i = 0; $i < count($xmlServices); $i++) {
 792					$services[] = new Zend_Service_WindowsAzure_Management_HostedServiceInstance(
 793					    (string)$xmlServices[$i]->Url,
 794					    (string)$xmlServices[$i]->ServiceName
 795					);
 796				}
 797			}
 798			return $services;
 799		} else {
 800			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
 801			throw new Zend_Service_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
 802		}
 803    }
 804    
 805    /**
 806     * The Create Hosted Service operation creates a new hosted service in Windows Azure.
 807     * 
 808     * @param string $serviceName A name for the hosted service that is unique to the subscription.
 809     * @param string $label A label for the hosted service. The label may be up to 100 characters in length.
 810     * @param string $description A description for the hosted service. The description may be up to 1024 characters in length.
 811     * @param string $location Required if AffinityGroup is not specified. The location where the hosted service will be created. 
 812     * @param string $affinityGroup Required if Location is not specified. The name of an existing affinity group associated with this subscription.
 813     */
 814    public function createHostedService($serviceName, $label, $description = '', $location = null, $affinityGroup = null)
 815    {
 816    	if ($serviceName == '' || is_null($serviceName)) {
 817			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
 818    		throw new Zend_Service_WindowsAzure_Management_Exception('Service name should be specified.');
 819    	}
 820    	if ($label == '' || is_null($label)) {
 821			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
 822    		throw new Zend_Service_WindowsAzure_Management_Exception('Label should be specified.');
 823    	}
 824        if (strlen($label) > 100) {
 825			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
 826    		throw new Zend_Service_WindowsAzure_Management_Exception('Label is too long. The maximum length is 100 characters.');
 827    	}
 828        if (strlen($description) > 1024) {
 829			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
 830    		throw new Zend_Service_WindowsAzure_Management_Exception('Description is too long. The maximum length is 1024 characters.');
 831    	}
 832    	if ( (is_null($location) && is_null($affinityGroup)) || (!is_null($location) && !is_null($affinityGroup)) ) {
 833			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
 834    		throw new Zend_Service_WindowsAzure_Management_Exception('Please specify a location -or- an affinity group for the service.');
 835    	}
 836    	
 837    	$locationOrAffinityGroup = is_null($location)
 838    		? '<AffinityGroup>' . $affinityGroup . '</AffinityGroup>'
 839    		: '<Location>' . $location . '</Location>';
 840    	
 841        $response = $this->_performRequest(self::OP_HOSTED_SERVICES, '',
 842    		Zend_Http_Client::POST,
 843    		array('Content-Type' => 'application/xml; charset=utf-8'),
 844    		'<CreateHostedService xmlns="http://schemas.microsoft.com/windowsazure"><ServiceName>' . $serviceName . '</ServiceName><Label>' . base64_encode($label) . '</Label><Description>' . $description . '</Description>' . $locationOrAffinityGroup . '</CreateHostedService>');
 845 	
 846    	if (!$response->isSuccessful()) {
 847			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
 848			throw new Zend_Service_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
 849		}
 850    }
 851    
 852    /**
 853     * The Update Hosted Service operation updates the label and/or the description for a hosted service in Windows Azure.
 854     * 
 855     * @param string $serviceName A name for the hosted service that is unique to the subscription.
 856     * @param string $label A label for the hosted service. The label may be up to 100 characters in length.
 857     * @param string $description A description for the hosted service. The description may be up to 1024 characters in length.
 858     */
 859    public function updateHostedService($serviceName, $label, $description = '')
 860    {
 861    	if ($serviceName == '' || is_null($serviceName)) {
 862			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
 863    		throw new Zend_Service_WindowsAzure_Management_Exception('Service name should be specified.');
 864    	}
 865    	if ($label == '' || is_null($label)) {
 866			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
 867    		throw new Zend_Service_WindowsAzure_Management_Exception('Label should be specified.');
 868    	}
 869        if (strlen($label) > 100) {
 870			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
 871    		throw new Zend_Service_WindowsAzure_Management_Exception('Label is too long. The maximum length is 100 characters.');
 872    	}
 873    	
 874        $response = $this->_performRequest(self::OP_HOSTED_SERVICES . '/' . $serviceName, '',
 875    		Zend_Http_Client::PUT,
 876    		array('Content-Type' => 'application/xml; charset=utf-8'),
 877    		'<UpdateHostedService xmlns="http://schemas.microsoft.com/windowsazure"><Label>' . base64_encode($label) . '</Label><Description>' . $description . '</Description></UpdateHostedService>');
 878 	
 879    	if (!$response->isSuccessful()) {
 880			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
 881			throw new Zend_Service_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
 882		}
 883    }
 884    
 885    /**
 886     * The Delete Hosted Service operation deletes the specified hosted service in Windows Azure.
 887     * 
 888     * @param string $serviceName A name for the hosted service that is unique to the subscription.
 889     */
 890    public function deleteHostedService($serviceName)
 891    {
 892    	if ($serviceName == '' || is_null($serviceName)) {
 893			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
 894    		throw new Zend_Service_WindowsAzure_Management_Exception('Service name should be specified.');
 895    	}
 896    	
 897        $response = $this->_performRequest(self::OP_HOSTED_SERVICES . '/' . $serviceName, '', Zend_Http_Client::DELETE);
 898 	
 899    	if (!$response->isSuccessful()) {
 900			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
 901			throw new Zend_Service_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
 902		}
 903    }
 904    
 905    /**
 906     * The Get Hosted Service Properties operation retrieves system properties
 907     * for the specified hosted service. These properties include the service
 908     * name and service type; the name of the affinity group to which the service
 909     * belongs, or its location if it is not part of an affinity group; and
 910     * optionally, information on the service's deployments.
 911     *
 912     * @param string $serviceName The name of your service.
 913     * @return Zend_Service_WindowsAzure_Management_HostedServiceInstance
 914     * @throws Zend_Service_WindowsAzure_Management_Exception
 915     */
 916    public function getHostedServiceProperties($serviceName)
 917    {
 918    	if ($serviceName == '' || is_null($serviceName)) {
 919			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
 920    		throw new Zend_Service_WindowsAzure_Management_Exception('Service name should be specified.');
 921    	}
 922    	
 923    	$response = $this->_performRequest(self::OP_HOSTED_SERVICES . '/' . $serviceName, '?embed-detail=true');
 924
 925    	if ($response->isSuccessful()) {
 926			$xmlService = $this->_parseResponse($response);
 927
 928			if (!is_null($xmlService)) {
 929				
 930				$returnValue = new Zend_Service_WindowsAzure_Management_HostedServiceInstance(
 931					(string)$xmlService->Url,
 932					(string)$xmlService->ServiceName,
 933					(string)$xmlService->HostedServiceProperties->Description,
 934					(string)$xmlService->HostedServiceProperties->AffinityGroup,
 935					(string)$xmlService->HostedServiceProperties->Location,
 936					(string)$xmlService->HostedServiceProperties->Label
 937				);
 938				
 939				// Deployments
 940		    	if (count($xmlService->Deployments->Deployment) > 1) {
 941    		    	$xmlServices = $xmlService->Deployments->Deployment;
 942    			} else {
 943    		    	$xmlServices = array($xmlService->Deployments->Deployment);
 944    			}
 945    			
 946    			$deployments = array();
 947    			foreach ($xmlServices as $xmlDeployment) {
 948					$deployments[] = $this->_convertXmlElementToDeploymentInstance($xmlDeployment);
 949    			}
 950				$returnValue->Deployments = $deployments;
 951				
 952				return $returnValue;
 953			}
 954			return null;
 955		} else {
 956			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
 957			throw new Zend_Service_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
 958		}
 959    }
 960
 961    /**
 962     * The Create Deployment operation uploads a new service package
 963     * and creates a new deployment on staging or production.
 964     * 
 965     * @param string $serviceName		The service name
 966     * @param string $deploymentSlot	The deployment slot (production or staging)
 967	 * @param string $name              The name for the deployment. The deployment ID as listed on the Windows Azure management portal must be unique among other deployments for the hosted service.
 968	 * @param string $label             A URL that refers to the location of the service package in the Blob service. The service package must be located in a storage account beneath the same subscription.
 969	 * @param string $packageUrl        The service configuration file for the deployment.
 970	 * @param string $configuration     A label for this deployment, up to 100 characters in length.
 971	 * @param boolean $startDeployment  Indicates whether to start the deployment immediately after it is created.
 972	 * @param boolean $treatWarningsAsErrors Indicates whether to treat package validation warnings as errors.
 973     * @throws Zend_Service_WindowsAzure_Management_Exception
 974     */
 975    public function createDeployment($serviceName, $deploymentSlot, $name, $label, $packageUrl, $configuration, $startDeployment = false, $treatWarningsAsErrors = false)
 976    {
 977        if ($serviceName == '' || is_null($serviceName)) {
 978			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
 979    		throw new Zend_Service_WindowsAzure_Management_Exception('Service name should be specified.');
 980    	}
 981    	$deploymentSlot = strtolower($deploymentSlot);
 982    	if ($deploymentSlot != 'production' && $deploymentSlot != 'staging') {
 983			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
 984    		throw new Zend_Service_WindowsAzure_Management_Exception('Deployment slot should be production|staging.');
 985    	}
 986    	if ($name == '' || is_null($name)) {
 987			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
 988    		throw new Zend_Service_WindowsAzure_Management_Exception('Name should be specified.');
 989    	}
 990    	if ($label == '' || is_null($label)) {
 991			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
 992    		throw new Zend_Service_WindowsAzure_Management_Exception('Label should be specified.');
 993    	}
 994        if (strlen($label) > 100) {
 995			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
 996    		throw new Zend_Service_WindowsAzure_Management_Exception('Label is too long. The maximum length is 100 characters.');
 997    	}
 998    	if ($packageUrl == '' || is_null($packageUrl)) {
 999			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
1000    		throw new Zend_Service_WindowsAzure_Management_Exception('Package URL should be specified.');
1001    	}
1002    	if ($configuration == '' || is_null($configuration)) {
1003			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
1004    		throw new Zend_Service_WindowsAzure_Management_Exception('Configuration should be specified.');
1005    	}
1006    	
1007    	if (@file_exists($configuration)) {
1008    		$configuration = utf8_decode(file_get_contents($configuration));
1009    	}
1010    	
1011    	// Clean up the configuration
1012    	$conformingConfiguration = $this->_cleanConfiguration($configuration);
1013    	
1014    	$operationUrl = self::OP_HOSTED_SERVICES . '/' . $serviceName . '/deploymentslots/' . $deploymentSlot;
1015        $response = $this->_performRequest($operationUrl, '',
1016    		Zend_Http_Client::POST,
1017    		array('Content-Type' => 'application/xml; charset=utf-8'),
1018    		'<CreateDeployment xmlns="http://schemas.microsoft.com/windowsazure"><Name>' . $name . '</Name><PackageUrl>' . $packageUrl . '</PackageUrl><Label>' . base64_encode($label) . '</Label><Configuration>' . base64_encode($conformingConfiguration) . '</Configuration><StartDeployment>' . ($startDeployment ? 'true' : 'false') . '</StartDeployment><TreatWarningsAsError>' . ($treatWarningsAsErrors ? 'true' : 'false') . '</TreatWarningsAsError></CreateDeployment>');
1019 	
1020    	if (!$response->isSuccessful()) {
1021			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
1022			throw new Zend_Service_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
1023		}    	
1024    }
1025    
1026    /**
1027     * The Get Deployment operation returns configuration information, status,
1028     * and system properties for the specified deployment.
1029     * 
1030     * @param string $serviceName		The service name
1031     * @param string $deploymentSlot	The deployment slot (production or staging)
1032     * @return Zend_Service_WindowsAzure_Management_DeploymentInstance
1033     * @throws Zend_Service_WindowsAzure_Management_Exception
1034     */
1035    public function getDeploymentBySlot($serviceName, $deploymentSlot)
1036    {
1037        if ($serviceName == '' || is_null($serviceName)) {
1038			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
1039    		throw new Zend_Service_WindowsAzure_Management_Exception('Service name should be specified.');
1040    	}
1041    	$deploymentSlot = strtolower($deploymentSlot);
1042    	if ($deploymentSlot != 'production' && $deploymentSlot != 'staging') {
1043			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
1044    		throw new Zend_Service_WindowsAzure_Management_Exception('Deployment slot should be production|staging.');
1045    	}
1046    	
1047    	$operationUrl = self::OP_HOSTED_SERVICES . '/' . $serviceName . '/deploymentslots/' . $deploymentSlot;
1048    	return $this->_getDeployment($operationUrl);
1049    }
1050    
1051    /**
1052     * The Get Deployment operation returns configuration information, status,
1053     * and system properties for the specified deployment.
1054     * 
1055     * @param string $serviceName		The service name
1056     * @param string $deploymentId	The deployment ID as listed on the Windows Azure management portal
1057     * @return Zend_Service_WindowsAzure_Management_DeploymentInstance
1058     * @throws Zend_Service_WindowsAzure_Management_Exception
1059     */
1060    public function getDeploymentByDeploymentId($serviceName, $deploymentId)
1061    {
1062        if ($serviceName == '' || is_null($serviceName)) {
1063			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
1064    		throw new Zend_Service_WindowsAzure_Management_Exception('Service name should be specified.');
1065    	}
1066        if ($deploymentId == '' || is_null($deploymentId)) {
1067			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
1068    		throw new Zend_Service_WindowsAzure_Management_Exception('Deployment ID should be specified.');
1069    	}
1070    	
1071    	$operationUrl = self::OP_HOSTED_SERVICES . '/' . $serviceName . '/deployments/' . $deploymentId;
1072    	return $this->_getDeployment($operationUrl);
1073    }
1074    
1075    /**
1076     * The Get Deployment operation returns configuration information, status,
1077     * and system properties for the specified deployment.
1078     * 
1079     * @param string $operationUrl		The operation url
1080     * @return Zend_Service_WindowsAzure_Management_DeploymentInstance
1081     * @throws Zend_Service_WindowsAzure_Management_Exception
1082     */
1083    protected function _getDeployment($operationUrl)
1084    {
1085        $response = $this->_performRequest($operationUrl);
1086
1087    	if ($response->isSuccessful()) {
1088			$xmlService = $this->_parseResponse($response);
1089			
1090			return $this->_convertXmlElementToDeploymentInstance($xmlService);
1091		} else {
1092			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
1093			throw new Zend_Service_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
1094		}
1095    }
1096    
1097    /**
1098     * The Swap Deployment operation initiates a virtual IP swap between
1099     * the staging and production deployment environments for a service.
1100     * If the service is currently running in the staging environment,
1101     * it will be swapped to the production environment. If it is running
1102     * in the production environment, it will be swapped to staging.
1103     * 
1104     * @param string $serviceName The service name.
1105     * @param string $productionDeploymentName The name of the production deployment.
1106     * @param string $sourceDeploymentName The name of the source deployment.
1107     * @throws Zend_Service_WindowsAzure_Management_Exception
1108     */
1109    public function swapDeployment($serviceName, $productionDeploymentName, $sourceDeploymentName)
1110    {
1111    	if ($serviceName == '' || is_null($serviceName)) {
1112			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
1113    		throw new Zend_Service_WindowsAzure_Management_Exception('Service name should be specified.');
1114    	}
1115    	if ($productionDeploymentName == '' || is_null($productionDeploymentName)) {
1116			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
1117    		throw new Zend_Service_WindowsAzure_Management_Exception('Production Deployment ID should be specified.');
1118    	}
1119    	if ($sourceDeploymentName == '' || is_null($sourceDeploymentName)) {
1120			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
1121    		throw new Zend_Service_WindowsAzure_Management_Exception('Source Deployment ID should be specified.');
1122    	}
1123    	
1124    	$operationUrl = self::OP_HOSTED_SERVICES . '/' . $serviceName;
1125        $response = $this->_performRequest($operationUrl, '',
1126    		Zend_Http_Client::POST,
1127    		array('Content-Type' => 'application/xml; charset=utf-8'),
1128    		'<Swap xmlns="http://schemas.microsoft.com/windowsazure"><Production>' . $productionDeploymentName . '</Production><SourceDeployment>' . $sourceDeploymentName . '</SourceDeployment></Swap>');
1129    		
1130    	if (!$response->isSuccessful()) {
1131			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
1132			throw new Zend_Service_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
1133		}    	
1134    }
1135    
1136    /**
1137     * The Delete Deployment operation deletes the specified deployment.
1138     * 
1139     * @param string $serviceName		The service name
1140     * @param string $deploymentSlot	The deployment slot (production or staging)
1141     * @throws Zend_Service_WindowsAzure_Management_Exception
1142     */
1143    public function deleteDeploymentBySlot($serviceName, $deploymentSlot)
1144    {
1145        if ($serviceName == '' || is_null($serviceName)) {
1146			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
1147    		throw new Zend_Service_WindowsAzure_Management_Exception('Service name should be specified.');
1148    	}
1149    	$deploymentSlot = strtolower($deploymentSlot);
1150    	if ($deploymentSlot != 'production' && $deploymentSlot != 'staging') {
1151			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
1152    		throw new Zend_Service_WindowsAzure_Management_Exception('Deployment slot should be production|staging.');
1153    	}
1154    	
1155    	$operationUrl = self::OP_HOSTED_SERVICES . '/' . $serviceName . '/deploymentslots/' . $deploymentSlot;
1156    	return $this->_deleteDeployment($operationUrl);
1157    }
1158    
1159    /**
1160     * The Delete Deployment operation deletes the specified deployment.
1161     * 
1162     * @param string $serviceName		The service name
1163     * @param string $deploymentId	The deployment ID as listed on the Windows Azure management portal
1164     * @throws Zend_Service_WindowsAzure_Management_Exception
1165     */
1166    public function deleteDeploymentByDeploymentId($serviceName, $deploymentId)
1167    {
1168        if ($serviceName == '' || is_null($serviceName)) {
1169			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
1170    		throw new Zend_Service_WindowsAzure_Management_Exception('Service name should be specified.');
1171    	}
1172    	if ($deploymentId == '' || is_null($deploymentId)) {
1173			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
1174    		throw new Zend_Service_WindowsAzure_Management_Exception('Deployment ID should be specified.');
1175    	}
1176    	
1177    	$operationUrl = self::OP_HOSTED_SERVICES . '/' . $serviceName . '/deployments/' . $deploymentId;
1178    	return $this->_deleteDeployment($operationUrl);
1179    }
1180    
1181    /**
1182     * The Delete Deployment operation deletes the specified deployment.
1183     * 
1184     * @param string $operationUrl		The operation url
1185     * @throws Zend_Service_WindowsAzure_Management_Exception
1186     */
1187    protected function _deleteDeployment($operationUrl)
1188    {
1189        $response = $this->_performRequest($operationUrl, '', Zend_Http_Client::DELETE);
1190			 
1191    	if (!$response->isSuccessful()) {
1192			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
1193			throw new Zend_Service_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
1194		}
1195    }
1196    
1197    /**
1198     * The Update Deployment Status operation initiates a change in deployment status.
1199     * 
1200     * @param string $serviceName		The service name
1201     * @param string $deploymentSlot	The deployment slot (production or staging)
1202     * @param string $status            The deployment status (running|suspended)
1203     * @throws Zend_Service_WindowsAzure_Management_Exception
1204     */
1205    public function updateDeploymentStatusBySlot($serviceName, $deploymentSlot, $status = 'running')
1206    {
1207        if ($serviceName == '' || is_null($serviceName)) {
1208			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
1209    		throw new Zend_Service_WindowsAzure_Management_Exception('Service name should be specified.');
1210    	}
1211    	$deploymentSlot = strtolower($deploymentSlot);
1212    	if ($deploymentSlot != 'production' && $deploymentSlot != 'staging') {
1213			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
1214    		throw new Zend_Service_WindowsAzure_Management_Exception('Deployment slot should be production|staging.');
1215    	}
1216    	$status = strtolower($status);
1217    	if ($status != 'running' && $status != 'suspended') {
1218			require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
1219    		throw new Zend_Service_WindowsAzure_Management_Exception('Status should be running|suspended.');
1220    	}
1221    	
1222    	$operationUrl = self::OP_HOSTED_SERVICES . '/' . $serviceName . '/deploymentslots/' . $deploymentSlot;
1223    	return $this->_updateDeploymentStatus($operationUrl, $status);
1224    }
1225    
1226    /**
1227     * The Update…

Large files files are truncated, but you can click here to view the full file