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 - 2011, 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 - 2011, 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 - 2011, 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-06-01';
  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_Client 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 array $query Query arguments
  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.         $query array(),
  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.         // Generate URL and sign request
  243.         $requestUrl $this->getBaseUrl(rawurlencode($path);
  244.         $requestHeaders $headers;
  245.         if (count($query0{
  246.             $queryString '';
  247.             foreach ($query as $key => $value{
  248.                 $queryString .= ($queryString '&' '?'rawurlencode($key'=' rawurlencode($value);
  249.             }            
  250.             $requestUrl .= $queryString;
  251.         }
  252.         
  253.         // Prepare request 
  254.         $this->_httpClientChannel->resetParameters(true);
  255.         $this->_httpClientChannel->setUri($requestUrl);
  256.         $this->_httpClientChannel->setHeaders($requestHeaders);
  257.         $this->_httpClientChannel->setRawData($rawData);
  258.  
  259.         // Execute request
  260.         $response $this->_retryPolicy->execute(
  261.             array($this->_httpClientChannel'request'),
  262.             array($httpVerb)
  263.         );
  264.         
  265.         // Store request id
  266.         $this->_lastRequestId = $response->getHeader('x-ms-request-id');
  267.         
  268.         return $response;
  269.     }
  270.     
  271.     /** 
  272.      * Parse result from Microsoft_Http_Response
  273.      *
  274.      * @param Microsoft_Http_Response $response Response from HTTP call
  275.      * @return object 
  276.      * @throws Microsoft_WindowsAzure_Exception
  277.      */
  278.     protected function _parseResponse(Microsoft_Http_Response $response null)
  279.     {
  280.         if (is_null($response)) {
  281.             throw new Microsoft_WindowsAzure_Exception('Response should not be null.');
  282.         }
  283.         
  284.         $xml @simplexml_load_string($response->getBody());
  285.         
  286.         if ($xml !== false{
  287.             // Fetch all namespaces 
  288.             $namespaces array_merge($xml->getNamespaces(true)$xml->getDocNamespaces(true))
  289.             
  290.             // Register all namespace prefixes
  291.             foreach ($namespaces as $prefix => $ns
  292.                 if ($prefix != ''{
  293.                     $xml->registerXPathNamespace($prefix$ns);
  294.                 
  295.             
  296.         }
  297.         
  298.         return $xml;
  299.     }
  300.     
  301.     /**
  302.      * Get error message from Microsoft_Http_Response
  303.      *
  304.      * @param Microsoft_Http_Response $response Repsonse
  305.      * @param string $alternativeError Alternative error message
  306.      * @return string 
  307.      */
  308.     protected function _getErrorMessage(Microsoft_Http_Response $response$alternativeError 'Unknown error.')
  309.     {
  310.         $response $this->_parseResponse($response);
  311.         if ($response && $response->Message{
  312.             return (string)$response->Message;
  313.         else {
  314.             return $alternativeError;
  315.         }
  316.     }
  317.     
  318.     /**
  319.      * The Get Operation Status operation returns the status of the specified operation.
  320.      * After calling an asynchronous operation, you can call Get Operation Status to
  321.      * determine whether the operation has succeed, failed, or is still in progress.
  322.      *
  323.      * @param string $requestId The request ID. If omitted, the last request ID will be used.
  324.      * @return Microsoft_WindowsAzure_Management_OperationStatusInstance 
  325.      * @throws Microsoft_WindowsAzure_Management_Exception
  326.      */
  327.     public function getOperationStatus($requestId '')
  328.     {
  329.         if ($requestId == ''{
  330.             $requestId $this->getLastRequestId();
  331.         }
  332.         
  333.         $response $this->_performRequest(self::OP_OPERATIONS '/' $requestId);
  334.  
  335.         if ($response->isSuccessful()) {
  336.             $result $this->_parseResponse($response);
  337.  
  338.             if (!is_null($result)) {
  339.                 return new Microsoft_WindowsAzure_Management_OperationStatusInstance(
  340.                     (string)$result->ID,
  341.                     (string)$result->Status,
  342.                     ($result->Error ? (string)$result->Error->Code ''),
  343.                     ($result->Error ? (string)$result->Error->Message '')
  344.                 );
  345.             }
  346.             return null;
  347.         else {
  348.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  349.         }
  350.     }
  351.     
  352.  
  353.     
  354.     /**
  355.      * The List Subscription Operations operation returns a list of create, update,
  356.      * and delete operations that were performed on a subscription during the specified timeframe.
  357.      * Documentation on the parameters can be found at http://msdn.microsoft.com/en-us/library/gg715318.aspx.
  358.      *
  359.      * @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.
  360.      * @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.
  361.      * @param string $objectIdFilter Returns subscription operations only for the specified object type and object ID.
  362.      * @param string $operationResultFilter Returns subscription operations only for the specified result status, either Succeeded, Failed, or InProgress.
  363.      * @param string $continuationToken Internal usage.
  364.      * @return array Array of Microsoft_WindowsAzure_Management_SubscriptionOperationInstance
  365.      * @throws Microsoft_WindowsAzure_Management_Exception
  366.      */
  367.     public function listSubscriptionOperations($startTime$endTime$objectIdFilter null$operationResultFilter null$continuationToken null)
  368.     {
  369.         if ($startTime == '' || is_null($startTime)) {
  370.             throw new Microsoft_WindowsAzure_Management_Exception('Start time should be specified.');
  371.         }
  372.         if ($endTime == '' || is_null($endTime)) {
  373.             throw new Microsoft_WindowsAzure_Management_Exception('End time should be specified.');
  374.         }
  375.         if ($operationResultFilter != '' && !is_null($operationResultFilter)) {
  376.             $operationResultFilter strtolower($operationResultFilter);
  377.             if ($operationResultFilter != 'succeeded' && $operationResultFilter != 'failed' && $operationResultFilter != 'inprogress'{
  378.                 throw new Microsoft_WindowsAzure_Management_Exception('OperationResultFilter should be succeeded|failed|inprogress.');
  379.             }
  380.         }
  381.         
  382.         $parameters array();
  383.         $parameters['StartTime'$startTime;
  384.         $parameters['EndTime'$endTime;
  385.         if ($objectIdFilter != '' && !is_null($objectIdFilter)) {
  386.             $parameters['ObjectIdFilter'$objectIdFilter;
  387.         }
  388.         if ($operationResultFilter != '' && !is_null($operationResultFilter)) {
  389.             $parameters['OperationResultFilter'ucfirst($operationResultFilter);
  390.         }
  391.         if ($continuationToken != '' && !is_null($continuationToken)) {
  392.             $parameters['ContinuationToken'$continuationToken;
  393.         }
  394.         
  395.         $response $this->_performRequest(self::OP_OPERATIONS$parameters);
  396.  
  397.         if ($response->isSuccessful()) {
  398.             $result $this->_parseResponse($response);
  399.             $namespaces $result->getDocNamespaces()
  400.             $result->registerXPathNamespace('__empty_ns'$namespaces['']);
  401.  
  402.             $xmlOperations $result->xpath('//__empty_ns:SubscriptionOperation');
  403.             
  404.             // Create return value
  405.             $returnValue array();            
  406.             foreach ($xmlOperations as $xmlOperation{
  407.                 // Create operation instance
  408.                 $operation new Microsoft_WindowsAzure_Management_SubscriptionOperationInstance(
  409.                     $xmlOperation->OperationId,
  410.                     $xmlOperation->OperationObjectId,
  411.                     $xmlOperation->OperationName,
  412.                     array(),
  413.                     (array)$xmlOperation->OperationCaller,
  414.                     (array)$xmlOperation->OperationStatus,
  415.                     (string)$xmlOperation->OperationStartedTime,
  416.                     (string)$xmlOperation->OperationCompletedTime
  417.                 );
  418.                 
  419.                 // Parse parameters
  420.                 $xmlOperation->registerXPathNamespace('__empty_ns'$namespaces[''])
  421.                 $xmlParameters $xmlOperation->xpath('.//__empty_ns:OperationParameter');
  422.                 foreach ($xmlParameters as $xmlParameter{
  423.                     $xmlParameterDetails $xmlParameter->children('http://schemas.datacontract.org/2004/07/Microsoft.Samples.WindowsAzure.ServiceManagement');
  424.                     $operation->addOperationParameter((string)$xmlParameterDetails->Name(string)$xmlParameterDetails->Value);
  425.                 }
  426.                 
  427.                 // Add to result
  428.                 $returnValue[$operation;
  429.             }
  430.             
  431.             // More data?
  432.             if (!is_null($result->ContinuationToken&& $result->ContinuationToken != ''{
  433.                 $returnValue array_merge($returnValue$this->listSubscriptionOperations($startTime$endTime$objectIdFilter$operationResultFilter(string)$result->ContinuationToken));
  434.             }
  435.             
  436.             // Return
  437.             return $returnValue;
  438.         else {
  439.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  440.         }
  441.     }
  442.     
  443.     /**
  444.      * Wait for an operation to complete
  445.      * 
  446.      * @param string $requestId The request ID. If omitted, the last request ID will be used.
  447.      * @param int $sleepInterval Sleep interval in milliseconds.
  448.      * @return Microsoft_WindowsAzure_Management_OperationStatusInstance 
  449.      * @throws Microsoft_WindowsAzure_Management_Exception
  450.      */
  451.     public function waitForOperation($requestId ''$sleepInterval 250)
  452.     {
  453.         if ($requestId == ''{
  454.             $requestId $this->getLastRequestId();
  455.         }
  456.         if ($requestId == '' || is_null($requestId)) {
  457.             return null;
  458.         }
  459.  
  460.         $status $this->getOperationStatus($requestId);
  461.         while ($status->Status == 'InProgress'{
  462.           $status $this->getOperationStatus($requestId);
  463.           usleep($sleepInterval);
  464.         }
  465.         
  466.         return $status;
  467.     }
  468.     
  469.     /**
  470.      * Creates a new Microsoft_WindowsAzure_Storage_Blob instance for the current account
  471.      *
  472.      * @param string $serviceName the service name to create a storage client for.
  473.      * @param Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy Retry policy to use when making requests
  474.      * @return Microsoft_WindowsAzure_Storage_Blob 
  475.      */
  476.     public function createBlobClientForService($serviceNameMicrosoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy null)
  477.     {
  478.         if ($serviceName == '' || is_null($serviceName)) {
  479.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  480.         }
  481.         
  482.         $storageKeys $this->getStorageAccountKeys($serviceName);
  483.         
  484.         return new Microsoft_WindowsAzure_Storage_Blob(
  485.             Microsoft_WindowsAzure_Storage::URL_CLOUD_BLOB,
  486.             $serviceName,
  487.             $storageKeys[0],
  488.             false,
  489.             $retryPolicy
  490.         );
  491.     }
  492.     
  493.     /**
  494.      * Creates a new Microsoft_WindowsAzure_Storage_Table instance for the current account
  495.      *
  496.      * @param string $serviceName the service name to create a storage client for.
  497.      * @param Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy Retry policy to use when making requests
  498.      * @return Microsoft_WindowsAzure_Storage_Table 
  499.      */
  500.     public function createTableClientForService($serviceNameMicrosoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy null)
  501.     {
  502.         if ($serviceName == '' || is_null($serviceName)) {
  503.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  504.         }
  505.         
  506.         $storageKeys $this->getStorageAccountKeys($serviceName);
  507.         
  508.         return new Microsoft_WindowsAzure_Storage_Table(
  509.             Microsoft_WindowsAzure_Storage::URL_CLOUD_TABLE,
  510.             $serviceName,
  511.             $storageKeys[0],
  512.             false,
  513.             $retryPolicy
  514.         );
  515.     }
  516.     
  517.     /**
  518.      * Creates a new Microsoft_WindowsAzure_Storage_Queue instance for the current account
  519.      *
  520.      * @param string $serviceName the service name to create a storage client for.
  521.      * @param Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy Retry policy to use when making requests
  522.      * @return Microsoft_WindowsAzure_Storage_Queue 
  523.      */
  524.     public function createQueueClientForService($serviceNameMicrosoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy null)
  525.     {
  526.         if ($serviceName == '' || is_null($serviceName)) {
  527.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  528.         }
  529.         
  530.         $storageKeys $this->getStorageAccountKeys($serviceName);
  531.         
  532.         return new Microsoft_WindowsAzure_Storage_Queue(
  533.             Microsoft_WindowsAzure_Storage::URL_CLOUD_QUEUE,
  534.             $serviceName,
  535.             $storageKeys[0],
  536.             false,
  537.             $retryPolicy
  538.         );
  539.     }
  540.     
  541.     /**
  542.      * The List Storage Accounts operation lists the storage accounts available under
  543.      * the current subscription.
  544.      *
  545.      * @return array An array of Microsoft_WindowsAzure_Management_StorageServiceInstance
  546.      */
  547.     public function listStorageAccounts()
  548.     {
  549.         $response $this->_performRequest(self::OP_STORAGE_ACCOUNTS);
  550.  
  551.         if ($response->isSuccessful()) {
  552.             $result $this->_parseResponse($response);
  553.             
  554.             if (!$result->StorageService{
  555.                 return array();
  556.             }
  557.             if (count($result->StorageService1{
  558.                 $xmlServices $result->StorageService;
  559.             else {
  560.                 $xmlServices array($result->StorageService);
  561.             }
  562.             
  563.             $services array();
  564.             if (!is_null($xmlServices)) {                
  565.                 for ($i 0$i count($xmlServices)$i++{
  566.                     $services[new Microsoft_WindowsAzure_Management_StorageServiceInstance(
  567.                         (string)$xmlServices[$i]->Url,
  568.                         (string)$xmlServices[$i]->ServiceName
  569.                     );
  570.                 }
  571.             }
  572.             return $services;
  573.         else {
  574.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  575.         }
  576.     }
  577.     
  578.     /**
  579.      * The Get Storage Account Properties operation returns the system properties for the
  580.      * specified storage account. These properties include: the address, description, and
  581.      * label of the storage account; and the name of the affinity group to which the service
  582.      * belongs, or its geo-location if it is not part of an affinity group.
  583.      *
  584.      * @param string $serviceName The name of your service.
  585.      * @return Microsoft_WindowsAzure_Management_StorageServiceInstance 
  586.      * @throws Microsoft_WindowsAzure_Management_Exception
  587.      */
  588.     public function getStorageAccountProperties($serviceName)
  589.     {
  590.         if ($serviceName == '' || is_null($serviceName)) {
  591.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  592.         }
  593.         
  594.         $response $this->_performRequest(self::OP_STORAGE_ACCOUNTS '/' $serviceName);
  595.  
  596.         if ($response->isSuccessful()) {
  597.             $xmlService $this->_parseResponse($response);
  598.  
  599.             if (!is_null($xmlService)) {
  600.                 return new Microsoft_WindowsAzure_Management_StorageServiceInstance(
  601.                     (string)$xmlService->Url,
  602.                     (string)$xmlService->ServiceName,
  603.                     (string)$xmlService->StorageServiceProperties->Description,
  604.                     (string)$xmlService->StorageServiceProperties->AffinityGroup,
  605.                     (string)$xmlService->StorageServiceProperties->Location,
  606.                     (string)$xmlService->StorageServiceProperties->Label
  607.                 );
  608.             }
  609.             return null;
  610.         else {
  611.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  612.         }
  613.     }
  614.     
  615.     /**
  616.      * The Get Storage Keys operation returns the primary
  617.      * and secondary access keys for the specified storage account.
  618.      *
  619.      * @param string $serviceName The name of your service.
  620.      * @return array An array of strings
  621.      * @throws Microsoft_WindowsAzure_Management_Exception
  622.      */
  623.     public function getStorageAccountKeys($serviceName)
  624.     {
  625.         if ($serviceName == '' || is_null($serviceName)) {
  626.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  627.         }
  628.         
  629.         $response $this->_performRequest(self::OP_STORAGE_ACCOUNTS '/' $serviceName '/keys');
  630.  
  631.         if ($response->isSuccessful()) {
  632.             $xmlService $this->_parseResponse($response);
  633.  
  634.             if (!is_null($xmlService)) {
  635.                 return array(
  636.                     (string)$xmlService->StorageServiceKeys->Primary,
  637.                     (string)$xmlService->StorageServiceKeys->Secondary
  638.                 );
  639.             }
  640.             return array();
  641.         else {
  642.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  643.         }
  644.     }
  645.     
  646.     /**
  647.      * The Regenerate Keys operation regenerates the primary
  648.      * or secondary access key for the specified storage account.
  649.      *
  650.      * @param string $serviceName The name of your service.
  651.      * @param string $key          The key to regenerate (primary or secondary)
  652.      * @return array An array of strings
  653.      * @throws Microsoft_WindowsAzure_Management_Exception
  654.      */
  655.     public function regenerateStorageAccountKey($serviceName$key 'primary')
  656.     {
  657.         if ($serviceName == '' || is_null($serviceName)) {
  658.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  659.         }
  660.         $key strtolower($key);
  661.         if ($key != 'primary' && $key != 'secondary'{
  662.             throw new Microsoft_WindowsAzure_Management_Exception('Key identifier should be primary|secondary.');
  663.         }
  664.         
  665.         $response $this->_performRequest(
  666.             self::OP_STORAGE_ACCOUNTS '/' $serviceName '/keys'array('action' => 'regenerate'),
  667.             Microsoft_Http_Client::POST,
  668.             array('Content-Type' => 'application/xml'),
  669.             '<?xml version="1.0" encoding="utf-8"?>
  670.              <RegenerateKeys xmlns="http://schemas.microsoft.com/windowsazure">
  671.                <KeyType>' ucfirst($key'</KeyType>
  672.              </RegenerateKeys>');
  673.  
  674.         if ($response->isSuccessful()) {
  675.             $xmlService $this->_parseResponse($response);
  676.  
  677.             if (!is_null($xmlService)) {
  678.                 return array(
  679.                     (string)$xmlService->StorageServiceKeys->Primary,
  680.                     (string)$xmlService->StorageServiceKeys->Secondary
  681.                 );
  682.             }
  683.             return array();
  684.         else {
  685.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  686.         }
  687.     }
  688.     
  689.     /**
  690.      * The Create Storage Account operation creates a new storage account in Windows Azure.
  691.      * 
  692.      * @param string $serviceName Required. A name for the storage account that is unique to the subscription.
  693.      * @param string $label Required. A label for the storage account that is Base64-encoded. The label may be up to 100 characters in length.
  694.      * @param string $description Optional. A description for the storage account. The description may be up to 1024 characters in length.
  695.      * @param string $location Required if AffinityGroup is not specified. The location where the storage account is created.
  696.      * @param string $affinityGroup Required if Location is not specified. The name of an existing affinity group in the specified subscription.
  697.      * @return Microsoft_WindowsAzure_Management_StorageServiceInstance 
  698.      * @throws Microsoft_WindowsAzure_Management_Exception
  699.      */
  700.     public function createStorageAccount($serviceName$label$description$location null$affinityGroup null)
  701.     {
  702.         if ($serviceName == '' || is_null($serviceName)) {
  703.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  704.         }
  705.         if ($label == '' || is_null($label)) {
  706.             throw new Microsoft_WindowsAzure_Management_Exception('Label should be specified.');
  707.         }
  708.         if (strlen($label100{
  709.             throw new Microsoft_WindowsAzure_Management_Exception('Label is too long. The maximum length is 100 characters.');
  710.         }
  711.         if ($description == '' || is_null($description)) {
  712.             throw new Microsoft_WindowsAzure_Management_Exception('Descritpion should be specified.');
  713.         }
  714.         if (strlen($description1024{
  715.             throw new Microsoft_WindowsAzure_Management_Exception('Description is too long. The maximum length is 1024 characters.');
  716.         }
  717.         if ( (is_null($location&& is_null($affinityGroup)) || (!is_null($location&& !is_null($affinityGroup)) ) {
  718.             throw new Microsoft_WindowsAzure_Management_Exception('Please specify a location -or- an affinity group for the service.');
  719.         }
  720.         
  721.         $locationOrAffinityGroup is_null($location)
  722.             ? '<AffinityGroup>' $affinityGroup '</AffinityGroup>'
  723.             : '<Location>' $location '</Location>';
  724.         
  725.         $response $this->_performRequest(self::OP_STORAGE_ACCOUNTSarray(),
  726.             Microsoft_Http_Client::POST,
  727.             array('Content-Type' => 'application/xml; charset=utf-8'),
  728.             '<CreateStorageServiceInput xmlns="http://schemas.microsoft.com/windowsazure"><ServiceName>' $serviceName '</ServiceName><Description>' $serviceName '</Description><Label>' base64_encode($label'</Label>' $locationOrAffinityGroup '</CreateStorageServiceInput>');
  729.      
  730.         if ($response->isSuccessful()) {
  731.             return new Microsoft_WindowsAzure_Management_StorageServiceInstance(
  732.                 'https://management.core.windows.net/' $this->getSubscriptionId('/services/storageservices/' $serviceName,
  733.                 $serviceName,
  734.                 $description,
  735.                 $affinityGroup,
  736.                 $location,
  737.                 base64_encode($label)
  738.             );
  739.         else {
  740.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  741.         }
  742.     }
  743.     
  744.     /**
  745.      * The Update Storage Account operation updates the label and/or the description for a storage account in Windows Azure.
  746.      * 
  747.      * @param string $serviceName Required. The name of the storage account to update.
  748.      * @param string $label Required. A label for the storage account that is Base64-encoded. The label may be up to 100 characters in length.
  749.      * @param string $description Optional. A description for the storage account. The description may be up to 1024 characters in length.
  750.      * @throws Microsoft_WindowsAzure_Management_Exception
  751.      */
  752.     public function updateStorageAccount($serviceName$label$description)
  753.     {
  754.         if ($serviceName == '' || is_null($serviceName)) {
  755.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  756.         }
  757.         if ($label == '' || is_null($label)) {
  758.             throw new Microsoft_WindowsAzure_Management_Exception('Label should be specified.');
  759.         }
  760.         if (strlen($label100{
  761.             throw new Microsoft_WindowsAzure_Management_Exception('Label is too long. The maximum length is 100 characters.');
  762.         }
  763.         if ($description == '' || is_null($description)) {
  764.             throw new Microsoft_WindowsAzure_Management_Exception('Descritpion should be specified.');
  765.         }
  766.         if (strlen($description1024{
  767.             throw new Microsoft_WindowsAzure_Management_Exception('Description is too long. The maximum length is 1024 characters.');
  768.         }
  769.         
  770.         $response $this->_performRequest(self::OP_STORAGE_ACCOUNTS '/' $serviceNamearray(),
  771.             Microsoft_Http_Client::PUT,
  772.             array('Content-Type' => 'application/xml; charset=utf-8'),
  773.             '<UpdateStorageServiceInput xmlns="http://schemas.microsoft.com/windowsazure"><Description>' $description '</Description><Label>' base64_encode($label'</Label></UpdateStorageServiceInput>');
  774.  
  775.         if (!$response->isSuccessful()) {
  776.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  777.         }
  778.     }
  779.     
  780.     /**
  781.      * The Delete Storage Account operation deletes the specified storage account from Windows Azure.
  782.      * 
  783.      * @param string $serviceName Required. The name of the storage account to delete.
  784.      * @throws Microsoft_WindowsAzure_Management_Exception
  785.      */
  786.     public function deleteStorageAccount($serviceName)
  787.     {
  788.         if ($serviceName == '' || is_null($serviceName)) {
  789.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  790.         }
  791.         
  792.         $response $this->_performRequest(self::OP_STORAGE_ACCOUNTS '/' $serviceNamearray()Microsoft_Http_Client::DELETE);
  793.         
  794.         if (!$response->isSuccessful()) {
  795.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  796.         }
  797.     }
  798.     
  799.     /**
  800.      * The List Hosted Services operation lists the hosted services available
  801.      * under the current subscription.
  802.      *
  803.      * @return array An array of Microsoft_WindowsAzure_Management_HostedServiceInstance
  804.      * @throws Microsoft_WindowsAzure_Management_Exception
  805.      */
  806.     public function listHostedServices()
  807.     {
  808.         $response $this->_performRequest(self::OP_HOSTED_SERVICES);
  809.  
  810.         if ($response->isSuccessful()) {
  811.             $result $this->_parseResponse($response);
  812.             
  813.             if (!$result->HostedService{
  814.                 return array();
  815.             }
  816.             if (count($result->HostedService1{
  817.                 $xmlServices $result->HostedService;
  818.             else {
  819.                 $xmlServices array($result->HostedService);
  820.             }
  821.             
  822.             $services array();
  823.             if (!is_null($xmlServices)) {                
  824.                 for ($i 0$i count($xmlServices)$i++{
  825.                     $services[new Microsoft_WindowsAzure_Management_HostedServiceInstance(
  826.                         (string)$xmlServices[$i]->Url,
  827.                         (string)$xmlServices[$i]->ServiceName
  828.                     );
  829.                 }
  830.             }
  831.             return $services;
  832.         else {
  833.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  834.         }
  835.     }
  836.     
  837.     /**
  838.      * The Create Hosted Service operation creates a new hosted service in Windows Azure.
  839.      * 
  840.      * @param string $serviceName A name for the hosted service that is unique to the subscription. This is the DNS name used for production deployments.
  841.      * @param string $label A label for the hosted service. The label may be up to 100 characters in length.
  842.      * @param string $description A description for the hosted service. The description may be up to 1024 characters in length.
  843.      * @param string $location Required if AffinityGroup is not specified. The location where the hosted service will be created.
  844.      * @param string $affinityGroup Required if Location is not specified. The name of an existing affinity group associated with this subscription.
  845.      */
  846.     public function createHostedService($serviceName$label$description ''$location null$affinityGroup null)
  847.     {
  848.         if ($serviceName == '' || is_null($serviceName)) {
  849.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  850.         }
  851.         if ($label == '' || is_null($label)) {
  852.             throw new Microsoft_WindowsAzure_Management_Exception('Label should be specified.');
  853.         }
  854.         if (strlen($label100{
  855.             throw new Microsoft_WindowsAzure_Management_Exception('Label is too long. The maximum length is 100 characters.');
  856.         }
  857.         if (strlen($description1024{
  858.             throw new Microsoft_WindowsAzure_Management_Exception('Description is too long. The maximum length is 1024 characters.');
  859.         }
  860.         if ( (is_null($location&& is_null($affinityGroup)) || (!is_null($location&& !is_null($affinityGroup)) ) {
  861.             throw new Microsoft_WindowsAzure_Management_Exception('Please specify a location -or- an affinity group for the service.');
  862.         }
  863.         
  864.         $locationOrAffinityGroup is_null($location)
  865.             ? '<AffinityGroup>' $affinityGroup '</AffinityGroup>'
  866.             : '<Location>' $location '</Location>';
  867.         
  868.         $response $this->_performRequest(self::OP_HOSTED_SERVICESarray(),
  869.             Microsoft_Http_Client::POST,
  870.             array('Content-Type' => 'application/xml; charset=utf-8'),
  871.             '<CreateHostedService xmlns="http://schemas.microsoft.com/windowsazure"><ServiceName>' $serviceName '</ServiceName><Label>' base64_encode($label'</Label><Description>' $description '</Description>' $locationOrAffinityGroup '</CreateHostedService>');
  872.      
  873.         if (!$response->isSuccessful()) {
  874.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  875.         }
  876.     }
  877.     
  878.     /**
  879.      * The Update Hosted Service operation updates the label and/or the description for a hosted service in Windows Azure.
  880.      * 
  881.      * @param string $serviceName A name for the hosted service that is unique to the subscription. This is the DNS name used for production deployments.
  882.      * @param string $label A label for the hosted service. The label may be up to 100 characters in length.
  883.      * @param string $description A description for the hosted service. The description may be up to 1024 characters in length.
  884.      */
  885.     public function updateHostedService($serviceName$label$description '')
  886.     {
  887.         if ($serviceName == '' || is_null($serviceName)) {
  888.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  889.         }
  890.         if ($label == '' || is_null($label)) {
  891.             throw new Microsoft_WindowsAzure_Management_Exception('Label should be specified.');
  892.         }
  893.         if (strlen($label100{
  894.             throw new Microsoft_WindowsAzure_Management_Exception('Label is too long. The maximum length is 100 characters.');
  895.         }
  896.         
  897.         $response $this->_performRequest(self::OP_HOSTED_SERVICES '/' $serviceNamearray(),
  898.             Microsoft_Http_Client::PUT,
  899.             array('Content-Type' => 'application/xml; charset=utf-8'),
  900.             '<UpdateHostedService xmlns="http://schemas.microsoft.com/windowsazure"><Label>' base64_encode($label'</Label><Description>' $description '</Description></UpdateHostedService>');
  901.      
  902.         if (!$response->isSuccessful()) {
  903.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  904.         }
  905.     }
  906.     
  907.     /**
  908.      * The Delete Hosted Service operation deletes the specified hosted service in Windows Azure.
  909.      * 
  910.      * @param string $serviceName A name for the hosted service that is unique to the subscription. This is the DNS name used for production deployments.
  911.      */
  912.     public function deleteHostedService($serviceName)
  913.     {
  914.         if ($serviceName == '' || is_null($serviceName)) {
  915.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  916.         }
  917.         
  918.         $response $this->_performRequest(self::OP_HOSTED_SERVICES '/' $serviceNamearray()Microsoft_Http_Client::DELETE);
  919.      
  920.         if (!$response->isSuccessful()) {
  921.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  922.         }
  923.     }
  924.     
  925.     /**
  926.      * The Get Hosted Service Properties operation retrieves system properties
  927.      * for the specified hosted service. These properties include the service
  928.      * name and service type; the name of the affinity group to which the service
  929.      * belongs, or its location if it is not part of an affinity group; and
  930.      * optionally, information on the service's deployments.
  931.      *
  932.      * @param string $serviceName The name of your service. This is the DNS name used for production deployments.
  933.      * @return Microsoft_WindowsAzure_Management_HostedServiceInstance 
  934.      * @throws Microsoft_WindowsAzure_Management_Exception
  935.      */
  936.     public function getHostedServiceProperties($serviceName)
  937.     {
  938.         if ($serviceName == '' || is_null($serviceName)) {
  939.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  940.         }
  941.         
  942.         $response $this->_performRequest(self::OP_HOSTED_SERVICES '/' $serviceNamearray('embed-detail' => 'true'));
  943.  
  944.         if ($response->isSuccessful()) {
  945.             $xmlService $this->_parseResponse($response);
  946.  
  947.             if (!is_null($xmlService)) {
  948.                 $returnValue new Microsoft_WindowsAzure_Management_HostedServiceInstance(
  949.                     (string)$xmlService->Url,
  950.                     (string)$xmlService->ServiceName,
  951.                     (string)$xmlService->HostedServiceProperties->Description,
  952.                     (string)$xmlService->HostedServiceProperties->AffinityGroup,
  953.                     (string)$xmlService->HostedServiceProperties->Location,
  954.                     (string)$xmlService->HostedServiceProperties->Label
  955.                 );
  956.                 
  957.                 // Deployments
  958.                 if (count($xmlService->Deployments->Deployment1{
  959.                     $xmlServices $xmlService->Deployments->Deployment;
  960.                 else {
  961.                     $xmlServices array($xmlService->Deployments->Deployment);
  962.                 }
  963.                 
  964.                 $deployments array();
  965.                 foreach ($xmlServices as $xmlDeployment{
  966.                     $deployments[$this->_convertXmlElementToDeploymentInstance($xmlDeployment);
  967.                 }
  968.                 $returnValue->Deployments $deployments;
  969.                 
  970.                 return $returnValue;
  971.             }
  972.             return null;
  973.         else {
  974.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  975.         }
  976.     }
  977.  
  978.     /**
  979.      * The Create Deployment operation uploads a new service package
  980.      * and creates a new deployment on staging or production.
  981.      * 
  982.      * @param string $serviceName        The service name. This is the DNS name used for production deployments.
  983.      * @param string $deploymentSlot    The deployment slot (production or staging)
  984.      * @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.
  985.      * @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.
  986.      * @param string $packageUrl        The service configuration file for the deployment.
  987.      * @param string $configuration     A label for this deployment, up to 100 characters in length.
  988.      * @param boolean $startDeployment  Indicates whether to start the deployment immediately after it is created.
  989.      * @param boolean $treatWarningsAsErrors Indicates whether to treat package validation warnings as errors.
  990.      * @throws Microsoft_WindowsAzure_Management_Exception
  991.      */
  992.     public function createDeployment($serviceName$deploymentSlot$name$label$packageUrl$configuration$startDeployment false$treatWarningsAsErrors false)
  993.     {
  994.         if ($serviceName == '' || is_null($serviceName)) {
  995.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  996.         }
  997.         $deploymentSlot strtolower($deploymentSlot);
  998.         if ($deploymentSlot != 'production' && $deploymentSlot != 'staging'{
  999.             throw new Microsoft_WindowsAzure_Management_Exception('Deployment slot should be production|staging.');
  1000.         }
  1001.         if ($name == '' || is_null($name)) {
  1002.             throw new Microsoft_WindowsAzure_Management_Exception('Name should be specified.');
  1003.         }
  1004.         if ($label == '' || is_null($label)) {
  1005.             throw new Microsoft_WindowsAzure_Management_Exception('Label should be specified.');
  1006.         }
  1007.         if (strlen($label100{
  1008.             throw new Microsoft_WindowsAzure_Management_Exception('Label is too long. The maximum length is 100 characters.');
  1009.         }
  1010.         if ($packageUrl == '' || is_null($packageUrl)) {
  1011.             throw new Microsoft_WindowsAzure_Management_Exception('Package URL should be specified.');
  1012.         }
  1013.         if ($configuration == '' || is_null($configuration)) {
  1014.             throw new Microsoft_WindowsAzure_Management_Exception('Configuration should be specified.');
  1015.         }
  1016.         
  1017.         if (@file_exists($configuration)) {
  1018.             $configuration utf8_decode(file_get_contents($configuration));
  1019.         }
  1020.         
  1021.         // Clean up the configuration
  1022.         $conformingConfiguration $this->_cleanConfiguration($configuration);
  1023.         
  1024.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/deploymentslots/' $deploymentSlot;
  1025.         $response $this->_performRequest($operationUrlarray(),
  1026.             Microsoft_Http_Client::POST,
  1027.             array('Content-Type' => 'application/xml; charset=utf-8'),
  1028.             '<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>');
  1029.      
  1030.         if (!$response->isSuccessful()) {
  1031.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  1032.         }        
  1033.     }
  1034.     
  1035.     /**
  1036.      * The Get Deployment operation returns configuration information, status,
  1037.      * and system properties for the specified deployment.
  1038.      * 
  1039.      * @param string $serviceName        The service name. This is the DNS name used for production deployments.
  1040.      * @param string $deploymentSlot    The deployment slot (production or staging)
  1041.      * @return Microsoft_WindowsAzure_Management_DeploymentInstance 
  1042.      * @throws Microsoft_WindowsAzure_Management_Exception
  1043.      */
  1044.     public function getDeploymentBySlot($serviceName$deploymentSlot)
  1045.     {
  1046.         if ($serviceName == '' || is_null($serviceName)) {
  1047.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  1048.         }
  1049.         $deploymentSlot strtolower($deploymentSlot);
  1050.         if ($deploymentSlot != 'production' && $deploymentSlot != 'staging'{
  1051.             throw new Microsoft_WindowsAzure_Management_Exception('Deployment slot should be production|staging.');
  1052.         }
  1053.         
  1054.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/deploymentslots/' $deploymentSlot;
  1055.         return $this->_getDeployment($operationUrl);
  1056.     }
  1057.     
  1058.     /**
  1059.      * The Get Deployment operation returns configuration information, status,
  1060.      * and system properties for the specified deployment.
  1061.      * 
  1062.      * @param string $serviceName        The service name. This is the DNS name used for production deployments.
  1063.      * @param string $deploymentId    The deployment ID as listed on the Windows Azure management portal
  1064.      * @return Microsoft_WindowsAzure_Management_DeploymentInstance 
  1065.      * @throws Microsoft_WindowsAzure_Management_Exception
  1066.      */
  1067.     public function getDeploymentByDeploymentId($serviceName$deploymentId)
  1068.     {
  1069.         if ($serviceName == '' || is_null($serviceName)) {
  1070.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  1071.         }
  1072.         if ($deploymentId == '' || is_null($deploymentId)) {
  1073.             throw new Microsoft_WindowsAzure_Management_Exception('Deployment ID should be specified.');
  1074.         }
  1075.         
  1076.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/deployments/' $deploymentId;
  1077.         return $this->_getDeployment($operationUrl);
  1078.     }
  1079.     
  1080.      /**
  1081.      * The Get Role Instances by Deployment Slot operation returns an array of arrays containing role instance information
  1082.      * for each role instance associated with the deployment specified by slot (staging or production).
  1083.      * 
  1084.      * @param string $serviceName        The service name. This is the DNS name used for production deployments.
  1085.      * @param string $deploymentSlot    The deployment slot (production or staging)
  1086.      * @return Array 
  1087.      * @throws Microsoft_WindowsAzure_Management_Exception
  1088.      */
  1089.     public function getRoleInstancesByDeploymentSlot($serviceName$deploymentSlot)
  1090.     {
  1091.         if ($serviceName == '' || is_null($serviceName)) {
  1092.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  1093.         }
  1094.         $deploymentSlot strtolower($deploymentSlot);
  1095.         if ($deploymentSlot != 'production' && $deploymentSlot != 'staging'{
  1096.             throw new Microsoft_WindowsAzure_Management_Exception('Deployment slot should be production|staging.');
  1097.         }
  1098.         
  1099.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/deploymentslots/' $deploymentSlot;
  1100.         $deployment $this->_getDeployment($operationUrl);
  1101.         return $deployment->roleInstanceList;
  1102.     }
  1103.     
  1104.     /**
  1105.      * The Get Role Instances by Deployment Slot operation returns an array of arrays containing role instance information
  1106.      * for each role instance associated with the deployment specified by Id.
  1107.      * 
  1108.      * @param string $serviceName        The service name. This is the DNS name used for production deployments.
  1109.      * @param string $deploymentId        The deployment ID as listed on the Windows Azure management portal
  1110.      * @return Array 
  1111.      * @throws Microsoft_WindowsAzure_Management_Exception
  1112.      */
  1113.     public function getRoleInstancesByDeploymentId($serviceName$deploymentId)
  1114.     {
  1115.         if ($serviceName == '' || is_null($serviceName)) {
  1116.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  1117.         }
  1118.         if ($deploymentId == '' || is_null($deploymentId)) {
  1119.             throw new Microsoft_WindowsAzure_Management_Exception('Deployment ID should be specified.');
  1120.         }
  1121.         
  1122.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/deployments/' $deploymentId;
  1123.         $deployment $this->_getDeployment($operationUrl);
  1124.         return $deployment->roleInstanceList;
  1125.     }
  1126.     
  1127.     /**
  1128.      * The Get Deployment operation returns configuration information, status,
  1129.      * and system properties for the specified deployment.
  1130.      * 
  1131.      * @param string $operationUrl        The operation url
  1132.      * @return Microsoft_WindowsAzure_Management_DeploymentInstance 
  1133.      * @throws Microsoft_WindowsAzure_Management_Exception
  1134.      */
  1135.     protected function _getDeployment($operationUrl)
  1136.     {
  1137.         $response $this->_performRequest($operationUrl);
  1138.  
  1139.         if ($response->isSuccessful()) {
  1140.             $xmlService $this->_parseResponse($response);
  1141.             
  1142.             return $this->_convertXmlElementToDeploymentInstance($xmlService);
  1143.         else {
  1144.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  1145.         }
  1146.     }
  1147.     
  1148.     /**
  1149.      * The Swap Deployment operation initiates a virtual IP swap between
  1150.      * the staging and production deployment environments for a service.
  1151.      * If the service is currently running in the staging environment,
  1152.      * it will be swapped to the production environment. If it is running
  1153.      * in the production environment, it will be swapped to staging.
  1154.      * 
  1155.      * @param string $serviceName The service name. This is the DNS name used for production deployments.
  1156.      * @param string $productionDeploymentName The name of the production deployment.
  1157.      * @param string $sourceDeploymentName The name of the source deployment.
  1158.      * @throws Microsoft_WindowsAzure_Management_Exception
  1159.      */
  1160.     public function swapDeployment($serviceName$productionDeploymentName$sourceDeploymentName)
  1161.     {
  1162.         if ($serviceName == '' || is_null($serviceName)) {
  1163.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  1164.         }
  1165.         if ($productionDeploymentName == '' || is_null($productionDeploymentName)) {
  1166.             throw new Microsoft_WindowsAzure_Management_Exception('Production Deployment ID should be specified.');
  1167.         }
  1168.         if ($sourceDeploymentName == '' || is_null($sourceDeploymentName)) {
  1169.             throw new Microsoft_WindowsAzure_Management_Exception('Source Deployment ID should be specified.');
  1170.         }
  1171.         
  1172.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName;
  1173.         $response $this->_performRequest($operationUrlarray(),
  1174.             Microsoft_Http_Client::POST,
  1175.             array('Content-Type' => 'application/xml; charset=utf-8'),
  1176.             '<Swap xmlns="http://schemas.microsoft.com/windowsazure"><Production>' $productionDeploymentName '</Production><SourceDeployment>' $sourceDeploymentName '</SourceDeployment></Swap>');
  1177.             
  1178.         if (!$response->isSuccessful()) {
  1179.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  1180.         }        
  1181.     }
  1182.     
  1183.     /**
  1184.      * The Delete Deployment operation deletes the specified deployment.
  1185.      * 
  1186.      * @param string $serviceName        The service name. This is the DNS name used for production deployments.
  1187.      * @param string $deploymentSlot    The deployment slot (production or staging)
  1188.      * @throws Microsoft_WindowsAzure_Management_Exception
  1189.      */
  1190.     public function deleteDeploymentBySlot($serviceName$deploymentSlot)
  1191.     {
  1192.         if ($serviceName == '' || is_null($serviceName)) {
  1193.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  1194.         }
  1195.         $deploymentSlot strtolower($deploymentSlot);
  1196.         if ($deploymentSlot != 'production' && $deploymentSlot != 'staging'{
  1197.             throw new Microsoft_WindowsAzure_Management_Exception('Deployment slot should be production|staging.');
  1198.         }
  1199.         
  1200.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/deploymentslots/' $deploymentSlot;
  1201.         return $this->_deleteDeployment($operationUrl);
  1202.     }
  1203.     
  1204.     /**
  1205.      * The Delete Deployment operation deletes the specified deployment.
  1206.      * 
  1207.      * @param string $serviceName        The service name. This is the DNS name used for production deployments.
  1208.      * @param string $deploymentId    The deployment ID as listed on the Windows Azure management portal
  1209.      * @throws Microsoft_WindowsAzure_Management_Exception
  1210.      */
  1211.     public function deleteDeploymentByDeploymentId($serviceName$deploymentId)
  1212.     {
  1213.         if ($serviceName == '' || is_null($serviceName)) {
  1214.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  1215.         }
  1216.         if ($deploymentId == '' || is_null($deploymentId)) {
  1217.             throw new Microsoft_WindowsAzure_Management_Exception('Deployment ID should be specified.');
  1218.         }
  1219.         
  1220.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/deployments/' $deploymentId;
  1221.         return $this->_deleteDeployment($operationUrl);
  1222.     }
  1223.     
  1224.     /**
  1225.      * The Delete Deployment operation deletes the specified deployment.
  1226.      * 
  1227.      * @param string $operationUrl        The operation url
  1228.      * @throws Microsoft_WindowsAzure_Management_Exception
  1229.      */
  1230.     protected function _deleteDeployment($operationUrl)
  1231.     {
  1232.         $response $this->_performRequest($operationUrlarray()Microsoft_Http_Client::DELETE);
  1233.              
  1234.         if (!$response->isSuccessful()) {
  1235.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  1236.         }
  1237.     }
  1238.     
  1239.     /**
  1240.      * The Update Deployment Status operation initiates a change in deployment status.
  1241.      * 
  1242.      * @param string $serviceName        The service name. This is the DNS name used for production deployments.
  1243.      * @param string $deploymentSlot    The deployment slot (production or staging)
  1244.      * @param string $status            The deployment status (running|suspended)
  1245.      * @throws Microsoft_WindowsAzure_Management_Exception
  1246.      */
  1247.     public function updateDeploymentStatusBySlot($serviceName$deploymentSlot$status 'running')
  1248.     {
  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.         $status strtolower($status);
  1257.         if ($status != 'running' && $status != 'suspended'{
  1258.             throw new Microsoft_WindowsAzure_Management_Exception('Status should be running|suspended.');
  1259.         }
  1260.         
  1261.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/deploymentslots/' $deploymentSlot;
  1262.         return $this->_updateDeploymentStatus($operationUrl$status);
  1263.     }
  1264.     
  1265.     /**
  1266.      * The Update Deployment Status operation initiates a change in deployment status.
  1267.      * 
  1268.      * @param string $serviceName        The service name. This is the DNS name used for production deployments.
  1269.      * @param string $deploymentId    The deployment ID as listed on the Windows Azure management portal
  1270.      * @param string $status            The deployment status (running|suspended)
  1271.      * @throws Microsoft_WindowsAzure_Management_Exception
  1272.      */
  1273.     public function updateDeploymentStatusByDeploymentId($serviceName$deploymentId$status 'running')
  1274.     {
  1275.         if ($serviceName == '' || is_null($serviceName)) {
  1276.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  1277.         }
  1278.         if ($deploymentId == '' || is_null($deploymentId)) {
  1279.             throw new Microsoft_WindowsAzure_Management_Exception('Deployment ID should be specified.');
  1280.         }
  1281.         $status strtolower($status);
  1282.         if ($status != 'running' && $status != 'suspended'{
  1283.             throw new Microsoft_WindowsAzure_Management_Exception('Status should be running|suspended.');
  1284.         }
  1285.         
  1286.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/deployments/' $deploymentId;
  1287.         return $this->_updateDeploymentStatus($operationUrl$status);
  1288.     }
  1289.     
  1290.     /**
  1291.      * The Update Deployment Status operation initiates a change in deployment status.
  1292.      * 
  1293.      * @param string $operationUrl        The operation url
  1294.      * @param string $status            The deployment status (running|suspended)
  1295.      * @throws Microsoft_WindowsAzure_Management_Exception
  1296.      */
  1297.     protected function _updateDeploymentStatus($operationUrl$status 'running')
  1298.     {
  1299.         $response $this->_performRequest($operationUrl '/'array('comp' => 'status'),
  1300.             Microsoft_Http_Client::POST,
  1301.             array('Content-Type' => 'application/xml; charset=utf-8'),
  1302.             '<UpdateDeploymentStatus xmlns="http://schemas.microsoft.com/windowsazure"><Status>' ucfirst($status'</Status></UpdateDeploymentStatus>');
  1303.             
  1304.         if (!$response->isSuccessful()) {
  1305.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  1306.         }
  1307.     }
  1308.     
  1309.     /**
  1310.      * Converts an XmlElement into a Microsoft_WindowsAzure_Management_DeploymentInstance
  1311.      * 
  1312.      * @param object $xmlService The XML Element
  1313.      * @return Microsoft_WindowsAzure_Management_DeploymentInstance 
  1314.      * @throws Microsoft_WindowsAzure_Management_Exception
  1315.      */
  1316.     protected function _convertXmlElementToDeploymentInstance($xmlService)
  1317.     {
  1318.         if (!is_null($xmlService)) {
  1319.             $returnValue new Microsoft_WindowsAzure_Management_DeploymentInstance(
  1320.                 (string)$xmlService->Name,
  1321.                 (string)$xmlService->DeploymentSlot,
  1322.                 (string)$xmlService->PrivateID,
  1323.                 (string)$xmlService->Label,
  1324.                 (string)$xmlService->Url,
  1325.                 (string)$xmlService->Configuration,
  1326.                 (string)$xmlService->Status,
  1327.                 (string)$xmlService->UpgradeStatus,
  1328.                 (string)$xmlService->UpgradeType,
  1329.                 (string)$xmlService->CurrentUpgradeDomainState,
  1330.                 (string)$xmlService->CurrentUpgradeDomain,
  1331.                 (string)$xmlService->UpgradeDomainCount,
  1332.                 (string)$xmlService->SdkVersion
  1333.             );
  1334.                 
  1335.             // Append role instances
  1336.             if ($xmlService->RoleInstanceList && $xmlService->RoleInstanceList->RoleInstance{
  1337.                 $xmlRoleInstances $xmlService->RoleInstanceList->RoleInstance;
  1338.                 if (count($xmlService->RoleInstanceList->RoleInstance== 1{
  1339.                     $xmlRoleInstances array($xmlService->RoleInstanceList->RoleInstance);
  1340.                 }
  1341.                     
  1342.                 $roleInstances array();
  1343.                 if (!is_null($xmlRoleInstances)) {                
  1344.                     for ($i 0$i count($xmlRoleInstances)$i++{
  1345.                         $roleInstances[array(
  1346.                             'rolename' => (string)$xmlRoleInstances[$i]->RoleName,
  1347.                             'instancename' => (string)$xmlRoleInstances[$i]->InstanceName,
  1348.                             'instancestatus' => (string)$xmlRoleInstances[$i]->InstanceStatus,
  1349.                             'instanceupgradedomain' => (string)$xmlRoleInstances[$i]->InstanceUpgradeDomain,
  1350.                             'instancefaultdomain' => (string)$xmlRoleInstances[$i]->InstanceFaultDomain,
  1351.                             'instancesize' => (string)$xmlRoleInstances[$i]->InstanceSize
  1352.                         );
  1353.                     }
  1354.                 }
  1355.                 
  1356.                 $returnValue->RoleInstanceList $roleInstances;
  1357.             }
  1358.             
  1359.             // Append roles
  1360.             if ($xmlService->RoleList && $xmlService->RoleList->Role{
  1361.                 $xmlRoles $xmlService->RoleList->Role;
  1362.                 if (count($xmlService->RoleList->Role== 1{
  1363.                     $xmlRoles array($xmlService->RoleList->Role);
  1364.                 }
  1365.                 
  1366.                 $roles array();
  1367.                 if (!is_null($xmlRoles)) {                
  1368.                     for ($i 0$i count($xmlRoles)$i++{
  1369.                         $roles[array(
  1370.                             'rolename' => (string)$xmlRoles[$i]->RoleName,
  1371.                             'osversion' => (!is_null($xmlRoles[$i]->OsVersion? (string)$xmlRoles[$i]->OsVersion : (string)$xmlRoles[$i]->OperatingSystemVersion)                    
  1372.                         );
  1373.                     }
  1374.                 }
  1375.                 $returnValue->RoleList $roles;
  1376.             }
  1377.  
  1378.             // Append InputEndpointList
  1379.             if ($xmlService->InputEndpointList && $xmlService->InputEndpointList->InputEndpoint{
  1380.                 $xmlInputEndpoints $xmlService->InputEndpointList->InputEndpoint;
  1381.                 if (count($xmlService->InputEndpointList->InputEndpoint== 1{
  1382.                     $xmlInputEndpoints array($xmlService->InputEndpointList->InputEndpoint);
  1383.                 }
  1384.                 
  1385.                 $inputEndpoints array();
  1386.                 if (!is_null($xmlInputEndpoints)) {                
  1387.                     for ($i 0$i count($xmlInputEndpoints)$i++{
  1388.                         $inputEndpoints[array(
  1389.                             'rolename' => (string)$xmlInputEndpoints[$i]->RoleName,
  1390.                             'vip' => (string)$xmlInputEndpoints[$i]->Vip,
  1391.                             'port' => (string)$xmlInputEndpoints[$i]->Port
  1392.                         );
  1393.                     }
  1394.                 }
  1395.                 
  1396.                 $returnValue->InputEndpoints $inputEndpoints;
  1397.             }
  1398.                 
  1399.             return $returnValue;
  1400.         }
  1401.         return null;
  1402.     }
  1403.     
  1404.     /**
  1405.      * Updates a deployment's role instance count.
  1406.      * 
  1407.      * @param string $serviceName        The service name. This is the DNS name used for production deployments.
  1408.      * @param string $deploymentSlot    The deployment slot (production or staging)
  1409.      * @param string|array$roleName    The role name
  1410.      * @param string|array$instanceCount The instance count
  1411.      * @throws Microsoft_WindowsAzure_Management_Exception
  1412.      */
  1413.     public function setInstanceCountBySlot($serviceName$deploymentSlot$roleName$instanceCount{
  1414.         if ($serviceName == '' || is_null($serviceName)) {
  1415.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  1416.         }
  1417.         $deploymentSlot strtolower($deploymentSlot);
  1418.         if ($deploymentSlot != 'production' && $deploymentSlot != 'staging'{
  1419.             throw new Microsoft_WindowsAzure_Management_Exception('Deployment slot should be production|staging.');
  1420.         }
  1421.         if ($roleName == '' || is_null($roleName)) {
  1422.             throw new Microsoft_WindowsAzure_Management_Exception('Role name name should be specified.');
  1423.         }
  1424.         
  1425.         // Get configuration
  1426.         $deployment $this->getDeploymentBySlot($serviceName$deploymentSlot);
  1427.         $configuration $deployment->Configuration;
  1428.         $configuration $this->_updateInstanceCountInConfiguration($roleName$instanceCount$configuration);
  1429.         
  1430.         // Update configuration
  1431.         $this->configureDeploymentBySlot($serviceName$deploymentSlot$configuration);        
  1432.     }
  1433.     
  1434.     /**
  1435.      * Updates a deployment's role instance count.
  1436.      * 
  1437.      * @param string $serviceName        The service name. This is the DNS name used for production deployments.
  1438.      * @param string $deploymentSlot    The deployment slot (production or staging)
  1439.      * @param string|array$roleName    The role name
  1440.      * @param string|array$instanceCount The instance count
  1441.      * @throws Microsoft_WindowsAzure_Management_Exception
  1442.      */
  1443.     public function setInstanceCountByDeploymentId($serviceName$deploymentId$roleName$instanceCount)
  1444.     {
  1445.         if ($serviceName == '' || is_null($serviceName)) {
  1446.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  1447.         }
  1448.         if ($deploymentId == '' || is_null($deploymentId)) {
  1449.             throw new Microsoft_WindowsAzure_Management_Exception('Deployment ID should be specified.');
  1450.         }
  1451.         if ($roleName == '' || is_null($roleName)) {
  1452.             throw new Microsoft_WindowsAzure_Management_Exception('Role name name should be specified.');
  1453.         }
  1454.         
  1455.         // Get configuration
  1456.         $deployment $this->getDeploymentByDeploymentId($serviceName$deploymentId);
  1457.         $configuration $deployment->Configuration;
  1458.         $configuration $this->_updateInstanceCountInConfiguration($roleName$instanceCount$configuration);
  1459.         
  1460.         // Update configuration
  1461.         $this->configureDeploymentByDeploymentId($serviceName$deploymentId$configuration);
  1462.     }
  1463.     
  1464.     /**
  1465.      * Updates instance count in configuration XML.
  1466.      * 
  1467.      * @param string|array$roleName            The role name
  1468.      * @param string|array$instanceCount        The instance count
  1469.      * @param string $configuration             XML configuration represented as a string
  1470.      * @throws Microsoft_WindowsAzure_Management_Exception
  1471.      */
  1472.     protected function _updateInstanceCountInConfiguration($roleName$instanceCount$configuration{
  1473.         // Change variables
  1474.         if (!is_array($roleName)) {
  1475.             $roleName array($roleName);
  1476.         }
  1477.         if (!is_array($instanceCount)) {
  1478.             $instanceCount array($instanceCount);
  1479.         }
  1480.  
  1481.         $configuration preg_replace('/(<\?xml[^?]+?)utf-16/i''$1utf-8'$configuration);
  1482.         //$configuration = '<?xml version="1.0">' . substr($configuration, strpos($configuration, '>') + 2);
  1483.  
  1484.         $xml simplexml_load_string($configuration)
  1485.         
  1486.         // http://www.php.net/manual/en/simplexmlelement.xpath.php#97818
  1487.         $namespaces $xml->getDocNamespaces();
  1488.         $xml->registerXPathNamespace('__empty_ns'$namespaces[''])
  1489.     
  1490.         for ($i 0$i count($roleName)$i++{
  1491.             $elements $xml->xpath('//__empty_ns:Role[@name="' $roleName[$i'"]/__empty_ns:Instances');
  1492.     
  1493.             if (count($elements== 1{
  1494.                 $element $elements[0];
  1495.                 $element['count'$instanceCount[$i];
  1496.             
  1497.         }
  1498.         
  1499.         $configuration $xml->asXML();
  1500.         //$configuration = preg_replace('/(<\?xml[^?]+?)utf-8/i', '$1utf-16', $configuration);
  1501.  
  1502.         return $configuration;
  1503.     }
  1504.     
  1505.     /**
  1506.      * The Change Deployment Configuration request may be specified as follows.
  1507.      * Note that you can change a deployment's configuration either by specifying the deployment
  1508.      * environment (staging or production), or by specifying the deployment's unique name.
  1509.      * 
  1510.      * @param string $serviceName        The service name. This is the DNS name used for production deployments.
  1511.      * @param string $deploymentSlot    The deployment slot (production or staging)
  1512.      * @param string $configuration     XML configuration represented as a string
  1513.      * @throws Microsoft_WindowsAzure_Management_Exception
  1514.      */
  1515.     public function configureDeploymentBySlot($serviceName$deploymentSlot$configuration)
  1516.     {
  1517.         if ($serviceName == '' || is_null($serviceName)) {
  1518.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  1519.         }
  1520.         $deploymentSlot strtolower($deploymentSlot);
  1521.         if ($deploymentSlot != 'production' && $deploymentSlot != 'staging'{
  1522.             throw new Microsoft_WindowsAzure_Management_Exception('Deployment slot should be production|staging.');
  1523.         }
  1524.         if ($configuration == '' || is_null($configuration)) {
  1525.             throw new Microsoft_WindowsAzure_Management_Exception('Configuration name should be specified.');
  1526.         }
  1527.         
  1528.         if (@file_exists($configuration)) {
  1529.             $configuration utf8_decode(file_get_contents($configuration));
  1530.         }
  1531.         
  1532.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/deploymentslots/' $deploymentSlot;
  1533.         return $this->_configureDeployment($operationUrl$configuration);
  1534.     }
  1535.     
  1536.     /**
  1537.      * The Change Deployment Configuration request may be specified as follows.
  1538.      * Note that you can change a deployment's configuration either by specifying the deployment
  1539.      * environment (staging or production), or by specifying the deployment's unique name.
  1540.      * 
  1541.      * @param string $serviceName        The service name. This is the DNS name used for production deployments.
  1542.      * @param string $deploymentId    The deployment ID as listed on the Windows Azure management portal
  1543.      * @param string $configuration     XML configuration represented as a string
  1544.      * @throws Microsoft_WindowsAzure_Management_Exception
  1545.      */
  1546.     public function configureDeploymentByDeploymentId($serviceName$deploymentId$configuration)
  1547.     {
  1548.         if ($serviceName == '' || is_null($serviceName)) {
  1549.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  1550.         }
  1551.         if ($deploymentId == '' || is_null($deploymentId)) {
  1552.             throw new Microsoft_WindowsAzure_Management_Exception('Deployment ID should be specified.');
  1553.         }
  1554.         if ($configuration == '' || is_null($configuration)) {
  1555.             throw new Microsoft_WindowsAzure_Management_Exception('Configuration name should be specified.');
  1556.         }
  1557.         
  1558.         if (@file_exists($configuration)) {
  1559.             $configuration utf8_decode(file_get_contents($configuration));
  1560.         }
  1561.         
  1562.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/deployments/' $deploymentId;
  1563.         return $this->_configureDeployment($operationUrl$configuration);
  1564.     }
  1565.     
  1566.     /**
  1567.      * The Change Deployment Configuration request may be specified as follows.
  1568.      * Note that you can change a deployment's configuration either by specifying the deployment
  1569.      * environment (staging or production), or by specifying the deployment's unique name.
  1570.      * 
  1571.      * @param string $operationUrl        The operation url
  1572.      * @param string $configuration     XML configuration represented as a string
  1573.      * @throws Microsoft_WindowsAzure_Management_Exception
  1574.      */
  1575.     protected function _configureDeployment($operationUrl$configuration)
  1576.     {
  1577.         // Clean up the configuration
  1578.         $conformingConfiguration $this->_cleanConfiguration($configuration);
  1579.  
  1580.         $response $this->_performRequest($operationUrl '/'array('comp' => 'config'),
  1581.             Microsoft_Http_Client::POST,
  1582.             array('Content-Type' => 'application/xml; charset=utf-8'),
  1583.             '<ChangeConfiguration xmlns="http://schemas.microsoft.com/windowsazure" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Configuration>' base64_encode($conformingConfiguration'</Configuration></ChangeConfiguration>');
  1584.              
  1585.         if (!$response->isSuccessful()) {
  1586.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  1587.         }
  1588.     }
  1589.     
  1590.     /**
  1591.      * The Upgrade Deployment operation initiates an upgrade.
  1592.      * 
  1593.      * @param string $serviceName        The service name. This is the DNS name used for production deployments.
  1594.      * @param string $deploymentSlot    The deployment slot (production or staging)
  1595.      * @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.
  1596.      * @param string $packageUrl        The service configuration file for the deployment.
  1597.      * @param string $configuration     A label for this deployment, up to 100 characters in length.
  1598.      * @param string $mode              The type of upgrade to initiate. Possible values are Auto or Manual.
  1599.      * @param string $roleToUpgrade     The name of the specific role to upgrade.
  1600.      * @throws Microsoft_WindowsAzure_Management_Exception
  1601.      */
  1602.     public function upgradeDeploymentBySlot($serviceName$deploymentSlot$label$packageUrl$configuration$mode 'auto'$roleToUpgrade null)
  1603.     {
  1604.         if ($serviceName == '' || is_null($serviceName)) {
  1605.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  1606.         }
  1607.         $deploymentSlot strtolower($deploymentSlot);
  1608.         if ($deploymentSlot != 'production' && $deploymentSlot != 'staging'{
  1609.             throw new Microsoft_WindowsAzure_Management_Exception('Deployment slot should be production|staging.');
  1610.         }
  1611.         if ($label == '' || is_null($label)) {
  1612.             throw new Microsoft_WindowsAzure_Management_Exception('Label should be specified.');
  1613.         }
  1614.         if (strlen($label100{
  1615.             throw new Microsoft_WindowsAzure_Management_Exception('Label is too long. The maximum length is 100 characters.');
  1616.         }
  1617.         if ($packageUrl == '' || is_null($packageUrl)) {
  1618.             throw new Microsoft_WindowsAzure_Management_Exception('Package URL should be specified.');
  1619.         }
  1620.         if ($configuration == '' || is_null($configuration)) {
  1621.             throw new Microsoft_WindowsAzure_Management_Exception('Configuration should be specified.');
  1622.         }
  1623.         $mode strtolower($mode);
  1624.         if ($mode != 'auto' && $mode != 'manual'{
  1625.             throw new Microsoft_WindowsAzure_Management_Exception('Mode should be auto|manual.');
  1626.         }
  1627.         
  1628.         if (@file_exists($configuration)) {
  1629.             $configuration utf8_decode(file_get_contents($configuration));
  1630.         }
  1631.         
  1632.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/deploymentslots/' $deploymentSlot;
  1633.         return $this->_upgradeDeployment($operationUrl$label$packageUrl$configuration$mode$roleToUpgrade);      
  1634.     }
  1635.     
  1636.     /**
  1637.      * The Upgrade Deployment operation initiates an upgrade.
  1638.      * 
  1639.      * @param string $serviceName        The service name. This is the DNS name used for production deployments.
  1640.      * @param string $deploymentId    The deployment ID as listed on the Windows Azure management portal
  1641.      * @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.
  1642.      * @param string $packageUrl        The service configuration file for the deployment.
  1643.      * @param string $configuration     A label for this deployment, up to 100 characters in length.
  1644.      * @param string $mode              The type of upgrade to initiate. Possible values are Auto or Manual.
  1645.      * @param string $roleToUpgrade     The name of the specific role to upgrade.
  1646.      * @throws Microsoft_WindowsAzure_Management_Exception
  1647.      */
  1648.     public function upgradeDeploymentByDeploymentId($serviceName$deploymentId$label$packageUrl$configuration$mode 'auto'$roleToUpgrade null)
  1649.     {
  1650.         if ($serviceName == '' || is_null($serviceName)) {
  1651.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  1652.         }
  1653.         if ($deploymentId == '' || is_null($deploymentId)) {
  1654.             throw new Microsoft_WindowsAzure_Management_Exception('Deployment ID should be specified.');
  1655.         }
  1656.         if ($label == '' || is_null($label)) {
  1657.             throw new Microsoft_WindowsAzure_Management_Exception('Label should be specified.');
  1658.         }
  1659.         if (strlen($label100{
  1660.             throw new Microsoft_WindowsAzure_Management_Exception('Label is too long. The maximum length is 100 characters.');
  1661.         }
  1662.         if ($packageUrl == '' || is_null($packageUrl)) {
  1663.             throw new Microsoft_WindowsAzure_Management_Exception('Package URL should be specified.');
  1664.         }
  1665.         if ($configuration == '' || is_null($configuration)) {
  1666.             throw new Microsoft_WindowsAzure_Management_Exception('Configuration should be specified.');
  1667.         }
  1668.         $mode strtolower($mode);
  1669.         if ($mode != 'auto' && $mode != 'manual'{
  1670.             throw new Microsoft_WindowsAzure_Management_Exception('Mode should be auto|manual.');
  1671.         }
  1672.         
  1673.         if (@file_exists($configuration)) {
  1674.             $configuration utf8_decode(file_get_contents($configuration));
  1675.         }
  1676.         
  1677.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/deployments/' $deploymentId;
  1678.         return $this->_upgradeDeployment($operationUrl$label$packageUrl$configuration$mode$roleToUpgrade);      
  1679.     }
  1680.     
  1681.     
  1682.     /**
  1683.      * The Upgrade Deployment operation initiates an upgrade.
  1684.      * 
  1685.      * @param string $operationUrl        The operation url
  1686.      * @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.
  1687.      * @param string $packageUrl        The service configuration file for the deployment.
  1688.      * @param string $configuration     A label for this deployment, up to 100 characters in length.
  1689.      * @param string $mode              The type of upgrade to initiate. Possible values are Auto or Manual.
  1690.      * @param string $roleToUpgrade     The name of the specific role to upgrade.
  1691.      * @throws Microsoft_WindowsAzure_Management_Exception
  1692.      */
  1693.     protected function _upgradeDeployment($operationUrl$label$packageUrl$configuration$mode$roleToUpgrade)
  1694.     {
  1695.         // Clean up the configuration
  1696.         $conformingConfiguration $this->_cleanConfiguration($configuration);
  1697.         
  1698.         $response $this->_performRequest($operationUrl '/'array('comp' => 'upgrade'),
  1699.             Microsoft_Http_Client::POST,
  1700.             array('Content-Type' => 'application/xml; charset=utf-8'),
  1701.             '<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>');        
  1702.             
  1703.         if (!$response->isSuccessful()) {
  1704.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  1705.         }
  1706.     }
  1707.     
  1708.     /**
  1709.      * The Walk Upgrade Domain operation specifies the next upgrade domain to be walked during an in-place upgrade.
  1710.      * 
  1711.      * @param string $serviceName        The service name. This is the DNS name used for production deployments.
  1712.      * @param string $deploymentSlot    The deployment slot (production or staging)
  1713.      * @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.
  1714.      * @throws Microsoft_WindowsAzure_Management_Exception
  1715.      */
  1716.     public function walkUpgradeDomainBySlot($serviceName$deploymentSlot$upgradeDomain 0)
  1717.     {
  1718.         if ($serviceName == '' || is_null($serviceName)) {
  1719.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  1720.         }
  1721.         $deploymentSlot strtolower($deploymentSlot);
  1722.         if ($deploymentSlot != 'production' && $deploymentSlot != 'staging'{
  1723.             throw new Microsoft_WindowsAzure_Management_Exception('Deployment slot should be production|staging.');
  1724.         }
  1725.         
  1726.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/deploymentslots/' $deploymentSlot;
  1727.         return $this->_walkUpgradeDomain($operationUrl$upgradeDomain);      
  1728.     }
  1729.     
  1730.     /**
  1731.      * The Walk Upgrade Domain operation specifies the next upgrade domain to be walked during an in-place upgrade.
  1732.      * 
  1733.      * @param string $serviceName        The service name. This is the DNS name used for production deployments.
  1734.      * @param string $deploymentId    The deployment ID as listed on the Windows Azure management portal
  1735.      * @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.
  1736.      * @throws Microsoft_WindowsAzure_Management_Exception
  1737.      */
  1738.     public function walkUpgradeDomainByDeploymentId($serviceName$deploymentId$upgradeDomain 0)
  1739.     {
  1740.         if ($serviceName == '' || is_null($serviceName)) {
  1741.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  1742.         }
  1743.         if ($deploymentId == '' || is_null($deploymentId)) {
  1744.             throw new Microsoft_WindowsAzure_Management_Exception('Deployment ID should be specified.');
  1745.         }
  1746.         
  1747.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/deployments/' $deploymentId;
  1748.         return $this->_walkUpgradeDomain($operationUrl$upgradeDomain);      
  1749.     }
  1750.     
  1751.     
  1752.     /**
  1753.      * The Walk Upgrade Domain operation specifies the next upgrade domain to be walked during an in-place upgrade.
  1754.      * 
  1755.      * @param string $operationUrl   The operation url
  1756.      * @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.
  1757.      * @throws Microsoft_WindowsAzure_Management_Exception
  1758.      */
  1759.     protected function _walkUpgradeDomain($operationUrl$upgradeDomain 0)
  1760.     {
  1761.         $response $this->_performRequest($operationUrl '/'array('comp' => 'walkupgradedomain'),
  1762.             Microsoft_Http_Client::POST,
  1763.             array('Content-Type' => 'application/xml; charset=utf-8'),
  1764.             '<WalkUpgradeDomain xmlns="http://schemas.microsoft.com/windowsazure"><UpgradeDomain>' $upgradeDomain '</UpgradeDomain></WalkUpgradeDomain>');        
  1765.  
  1766.         if (!$response->isSuccessful()) {
  1767.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  1768.         }
  1769.     }
  1770.     
  1771.     /**
  1772.      * The Reboot Role Instance operation requests a reboot of a role instance
  1773.      * that is running in a deployment.
  1774.      * 
  1775.      * @param string $serviceName        The service name. This is the DNS name used for production deployments.
  1776.      * @param string $deploymentSlot    The deployment slot (production or staging)
  1777.      * @param string $roleInstanceName  The role instance name
  1778.      * @throws Microsoft_WindowsAzure_Management_Exception
  1779.      */
  1780.     public function rebootRoleInstanceBySlot($serviceName$deploymentSlot$roleInstanceName)
  1781.     {
  1782.         if ($serviceName == '' || is_null($serviceName)) {
  1783.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  1784.         }
  1785.         $deploymentSlot strtolower($deploymentSlot);
  1786.         if ($deploymentSlot != 'production' && $deploymentSlot != 'staging'{
  1787.             throw new Microsoft_WindowsAzure_Management_Exception('Deployment slot should be production|staging.');
  1788.         }
  1789.         if ($roleInstanceName == '' || is_null($roleInstanceName)) {
  1790.             throw new Microsoft_WindowsAzure_Management_Exception('Role instance name should be specified.');
  1791.         }
  1792.         
  1793.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/deploymentslots/' $deploymentSlot '/roleinstances/' $roleInstanceName;
  1794.         return $this->_rebootOrReimageRoleInstance($operationUrl'reboot');
  1795.     }
  1796.     
  1797.     /**
  1798.      * The Reboot Role Instance operation requests a reboot of a role instance
  1799.      * that is running in a deployment.
  1800.      * 
  1801.      * @param string $serviceName        The service name. This is the DNS name used for production deployments.
  1802.      * @param string $deploymentId    The deployment ID as listed on the Windows Azure management portal
  1803.      * @param string $roleInstanceName  The role instance name
  1804.      * @throws Microsoft_WindowsAzure_Management_Exception
  1805.      */
  1806.     public function rebootRoleInstanceByDeploymentId($serviceName$deploymentId$roleInstanceName)
  1807.     {
  1808.         if ($serviceName == '' || is_null($serviceName)) {
  1809.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  1810.         }
  1811.         if ($deploymentId == '' || is_null($deploymentId)) {
  1812.             throw new Microsoft_WindowsAzure_Management_Exception('Deployment ID should be specified.');
  1813.         }
  1814.         if ($roleInstanceName == '' || is_null($roleInstanceName)) {
  1815.             throw new Microsoft_WindowsAzure_Management_Exception('Role instance name should be specified.');
  1816.         }
  1817.         
  1818.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/deployments/' $deploymentId '/roleinstances/' $roleInstanceName;
  1819.         return $this->_rebootOrReimageRoleInstance($operationUrl'reboot');
  1820.     }
  1821.  
  1822.     /**
  1823.      * The Reimage Role Instance operation requests a reimage of a role instance
  1824.      * that is running in a deployment.
  1825.      * 
  1826.      * @param string $serviceName        The service name. This is the DNS name used for production deployments.
  1827.      * @param string $deploymentSlot    The deployment slot (production or staging)
  1828.      * @param string $roleInstanceName  The role instance name
  1829.      * @throws Microsoft_WindowsAzure_Management_Exception
  1830.      */
  1831.     public function reimageRoleInstanceBySlot($serviceName$deploymentSlot$roleInstanceName)
  1832.     {
  1833.         if ($serviceName == '' || is_null($serviceName)) {
  1834.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  1835.         }
  1836.         $deploymentSlot strtolower($deploymentSlot);
  1837.         if ($deploymentSlot != 'production' && $deploymentSlot != 'staging'{
  1838.             throw new Microsoft_WindowsAzure_Management_Exception('Deployment slot should be production|staging.');
  1839.         }
  1840.         if ($roleInstanceName == '' || is_null($roleInstanceName)) {
  1841.             throw new Microsoft_WindowsAzure_Management_Exception('Role instance name should be specified.');
  1842.         }
  1843.         
  1844.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/deploymentslots/' $deploymentSlot '/roleinstances/' $roleInstanceName;
  1845.         return $this->_rebootOrReimageRoleInstance($operationUrl'reimage');
  1846.     }
  1847.     
  1848.     /**
  1849.      * The Reimage Role Instance operation requests a reimage of a role instance
  1850.      * that is running in a deployment.
  1851.      * 
  1852.      * @param string $serviceName        The service name. This is the DNS name used for production deployments.
  1853.      * @param string $deploymentId        The deployment ID as listed on the Windows Azure management portal
  1854.      * @param string $roleInstanceName  The role instance name
  1855.      * @throws Microsoft_WindowsAzure_Management_Exception
  1856.      */
  1857.     public function reimageRoleInstanceByDeploymentId($serviceName$deploymentId$roleInstanceName)
  1858.     {
  1859.         if ($serviceName == '' || is_null($serviceName)) {
  1860.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  1861.         }
  1862.         if ($deploymentId == '' || is_null($deploymentId)) {
  1863.             throw new Microsoft_WindowsAzure_Management_Exception('Deployment ID should be specified.');
  1864.         }
  1865.         if ($roleInstanceName == '' || is_null($roleInstanceName)) {
  1866.             throw new Microsoft_WindowsAzure_Management_Exception('Role instance name should be specified.');
  1867.         }
  1868.         
  1869.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/deployments/' $deploymentId '/roleinstances/' $roleInstanceName;
  1870.         return $this->_rebootOrReimageRoleInstance($operationUrl'reimage');
  1871.     }
  1872.     
  1873.     /**
  1874.      * Reboots or reimages a role instance.
  1875.      * 
  1876.      * @param string $operationUrl        The operation url
  1877.      * @param string $operation The operation (reboot|reimage)
  1878.      * @throws Microsoft_WindowsAzure_Management_Exception
  1879.      */
  1880.     protected function _rebootOrReimageRoleInstance($operationUrl$operation 'reboot')
  1881.     {
  1882.         $response $this->_performRequest($operationUrlarray('comp' => $operation)Microsoft_Http_Client::POST);
  1883.             
  1884.         if (!$response->isSuccessful()) {
  1885.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  1886.         }
  1887.     }
  1888.     
  1889.     /**
  1890.      * The List Certificates operation lists all certificates associated with
  1891.      * the specified hosted service.
  1892.      * 
  1893.      * @param string $serviceName        The service name
  1894.      * @return array Array of Microsoft_WindowsAzure_Management_CertificateInstance
  1895.      * @throws Microsoft_WindowsAzure_Management_Exception
  1896.      */
  1897.     public function listCertificates($serviceName)
  1898.     {
  1899.         if ($serviceName == '' || is_null($serviceName)) {
  1900.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  1901.         }
  1902.         
  1903.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/certificates';
  1904.         $response $this->_performRequest($operationUrl);
  1905.  
  1906.         if ($response->isSuccessful()) {
  1907.             $result $this->_parseResponse($response);
  1908.  
  1909.             if (!$result->Certificate{
  1910.                 return array();
  1911.             }
  1912.             if (count($result->Certificate1{
  1913.                 $xmlServices $result->Certificate;
  1914.             else {
  1915.                 $xmlServices array($result->Certificate);
  1916.             }
  1917.  
  1918.             $services array();
  1919.             if (!is_null($xmlServices)) {                
  1920.                 for ($i 0$i count($xmlServices)$i++{
  1921.                     $services[new Microsoft_WindowsAzure_Management_CertificateInstance(
  1922.                         (string)$xmlServices[$i]->CertificateUrl,
  1923.                         (string)$xmlServices[$i]->Thumbprint,
  1924.                         (string)$xmlServices[$i]->ThumbprintAlgorithm,
  1925.                         (string)$xmlServices[$i]->Data
  1926.                     );
  1927.                 }
  1928.             }
  1929.             return $services;
  1930.         else {
  1931.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  1932.         }
  1933.     }
  1934.     
  1935.     /**
  1936.      * The Get Certificate operation returns the public data for the specified certificate.
  1937.      * 
  1938.      * @param string $serviceName|$certificateUrl    The service name -or- the certificate URL
  1939.      * @param string $algorithm                     Algorithm
  1940.      * @param string $thumbprint                    Thumbprint
  1941.      * @return Microsoft_WindowsAzure_Management_CertificateInstance 
  1942.      * @throws Microsoft_WindowsAzure_Management_Exception
  1943.      */
  1944.     public function getCertificate($serviceName$algorithm ''$thumbprint '')
  1945.     {
  1946.         if ($serviceName == '' || is_null($serviceName)) {
  1947.             throw new Microsoft_WindowsAzure_Management_Exception('Service name or certificate URL should be specified.');
  1948.         }
  1949.         if (strpos($serviceName'https'=== false && ($algorithm == '' || is_null($algorithm)) && ($thumbprint == '' || is_null($thumbprint))) {
  1950.             throw new Microsoft_WindowsAzure_Management_Exception('Algorithm and thumbprint should be specified.');
  1951.         }
  1952.         
  1953.         $operationUrl str_replace($this->getBaseUrl()''$serviceName);
  1954.         if (strpos($serviceName'https'=== false{
  1955.             $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/certificates/' $algorithm '-' strtoupper($thumbprint);
  1956.         }
  1957.         
  1958.         $response $this->_performRequest($operationUrl);
  1959.  
  1960.         if ($response->isSuccessful()) {
  1961.             $result $this->_parseResponse($response);
  1962.             
  1963.             return new Microsoft_WindowsAzure_Management_CertificateInstance(
  1964.                 $this->getBaseUrl($operationUrl,
  1965.                 $thumbprint,
  1966.                 $algorithm,
  1967.                 (string)$result->Data
  1968.             );
  1969.         else {
  1970.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  1971.         }
  1972.     }
  1973.     
  1974.     /**
  1975.      * The Add Certificate operation adds a certificate to the subscription.
  1976.      * 
  1977.      * @param string $serviceName         The service name
  1978.      * @param string $certificateData     Certificate data
  1979.      * @param string $certificatePassword The certificate password
  1980.      * @param string $certificateFormat   The certificate format. Currently, only 'pfx' is supported.
  1981.      * @throws Microsoft_WindowsAzure_Management_Exception
  1982.      */
  1983.     public function addCertificate($serviceName$certificateData$certificatePassword$certificateFormat 'pfx')
  1984.     {
  1985.         if ($serviceName == '' || is_null($serviceName)) {
  1986.             throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
  1987.         }
  1988.         if ($certificateData == '' || is_null($certificateData)) {
  1989.             throw new Microsoft_WindowsAzure_Management_Exception('Certificate data should be specified.');
  1990.         }
  1991.         if ($certificatePassword == '' || is_null($certificatePassword)) {
  1992.             throw new Microsoft_WindowsAzure_Management_Exception('Certificate password should be specified.');
  1993.         }
  1994.         if ($certificateFormat != 'pfx'{
  1995.             throw new Microsoft_WindowsAzure_Management_Exception('Certificate format should be "pfx".');
  1996.         }
  1997.         
  1998.         if (@file_exists($certificateData)) {
  1999.             $certificateData file_get_contents($certificateData);
  2000.         }
  2001.         
  2002.         $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/certificates';
  2003.         $response $this->_performRequest($operationUrlarray(),
  2004.             Microsoft_Http_Client::POST,
  2005.             array('Content-Type' => 'application/xml; charset=utf-8'),
  2006.             '<CertificateFile xmlns="http://schemas.microsoft.com/windowsazure"><Data>' base64_encode($certificateData'</Data><CertificateFormat>' $certificateFormat '</CertificateFormat><Password>' $certificatePassword '</Password></CertificateFile>');
  2007.  
  2008.         if (!$response->isSuccessful()) {
  2009.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  2010.         }
  2011.     }
  2012.     
  2013.     /**
  2014.      * The Delete Certificate operation deletes a certificate from the subscription's certificate store.
  2015.      * 
  2016.      * @param string $serviceName|$certificateUrl    The service name -or- the certificate URL
  2017.      * @param string $algorithm                     Algorithm
  2018.      * @param string $thumbprint                    Thumbprint
  2019.      * @throws Microsoft_WindowsAzure_Management_Exception
  2020.      */
  2021.     public function deleteCertificate($serviceName$algorithm ''$thumbprint '')
  2022.     {
  2023.         if ($serviceName == '' || is_null($serviceName)) {
  2024.             throw new Microsoft_WindowsAzure_Management_Exception('Service name or certificate URL should be specified.');
  2025.         }
  2026.         if (strpos($serviceName'https'=== false && ($algorithm == '' || is_null($algorithm)) && ($thumbprint == '' || is_null($thumbprint))) {
  2027.             throw new Microsoft_WindowsAzure_Management_Exception('Algorithm and thumbprint should be specified.');
  2028.         }
  2029.         
  2030.         $operationUrl str_replace($this->getBaseUrl()''$serviceName);
  2031.         if (strpos($serviceName'https'=== false{
  2032.             $operationUrl self::OP_HOSTED_SERVICES '/' $serviceName '/certificates/' $algorithm '-' strtoupper($thumbprint);
  2033.         }
  2034.         
  2035.         $response $this->_performRequest($operationUrlarray()Microsoft_Http_Client::DELETE);
  2036.  
  2037.         if (!$response->isSuccessful()) {
  2038.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  2039.         }
  2040.     }
  2041.     
  2042.     /**
  2043.      * The List Affinity Groups operation lists the affinity groups associated with
  2044.      * the specified subscription.
  2045.      * 
  2046.      * @return array Array of Microsoft_WindowsAzure_Management_AffinityGroupInstance
  2047.      * @throws Microsoft_WindowsAzure_Management_Exception
  2048.      */
  2049.     public function listAffinityGroups()
  2050.     {
  2051.         $response $this->_performRequest(self::OP_AFFINITYGROUPS);
  2052.  
  2053.         if ($response->isSuccessful()) {
  2054.             $result $this->_parseResponse($response);
  2055.             
  2056.             if (!$result->AffinityGroup{
  2057.                 return array();
  2058.             }
  2059.             if (count($result->AffinityGroup1{
  2060.                 $xmlServices $result->AffinityGroup;
  2061.             else {
  2062.                 $xmlServices array($result->AffinityGroup);
  2063.             }
  2064.             
  2065.             $services array();
  2066.             if (!is_null($xmlServices)) {                
  2067.                 for ($i 0$i count($xmlServices)$i++{
  2068.                     $services[new Microsoft_WindowsAzure_Management_AffinityGroupInstance(
  2069.                         (string)$xmlServices[$i]->Name,
  2070.                         (string)$xmlServices[$i]->Label,
  2071.                         (string)$xmlServices[$i]->Description,
  2072.                         (string)$xmlServices[$i]->Location
  2073.                     );
  2074.                 }
  2075.             }
  2076.             return $services;
  2077.         else {
  2078.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  2079.         }
  2080.     }
  2081.     
  2082.     /**
  2083.      * The Create Affinity Group operation creates a new affinity group for the specified subscription.
  2084.      * 
  2085.      * @param string $name A name for the affinity group that is unique to the subscription.
  2086.      * @param string $label A label for the affinity group. The label may be up to 100 characters in length.
  2087.      * @param string $description A description for the affinity group. The description may be up to 1024 characters in length.
  2088.      * @param string $location The location where the affinity group will be created. To list available locations, use the List Locations operation.
  2089.      */
  2090.     public function createAffinityGroup($name$label$description ''$location '')
  2091.     {
  2092.         if ($name == '' || is_null($name)) {
  2093.             throw new Microsoft_WindowsAzure_Management_Exception('Affinity group name should be specified.');
  2094.         }
  2095.         if ($label == '' || is_null($label)) {
  2096.             throw new Microsoft_WindowsAzure_Management_Exception('Label should be specified.');
  2097.         }
  2098.         if (strlen($label100{
  2099.             throw new Microsoft_WindowsAzure_Management_Exception('Label is too long. The maximum length is 100 characters.');
  2100.         }
  2101.         if (strlen($description1024{
  2102.             throw new Microsoft_WindowsAzure_Management_Exception('Description is too long. The maximum length is 1024 characters.');
  2103.         }
  2104.         if ($location == '' || is_null($location)) {
  2105.             throw new Microsoft_WindowsAzure_Management_Exception('Location should be specified.');
  2106.         }
  2107.         
  2108.         $response $this->_performRequest(self::OP_AFFINITYGROUPSarray(),
  2109.             Microsoft_Http_Client::POST,
  2110.             array('Content-Type' => 'application/xml; charset=utf-8'),
  2111.             '<CreateAffinityGroup xmlns="http://schemas.microsoft.com/windowsazure"><Name>' $name '</Name><Label>' base64_encode($label'</Label><Description>' $description '</Description><Location>' $location '</Location></CreateAffinityGroup>');    
  2112.             
  2113.         if (!$response->isSuccessful()) {
  2114.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  2115.         }
  2116.     }
  2117.     
  2118.     /**
  2119.      * The Update Affinity Group operation updates the label and/or the description for an affinity group for the specified subscription.
  2120.      * 
  2121.      * @param string $name The name for the affinity group that should be updated.
  2122.      * @param string $label A label for the affinity group. The label may be up to 100 characters in length.
  2123.      * @param string $description A description for the affinity group. The description may be up to 1024 characters in length.
  2124.      */
  2125.     public function updateAffinityGroup($name$label$description '')
  2126.     {
  2127.         if ($name == '' || is_null($name)) {
  2128.             throw new Microsoft_WindowsAzure_Management_Exception('Affinity group name should be specified.');
  2129.         }
  2130.         if ($label == '' || is_null($label)) {
  2131.             throw new Microsoft_WindowsAzure_Management_Exception('Label should be specified.');
  2132.         }
  2133.         if (strlen($label100{
  2134.             throw new Microsoft_WindowsAzure_Management_Exception('Label is too long. The maximum length is 100 characters.');
  2135.         }
  2136.         if (strlen($description1024{
  2137.             throw new Microsoft_WindowsAzure_Management_Exception('Description is too long. The maximum length is 1024 characters.');
  2138.         }
  2139.         
  2140.         $response $this->_performRequest(self::OP_AFFINITYGROUPS '/' $namearray(),
  2141.             Microsoft_Http_Client::PUT,
  2142.             array('Content-Type' => 'application/xml; charset=utf-8'),
  2143.             '<UpdateAffinityGroup xmlns="http://schemas.microsoft.com/windowsazure"><Label>' base64_encode($label'</Label><Description>' $description '</Description></UpdateAffinityGroup>');    
  2144.             
  2145.         if (!$response->isSuccessful()) {
  2146.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  2147.         }
  2148.     }
  2149.     
  2150.     /**
  2151.      * The Delete Affinity Group operation deletes an affinity group in the specified subscription.
  2152.      * 
  2153.      * @param string $name The name for the affinity group that should be deleted.
  2154.      */
  2155.     public function deleteAffinityGroup($name)
  2156.     {
  2157.         if ($name == '' || is_null($name)) {
  2158.             throw new Microsoft_WindowsAzure_Management_Exception('Affinity group name should be specified.');
  2159.         }
  2160.         
  2161.         $response $this->_performRequest(self::OP_AFFINITYGROUPS '/' $namearray(),
  2162.             Microsoft_Http_Client::DELETE);
  2163.             
  2164.         if (!$response->isSuccessful()) {
  2165.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  2166.         }
  2167.     }
  2168.     
  2169.     /**
  2170.      * The Get Affinity Group Properties operation returns the
  2171.      * system properties associated with the specified affinity group.
  2172.      * 
  2173.      * @param string $affinityGroupName The affinity group name.
  2174.      * @return Microsoft_WindowsAzure_Management_AffinityGroupInstance 
  2175.      * @throws Microsoft_WindowsAzure_Management_Exception
  2176.      */
  2177.     public function getAffinityGroupProperties($affinityGroupName)
  2178.     {
  2179.         if ($affinityGroupName == '' || is_null($affinityGroupName)) {
  2180.             throw new Microsoft_WindowsAzure_Management_Exception('Affinity group name should be specified.');
  2181.         }
  2182.         
  2183.         $response $this->_performRequest(self::OP_AFFINITYGROUPS '/' $affinityGroupName);
  2184.  
  2185.         if ($response->isSuccessful()) {
  2186.             $result $this->_parseResponse($response);
  2187.             
  2188.             $affinityGroup new Microsoft_WindowsAzure_Management_AffinityGroupInstance(
  2189.                 $affinityGroupName,
  2190.                 (string)$result->Label,
  2191.                 (string)$result->Description,
  2192.                 (string)$result->Location
  2193.             );
  2194.  
  2195.             // Hosted services
  2196.             if (count($result->HostedServices->HostedService1{
  2197.                 $xmlService $result->HostedServices->HostedService;
  2198.             else {
  2199.                 $xmlService array($result->HostedServices->HostedService);
  2200.             }
  2201.                     
  2202.             $services array();
  2203.             if (!is_null($xmlService)) {                
  2204.                 for ($i 0$i count($xmlService)$i++{
  2205.                     $services[array(
  2206.                         'url' => (string)$xmlService[$i]->Url,
  2207.                         'name' => (string)$xmlService[$i]->ServiceName
  2208.                     );
  2209.                 }
  2210.             }
  2211.             $affinityGroup->HostedServices $services;
  2212.             
  2213.             // Storage services
  2214.             if (count($result->StorageServices->StorageService1{
  2215.                 $xmlService $result->StorageServices->StorageService;
  2216.             else {
  2217.                 $xmlService array($result->StorageServices->StorageService);
  2218.             }
  2219.                     
  2220.             $services array();
  2221.             if (!is_null($xmlService)) {                
  2222.                 for ($i 0$i count($xmlService)$i++{
  2223.                     $services[array(
  2224.                         'url' => (string)$xmlService[$i]->Url,
  2225.                         'name' => (string)$xmlService[$i]->ServiceName
  2226.                     );
  2227.                 }
  2228.             }
  2229.             $affinityGroup->StorageServices $services;    
  2230.             
  2231.             return $affinityGroup;
  2232.         else {
  2233.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  2234.         }
  2235.     }
  2236.     
  2237.     /**
  2238.      * The List Locations operation lists all of the data center locations
  2239.      * that are valid for your subscription.
  2240.      * 
  2241.      * @return array Array of Microsoft_WindowsAzure_Management_LocationInstance
  2242.      * @throws Microsoft_WindowsAzure_Management_Exception
  2243.      */
  2244.     public function listLocations()
  2245.     {
  2246.         $response $this->_performRequest(self::OP_LOCATIONS);
  2247.  
  2248.         if ($response->isSuccessful()) {
  2249.             $result $this->_parseResponse($response);
  2250.             
  2251.             if (!$result->Location{
  2252.                 return array();
  2253.             }
  2254.             if (count($result->Location1{
  2255.                 $xmlServices $result->Location;
  2256.             else {
  2257.                 $xmlServices array($result->Location);
  2258.             }
  2259.             
  2260.             $services array();
  2261.             if (!is_null($xmlServices)) {                
  2262.                 for ($i 0$i count($xmlServices)$i++{
  2263.                     $services[new Microsoft_WindowsAzure_Management_LocationInstance(
  2264.                         (string)$xmlServices[$i]->Name
  2265.                     );
  2266.                 }
  2267.             }
  2268.             return $services;
  2269.         else {
  2270.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  2271.         }
  2272.     }
  2273.     
  2274.     /**
  2275.      * The List Operating Systems operation lists the versions of the guest operating system
  2276.      * that are currently available in Windows Azure. The 2010-10-28 version of List Operating
  2277.      * Systems also indicates what family an operating system version belongs to.
  2278.      * Currently Windows Azure supports two operating system families: the Windows Azure guest
  2279.      * operating system that is substantially compatible with Windows Server 2008 SP2,
  2280.      * and the Windows Azure guest operating system that is substantially compatible with
  2281.      * Windows Server 2008 R2.
  2282.      * 
  2283.      * @return array Array of Microsoft_WindowsAzure_Management_OperatingSystemInstance
  2284.      * @throws Microsoft_WindowsAzure_Management_Exception
  2285.      */
  2286.     public function listOperatingSystems()
  2287.     {
  2288.         $response $this->_performRequest(self::OP_OPERATINGSYSTEMS);
  2289.  
  2290.         if ($response->isSuccessful()) {
  2291.             $result $this->_parseResponse($response);
  2292.             
  2293.             if (!$result->OperatingSystem{
  2294.                 return array();
  2295.             }
  2296.             if (count($result->OperatingSystem1{
  2297.                 $xmlServices $result->OperatingSystem;
  2298.             else {
  2299.                 $xmlServices array($result->OperatingSystem);
  2300.             }
  2301.             
  2302.             $services array();
  2303.             if (!is_null($xmlServices)) {                
  2304.                 for ($i 0$i count($xmlServices)$i++{
  2305.                     $services[new Microsoft_WindowsAzure_Management_OperatingSystemInstance(
  2306.                         (string)$xmlServices[$i]->Version,
  2307.                         (string)$xmlServices[$i]->Label,
  2308.                         ((string)$xmlServices[$i]->IsDefault == 'true'),
  2309.                         ((string)$xmlServices[$i]->IsActive == 'true'),
  2310.                         (string)$xmlServices[$i]->Family,
  2311.                         (string)$xmlServices[$i]->FamilyLabel
  2312.                     );
  2313.                 }
  2314.             }
  2315.             return $services;
  2316.         else {
  2317.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  2318.         }
  2319.     }
  2320.     
  2321.     /**
  2322.      * The List OS Families operation lists the guest operating system families
  2323.      * available in Windows Azure, and also lists the operating system versions
  2324.      * available for each family. Currently Windows Azure supports two operating
  2325.      * system families: the Windows Azure guest operating system that is
  2326.      * substantially compatible with Windows Server 2008 SP2, and the Windows
  2327.      * Azure guest operating system that is substantially compatible with
  2328.      * Windows Server 2008 R2.
  2329.      * 
  2330.      * @return array Array of Microsoft_WindowsAzure_Management_OperatingSystemFamilyInstance
  2331.      * @throws Microsoft_WindowsAzure_Management_Exception
  2332.      */
  2333.     public function listOperatingSystemFamilies()
  2334.     {
  2335.         $response $this->_performRequest(self::OP_OPERATINGSYSTEMFAMILIES);
  2336.  
  2337.         if ($response->isSuccessful()) {
  2338.             $result $this->_parseResponse($response);
  2339.             
  2340.             if (!$result->OperatingSystemFamily{
  2341.                 return array();
  2342.             }
  2343.             if (count($result->OperatingSystemFamily1{
  2344.                 $xmlServices $result->OperatingSystemFamily;
  2345.             else {
  2346.                 $xmlServices array($result->OperatingSystemFamily);
  2347.             }
  2348.             
  2349.             $services array();
  2350.             if (!is_null($xmlServices)) {                
  2351.                 for ($i 0$i count($xmlServices)$i++{
  2352.                     $services[new Microsoft_WindowsAzure_Management_OperatingSystemFamilyInstance(
  2353.                         (string)$xmlServices[$i]->Name,
  2354.                         (string)$xmlServices[$i]->Label
  2355.                     );
  2356.                                 
  2357.                     if (count($xmlServices[$i]->OperatingSystems->OperatingSystem1{
  2358.                         $xmlOperatingSystems $xmlServices[$i]->OperatingSystems->OperatingSystem;
  2359.                     else {
  2360.                         $xmlOperatingSystems array($xmlServices[$i]->OperatingSystems->OperatingSystem);
  2361.                     }
  2362.                     
  2363.                     $operatingSystems array();
  2364.                     if (!is_null($xmlOperatingSystems)) {                
  2365.                         for ($i 0$i count($xmlOperatingSystems)$i++{
  2366.                             $operatingSystems[new Microsoft_WindowsAzure_Management_OperatingSystemInstance(
  2367.                                 (string)$xmlOperatingSystems[$i]->Version,
  2368.                                 (string)$xmlOperatingSystems[$i]->Label,
  2369.                                 ((string)$xmlOperatingSystems[$i]->IsDefault == 'true'),
  2370.                                 ((string)$xmlOperatingSystems[$i]->IsActive == 'true'),
  2371.                                 (string)$xmlServices[$i]->Name,
  2372.                                 (string)$xmlServices[$i]->Label
  2373.                             );
  2374.                         }
  2375.                     }
  2376.                     $servicescount($services]->OperatingSystems $operatingSystems;
  2377.                 }
  2378.             }
  2379.             return $services;
  2380.         else {
  2381.             throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response'Resource could not be accessed.'));
  2382.         }
  2383.     }
  2384.     
  2385.     /**
  2386.      * Clean configuration
  2387.      * 
  2388.      * @param string $configuration Configuration to clean.
  2389.      * @return string 
  2390.      */
  2391.     public function _cleanConfiguration($configuration{
  2392.         $configuration str_replace('?<?''<?'$configuration);
  2393.         $configuration str_replace("\r"""$configuration);
  2394.         $configuration str_replace("\n"""$configuration);
  2395.         
  2396.         return $configuration;
  2397.     }
  2398. }

Documentation generated on Sat, 03 Dec 2011 13:59:11 +0100 by phpDocumentor 1.4.3