Microsoft_WindowsAzure
[ class tree: Microsoft_WindowsAzure ] [ index: Microsoft_WindowsAzure ] [ all elements ]

Source for file Client.php

Documentation is available at Client.php

  1. <?php
  2. /**
  3.  * Copyright (c) 2009 - 2010, RealDolmen
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions are met:
  8.  *     * Redistributions of source code must retain the above copyright
  9.  *       notice, this list of conditions and the following disclaimer.
  10.  *     * Redistributions in binary form must reproduce the above copyright
  11.  *       notice, this list of conditions and the following disclaimer in the
  12.  *       documentation and/or other materials provided with the distribution.
  13.  *     * Neither the name of RealDolmen nor the
  14.  *       names of its contributors may be used to endorse or promote products
  15.  *       derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY RealDolmen ''AS IS'' AND ANY
  18.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20.  * DISCLAIMED. IN NO EVENT SHALL RealDolmen BE LIABLE FOR ANY
  21.  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  *
  28.  * @category   Microsoft
  29.  * @package    Microsoft_WindowsAzure
  30.  * @subpackage Management
  31.  * @copyright  Copyright (c) 2009 - 2010, RealDolmen (http://www.realdolmen.com)
  32.  * @license    http://phpazure.codeplex.com/license
  33.  * @version    $Id: Storage.php 51671 2010-09-30 08:33:45Z unknown $
  34.  */
  35.  
  36. /**
  37.  * @see Microsoft_AutoLoader
  38.  */
  39. require_once dirname(__FILE__'/../../AutoLoader.php';
  40.  
  41. /**
  42.  * @category   Microsoft
  43.  * @package    Microsoft_WindowsAzure
  44.  * @subpackage Management
  45.  * @copyright  Copyright (c) 2009 - 2010, RealDolmen (http://www.realdolmen.com)
  46.  * @license    http://phpazure.codeplex.com/license
  47.  */
  48. {
  49.     /**
  50.      * Management service URL
  51.      */
  52.     const URL_MANAGEMENT        "https://management.core.windows.net";
  53.     
  54.     /**
  55.      * Operations
  56.      */
  57.     const OP_OPERATIONS                "operations";
  58.     const OP_STORAGE_ACCOUNTS          "services/storageservices";
  59.     const OP_HOSTED_SERVICES           "services/hostedservices";
  60.     const OP_AFFINITYGROUPS            "affinitygroups";
  61.     const OP_LOCATIONS                 "locations";
  62.     const OP_OPERATINGSYSTEMS          "operatingsystems";
  63.     const OP_OPERATINGSYSTEMFAMILIES   "operatingsystemfamilies";
  64.  
  65.     /**
  66.      * Current API version
  67.      * 
  68.      * @var string 
  69.      */
  70.     protected $_apiVersion = '2011-02-25';
  71.     
  72.     /**
  73.      * Subscription ID
  74.      *
  75.      * @var string 
  76.      */
  77.     protected $_subscriptionId = '';
  78.     
  79.     /**
  80.      * Management certificate path (.PEM)
  81.      *
  82.      * @var string 
  83.      */
  84.     protected $_certificatePath = '';
  85.     
  86.     /**
  87.      * Management certificate passphrase
  88.      *
  89.      * @var string 
  90.      */
  91.     protected $_certificatePassphrase = '';
  92.     
  93.     /**
  94.      * Microsoft_Http_Client channel used for communication with REST services
  95.      * 
  96.      * @var Microsoft_Http_Client 
  97.      */
  98.     protected $_httpClientChannel = null;    
  99.  
  100.     /**
  101.      * Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract instance
  102.      * 
  103.      * @var Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract 
  104.      */
  105.     protected $_retryPolicy = null;
  106.     
  107.     /**
  108.      * Returns the last request ID
  109.      * 
  110.      * @var string 
  111.      */
  112.     protected $_lastRequestId = null;
  113.     
  114.     /**
  115.      * Creates a new Microsoft_WindowsAzure_Management instance
  116.      * 
  117.      * @param string $subscriptionId Subscription ID
  118.      * @param string $certificatePath Management certificate path (.PEM)
  119.      * @param string $certificatePassphrase Management certificate passphrase
  120.      * @param Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy Retry policy to use when making requests
  121.      */
  122.     public function __construct(
  123.         $subscriptionId,
  124.         $certificatePath,
  125.         $certificatePassphrase,
  126.         Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy null
  127.     {
  128.         $this->_subscriptionId = $subscriptionId;
  129.         $this->_certificatePath = $certificatePath;
  130.         $this->_certificatePassphrase = $certificatePassphrase;
  131.         
  132.         $this->_retryPolicy = $retryPolicy;
  133.         if (is_null($this->_retryPolicy)) {
  134.         }
  135.         
  136.         // Setup default Microsoft_Http_Client channel
  137.         $options array(
  138.             'adapter'       => 'Microsoft_Http_Client_Adapter_Socket',
  139.             'ssltransport'  => 'ssl',
  140.             'sslcert'       => $this->_certificatePath,
  141.             'sslpassphrase' => $this->_certificatePassphrase,
  142.             'sslusecontext' => true,
  143.         );
  144.         if (function_exists('curl_init')) {
  145.             // Set cURL options if cURL is used afterwards
  146.             $options['curloptions'array(
  147.                     CURLOPT_FOLLOWLOCATION => true,
  148.                     CURLOPT_TIMEOUT => 120,
  149.             );
  150.         }
  151.         $this->_httpClientChannel = new Microsoft_Http_Client(null$options);
  152.     }
  153.     
  154.     /**
  155.      * Set the HTTP client channel to use
  156.      * 
  157.      * @param Microsoft_Http_Client_Adapter_Interface|string$adapterInstance Adapter instance or adapter class name.
  158.      */
  159.     public function setHttpClientChannel($adapterInstance 'Microsoft_Http_Client_Adapter_Socket')
  160.     {
  161.         $this->_httpClientChannel->setAdapter($adapterInstance);
  162.     }
  163.     
  164.     /**
  165.      * Retrieve HTTP client channel
  166.      * 
  167.      * @return Microsoft_Http_Client_Adapter_Interface 
  168.      */
  169.     public function getHttpClientChannel()
  170.     {
  171.         return $this->_httpClientChannel;
  172.     }
  173.     
  174.     /**
  175.      * Returns the Windows Azure subscription ID
  176.      * 
  177.      * @return string 
  178.      */
  179.     public function getSubscriptionId()
  180.     {
  181.         return $this->_subscriptionId;
  182.     }
  183.     
  184.     /**
  185.      * Returns the last request ID.
  186.      * 
  187.      * @return string 
  188.      */
  189.     public function getLastRequestId()
  190.     {
  191.         return $this->_lastRequestId;
  192.     }
  193.     
  194.     /**
  195.      * Get base URL for creating requests
  196.      *
  197.      * @return string 
  198.      */
  199.     public function getBaseUrl()
  200.     {
  201.         return self::URL_MANAGEMENT '/' $this->_subscriptionId;
  202.     }
  203.     
  204.     /**
  205.      * Perform request using Microsoft_Http_Client channel
  206.      *
  207.      * @param string $path Path
  208.      * @param string $queryString Query string
  209.      * @param string $httpVerb HTTP verb the request will use
  210.      * @param array $headers x-ms headers to add
  211.      * @param mixed $rawData Optional RAW HTTP data to be sent over the wire
  212.      * @return Microsoft_Http_Response 
  213.      */
  214.     protected function _performRequest(
  215.         $path '/',
  216.         $queryString '',
  217.         $httpVerb Microsoft_Http_Client::GET,
  218.         $headers array(),
  219.         $rawData null
  220.     {
  221.         // Clean path
  222.         if (strpos($path'/'!== 0{
  223.             $path '/' $path;
  224.         }
  225.             
  226.         // Clean headers
  227.         if (is_null($headers)) {
  228.             $headers array();
  229.         }
  230.         
  231.         // Ensure cUrl will also work correctly:
  232.         //  - disable Content-Type if required
  233.         //  - disable Expect: 100 Continue
  234.         if (!isset($headers["Content-Type"])) {
  235.             $headers["Content-Type"'';
  236.         }
  237.         //$headers["Expect"] = '';
  238.  
  239.         // Add version header
  240.         $headers['x-ms-version'$this->_apiVersion;
  241.             
  242.         // URL encoding
  243.         $path           self::urlencode($path);
  244.         $queryString    self::urlencode($queryString);
  245.  
  246.         // Generate URL and sign request
  247.         $requestUrl     $this->getBaseUrl($path $queryString;
  248.         $requestHeaders $headers;
  249.  
  250.         // Prepare request 
  251.         $this->_httpClientChannel->resetParameters(true);
  252.         $this->_httpClientChannel->setUri($requestUrl);
  253.         $this->_httpClientChannel->setHeaders($requestHeaders);
  254.         $this->_httpClientChannel->setRawData($rawData);
  255.  
  256.         // Execute request
  257.         $response $this->_retryPolicy->execute(
  258.             array($this->_httpClientChannel'request'),
  259.             array($httpVerb)
  260.         );
  261.         
  262.         // Store request id
  263.         $this->_lastRequestId = $response->getHeader('x-ms-request-id');
  264.         
  265.         return $response;
  266.     }
  267.     
  268.     /** 
  269.      * Parse result from Microsoft_Http_Response
  270.      *
  271.      * @param Microsoft_Http_Response $response Response from HTTP call
  272.      * @return object 
  273.      * @throws Microsoft_WindowsAzure_Exception
  274.      */
  275.     protected function _parseResponse(Microsoft_Http_Response $response null)
  276.     {
  277.         if (is_null($response)) {
  278.             throw new Microsoft_WindowsAzure_Exception('Response should not be null.');
  279.         }
  280.         
  281.         $xml @simplexml_load_string($response->getBody());
  282.         
  283.         if ($xml !== false{
  284.             // Fetch all namespaces 
  285.             $namespaces array_merge($xml->getNamespaces(true)$xml->getDocNamespaces(true))
  286.             
  287.             // Register all namespace prefixes
  288.             foreach ($namespaces as $prefix => $ns
  289.                 if ($prefix != ''{
  290.                     $xml->registerXPathNamespace($prefix$ns);
  291.                 
  292.             
  293.         }
  294.         
  295.         return $xml;
  296.     }
  297.     
  298.     /**
  299.      * URL encode function
  300.      * 
  301.      * @param  string $value Value to encode
  302.      * @return string        Encoded value
  303.      */
  304.     public static function urlencode($value)
  305.     {
  306.         return str_replace(' ''%20'$value);
  307.     }
  308.     
  309.     /**
  310.      * Builds a query string from an array of elements
  311.      * 
  312.      * @param array     Array of elements
  313.      * @return string   Assembled query string
  314.      */
  315.     public static function createQueryStringFromArray($queryString)
  316.     {
  317.         return count($queryString'?' implode('&'$queryString'';
  318.     }
  319.     
  320.     /**
  321.      * Get error message from Microsoft_Http_Response
  322.      *
  323.      * @param Microsoft_Http_Response $response Repsonse
  324.      * @param string $alternativeError Alternative error message
  325.      * @return string 
  326.      */
  327.     protected function _getErrorMessage(Microsoft_Http_Response $response$alternativeError 'Unknown error.')
  328.     {
  329.         $response $this->_parseResponse($response);
  330.         if ($response && $response->Message{
  331.             return (string)$response->Message;
  332.         else {
  333.             return $alternativeError;
  334.         }
  335.     }
  336.     
  337.     /**
  338.      * The Get Operation Status operation returns the status of the specified operation.
  339.      * After calling an asynchronous operation, you can call Get Operation Status to
  340.      * determine whether the operation has succeed, failed, or is still in progress.
  341.      *
  342.      * @param string $requestId The request ID. If omitted, the last request ID will be used.
  343.      * @return Microsoft_WindowsAzure_Management_OperationStatusInstance 
  344.      * @throws Microsoft_WindowsAzure_Management_Exception
  345.      */
  346.     public function getOperationStatus($requestId '')
  347.     {
  348.         if ($requestId == ''{
  349.             $requestId $this->getLastRequestId();
  350.         }
  351.         
  352.         $response $this->_performRequest(self::OP_OPERATIONS '/' $requestId);
  353.  
  354.         if ($response->isSuccessful()) {
  355.             $result $this->_parseResponse($response);
  356.  
  357.             if (!is_null($result)) {
  358.                 return new Microsoft_WindowsAzure_Management_OperationStatusInstance(
  359.                     (string)$result->ID,
  360.                     (string)$result->Status,
  361.                     ($result->Error ? (string)$result->Error->Code ''),
  362.                     ($result->Error ? (string)$result->Error->Message '')
  363.                 );
  364.             }
  365.             return null;
  366.         else {
  367.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  368.         }
  369.     }
  370.     
  371.  
  372.     
  373.     /**
  374.      * The List Subscription Operations operation returns a list of create, update,
  375.      * and delete operations that were performed on a subscription during the specified timeframe.
  376.      * Documentation on the parameters can be found at http://msdn.microsoft.com/en-us/library/gg715318.aspx.
  377.      *
  378.      * @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.
  379.      * @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.
  380.      * @param string $objectIdFilter Returns subscription operations only for the specified object type and object ID.
  381.      * @param string $operationResultFilter Returns subscription operations only for the specified result status, either Succeeded, Failed, or InProgress.
  382.      * @param string $continuationToken Internal usage.
  383.      * @return array Array of Microsoft_WindowsAzure_Management_SubscriptionOperationInstance
  384.      * @throws Microsoft_WindowsAzure_Management_Exception
  385.      */
  386.     public function listSubscriptionOperations($startTime$endTime$objectIdFilter null$operationResultFilter null$continuationToken null)
  387.     {
  388.         if ($startTime == '' || is_null($startTime)) {
  389.             throw new Microsoft_WindowsAzure_Management_Exception('Start time should be specified.');
  390.         }
  391.         if ($endTime == '' || is_null($endTime)) {
  392.             throw new Microsoft_WindowsAzure_Management_Exception('End time should be specified.');
  393.         }
  394.         if ($operationResultFilter != '' && !is_null($operationResultFilter)) {
  395.             $operationResultFilter strtolower($operationResultFilter);
  396.             if ($operationResultFilter != 'succeeded' && $operationResultFilter != 'failed' && $operationResultFilter != 'inprogress'{
  397.                 throw new Microsoft_WindowsAzure_Management_Exception('OperationResultFilter should be succeeded|failed|inprogress.');
  398.             }
  399.         }
  400.         
  401.         $parameters array();
  402.         $parameters['StartTime=' $startTime;
  403.         $parameters['EndTime=' $endTime;
  404.         if ($objectIdFilter != '' && !is_null($objectIdFilter)) {
  405.             $parameters['ObjectIdFilter=' $objectIdFilter;
  406.         }
  407.         if ($operationResultFilter != '' && !is_null($operationResultFilter)) {
  408.             $parameters['OperationResultFilter=' ucfirst($operationResultFilter);
  409.         }
  410.         if ($continuationToken != '' && !is_null($continuationToken)) {
  411.             $parameters['ContinuationToken=' $continuationToken;
  412.         }
  413.         
  414.         $response $this->_performRequest(self::OP_OPERATIONS'?' implode('&'$parameters));
  415.  
  416.         if ($response->isSuccessful()) {
  417.             $result $this->_parseResponse($response);
  418.             $namespaces $result->getDocNamespaces()
  419.             $result->registerXPathNamespace('__empty_ns'$namespaces['']);
  420.  
  421.             $xmlOperations $result->xpath('//__empty_ns:SubscriptionOperation');
  422.             
  423.             // Create return value
  424.             $returnValue array();            
  425.             foreach ($xmlOperations as $xmlOperation{
  426.                 // Create operation instance
  427.                 $operation new Microsoft_WindowsAzure_Management_SubscriptionOperationInstance(
  428.                     $xmlOperation->OperationId,
  429.                     $xmlOperation->OperationObjectId,
  430.                     $xmlOperation->OperationName,
  431.                     array(),
  432.                     (array)$xmlOperation->OperationCaller,
  433.                     (array)$xmlOperation->OperationStatus
  434.                 );
  435.                 
  436.                 // Parse parameters
  437.                 $xmlOperation->registerXPathNamespace('__empty_ns'$namespaces[''])
  438.                 $xmlParameters $xmlOperation->xpath('.//__empty_ns:OperationParameter');
  439.                 foreach ($xmlParameters as $xmlParameter{
  440.                     $xmlParameterDetails $xmlParameter->children('http://schemas.datacontract.org/2004/07/Microsoft.Samples.WindowsAzure.ServiceManagement');
  441.                     $operation->addOperationParameter((string)$xmlParameterDetails->Name(string)$xmlParameterDetails->Value);
  442.                 }
  443.                 
  444.                 // Add to result
  445.                 $returnValue[$operation;
  446.             }
  447.             
  448.             // More data?
  449.             if (!is_null($result->ContinuationToken&& $result->ContinuationToken != ''{
  450.                 $returnValue array_merge($returnValue$this->listSubscriptionOperations($startTime$endTime$objectIdFilter$operationResultFilter(string)$result->ContinuationToken));
  451.             }
  452.             
  453.             // Return
  454.             return $returnValue;
  455.         else {
  456.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  457.         }
  458.     }
  459.     
  460.     /**
  461.      * Wait for an operation to complete
  462.      * 
  463.      * @param string $requestId The request ID. If omitted, the last request ID will be used.
  464.      * @param int $sleepInterval Sleep interval in milliseconds.
  465.      * @return Microsoft_WindowsAzure_Management_OperationStatusInstance 
  466.      * @throws Microsoft_WindowsAzure_Management_Exception
  467.      */
  468.     public function waitForOperation($requestId ''$sleepInterval 250)
  469.     {
  470.         if ($requestId == ''{
  471.             $requestId $this->getLastRequestId();
  472.         }
  473.         if ($requestId == '' || is_null($requestId)) {
  474.             return null;
  475.         }
  476.  
  477.         $status $this->getOperationStatus($requestId);
  478.         while ($status->Status == 'InProgress'{
  479.           $status $this->getOperationStatus($requestId);
  480.           usleep($sleepInterval);
  481.         }
  482.         
  483.         return $status;
  484.     }
  485.     
  486.     /**
  487.      * Creates a new Microsoft_WindowsAzure_Storage_Blob instance for the current account
  488.      *
  489.      * @param string $serviceName the service name to create a storage client for.
  490.      * @param Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy Retry policy to use when making requests
  491.      * @return Microsoft_WindowsAzure_Storage_Blob 
  492.      */
  493.     public function createBlobClientForService($serviceNameMicrosoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy null)
  494.     {
  495.         if ($serviceName == '' || is_null($serviceName)) {
  496.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  497.         }
  498.         
  499.         $storageKeys $this->getStorageAccountKeys($serviceName);
  500.         
  501.         return new Microsoft_WindowsAzure_Storage_Blob(
  502.             Microsoft_WindowsAzure_Storage::URL_CLOUD_BLOB,
  503.             $serviceName,
  504.             $storageKeys[0],
  505.             false,
  506.             $retryPolicy
  507.         );
  508.     }
  509.     
  510.     /**
  511.      * Creates a new Microsoft_WindowsAzure_Storage_Table instance for the current account
  512.      *
  513.      * @param string $serviceName the service name to create a storage client for.
  514.      * @param Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy Retry policy to use when making requests
  515.      * @return Microsoft_WindowsAzure_Storage_Table 
  516.      */
  517.     public function createTableClientForService($serviceNameMicrosoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy null)
  518.     {
  519.         if ($serviceName == '' || is_null($serviceName)) {
  520.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  521.         }
  522.         
  523.         $storageKeys $this->getStorageAccountKeys($serviceName);
  524.         
  525.         return new Microsoft_WindowsAzure_Storage_Table(
  526.             Microsoft_WindowsAzure_Storage::URL_CLOUD_TABLE,
  527.             $serviceName,
  528.             $storageKeys[0],
  529.             false,
  530.             $retryPolicy
  531.         );
  532.     }
  533.     
  534.     /**
  535.      * Creates a new Microsoft_WindowsAzure_Storage_Queue instance for the current account
  536.      *
  537.      * @param string $serviceName the service name to create a storage client for.
  538.      * @param Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy Retry policy to use when making requests
  539.      * @return Microsoft_WindowsAzure_Storage_Queue 
  540.      */
  541.     public function createQueueClientForService($serviceNameMicrosoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy null)
  542.     {
  543.         if ($serviceName == '' || is_null($serviceName)) {
  544.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  545.         }
  546.         
  547.         $storageKeys $this->getStorageAccountKeys($serviceName);
  548.         
  549.         return new Microsoft_WindowsAzure_Storage_Queue(
  550.             Microsoft_WindowsAzure_Storage::URL_CLOUD_QUEUE,
  551.             $serviceName,
  552.             $storageKeys[0],
  553.             false,
  554.             $retryPolicy
  555.         );
  556.     }
  557.     
  558.     /**
  559.      * The List Storage Accounts operation lists the storage accounts available under
  560.      * the current subscription.
  561.      *
  562.      * @return array An array of Microsoft_WindowsAzure_Management_StorageServiceInstance
  563.      */
  564.     public function listStorageAccounts()
  565.     {
  566.         $response $this->_performRequest(self::OP_STORAGE_ACCOUNTS);
  567.  
  568.         if ($response->isSuccessful()) {
  569.             $result $this->_parseResponse($response);
  570.             
  571.             if (!$result->StorageService{
  572.                 return array();
  573.             }
  574.             if (count($result->StorageService1{
  575.                 $xmlServices $result->StorageService;
  576.             else {
  577.                 $xmlServices array($result->StorageService);
  578.             }
  579.             
  580.             $services array();
  581.             if (!is_null($xmlServices)) {                
  582.                 for ($i 0$i count($xmlServices)$i++{
  583.                     $services[new Microsoft_WindowsAzure_Management_StorageServiceInstance(
  584.                         (string)$xmlServices[$i]->Url,
  585.                         (string)$xmlServices[$i]->ServiceName
  586.                     );
  587.                 }
  588.             }
  589.             return $services;
  590.         else {
  591.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  592.         }
  593.     }
  594.     
  595.     /**
  596.      * The Get Storage Account Properties operation returns the system properties for the
  597.      * specified storage account. These properties include: the address, description, and
  598.      * label of the storage account; and the name of the affinity group to which the service
  599.      * belongs, or its geo-location if it is not part of an affinity group.
  600.      *
  601.      * @param string $serviceName The name of your service.
  602.      * @return Microsoft_WindowsAzure_Management_StorageServiceInstance 
  603.      * @throws Microsoft_WindowsAzure_Management_Exception
  604.      */
  605.     public function getStorageAccountProperties($serviceName)
  606.     {
  607.         if ($serviceName == '' || is_null($serviceName)) {
  608.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  609.         }
  610.         
  611.         $response $this->_performRequest(self::OP_STORAGE_ACCOUNTS '/' $serviceName);
  612.  
  613.         if ($response->isSuccessful()) {
  614.             $xmlService $this->_parseResponse($response);
  615.  
  616.             if (!is_null($xmlService)) {
  617.                 return new Microsoft_WindowsAzure_Management_StorageServiceInstance(
  618.                     (string)$xmlService->Url,
  619.                     (string)$xmlService->ServiceName,
  620.                     (string)$xmlService->StorageServiceProperties->Description,
  621.                     (string)$xmlService->StorageServiceProperties->AffinityGroup,
  622.                     (string)$xmlService->StorageServiceProperties->Location,
  623.                     (string)$xmlService->StorageServiceProperties->Label
  624.                 );
  625.             }
  626.             return null;
  627.         else {
  628.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  629.         }
  630.     }
  631.     
  632.     /**
  633.      * The Get Storage Keys operation returns the primary
  634.      * and secondary access keys for the specified storage account.
  635.      *
  636.      * @param string $serviceName The name of your service.
  637.      * @return array An array of strings
  638.      * @throws Microsoft_WindowsAzure_Management_Exception
  639.      */
  640.     public function getStorageAccountKeys($serviceName)
  641.     {
  642.         if ($serviceName == '' || is_null($serviceName)) {
  643.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  644.         }
  645.         
  646.         $response $this->_performRequest(self::OP_STORAGE_ACCOUNTS '/' $serviceName '/keys');
  647.  
  648.         if ($response->isSuccessful()) {
  649.             $xmlService $this->_parseResponse($response);
  650.  
  651.             if (!is_null($xmlService)) {
  652.                 return array(
  653.                     (string)$xmlService->StorageServiceKeys->Primary,
  654.                     (string)$xmlService->StorageServiceKeys->Secondary
  655.                 );
  656.             }
  657.             return array();
  658.         else {
  659.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  660.         }
  661.     }
  662.     
  663.     /**
  664.      * The Regenerate Keys operation regenerates the primary
  665.      * or secondary access key for the specified storage account.
  666.      *
  667.      * @param string $serviceName The name of your service.
  668.      * @param string $key          The key to regenerate (primary or secondary)
  669.      * @return array An array of strings
  670.      * @throws Microsoft_WindowsAzure_Management_Exception
  671.      */
  672.     public function regenerateStorageAccountKey($serviceName$key 'primary')
  673.     {
  674.         if ($serviceName == '' || is_null($serviceName)) {
  675.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  676.         }
  677.         $key strtolower($key);
  678.         if ($key != 'primary' && $key != 'secondary'{
  679.             throw new Microsoft_WindowsAzure_Management_Exception('Key identifier should be primary|secondary.');
  680.         }
  681.         
  682.         $response $this->_performRequest(
  683.             self::OP_STORAGE_ACCOUNTS '/' $serviceName '/keys''?action=regenerate',
  684.             Microsoft_Http_Client::POST,
  685.             array('Content-Type' => 'application/xml'),
  686.             '<?xml version="1.0" encoding="utf-8"?>
  687.              <RegenerateKeys xmlns="http://schemas.microsoft.com/windowsazure">
  688.                <KeyType>' ucfirst($key'</KeyType>
  689.              </RegenerateKeys>');
  690.  
  691.         if ($response->isSuccessful()) {
  692.             $xmlService $this->_parseResponse($response);
  693.  
  694.             if (!is_null($xmlService)) {
  695.                 return array(
  696.                     (string)$xmlService->StorageServiceKeys->Primary,
  697.                     (string)$xmlService->StorageServiceKeys->Secondary
  698.                 );
  699.             }
  700.             return array();
  701.         else {
  702.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  703.         }
  704.     }
  705.     
  706.     /**
  707.      * The List Hosted Services operation lists the hosted services available
  708.      * under the current subscription.
  709.      *
  710.      * @return array An array of Microsoft_WindowsAzure_Management_HostedServiceInstance
  711.      * @throws Microsoft_WindowsAzure_Management_Exception
  712.      */
  713.     public function listHostedServices()
  714.     {
  715.         $response $this->_performRequest(self::OP_HOSTED_SERVICES);
  716.  
  717.         if ($response->isSuccessful()) {
  718.             $result $this->_parseResponse($response);
  719.             
  720.             if (!$result->HostedService{
  721.                 return array();
  722.             }
  723.             if (count($result->HostedService1{
  724.                 $xmlServices $result->HostedService;
  725.             else {
  726.                 $xmlServices array($result->HostedService);
  727.             }
  728.             
  729.             $services array();
  730.             if (!is_null($xmlServices)) {                
  731.                 for ($i 0$i count($xmlServices)$i++{
  732.                     $services[new Microsoft_WindowsAzure_Management_HostedServiceInstance(
  733.                         (string)$xmlServices[$i]->Url,
  734.                         (string)$xmlServices[$i]->ServiceName
  735.                     );
  736.                 }
  737.             }
  738.             return $services;
  739.         else {
  740.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  741.         }
  742.     }
  743.     
  744.     /**
  745.      * The Create Hosted Service operation creates a new hosted service in Windows Azure.
  746.      * 
  747.      * @param string $serviceName A name for the hosted service that is unique to the subscription.
  748.      * @param string $label A label for the hosted service. The label may be up to 100 characters in length.
  749.      * @param string $description A description for the hosted service. The description may be up to 1024 characters in length.
  750.      * @param string $location Required if AffinityGroup is not specified. The location where the hosted service will be created.
  751.      * @param string $affinityGroup Required if Location is not specified. The name of an existing affinity group associated with this subscription.
  752.      */
  753.     public function createHostedService($serviceName$label$description ''$location null$affinityGroup null)
  754.     {
  755.         if ($serviceName == '' || is_null($serviceName)) {
  756.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  757.         }
  758.         if ($label == '' || is_null($label)) {
  759.             throw new Microsoft_WindowsAzure_Management_Exception('Label should be specified.');
  760.         }
  761.         if (strlen($label100{
  762.             throw new Microsoft_WindowsAzure_Management_Exception('Label is too long. The maximum length is 100 characters.');
  763.         }
  764.         if (strlen($description1024{
  765.             throw new Microsoft_WindowsAzure_Management_Exception('Description is too long. The maximum length is 1024 characters.');
  766.         }
  767.         if ( (is_null($location&& is_null($affinityGroup)) || (!is_null($location&& !is_null($affinityGroup)) ) {
  768.             throw new Microsoft_WindowsAzure_Management_Exception('Please specify a location -or- an affinity group for the service.');
  769.         }
  770.         
  771.         $locationOrAffinityGroup is_null($location)
  772.             ? '<AffinityGroup>' $affinityGroup '</AffinityGroup>'
  773.             : '<Location>' $location '</Location>';
  774.         
  775.         $response $this->_performRequest(self::OP_HOSTED_SERVICES'',
  776.             Microsoft_Http_Client::POST,
  777.             array('Content-Type' => 'application/xml; charset=utf-8'),
  778.             '<CreateHostedService xmlns="http://schemas.microsoft.com/windowsazure"><ServiceName>' $serviceName '</ServiceName><Label>' base64_encode($label'</Label><Description>' $description '</Description>' $locationOrAffinityGroup '</CreateHostedService>');
  779.      
  780.         if (!$response->isSuccessful()) {
  781.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  782.         }
  783.     }
  784.     
  785.     /**
  786.      * The Update Hosted Service operation updates the label and/or the description for a hosted service in Windows Azure.
  787.      * 
  788.      * @param string $serviceName A name for the hosted service that is unique to the subscription.
  789.      * @param string $label A label for the hosted service. The label may be up to 100 characters in length.
  790.      * @param string $description A description for the hosted service. The description may be up to 1024 characters in length.
  791.      */
  792.     public function updateHostedService($serviceName$label$description '')
  793.     {
  794.         if ($serviceName == '' || is_null($serviceName)) {
  795.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  796.         }
  797.         if ($label == '' || is_null($label)) {
  798.             throw new Microsoft_WindowsAzure_Management_Exception('Label should be specified.');
  799.         }
  800.         if (strlen($label100{
  801.             throw new Microsoft_WindowsAzure_Management_Exception('Label is too long. The maximum length is 100 characters.');
  802.         }
  803.         
  804.         $response $this->_performRequest(self::OP_HOSTED_SERVICES '/' $serviceName'',
  805.             Microsoft_Http_Client::PUT,
  806.             array('Content-Type' => 'application/xml; charset=utf-8'),
  807.             '<UpdateHostedService xmlns="http://schemas.microsoft.com/windowsazure"><Label>' base64_encode($label'</Label><Description>' $description '</Description></UpdateHostedService>');
  808.      
  809.         if (!$response->isSuccessful()) {
  810.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  811.         }
  812.     }
  813.     
  814.     /**
  815.      * The Delete Hosted Service operation deletes the specified hosted service in Windows Azure.
  816.      * 
  817.      * @param string $serviceName A name for the hosted service that is unique to the subscription.
  818.      */
  819.     public function deleteHostedService($serviceName)
  820.     {
  821.         if ($serviceName == '' || is_null($serviceName)) {
  822.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  823.         }
  824.         
  825.         $response $this->_performRequest(self::OP_HOSTED_SERVICES '/' $serviceName''Microsoft_Http_Client::DELETE);
  826.      
  827.         if (!$response->isSuccessful()) {
  828.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  829.         }
  830.     }
  831.     
  832.     /**
  833.      * The Get Hosted Service Properties operation retrieves system properties
  834.      * for the specified hosted service. These properties include the service
  835.      * name and service type; the name of the affinity group to which the service
  836.      * belongs, or its location if it is not part of an affinity group; and
  837.      * optionally, information on the service's deployments.
  838.      *
  839.      * @param string $serviceName The name of your service.
  840.      * @return Microsoft_WindowsAzure_Management_HostedServiceInstance 
  841.      * @throws Microsoft_WindowsAzure_Management_Exception
  842.      */
  843.     public function getHostedServiceProperties($serviceName)
  844.     {
  845.         if ($serviceName == '' || is_null($serviceName)) {
  846.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  847.         }
  848.         
  849.         $response $this->_performRequest(self::OP_HOSTED_SERVICES '/' $serviceName'?embed-detail=true');
  850.  
  851.         if ($response->isSuccessful()) {
  852.             $xmlService $this->_parseResponse($response);
  853.  
  854.             if (!is_null($xmlService)) {
  855.                 $returnValue new Microsoft_WindowsAzure_Management_HostedServiceInstance(
  856.                     (string)$xmlService->Url,
  857.                     (string)$xmlService->ServiceName,
  858.                     (string)$xmlService->HostedServiceProperties->Description,
  859.                     (string)$xmlService->HostedServiceProperties->AffinityGroup,
  860.                     (string)$xmlService->HostedServiceProperties->Location,
  861.                     (string)$xmlService->HostedServiceProperties->Label
  862.                 );
  863.                 
  864.                 // Deployments
  865.                 if (count($xmlService->Deployments->Deployment1{
  866.                     $xmlServices $xmlService->Deployments->Deployment;
  867.                 else {
  868.                     $xmlServices array($xmlService->Deployments->Deployment);
  869.                 }
  870.                 
  871.                 $deployments array();
  872.                 foreach ($xmlServices as $xmlDeployment{
  873.                     $deployments[$this->_convertXmlElementToDeploymentInstance($xmlDeployment);
  874.                 }
  875.                 $returnValue->Deployments $deployments;
  876.                 
  877.                 return $returnValue;
  878.             }
  879.             return null;
  880.         else {
  881.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  882.         }
  883.     }
  884.  
  885.     /**
  886.      * The Create Deployment operation uploads a new service package
  887.      * and creates a new deployment on staging or production.
  888.      * 
  889.      * @param string $serviceName        The service name
  890.      * @param string $deploymentSlot    The deployment slot (production or staging)
  891.      * @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.
  892.      * @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.
  893.      * @param string $packageUrl        The service configuration file for the deployment.
  894.      * @param string $configuration     A label for this deployment, up to 100 characters in length.
  895.      * @param boolean $startDeployment  Indicates whether to start the deployment immediately after it is created.
  896.      * @param boolean $treatWarningsAsErrors Indicates whether to treat package validation warnings as errors.
  897.      * @throws Microsoft_WindowsAzure_Management_Exception
  898.      */
  899.     public function createDeployment($serviceName$deploymentSlot$name$label$packageUrl$configuration$startDeployment false$treatWarningsAsErrors false)
  900.     {
  901.         if ($serviceName == '' || is_null($serviceName)) {
  902.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  903.         }
  904.         $deploymentSlot strtolower($deploymentSlot);
  905.         if ($deploymentSlot != 'production' && $deploymentSlot != 'staging'{
  906.             throw new Microsoft_WindowsAzure_Management_Exception('Deployment slot should be production|staging.');
  907.         }
  908.         if ($name == '' || is_null($name)) {
  909.             throw new Microsoft_WindowsAzure_Management_Exception('Name should be specified.');
  910.         }
  911.         if ($label == '' || is_null($label)) {
  912.             throw new Microsoft_WindowsAzure_Management_Exception('Label should be specified.');
  913.         }
  914.         if (strlen($label100{
  915.             throw new Microsoft_WindowsAzure_Management_Exception('Label is too long. The maximum length is 100 characters.');
  916.         }
  917.         if ($packageUrl == '' || is_null($packageUrl)) {
  918.             throw new Microsoft_WindowsAzure_Management_Exception('Package URL should be specified.');
  919.         }
  920.         if ($configuration == '' || is_null($configuration)) {
  921.             throw new Microsoft_WindowsAzure_Management_Exception('Configuration should be specified.');
  922.         }
  923.         
  924.         if (@file_exists($configuration)) {
  925.             $configuration utf8_decode(file_get_contents($configuration));
  926.         }
  927.         
  928.         // Clean up the configuration
  929.         $conformingConfiguration $this->_cleanConfiguration($configuration);
  930.         
  931.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/deploymentslots/' $deploymentSlot;
  932.         $response $this->_performRequest($operationUrl'',
  933.             Microsoft_Http_Client::POST,
  934.             array('Content-Type' => 'application/xml; charset=utf-8'),
  935.             '<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>');
  936.      
  937.         if (!$response->isSuccessful()) {
  938.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  939.         }        
  940.     }
  941.     
  942.     /**
  943.      * The Get Deployment operation returns configuration information, status,
  944.      * and system properties for the specified deployment.
  945.      * 
  946.      * @param string $serviceName        The service name
  947.      * @param string $deploymentSlot    The deployment slot (production or staging)
  948.      * @return Microsoft_WindowsAzure_Management_DeploymentInstance 
  949.      * @throws Microsoft_WindowsAzure_Management_Exception
  950.      */
  951.     public function getDeploymentBySlot($serviceName$deploymentSlot)
  952.     {
  953.         if ($serviceName == '' || is_null($serviceName)) {
  954.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  955.         }
  956.         $deploymentSlot strtolower($deploymentSlot);
  957.         if ($deploymentSlot != 'production' && $deploymentSlot != 'staging'{
  958.             throw new Microsoft_WindowsAzure_Management_Exception('Deployment slot should be production|staging.');
  959.         }
  960.         
  961.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/deploymentslots/' $deploymentSlot;
  962.         return $this->_getDeployment($operationUrl);
  963.     }
  964.     
  965.     /**
  966.      * The Get Deployment operation returns configuration information, status,
  967.      * and system properties for the specified deployment.
  968.      * 
  969.      * @param string $serviceName        The service name
  970.      * @param string $deploymentId    The deployment ID as listed on the Windows Azure management portal
  971.      * @return Microsoft_WindowsAzure_Management_DeploymentInstance 
  972.      * @throws Microsoft_WindowsAzure_Management_Exception
  973.      */
  974.     public function getDeploymentByDeploymentId($serviceName$deploymentId)
  975.     {
  976.         if ($serviceName == '' || is_null($serviceName)) {
  977.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  978.         }
  979.         if ($deploymentId == '' || is_null($deploymentId)) {
  980.             throw new Microsoft_WindowsAzure_Management_Exception('Deployment ID should be specified.');
  981.         }
  982.         
  983.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/deployments/' $deploymentId;
  984.         return $this->_getDeployment($operationUrl);
  985.     }
  986.     
  987.     /**
  988.      * The Get Deployment operation returns configuration information, status,
  989.      * and system properties for the specified deployment.
  990.      * 
  991.      * @param string $operationUrl        The operation url
  992.      * @return Microsoft_WindowsAzure_Management_DeploymentInstance 
  993.      * @throws Microsoft_WindowsAzure_Management_Exception
  994.      */
  995.     protected function _getDeployment($operationUrl)
  996.     {
  997.         $response $this->_performRequest($operationUrl);
  998.  
  999.         if ($response->isSuccessful()) {
  1000.             $xmlService $this->_parseResponse($response);
  1001.             
  1002.             return $this->_convertXmlElementToDeploymentInstance($xmlService);
  1003.         else {
  1004.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  1005.         }
  1006.     }
  1007.     
  1008.     /**
  1009.      * The Swap Deployment operation initiates a virtual IP swap between
  1010.      * the staging and production deployment environments for a service.
  1011.      * If the service is currently running in the staging environment,
  1012.      * it will be swapped to the production environment. If it is running
  1013.      * in the production environment, it will be swapped to staging.
  1014.      * 
  1015.      * @param string $serviceName The service name.
  1016.      * @param string $productionDeploymentName The name of the production deployment.
  1017.      * @param string $sourceDeploymentName The name of the source deployment.
  1018.      * @throws Microsoft_WindowsAzure_Management_Exception
  1019.      */
  1020.     public function swapDeployment($serviceName$productionDeploymentName$sourceDeploymentName)
  1021.     {
  1022.         if ($serviceName == '' || is_null($serviceName)) {
  1023.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  1024.         }
  1025.         if ($productionDeploymentName == '' || is_null($productionDeploymentName)) {
  1026.             throw new Microsoft_WindowsAzure_Management_Exception('Production Deployment ID should be specified.');
  1027.         }
  1028.         if ($sourceDeploymentName == '' || is_null($sourceDeploymentName)) {
  1029.             throw new Microsoft_WindowsAzure_Management_Exception('Source Deployment ID should be specified.');
  1030.         }
  1031.         
  1032.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName;
  1033.         $response $this->_performRequest($operationUrl'',
  1034.             Microsoft_Http_Client::POST,
  1035.             array('Content-Type' => 'application/xml; charset=utf-8'),
  1036.             '<Swap xmlns="http://schemas.microsoft.com/windowsazure"><Production>' $productionDeploymentName '</Production><SourceDeployment>' $sourceDeploymentName '</SourceDeployment></Swap>');
  1037.             
  1038.         if (!$response->isSuccessful()) {
  1039.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  1040.         }        
  1041.     }
  1042.     
  1043.     /**
  1044.      * The Delete Deployment operation deletes the specified deployment.
  1045.      * 
  1046.      * @param string $serviceName        The service name
  1047.      * @param string $deploymentSlot    The deployment slot (production or staging)
  1048.      * @throws Microsoft_WindowsAzure_Management_Exception
  1049.      */
  1050.     public function deleteDeploymentBySlot($serviceName$deploymentSlot)
  1051.     {
  1052.         if ($serviceName == '' || is_null($serviceName)) {
  1053.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  1054.         }
  1055.         $deploymentSlot strtolower($deploymentSlot);
  1056.         if ($deploymentSlot != 'production' && $deploymentSlot != 'staging'{
  1057.             throw new Microsoft_WindowsAzure_Management_Exception('Deployment slot should be production|staging.');
  1058.         }
  1059.         
  1060.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/deploymentslots/' $deploymentSlot;
  1061.         return $this->_deleteDeployment($operationUrl);
  1062.     }
  1063.     
  1064.     /**
  1065.      * The Delete Deployment operation deletes the specified deployment.
  1066.      * 
  1067.      * @param string $serviceName        The service name
  1068.      * @param string $deploymentId    The deployment ID as listed on the Windows Azure management portal
  1069.      * @throws Microsoft_WindowsAzure_Management_Exception
  1070.      */
  1071.     public function deleteDeploymentByDeploymentId($serviceName$deploymentId)
  1072.     {
  1073.         if ($serviceName == '' || is_null($serviceName)) {
  1074.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  1075.         }
  1076.         if ($deploymentId == '' || is_null($deploymentId)) {
  1077.             throw new Microsoft_WindowsAzure_Management_Exception('Deployment ID should be specified.');
  1078.         }
  1079.         
  1080.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/deployments/' $deploymentId;
  1081.         return $this->_deleteDeployment($operationUrl);
  1082.     }
  1083.     
  1084.     /**
  1085.      * The Delete Deployment operation deletes the specified deployment.
  1086.      * 
  1087.      * @param string $operationUrl        The operation url
  1088.      * @throws Microsoft_WindowsAzure_Management_Exception
  1089.      */
  1090.     protected function _deleteDeployment($operationUrl)
  1091.     {
  1092.         $response $this->_performRequest($operationUrl''Microsoft_Http_Client::DELETE);
  1093.              
  1094.         if (!$response->isSuccessful()) {
  1095.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  1096.         }
  1097.     }
  1098.     
  1099.     /**
  1100.      * The Update Deployment Status operation initiates a change in deployment status.
  1101.      * 
  1102.      * @param string $serviceName        The service name
  1103.      * @param string $deploymentSlot    The deployment slot (production or staging)
  1104.      * @param string $status            The deployment status (running|suspended)
  1105.      * @throws Microsoft_WindowsAzure_Management_Exception
  1106.      */
  1107.     public function updateDeploymentStatusBySlot($serviceName$deploymentSlot$status 'running')
  1108.     {
  1109.         if ($serviceName == '' || is_null($serviceName)) {
  1110.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  1111.         }
  1112.         $deploymentSlot strtolower($deploymentSlot);
  1113.         if ($deploymentSlot != 'production' && $deploymentSlot != 'staging'{
  1114.             throw new Microsoft_WindowsAzure_Management_Exception('Deployment slot should be production|staging.');
  1115.         }
  1116.         $status strtolower($status);
  1117.         if ($status != 'running' && $status != 'suspended'{
  1118.             throw new Microsoft_WindowsAzure_Management_Exception('Status should be running|suspended.');
  1119.         }
  1120.         
  1121.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/deploymentslots/' $deploymentSlot;
  1122.         return $this->_updateDeploymentStatus($operationUrl$status);
  1123.     }
  1124.     
  1125.     /**
  1126.      * The Update Deployment Status operation initiates a change in deployment status.
  1127.      * 
  1128.      * @param string $serviceName        The service name
  1129.      * @param string $deploymentId    The deployment ID as listed on the Windows Azure management portal
  1130.      * @param string $status            The deployment status (running|suspended)
  1131.      * @throws Microsoft_WindowsAzure_Management_Exception
  1132.      */
  1133.     public function updateDeploymentStatusByDeploymentId($serviceName$deploymentId$status 'running')
  1134.     {
  1135.         if ($serviceName == '' || is_null($serviceName)) {
  1136.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  1137.         }
  1138.         if ($deploymentId == '' || is_null($deploymentId)) {
  1139.             throw new Microsoft_WindowsAzure_Management_Exception('Deployment ID should be specified.');
  1140.         }
  1141.         $status strtolower($status);
  1142.         if ($status != 'running' && $status != 'suspended'{
  1143.             throw new Microsoft_WindowsAzure_Management_Exception('Status should be running|suspended.');
  1144.         }
  1145.         
  1146.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/deployments/' $deploymentId;
  1147.         return $this->_updateDeploymentStatus($operationUrl$status);
  1148.     }
  1149.     
  1150.     /**
  1151.      * The Update Deployment Status operation initiates a change in deployment status.
  1152.      * 
  1153.      * @param string $operationUrl        The operation url
  1154.      * @param string $status            The deployment status (running|suspended)
  1155.      * @throws Microsoft_WindowsAzure_Management_Exception
  1156.      */
  1157.     protected function _updateDeploymentStatus($operationUrl$status 'running')
  1158.     {
  1159.         $response $this->_performRequest($operationUrl '/''?comp=status',
  1160.             Microsoft_Http_Client::POST,
  1161.             array('Content-Type' => 'application/xml; charset=utf-8'),
  1162.             '<UpdateDeploymentStatus xmlns="http://schemas.microsoft.com/windowsazure"><Status>' ucfirst($status'</Status></UpdateDeploymentStatus>');
  1163.             
  1164.         if (!$response->isSuccessful()) {
  1165.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  1166.         }
  1167.     }
  1168.     
  1169.     /**
  1170.      * Converts an XmlElement into a Microsoft_WindowsAzure_Management_DeploymentInstance
  1171.      * 
  1172.      * @param object $xmlService The XML Element
  1173.      * @return Microsoft_WindowsAzure_Management_DeploymentInstance 
  1174.      * @throws Microsoft_WindowsAzure_Management_Exception
  1175.      */
  1176.     protected function _convertXmlElementToDeploymentInstance($xmlService)
  1177.     {
  1178.         if (!is_null($xmlService)) {
  1179.             $returnValue new Microsoft_WindowsAzure_Management_DeploymentInstance(
  1180.                 (string)$xmlService->Name,
  1181.                 (string)$xmlService->DeploymentSlot,
  1182.                 (string)$xmlService->PrivateID,
  1183.                 (string)$xmlService->Label,
  1184.                 (string)$xmlService->Url,
  1185.                 (string)$xmlService->Configuration,
  1186.                 (string)$xmlService->Status,
  1187.                 (string)$xmlService->UpgradeStatus,
  1188.                 (string)$xmlService->UpgradeType,
  1189.                 (string)$xmlService->CurrentUpgradeDomainState,
  1190.                 (string)$xmlService->CurrentUpgradeDomain,
  1191.                 (string)$xmlService->UpgradeDomainCount
  1192.             );
  1193.                 
  1194.             // Append role instances
  1195.             if ($xmlService->RoleInstanceList && $xmlService->RoleInstanceList->RoleInstance{
  1196.                 $xmlRoleInstances $xmlService->RoleInstanceList->RoleInstance;
  1197.                 if (count($xmlService->RoleInstanceList->RoleInstance== 1{
  1198.                     $xmlRoleInstances array($xmlService->RoleInstanceList->RoleInstance);
  1199.                 }
  1200.                     
  1201.                 $roleInstances array();
  1202.                 if (!is_null($xmlRoleInstances)) {                
  1203.                     for ($i 0$i count($xmlRoleInstances)$i++{
  1204.                         $roleInstances[array(
  1205.                             'rolename' => (string)$xmlRoleInstances[$i]->RoleName,
  1206.                             'instancename' => (string)$xmlRoleInstances[$i]->InstanceName,
  1207.                             'instancestatus' => (string)$xmlRoleInstances[$i]->InstanceStatus
  1208.                         );
  1209.                     }
  1210.                 }
  1211.                 
  1212.                 $returnValue->RoleInstanceList $roleInstances;
  1213.             }
  1214.             
  1215.             // Append roles
  1216.             if ($xmlService->RoleList && $xmlService->RoleList->Role{
  1217.                 $xmlRoles $xmlService->RoleList->Role;
  1218.                 if (count($xmlService->RoleList->Role== 1{
  1219.                     $xmlRoles array($xmlService->RoleList->Role);
  1220.                 }
  1221.                 
  1222.                 $roles array();
  1223.                 if (!is_null($xmlRoles)) {                
  1224.                     for ($i 0$i count($xmlRoles)$i++{
  1225.                         $roles[array(
  1226.                             'rolename' => (string)$xmlRoles[$i]->RoleName,
  1227.                             'osversion' => (!is_null($xmlRoles[$i]->OsVersion? (string)$xmlRoles[$i]->OsVersion : (string)$xmlRoles[$i]->OperatingSystemVersion)                    
  1228.                         );
  1229.                     }
  1230.                 }
  1231.                 $returnValue->RoleList $roles;
  1232.             }
  1233.                 
  1234.             return $returnValue;
  1235.         }
  1236.         return null;
  1237.     }
  1238.     
  1239.     /**
  1240.      * Updates a deployment's role instance count.
  1241.      * 
  1242.      * @param string $serviceName        The service name
  1243.      * @param string $deploymentSlot    The deployment slot (production or staging)
  1244.      * @param string|array$roleName    The role name
  1245.      * @param string|array$instanceCount The instance count
  1246.      * @throws Microsoft_WindowsAzure_Management_Exception
  1247.      */
  1248.     public function setInstanceCountBySlot($serviceName$deploymentSlot$roleName$instanceCount{
  1249.         if ($serviceName == '' || is_null($serviceName)) {
  1250.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  1251.         }
  1252.         $deploymentSlot strtolower($deploymentSlot);
  1253.         if ($deploymentSlot != 'production' && $deploymentSlot != 'staging'{
  1254.             throw new Microsoft_WindowsAzure_Management_Exception('Deployment slot should be production|staging.');
  1255.         }
  1256.         if ($roleName == '' || is_null($roleName)) {
  1257.             throw new Microsoft_WindowsAzure_Management_Exception('Role name name should be specified.');
  1258.         }
  1259.         
  1260.         // Get configuration
  1261.         $deployment $this->getDeploymentBySlot($serviceName$deploymentSlot);
  1262.         $configuration $deployment->Configuration;
  1263.         $configuration $this->_updateInstanceCountInConfiguration($roleName$instanceCount$configuration);
  1264.         
  1265.         // Update configuration
  1266.         $this->configureDeploymentBySlot($serviceName$deploymentSlot$configuration);        
  1267.     }
  1268.     
  1269.     /**
  1270.      * Updates a deployment's role instance count.
  1271.      * 
  1272.      * @param string $serviceName        The service name
  1273.      * @param string $deploymentSlot    The deployment slot (production or staging)
  1274.      * @param string|array$roleName    The role name
  1275.      * @param string|array$instanceCount The instance count
  1276.      * @throws Microsoft_WindowsAzure_Management_Exception
  1277.      */
  1278.     public function setInstanceCountByDeploymentId($serviceName$deploymentId$roleName$instanceCount)
  1279.     {
  1280.         if ($serviceName == '' || is_null($serviceName)) {
  1281.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  1282.         }
  1283.         if ($deploymentId == '' || is_null($deploymentId)) {
  1284.             throw new Microsoft_WindowsAzure_Management_Exception('Deployment ID should be specified.');
  1285.         }
  1286.         if ($roleName == '' || is_null($roleName)) {
  1287.             throw new Microsoft_WindowsAzure_Management_Exception('Role name name should be specified.');
  1288.         }
  1289.         
  1290.         // Get configuration
  1291.         $deployment $this->getDeploymentByDeploymentId($serviceName$deploymentId);
  1292.         $configuration $deployment->Configuration;
  1293.         $configuration $this->_updateInstanceCountInConfiguration($roleName$instanceCount$configuration);
  1294.         
  1295.         // Update configuration
  1296.         $this->configureDeploymentByDeploymentId($serviceName$deploymentId$configuration);
  1297.     }
  1298.     
  1299.     /**
  1300.      * Updates instance count in configuration XML.
  1301.      * 
  1302.      * @param string|array$roleName            The role name
  1303.      * @param string|array$instanceCount        The instance count
  1304.      * @param string $configuration             XML configuration represented as a string
  1305.      * @throws Microsoft_WindowsAzure_Management_Exception
  1306.      */
  1307.     protected function _updateInstanceCountInConfiguration($roleName$instanceCount$configuration{
  1308.         // Change variables
  1309.         if (!is_array($roleName)) {
  1310.             $roleName array($roleName);
  1311.         }
  1312.         if (!is_array($instanceCount)) {
  1313.             $instanceCount array($instanceCount);
  1314.         }
  1315.  
  1316.         $configuration preg_replace('/(<\?xml[^?]+?)utf-16/i''$1utf-8'$configuration);
  1317.         //$configuration = '<?xml version="1.0">' . substr($configuration, strpos($configuration, '>') + 2);
  1318.  
  1319.         $xml simplexml_load_string($configuration)
  1320.         
  1321.         // http://www.php.net/manual/en/simplexmlelement.xpath.php#97818
  1322.         $namespaces $xml->getDocNamespaces();
  1323.         $xml->registerXPathNamespace('__empty_ns'$namespaces[''])
  1324.     
  1325.         for ($i 0$i count($roleName)$i++{
  1326.             $elements $xml->xpath('//__empty_ns:Role[@name="' $roleName[$i'"]/__empty_ns:Instances');
  1327.     
  1328.             if (count($elements== 1{
  1329.                 $element $elements[0];
  1330.                 $element['count'$instanceCount[$i];
  1331.             
  1332.         }
  1333.         
  1334.         $configuration $xml->asXML();
  1335.         //$configuration = preg_replace('/(<\?xml[^?]+?)utf-8/i', '$1utf-16', $configuration);
  1336.  
  1337.         return $configuration;
  1338.     }
  1339.     
  1340.     /**
  1341.      * The Change Deployment Configuration request may be specified as follows.
  1342.      * Note that you can change a deployment's configuration either by specifying the deployment
  1343.      * environment (staging or production), or by specifying the deployment's unique name.
  1344.      * 
  1345.      * @param string $serviceName        The service name
  1346.      * @param string $deploymentSlot    The deployment slot (production or staging)
  1347.      * @param string $configuration     XML configuration represented as a string
  1348.      * @throws Microsoft_WindowsAzure_Management_Exception
  1349.      */
  1350.     public function configureDeploymentBySlot($serviceName$deploymentSlot$configuration)
  1351.     {
  1352.         if ($serviceName == '' || is_null($serviceName)) {
  1353.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  1354.         }
  1355.         $deploymentSlot strtolower($deploymentSlot);
  1356.         if ($deploymentSlot != 'production' && $deploymentSlot != 'staging'{
  1357.             throw new Microsoft_WindowsAzure_Management_Exception('Deployment slot should be production|staging.');
  1358.         }
  1359.         if ($configuration == '' || is_null($configuration)) {
  1360.             throw new Microsoft_WindowsAzure_Management_Exception('Configuration name should be specified.');
  1361.         }
  1362.         
  1363.         if (@file_exists($configuration)) {
  1364.             $configuration utf8_decode(file_get_contents($configuration));
  1365.         }
  1366.         
  1367.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/deploymentslots/' $deploymentSlot;
  1368.         return $this->_configureDeployment($operationUrl$configuration);
  1369.     }
  1370.     
  1371.     /**
  1372.      * The Change Deployment Configuration request may be specified as follows.
  1373.      * Note that you can change a deployment's configuration either by specifying the deployment
  1374.      * environment (staging or production), or by specifying the deployment's unique name.
  1375.      * 
  1376.      * @param string $serviceName        The service name
  1377.      * @param string $deploymentId    The deployment ID as listed on the Windows Azure management portal
  1378.      * @param string $configuration     XML configuration represented as a string
  1379.      * @throws Microsoft_WindowsAzure_Management_Exception
  1380.      */
  1381.     public function configureDeploymentByDeploymentId($serviceName$deploymentId$configuration)
  1382.     {
  1383.         if ($serviceName == '' || is_null($serviceName)) {
  1384.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  1385.         }
  1386.         if ($deploymentId == '' || is_null($deploymentId)) {
  1387.             throw new Microsoft_WindowsAzure_Management_Exception('Deployment ID should be specified.');
  1388.         }
  1389.         if ($configuration == '' || is_null($configuration)) {
  1390.             throw new Microsoft_WindowsAzure_Management_Exception('Configuration name should be specified.');
  1391.         }
  1392.         
  1393.         if (@file_exists($configuration)) {
  1394.             $configuration utf8_decode(file_get_contents($configuration));
  1395.         }
  1396.         
  1397.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/deployments/' $deploymentId;
  1398.         return $this->_configureDeployment($operationUrl$configuration);
  1399.     }
  1400.     
  1401.     /**
  1402.      * The Change Deployment Configuration request may be specified as follows.
  1403.      * Note that you can change a deployment's configuration either by specifying the deployment
  1404.      * environment (staging or production), or by specifying the deployment's unique name.
  1405.      * 
  1406.      * @param string $operationUrl        The operation url
  1407.      * @param string $configuration     XML configuration represented as a string
  1408.      * @throws Microsoft_WindowsAzure_Management_Exception
  1409.      */
  1410.     protected function _configureDeployment($operationUrl$configuration)
  1411.     {
  1412.         // Clean up the configuration
  1413.         $conformingConfiguration $this->_cleanConfiguration($configuration);
  1414.  
  1415.         $response $this->_performRequest($operationUrl '/''?comp=config',
  1416.             Microsoft_Http_Client::POST,
  1417.             array('Content-Type' => 'application/xml; charset=utf-8'),
  1418.             '<ChangeConfiguration xmlns="http://schemas.microsoft.com/windowsazure" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Configuration>' base64_encode($conformingConfiguration'</Configuration></ChangeConfiguration>');
  1419.              
  1420.         if (!$response->isSuccessful()) {
  1421.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  1422.         }
  1423.     }
  1424.     
  1425.     /**
  1426.      * The Upgrade Deployment operation initiates an upgrade.
  1427.      * 
  1428.      * @param string $serviceName        The service name
  1429.      * @param string $deploymentSlot    The deployment slot (production or staging)
  1430.      * @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.
  1431.      * @param string $packageUrl        The service configuration file for the deployment.
  1432.      * @param string $configuration     A label for this deployment, up to 100 characters in length.
  1433.      * @param string $mode              The type of upgrade to initiate. Possible values are Auto or Manual.
  1434.      * @param string $roleToUpgrade     The name of the specific role to upgrade.
  1435.      * @throws Microsoft_WindowsAzure_Management_Exception
  1436.      */
  1437.     public function upgradeDeploymentBySlot($serviceName$deploymentSlot$label$packageUrl$configuration$mode 'auto'$roleToUpgrade null)
  1438.     {
  1439.         if ($serviceName == '' || is_null($serviceName)) {
  1440.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  1441.         }
  1442.         $deploymentSlot strtolower($deploymentSlot);
  1443.         if ($deploymentSlot != 'production' && $deploymentSlot != 'staging'{
  1444.             throw new Microsoft_WindowsAzure_Management_Exception('Deployment slot should be production|staging.');
  1445.         }
  1446.         if ($label == '' || is_null($label)) {
  1447.             throw new Microsoft_WindowsAzure_Management_Exception('Label should be specified.');
  1448.         }
  1449.         if (strlen($label100{
  1450.             throw new Microsoft_WindowsAzure_Management_Exception('Label is too long. The maximum length is 100 characters.');
  1451.         }
  1452.         if ($packageUrl == '' || is_null($packageUrl)) {
  1453.             throw new Microsoft_WindowsAzure_Management_Exception('Package URL should be specified.');
  1454.         }
  1455.         if ($configuration == '' || is_null($configuration)) {
  1456.             throw new Microsoft_WindowsAzure_Management_Exception('Configuration should be specified.');
  1457.         }
  1458.         $mode strtolower($mode);
  1459.         if ($mode != 'auto' && $mode != 'manual'{
  1460.             throw new Microsoft_WindowsAzure_Management_Exception('Mode should be auto|manual.');
  1461.         }
  1462.         
  1463.         if (@file_exists($configuration)) {
  1464.             $configuration utf8_decode(file_get_contents($configuration));
  1465.         }
  1466.         
  1467.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/deploymentslots/' $deploymentSlot;
  1468.         return $this->_upgradeDeployment($operationUrl$label$packageUrl$configuration$mode$roleToUpgrade);      
  1469.     }
  1470.     
  1471.     /**
  1472.      * The Upgrade Deployment operation initiates an upgrade.
  1473.      * 
  1474.      * @param string $serviceName        The service name
  1475.      * @param string $deploymentId    The deployment ID as listed on the Windows Azure management portal
  1476.      * @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.
  1477.      * @param string $packageUrl        The service configuration file for the deployment.
  1478.      * @param string $configuration     A label for this deployment, up to 100 characters in length.
  1479.      * @param string $mode              The type of upgrade to initiate. Possible values are Auto or Manual.
  1480.      * @param string $roleToUpgrade     The name of the specific role to upgrade.
  1481.      * @throws Microsoft_WindowsAzure_Management_Exception
  1482.      */
  1483.     public function upgradeDeploymentByDeploymentId($serviceName$deploymentId$label$packageUrl$configuration$mode 'auto'$roleToUpgrade null)
  1484.     {
  1485.         if ($serviceName == '' || is_null($serviceName)) {
  1486.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  1487.         }
  1488.         if ($deploymentId == '' || is_null($deploymentId)) {
  1489.             throw new Microsoft_WindowsAzure_Management_Exception('Deployment ID should be specified.');
  1490.         }
  1491.         if ($label == '' || is_null($label)) {
  1492.             throw new Microsoft_WindowsAzure_Management_Exception('Label should be specified.');
  1493.         }
  1494.         if (strlen($label100{
  1495.             throw new Microsoft_WindowsAzure_Management_Exception('Label is too long. The maximum length is 100 characters.');
  1496.         }
  1497.         if ($packageUrl == '' || is_null($packageUrl)) {
  1498.             throw new Microsoft_WindowsAzure_Management_Exception('Package URL should be specified.');
  1499.         }
  1500.         if ($configuration == '' || is_null($configuration)) {
  1501.             throw new Microsoft_WindowsAzure_Management_Exception('Configuration should be specified.');
  1502.         }
  1503.         $mode strtolower($mode);
  1504.         if ($mode != 'auto' && $mode != 'manual'{
  1505.             throw new Microsoft_WindowsAzure_Management_Exception('Mode should be auto|manual.');
  1506.         }
  1507.         
  1508.         if (@file_exists($configuration)) {
  1509.             $configuration utf8_decode(file_get_contents($configuration));
  1510.         }
  1511.         
  1512.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/deployments/' $deploymentId;
  1513.         return $this->_upgradeDeployment($operationUrl$label$packageUrl$configuration$mode$roleToUpgrade);      
  1514.     }
  1515.     
  1516.     
  1517.     /**
  1518.      * The Upgrade Deployment operation initiates an upgrade.
  1519.      * 
  1520.      * @param string $operationUrl        The operation url
  1521.      * @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.
  1522.      * @param string $packageUrl        The service configuration file for the deployment.
  1523.      * @param string $configuration     A label for this deployment, up to 100 characters in length.
  1524.      * @param string $mode              The type of upgrade to initiate. Possible values are Auto or Manual.
  1525.      * @param string $roleToUpgrade     The name of the specific role to upgrade.
  1526.      * @throws Microsoft_WindowsAzure_Management_Exception
  1527.      */
  1528.     protected function _upgradeDeployment($operationUrl$label$packageUrl$configuration$mode$roleToUpgrade)
  1529.     {
  1530.         // Clean up the configuration
  1531.         $conformingConfiguration $this->_cleanConfiguration($configuration);
  1532.         
  1533.         $response $this->_performRequest($operationUrl '/''?comp=upgrade',
  1534.             Microsoft_Http_Client::POST,
  1535.             array('Content-Type' => 'application/xml; charset=utf-8'),
  1536.             '<UpgradeDeployment xmlns="http://schemas.microsoft.com/windowsazure"><Mode>' ucfirst($mode'</Mode><PackageUrl>' $packageUrl '</PackageUrl><Configuration>' base64_encode($conformingConfiguration'</Configuration><Label>' base64_encode($label'</Label>' (!is_null($roleToUpgrade'<RoleToUpgrade>' $roleToUpgrade '</RoleToUpgrade>' '''</UpgradeDeployment>');        
  1537.             
  1538.         if (!$response->isSuccessful()) {
  1539.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  1540.         }
  1541.     }
  1542.     
  1543.     /**
  1544.      * The Walk Upgrade Domain operation specifies the next upgrade domain to be walked during an in-place upgrade.
  1545.      * 
  1546.      * @param string $serviceName        The service name
  1547.      * @param string $deploymentSlot    The deployment slot (production or staging)
  1548.      * @param int $upgradeDomain     An integer value that identifies the upgrade domain to walk. Upgrade domains are identified with a zero-based index: the first upgrade domain has an ID of 0, the second has an ID of 1, and so on.
  1549.      * @throws Microsoft_WindowsAzure_Management_Exception
  1550.      */
  1551.     public function walkUpgradeDomainBySlot($serviceName$deploymentSlot$upgradeDomain 0)
  1552.     {
  1553.         if ($serviceName == '' || is_null($serviceName)) {
  1554.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  1555.         }
  1556.         $deploymentSlot strtolower($deploymentSlot);
  1557.         if ($deploymentSlot != 'production' && $deploymentSlot != 'staging'{
  1558.             throw new Microsoft_WindowsAzure_Management_Exception('Deployment slot should be production|staging.');
  1559.         }
  1560.         
  1561.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/deploymentslots/' $deploymentSlot;
  1562.         return $this->_walkUpgradeDomain($operationUrl$upgradeDomain);      
  1563.     }
  1564.     
  1565.     /**
  1566.      * The Walk Upgrade Domain operation specifies the next upgrade domain to be walked during an in-place upgrade.
  1567.      * 
  1568.      * @param string $serviceName        The service name
  1569.      * @param string $deploymentId    The deployment ID as listed on the Windows Azure management portal
  1570.      * @param int $upgradeDomain     An integer value that identifies the upgrade domain to walk. Upgrade domains are identified with a zero-based index: the first upgrade domain has an ID of 0, the second has an ID of 1, and so on.
  1571.      * @throws Microsoft_WindowsAzure_Management_Exception
  1572.      */
  1573.     public function walkUpgradeDomainByDeploymentId($serviceName$deploymentId$upgradeDomain 0)
  1574.     {
  1575.         if ($serviceName == '' || is_null($serviceName)) {
  1576.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  1577.         }
  1578.         if ($deploymentId == '' || is_null($deploymentId)) {
  1579.             throw new Microsoft_WindowsAzure_Management_Exception('Deployment ID should be specified.');
  1580.         }
  1581.         
  1582.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/deployments/' $deploymentId;
  1583.         return $this->_walkUpgradeDomain($operationUrl$upgradeDomain);      
  1584.     }
  1585.     
  1586.     
  1587.     /**
  1588.      * The Walk Upgrade Domain operation specifies the next upgrade domain to be walked during an in-place upgrade.
  1589.      * 
  1590.      * @param string $operationUrl   The operation url
  1591.      * @param int $upgradeDomain     An integer value that identifies the upgrade domain to walk. Upgrade domains are identified with a zero-based index: the first upgrade domain has an ID of 0, the second has an ID of 1, and so on.
  1592.      * @throws Microsoft_WindowsAzure_Management_Exception
  1593.      */
  1594.     protected function _walkUpgradeDomain($operationUrl$upgradeDomain 0)
  1595.     {
  1596.         $response $this->_performRequest($operationUrl '/''?comp=walkupgradedomain',
  1597.             Microsoft_Http_Client::POST,
  1598.             array('Content-Type' => 'application/xml; charset=utf-8'),
  1599.             '<WalkUpgradeDomain xmlns="http://schemas.microsoft.com/windowsazure"><UpgradeDomain>' $upgradeDomain '</UpgradeDomain></WalkUpgradeDomain>');        
  1600.  
  1601.         if (!$response->isSuccessful()) {
  1602.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  1603.         }
  1604.     }
  1605.     
  1606.     /**
  1607.      * The Reboot Role Instance operation requests a reboot of a role instance
  1608.      * that is running in a deployment.
  1609.      * 
  1610.      * @param string $serviceName        The service name
  1611.      * @param string $deploymentSlot    The deployment slot (production or staging)
  1612.      * @param string $roleInstanceName  The role instance name
  1613.      * @throws Microsoft_WindowsAzure_Management_Exception
  1614.      */
  1615.     public function rebootRoleInstanceBySlot($serviceName$deploymentSlot$roleInstanceName)
  1616.     {
  1617.         if ($serviceName == '' || is_null($serviceName)) {
  1618.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  1619.         }
  1620.         $deploymentSlot strtolower($deploymentSlot);
  1621.         if ($deploymentSlot != 'production' && $deploymentSlot != 'staging'{
  1622.             throw new Microsoft_WindowsAzure_Management_Exception('Deployment slot should be production|staging.');
  1623.         }
  1624.         if ($roleInstanceName == '' || is_null($roleInstanceName)) {
  1625.             throw new Microsoft_WindowsAzure_Management_Exception('Role instance name should be specified.');
  1626.         }
  1627.         
  1628.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/deploymentslots/' $deploymentSlot '/roleinstances/' $roleInstanceName;
  1629.         return $this->_rebootOrReimageRoleInstance($operationUrl'reboot');
  1630.     }
  1631.     
  1632.     /**
  1633.      * The Reboot Role Instance operation requests a reboot of a role instance
  1634.      * that is running in a deployment.
  1635.      * 
  1636.      * @param string $serviceName        The service name
  1637.      * @param string $deploymentId    The deployment ID as listed on the Windows Azure management portal
  1638.      * @param string $roleInstanceName  The role instance name
  1639.      * @throws Microsoft_WindowsAzure_Management_Exception
  1640.      */
  1641.     public function rebootRoleInstanceByDeploymentId($serviceName$deploymentId$roleInstanceName)
  1642.     {
  1643.         if ($serviceName == '' || is_null($serviceName)) {
  1644.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  1645.         }
  1646.         if ($deploymentId == '' || is_null($deploymentId)) {
  1647.             throw new Microsoft_WindowsAzure_Management_Exception('Deployment ID should be specified.');
  1648.         }
  1649.         if ($roleInstanceName == '' || is_null($roleInstanceName)) {
  1650.             throw new Microsoft_WindowsAzure_Management_Exception('Role instance name should be specified.');
  1651.         }
  1652.         
  1653.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/deployments/' $deploymentId '/roleinstances/' $roleInstanceName;
  1654.         return $this->_rebootOrReimageRoleInstance($operationUrl'reboot');
  1655.     }
  1656.  
  1657.     /**
  1658.      * The Reimage Role Instance operation requests a reimage of a role instance
  1659.      * that is running in a deployment.
  1660.      * 
  1661.      * @param string $serviceName        The service name
  1662.      * @param string $deploymentSlot    The deployment slot (production or staging)
  1663.      * @param string $roleInstanceName  The role instance name
  1664.      * @throws Microsoft_WindowsAzure_Management_Exception
  1665.      */
  1666.     public function reimageRoleInstanceBySlot($serviceName$deploymentSlot$roleInstanceName)
  1667.     {
  1668.         if ($serviceName == '' || is_null($serviceName)) {
  1669.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  1670.         }
  1671.         $deploymentSlot strtolower($deploymentSlot);
  1672.         if ($deploymentSlot != 'production' && $deploymentSlot != 'staging'{
  1673.             throw new Microsoft_WindowsAzure_Management_Exception('Deployment slot should be production|staging.');
  1674.         }
  1675.         if ($roleInstanceName == '' || is_null($roleInstanceName)) {
  1676.             throw new Microsoft_WindowsAzure_Management_Exception('Role instance name should be specified.');
  1677.         }
  1678.         
  1679.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/deploymentslots/' $deploymentSlot '/roleinstances/' $roleInstanceName;
  1680.         return $this->_rebootOrReimageRoleInstance($operationUrl'reimage');
  1681.     }
  1682.     
  1683.     /**
  1684.      * The Reimage Role Instance operation requests a reimage of a role instance
  1685.      * that is running in a deployment.
  1686.      * 
  1687.      * @param string $serviceName        The service name
  1688.      * @param string $deploymentId        The deployment ID as listed on the Windows Azure management portal
  1689.      * @param string $roleInstanceName  The role instance name
  1690.      * @throws Microsoft_WindowsAzure_Management_Exception
  1691.      */
  1692.     public function reimageRoleInstanceByDeploymentId($serviceName$deploymentId$roleInstanceName)
  1693.     {
  1694.         if ($serviceName == '' || is_null($serviceName)) {
  1695.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  1696.         }
  1697.         if ($deploymentId == '' || is_null($deploymentId)) {
  1698.             throw new Microsoft_WindowsAzure_Management_Exception('Deployment ID should be specified.');
  1699.         }
  1700.         if ($roleInstanceName == '' || is_null($roleInstanceName)) {
  1701.             throw new Microsoft_WindowsAzure_Management_Exception('Role instance name should be specified.');
  1702.         }
  1703.         
  1704.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/deployments/' $deploymentId '/roleinstances/' $roleInstanceName;
  1705.         return $this->_rebootOrReimageRoleInstance($operationUrl'reimage');
  1706.     }
  1707.     
  1708.     /**
  1709.      * Reboots or reimages a role instance.
  1710.      * 
  1711.      * @param string $operationUrl        The operation url
  1712.      * @param string $operation The operation (reboot|reimage)
  1713.      * @throws Microsoft_WindowsAzure_Management_Exception
  1714.      */
  1715.     protected function _rebootOrReimageRoleInstance($operationUrl$operation 'reboot')
  1716.     {
  1717.         $response $this->_performRequest($operationUrl'?comp=' $operationMicrosoft_Http_Client::POST);
  1718.             
  1719.         if (!$response->isSuccessful()) {
  1720.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  1721.         }
  1722.     }
  1723.     
  1724.     /**
  1725.      * The List Certificates operation lists all certificates associated with
  1726.      * the specified hosted service.
  1727.      * 
  1728.      * @param string $serviceName        The service name
  1729.      * @return array Array of Microsoft_WindowsAzure_Management_CertificateInstance
  1730.      * @throws Microsoft_WindowsAzure_Management_Exception
  1731.      */
  1732.     public function listCertificates($serviceName)
  1733.     {
  1734.         if ($serviceName == '' || is_null($serviceName)) {
  1735.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  1736.         }
  1737.         
  1738.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/certificates';
  1739.         $response $this->_performRequest($operationUrl);
  1740.  
  1741.         if ($response->isSuccessful()) {
  1742.             $result $this->_parseResponse($response);
  1743.  
  1744.             if (!$result->Certificate{
  1745.                 return array();
  1746.             }
  1747.             if (count($result->Certificate1{
  1748.                 $xmlServices $result->Certificate;
  1749.             else {
  1750.                 $xmlServices array($result->Certificate);
  1751.             }
  1752.  
  1753.             $services array();
  1754.             if (!is_null($xmlServices)) {                
  1755.                 for ($i 0$i count($xmlServices)$i++{
  1756.                     $services[new Microsoft_WindowsAzure_Management_CertificateInstance(
  1757.                         (string)$xmlServices[$i]->CertificateUrl,
  1758.                         (string)$xmlServices[$i]->Thumbprint,
  1759.                         (string)$xmlServices[$i]->ThumbprintAlgorithm,
  1760.                         (string)$xmlServices[$i]->Data
  1761.                     );
  1762.                 }
  1763.             }
  1764.             return $services;
  1765.         else {
  1766.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  1767.         }
  1768.     }
  1769.     
  1770.     /**
  1771.      * The Get Certificate operation returns the public data for the specified certificate.
  1772.      * 
  1773.      * @param string $serviceName|$certificateUrl    The service name -or- the certificate URL
  1774.      * @param string $algorithm                     Algorithm
  1775.      * @param string $thumbprint                    Thumbprint
  1776.      * @return Microsoft_WindowsAzure_Management_CertificateInstance 
  1777.      * @throws Microsoft_WindowsAzure_Management_Exception
  1778.      */
  1779.     public function getCertificate($serviceName$algorithm ''$thumbprint '')
  1780.     {
  1781.         if ($serviceName == '' || is_null($serviceName)) {
  1782.             throw new Microsoft_WindowsAzure_Management_Exception('Service name or certificate URL should be specified.');
  1783.         }
  1784.         if (strpos($serviceName'https'=== false && ($algorithm == '' || is_null($algorithm)) && ($thumbprint == '' || is_null($thumbprint))) {
  1785.             throw new Microsoft_WindowsAzure_Management_Exception('Algorithm and thumbprint should be specified.');
  1786.         }
  1787.         
  1788.         $operationUrl str_replace($this->getBaseUrl()''$serviceName);
  1789.         if (strpos($serviceName'https'=== false{
  1790.             $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/certificates/' $algorithm '-' strtoupper($thumbprint);
  1791.         }
  1792.         
  1793.         $response $this->_performRequest($operationUrl);
  1794.  
  1795.         if ($response->isSuccessful()) {
  1796.             $result $this->_parseResponse($response);
  1797.             
  1798.             return new Microsoft_WindowsAzure_Management_CertificateInstance(
  1799.                 $this->getBaseUrl($operationUrl,
  1800.                 $algorithm,
  1801.                 $thumbprint,
  1802.                 (string)$result->Data
  1803.             );
  1804.         else {
  1805.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  1806.         }
  1807.     }
  1808.     
  1809.     /**
  1810.      * The Add Certificate operation adds a certificate to the subscription.
  1811.      * 
  1812.      * @param string $serviceName         The service name
  1813.      * @param string $certificateData     Certificate data
  1814.      * @param string $certificatePassword The certificate password
  1815.      * @param string $certificateFormat   The certificate format. Currently, only 'pfx' is supported.
  1816.      * @throws Microsoft_WindowsAzure_Management_Exception
  1817.      */
  1818.     public function addCertificate($serviceName$certificateData$certificatePassword$certificateFormat 'pfx')
  1819.     {
  1820.         if ($serviceName == '' || is_null($serviceName)) {
  1821.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  1822.         }
  1823.         if ($certificateData == '' || is_null($certificateData)) {
  1824.             throw new Microsoft_WindowsAzure_Management_Exception('Certificate data should be specified.');
  1825.         }
  1826.         if ($certificatePassword == '' || is_null($certificatePassword)) {
  1827.             throw new Microsoft_WindowsAzure_Management_Exception('Certificate password should be specified.');
  1828.         }
  1829.         if ($certificateFormat != 'pfx'{
  1830.             throw new Microsoft_WindowsAzure_Management_Exception('Certificate format should be "pfx".');
  1831.         }
  1832.         
  1833.         if (@file_exists($certificateData)) {
  1834.             $certificateData file_get_contents($certificateData);
  1835.         }
  1836.         
  1837.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/certificates';
  1838.         $response $this->_performRequest($operationUrl'',
  1839.             Microsoft_Http_Client::POST,
  1840.             array('Content-Type' => 'application/xml; charset=utf-8'),
  1841.             '<CertificateFile xmlns="http://schemas.microsoft.com/windowsazure"><Data>' base64_encode($certificateData'</Data><CertificateFormat>' $certificateFormat '</CertificateFormat><Password>' $certificatePassword '</Password></CertificateFile>');
  1842.  
  1843.         if (!$response->isSuccessful()) {
  1844.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  1845.         }
  1846.     }
  1847.     
  1848.     /**
  1849.      * The Delete Certificate operation deletes a certificate from the subscription's certificate store.
  1850.      * 
  1851.      * @param string $serviceName|$certificateUrl    The service name -or- the certificate URL
  1852.      * @param string $algorithm                     Algorithm
  1853.      * @param string $thumbprint                    Thumbprint
  1854.      * @throws Microsoft_WindowsAzure_Management_Exception
  1855.      */
  1856.     public function deleteCertificate($serviceName$algorithm ''$thumbprint '')
  1857.     {
  1858.         if ($serviceName == '' || is_null($serviceName)) {
  1859.             throw new Microsoft_WindowsAzure_Management_Exception('Service name or certificate URL should be specified.');
  1860.         }
  1861.         if (strpos($serviceName'https'=== false && ($algorithm == '' || is_null($algorithm)) && ($thumbprint == '' || is_null($thumbprint))) {
  1862.             throw new Microsoft_WindowsAzure_Management_Exception('Algorithm and thumbprint should be specified.');
  1863.         }
  1864.         
  1865.         $operationUrl str_replace($this->getBaseUrl()''$serviceName);
  1866.         if (strpos($serviceName'https'=== false{
  1867.             $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/certificates/' $algorithm '-' strtoupper($thumbprint);
  1868.         }
  1869.         
  1870.         $response $this->_performRequest($operationUrl''Microsoft_Http_Client::DELETE);
  1871.  
  1872.         if (!$response->isSuccessful()) {
  1873.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  1874.         }
  1875.     }
  1876.     
  1877.     /**
  1878.      * The List Affinity Groups operation lists the affinity groups associated with
  1879.      * the specified subscription.
  1880.      * 
  1881.      * @return array Array of Microsoft_WindowsAzure_Management_AffinityGroupInstance
  1882.      * @throws Microsoft_WindowsAzure_Management_Exception
  1883.      */
  1884.     public function listAffinityGroups()
  1885.     {
  1886.         $response $this->_performRequest(self::OP_AFFINITYGROUPS);
  1887.  
  1888.         if ($response->isSuccessful()) {
  1889.             $result $this->_parseResponse($response);
  1890.             
  1891.             if (!$result->AffinityGroup{
  1892.                 return array();
  1893.             }
  1894.             if (count($result->AffinityGroup1{
  1895.                 $xmlServices $result->AffinityGroup;
  1896.             else {
  1897.                 $xmlServices array($result->AffinityGroup);
  1898.             }
  1899.             
  1900.             $services array();
  1901.             if (!is_null($xmlServices)) {                
  1902.                 for ($i 0$i count($xmlServices)$i++{
  1903.                     $services[new Microsoft_WindowsAzure_Management_AffinityGroupInstance(
  1904.                         (string)$xmlServices[$i]->Name,
  1905.                         (string)$xmlServices[$i]->Label,
  1906.                         (string)$xmlServices[$i]->Description,
  1907.                         (string)$xmlServices[$i]->Location
  1908.                     );
  1909.                 }
  1910.             }
  1911.             return $services;
  1912.         else {
  1913.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  1914.         }
  1915.     }
  1916.     
  1917.     /**
  1918.      * The Create Affinity Group operation creates a new affinity group for the specified subscription.
  1919.      * 
  1920.      * @param string $name A name for the affinity group that is unique to the subscription.
  1921.      * @param string $label A label for the affinity group. The label may be up to 100 characters in length.
  1922.      * @param string $description A description for the affinity group. The description may be up to 1024 characters in length.
  1923.      * @param string $location The location where the affinity group will be created. To list available locations, use the List Locations operation.
  1924.      */
  1925.     public function createAffinityGroup($name$label$description ''$location '')
  1926.     {
  1927.         if ($name == '' || is_null($name)) {
  1928.             throw new Microsoft_WindowsAzure_Management_Exception('Affinity group name should be specified.');
  1929.         }
  1930.         if ($label == '' || is_null($label)) {
  1931.             throw new Microsoft_WindowsAzure_Management_Exception('Label should be specified.');
  1932.         }
  1933.         if (strlen($label100{
  1934.             throw new Microsoft_WindowsAzure_Management_Exception('Label is too long. The maximum length is 100 characters.');
  1935.         }
  1936.         if (strlen($description1024{
  1937.             throw new Microsoft_WindowsAzure_Management_Exception('Description is too long. The maximum length is 1024 characters.');
  1938.         }
  1939.         if ($location == '' || is_null($location)) {
  1940.             throw new Microsoft_WindowsAzure_Management_Exception('Location should be specified.');
  1941.         }
  1942.         
  1943.         $response $this->_performRequest(self::OP_AFFINITYGROUPS'',
  1944.             Microsoft_Http_Client::POST,
  1945.             array('Content-Type' => 'application/xml; charset=utf-8'),
  1946.             '<CreateAffinityGroup xmlns="http://schemas.microsoft.com/windowsazure"><Name>' $name '</Name><Label>' base64_encode($label'</Label><Description>' $description '</Description><Location>' $location '</Location></CreateAffinityGroup>');    
  1947.             
  1948.         if (!$response->isSuccessful()) {
  1949.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  1950.         }
  1951.     }
  1952.     
  1953.     /**
  1954.      * The Update Affinity Group operation updates the label and/or the description for an affinity group for the specified subscription.
  1955.      * 
  1956.      * @param string $name The name for the affinity group that should be updated.
  1957.      * @param string $label A label for the affinity group. The label may be up to 100 characters in length.
  1958.      * @param string $description A description for the affinity group. The description may be up to 1024 characters in length.
  1959.      */
  1960.     public function updateAffinityGroup($name$label$description '')
  1961.     {
  1962.         if ($name == '' || is_null($name)) {
  1963.             throw new Microsoft_WindowsAzure_Management_Exception('Affinity group name should be specified.');
  1964.         }
  1965.         if ($label == '' || is_null($label)) {
  1966.             throw new Microsoft_WindowsAzure_Management_Exception('Label should be specified.');
  1967.         }
  1968.         if (strlen($label100{
  1969.             throw new Microsoft_WindowsAzure_Management_Exception('Label is too long. The maximum length is 100 characters.');
  1970.         }
  1971.         if (strlen($description1024{
  1972.             throw new Microsoft_WindowsAzure_Management_Exception('Description is too long. The maximum length is 1024 characters.');
  1973.         }
  1974.         
  1975.         $response $this->_performRequest(self::OP_AFFINITYGROUPS '/' $name'',
  1976.             Microsoft_Http_Client::PUT,
  1977.             array('Content-Type' => 'application/xml; charset=utf-8'),
  1978.             '<UpdateAffinityGroup xmlns="http://schemas.microsoft.com/windowsazure"><Label>' base64_encode($label'</Label><Description>' $description '</Description></UpdateAffinityGroup>');    
  1979.             
  1980.         if (!$response->isSuccessful()) {
  1981.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  1982.         }
  1983.     }
  1984.     
  1985.     /**
  1986.      * The Delete Affinity Group operation deletes an affinity group in the specified subscription.
  1987.      * 
  1988.      * @param string $name The name for the affinity group that should be deleted.
  1989.      */
  1990.     public function deleteAffinityGroup($name)
  1991.     {
  1992.         if ($name == '' || is_null($name)) {
  1993.             throw new Microsoft_WindowsAzure_Management_Exception('Affinity group name should be specified.');
  1994.         }
  1995.         
  1996.         $response $this->_performRequest(self::OP_AFFINITYGROUPS '/' $name'',
  1997.             Microsoft_Http_Client::DELETE);
  1998.             
  1999.         if (!$response->isSuccessful()) {
  2000.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  2001.         }
  2002.     }
  2003.     
  2004.     /**
  2005.      * The Get Affinity Group Properties operation returns the
  2006.      * system properties associated with the specified affinity group.
  2007.      * 
  2008.      * @param string $affinityGroupName The affinity group name.
  2009.      * @return Microsoft_WindowsAzure_Management_AffinityGroupInstance 
  2010.      * @throws Microsoft_WindowsAzure_Management_Exception
  2011.      */
  2012.     public function getAffinityGroupProperties($affinityGroupName)
  2013.     {
  2014.         if ($affinityGroupName == '' || is_null($affinityGroupName)) {
  2015.             throw new Microsoft_WindowsAzure_Management_Exception('Affinity group name should be specified.');
  2016.         }
  2017.         
  2018.         $response $this->_performRequest(self::OP_AFFINITYGROUPS '/' $affinityGroupName);
  2019.  
  2020.         if ($response->isSuccessful()) {
  2021.             $result $this->_parseResponse($response);
  2022.             
  2023.             $affinityGroup new Microsoft_WindowsAzure_Management_AffinityGroupInstance(
  2024.                 $affinityGroupName,
  2025.                 (string)$result->Label,
  2026.                 (string)$result->Description,
  2027.                 (string)$result->Location
  2028.             );
  2029.  
  2030.             // Hosted services
  2031.             if (count($result->HostedServices->HostedService1{
  2032.                 $xmlService $result->HostedServices->HostedService;
  2033.             else {
  2034.                 $xmlService array($result->HostedServices->HostedService);
  2035.             }
  2036.                     
  2037.             $services array();
  2038.             if (!is_null($xmlService)) {                
  2039.                 for ($i 0$i count($xmlService)$i++{
  2040.                     $services[array(
  2041.                         'url' => (string)$xmlService[$i]->Url,
  2042.                         'name' => (string)$xmlService[$i]->ServiceName
  2043.                     );
  2044.                 }
  2045.             }
  2046.             $affinityGroup->HostedServices $services;
  2047.             
  2048.             // Storage services
  2049.             if (count($result->StorageServices->StorageService1{
  2050.                 $xmlService $result->StorageServices->StorageService;
  2051.             else {
  2052.                 $xmlService array($result->StorageServices->StorageService);
  2053.             }
  2054.                     
  2055.             $services array();
  2056.             if (!is_null($xmlService)) {                
  2057.                 for ($i 0$i count($xmlService)$i++{
  2058.                     $services[array(
  2059.                         'url' => (string)$xmlService[$i]->Url,
  2060.                         'name' => (string)$xmlService[$i]->ServiceName
  2061.                     );
  2062.                 }
  2063.             }
  2064.             $affinityGroup->StorageServices $services;    
  2065.             
  2066.             return $affinityGroup;
  2067.         else {
  2068.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  2069.         }
  2070.     }
  2071.     
  2072.     /**
  2073.      * The List Locations operation lists all of the data center locations
  2074.      * that are valid for your subscription.
  2075.      * 
  2076.      * @return array Array of Microsoft_WindowsAzure_Management_LocationInstance
  2077.      * @throws Microsoft_WindowsAzure_Management_Exception
  2078.      */
  2079.     public function listLocations()
  2080.     {
  2081.         $response $this->_performRequest(self::OP_LOCATIONS);
  2082.  
  2083.         if ($response->isSuccessful()) {
  2084.             $result $this->_parseResponse($response);
  2085.             
  2086.             if (!$result->Location{
  2087.                 return array();
  2088.             }
  2089.             if (count($result->Location1{
  2090.                 $xmlServices $result->Location;
  2091.             else {
  2092.                 $xmlServices array($result->Location);
  2093.             }
  2094.             
  2095.             $services array();
  2096.             if (!is_null($xmlServices)) {                
  2097.                 for ($i 0$i count($xmlServices)$i++{
  2098.                     $services[new Microsoft_WindowsAzure_Management_LocationInstance(
  2099.                         (string)$xmlServices[$i]->Name
  2100.                     );
  2101.                 }
  2102.             }
  2103.             return $services;
  2104.         else {
  2105.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  2106.         }
  2107.     }
  2108.     
  2109.     /**
  2110.      * The List Operating Systems operation lists the versions of the guest operating system
  2111.      * that are currently available in Windows Azure. The 2010-10-28 version of List Operating
  2112.      * Systems also indicates what family an operating system version belongs to.
  2113.      * Currently Windows Azure supports two operating system families: the Windows Azure guest
  2114.      * operating system that is substantially compatible with Windows Server 2008 SP2,
  2115.      * and the Windows Azure guest operating system that is substantially compatible with
  2116.      * Windows Server 2008 R2.
  2117.      * 
  2118.      * @return array Array of Microsoft_WindowsAzure_Management_OperatingSystemInstance
  2119.      * @throws Microsoft_WindowsAzure_Management_Exception
  2120.      */
  2121.     public function listOperatingSystems()
  2122.     {
  2123.         $response $this->_performRequest(self::OP_OPERATINGSYSTEMS);
  2124.  
  2125.         if ($response->isSuccessful()) {
  2126.             $result $this->_parseResponse($response);
  2127.             
  2128.             if (!$result->OperatingSystem{
  2129.                 return array();
  2130.             }
  2131.             if (count($result->OperatingSystem1{
  2132.                 $xmlServices $result->OperatingSystem;
  2133.             else {
  2134.                 $xmlServices array($result->OperatingSystem);
  2135.             }
  2136.             
  2137.             $services array();
  2138.             if (!is_null($xmlServices)) {                
  2139.                 for ($i 0$i count($xmlServices)$i++{
  2140.                     $services[new Microsoft_WindowsAzure_Management_OperatingSystemInstance(
  2141.                         (string)$xmlServices[$i]->Version,
  2142.                         (string)$xmlServices[$i]->Label,
  2143.                         ((string)$xmlServices[$i]->IsDefault == 'true'),
  2144.                         ((string)$xmlServices[$i]->IsActive == 'true'),
  2145.                         (string)$xmlServices[$i]->Family,
  2146.                         (string)$xmlServices[$i]->FamilyLabel
  2147.                     );
  2148.                 }
  2149.             }
  2150.             return $services;
  2151.         else {
  2152.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  2153.         }
  2154.     }
  2155.     
  2156.     /**
  2157.      * The List OS Families operation lists the guest operating system families
  2158.      * available in Windows Azure, and also lists the operating system versions
  2159.      * available for each family. Currently Windows Azure supports two operating
  2160.      * system families: the Windows Azure guest operating system that is
  2161.      * substantially compatible with Windows Server 2008 SP2, and the Windows
  2162.      * Azure guest operating system that is substantially compatible with
  2163.      * Windows Server 2008 R2.
  2164.      * 
  2165.      * @return array Array of Microsoft_WindowsAzure_Management_OperatingSystemFamilyInstance
  2166.      * @throws Microsoft_WindowsAzure_Management_Exception
  2167.      */
  2168.     public function listOperatingSystemFamilies()
  2169.     {
  2170.         $response $this->_performRequest(self::OP_OPERATINGSYSTEMFAMILIES);
  2171.  
  2172.         if ($response->isSuccessful()) {
  2173.             $result $this->_parseResponse($response);
  2174.             
  2175.             if (!$result->OperatingSystemFamily{
  2176.                 return array();
  2177.             }
  2178.             if (count($result->OperatingSystemFamily1{
  2179.                 $xmlServices $result->OperatingSystemFamily;
  2180.             else {
  2181.                 $xmlServices array($result->OperatingSystemFamily);
  2182.             }
  2183.             
  2184.             $services array();
  2185.             if (!is_null($xmlServices)) {                
  2186.                 for ($i 0$i count($xmlServices)$i++{
  2187.                     $services[new Microsoft_WindowsAzure_Management_OperatingSystemFamilyInstance(
  2188.                         (string)$xmlServices[$i]->Name,
  2189.                         (string)$xmlServices[$i]->Label
  2190.                     );
  2191.                                 
  2192.                     if (count($xmlServices[$i]->OperatingSystems->OperatingSystem1{
  2193.                         $xmlOperatingSystems $xmlServices[$i]->OperatingSystems->OperatingSystem;
  2194.                     else {
  2195.                         $xmlOperatingSystems array($xmlServices[$i]->OperatingSystems->OperatingSystem);
  2196.                     }
  2197.                     
  2198.                     $operatingSystems array();
  2199.                     if (!is_null($xmlOperatingSystems)) {                
  2200.                         for ($i 0$i count($xmlOperatingSystems)$i++{
  2201.                             $operatingSystems[new Microsoft_WindowsAzure_Management_OperatingSystemInstance(
  2202.                                 (string)$xmlOperatingSystems[$i]->Version,
  2203.                                 (string)$xmlOperatingSystems[$i]->Label,
  2204.                                 ((string)$xmlOperatingSystems[$i]->IsDefault == 'true'),
  2205.                                 ((string)$xmlOperatingSystems[$i]->IsActive == 'true'),
  2206.                                 (string)$xmlServices[$i]->Name,
  2207.                                 (string)$xmlServices[$i]->Label
  2208.                             );
  2209.                         }
  2210.                     }
  2211.                     $servicescount($services]->OperatingSystems $operatingSystems;
  2212.                 }
  2213.             }
  2214.             return $services;
  2215.         else {
  2216.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  2217.         }
  2218.     }
  2219.     
  2220.     /**
  2221.      * Clean configuration
  2222.      * 
  2223.      * @param string $configuration Configuration to clean.
  2224.      * @return string 
  2225.      */
  2226.     public function _cleanConfiguration($configuration{
  2227.         $configuration str_replace('?<?''<?'$configuration);
  2228.         $configuration str_replace("\r"""$configuration);
  2229.         $configuration str_replace("\n"""$configuration);
  2230.         
  2231.         return $configuration;
  2232.     }
  2233. }

Documentation generated on Wed, 18 May 2011 12:06:18 +0200 by phpDocumentor 1.4.3