/profiles/acquia/modules/azure/phpazure/library/Microsoft/WindowsAzure/Management/Client.php
PHP | 2203 lines | 1286 code | 238 blank | 679 comment | 479 complexity | 25c2d0d6848ef8025098d18f16d3780c MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0
Large files files are truncated, but you can click here to view the full file
- <?php
- /**
- * Copyright (c) 2009 - 2010, RealDolmen
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * * Neither the name of RealDolmen nor the
- * names of its contributors may be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY RealDolmen ''AS IS'' AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL RealDolmen BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * @category Microsoft
- * @package Microsoft_WindowsAzure
- * @subpackage Management
- * @copyright Copyright (c) 2009 - 2010, RealDolmen (http://www.realdolmen.com)
- * @license http://phpazure.codeplex.com/license
- * @version $Id: Storage.php 51671 2010-09-30 08:33:45Z unknown $
- */
- /**
- * @see Microsoft_AutoLoader
- */
- require_once dirname(__FILE__) . '/../../AutoLoader.php';
- /**
- * @category Microsoft
- * @package Microsoft_WindowsAzure
- * @subpackage Management
- * @copyright Copyright (c) 2009 - 2010, RealDolmen (http://www.realdolmen.com)
- * @license http://phpazure.codeplex.com/license
- */
- class Microsoft_WindowsAzure_Management_Client
- {
- /**
- * Management service URL
- */
- const URL_MANAGEMENT = "https://management.core.windows.net";
-
- /**
- * Operations
- */
- const OP_OPERATIONS = "operations";
- const OP_STORAGE_ACCOUNTS = "services/storageservices";
- const OP_HOSTED_SERVICES = "services/hostedservices";
- const OP_AFFINITYGROUPS = "affinitygroups";
- const OP_LOCATIONS = "locations";
- const OP_OPERATINGSYSTEMS = "operatingsystems";
- const OP_OPERATINGSYSTEMFAMILIES = "operatingsystemfamilies";
- /**
- * Current API version
- *
- * @var string
- */
- protected $_apiVersion = '2011-02-25';
-
- /**
- * Subscription ID
- *
- * @var string
- */
- protected $_subscriptionId = '';
-
- /**
- * Management certificate path (.PEM)
- *
- * @var string
- */
- protected $_certificatePath = '';
-
- /**
- * Management certificate passphrase
- *
- * @var string
- */
- protected $_certificatePassphrase = '';
-
- /**
- * Microsoft_Http_Client channel used for communication with REST services
- *
- * @var Microsoft_Http_Client
- */
- protected $_httpClientChannel = null;
- /**
- * Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract instance
- *
- * @var Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract
- */
- protected $_retryPolicy = null;
-
- /**
- * Returns the last request ID
- *
- * @var string
- */
- protected $_lastRequestId = null;
-
- /**
- * Creates a new Microsoft_WindowsAzure_Management instance
- *
- * @param string $subscriptionId Subscription ID
- * @param string $certificatePath Management certificate path (.PEM)
- * @param string $certificatePassphrase Management certificate passphrase
- * @param Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy Retry policy to use when making requests
- */
- public function __construct(
- $subscriptionId,
- $certificatePath,
- $certificatePassphrase,
- Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy = null
- ) {
- $this->_subscriptionId = $subscriptionId;
- $this->_certificatePath = $certificatePath;
- $this->_certificatePassphrase = $certificatePassphrase;
-
- $this->_retryPolicy = $retryPolicy;
- if (is_null($this->_retryPolicy)) {
- $this->_retryPolicy = Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract::noRetry();
- }
-
- // Setup default Microsoft_Http_Client channel
- $options = array(
- 'adapter' => 'Microsoft_Http_Client_Adapter_Socket',
- 'ssltransport' => 'ssl',
- 'sslcert' => $this->_certificatePath,
- 'sslpassphrase' => $this->_certificatePassphrase,
- 'sslusecontext' => true,
- );
- if (function_exists('curl_init')) {
- // Set cURL options if cURL is used afterwards
- $options['curloptions'] = array(
- CURLOPT_FOLLOWLOCATION => true,
- CURLOPT_TIMEOUT => 120,
- );
- }
- $this->_httpClientChannel = new Microsoft_Http_Client(null, $options);
- }
-
- /**
- * Set the HTTP client channel to use
- *
- * @param Microsoft_Http_Client_Adapter_Interface|string $adapterInstance Adapter instance or adapter class name.
- */
- public function setHttpClientChannel($adapterInstance = 'Microsoft_Http_Client_Adapter_Socket')
- {
- $this->_httpClientChannel->setAdapter($adapterInstance);
- }
-
- /**
- * Retrieve HTTP client channel
- *
- * @return Microsoft_Http_Client_Adapter_Interface
- */
- public function getHttpClientChannel()
- {
- return $this->_httpClientChannel;
- }
-
- /**
- * Returns the Windows Azure subscription ID
- *
- * @return string
- */
- public function getSubscriptionId()
- {
- return $this->_subscriptionId;
- }
-
- /**
- * Returns the last request ID.
- *
- * @return string
- */
- public function getLastRequestId()
- {
- return $this->_lastRequestId;
- }
-
- /**
- * Get base URL for creating requests
- *
- * @return string
- */
- public function getBaseUrl()
- {
- return self::URL_MANAGEMENT . '/' . $this->_subscriptionId;
- }
-
- /**
- * Perform request using Microsoft_Http_Client channel
- *
- * @param string $path Path
- * @param string $queryString Query string
- * @param string $httpVerb HTTP verb the request will use
- * @param array $headers x-ms headers to add
- * @param mixed $rawData Optional RAW HTTP data to be sent over the wire
- * @return Microsoft_Http_Response
- */
- protected function _performRequest(
- $path = '/',
- $queryString = '',
- $httpVerb = Microsoft_Http_Client::GET,
- $headers = array(),
- $rawData = null
- ) {
- // Clean path
- if (strpos($path, '/') !== 0) {
- $path = '/' . $path;
- }
-
- // Clean headers
- if (is_null($headers)) {
- $headers = array();
- }
-
- // Ensure cUrl will also work correctly:
- // - disable Content-Type if required
- // - disable Expect: 100 Continue
- if (!isset($headers["Content-Type"])) {
- $headers["Content-Type"] = '';
- }
- //$headers["Expect"] = '';
- // Add version header
- $headers['x-ms-version'] = $this->_apiVersion;
-
- // URL encoding
- $path = self::urlencode($path);
- $queryString = self::urlencode($queryString);
- // Generate URL and sign request
- $requestUrl = $this->getBaseUrl() . $path . $queryString;
- $requestHeaders = $headers;
- // Prepare request
- $this->_httpClientChannel->resetParameters(true);
- $this->_httpClientChannel->setUri($requestUrl);
- $this->_httpClientChannel->setHeaders($requestHeaders);
- $this->_httpClientChannel->setRawData($rawData);
- // Execute request
- $response = $this->_retryPolicy->execute(
- array($this->_httpClientChannel, 'request'),
- array($httpVerb)
- );
-
- // Store request id
- $this->_lastRequestId = $response->getHeader('x-ms-request-id');
-
- return $response;
- }
-
- /**
- * Parse result from Microsoft_Http_Response
- *
- * @param Microsoft_Http_Response $response Response from HTTP call
- * @return object
- * @throws Microsoft_WindowsAzure_Exception
- */
- protected function _parseResponse(Microsoft_Http_Response $response = null)
- {
- if (is_null($response)) {
- throw new Microsoft_WindowsAzure_Exception('Response should not be null.');
- }
-
- $xml = @simplexml_load_string($response->getBody());
-
- if ($xml !== false) {
- // Fetch all namespaces
- $namespaces = array_merge($xml->getNamespaces(true), $xml->getDocNamespaces(true));
-
- // Register all namespace prefixes
- foreach ($namespaces as $prefix => $ns) {
- if ($prefix != '') {
- $xml->registerXPathNamespace($prefix, $ns);
- }
- }
- }
-
- return $xml;
- }
-
- /**
- * URL encode function
- *
- * @param string $value Value to encode
- * @return string Encoded value
- */
- public static function urlencode($value)
- {
- return str_replace(' ', '%20', $value);
- }
-
- /**
- * Builds a query string from an array of elements
- *
- * @param array Array of elements
- * @return string Assembled query string
- */
- public static function createQueryStringFromArray($queryString)
- {
- return count($queryString) > 0 ? '?' . implode('&', $queryString) : '';
- }
-
- /**
- * Get error message from Microsoft_Http_Response
- *
- * @param Microsoft_Http_Response $response Repsonse
- * @param string $alternativeError Alternative error message
- * @return string
- */
- protected function _getErrorMessage(Microsoft_Http_Response $response, $alternativeError = 'Unknown error.')
- {
- $response = $this->_parseResponse($response);
- if ($response && $response->Message) {
- return (string)$response->Message;
- } else {
- return $alternativeError;
- }
- }
-
- /**
- * The Get Operation Status operation returns the status of the specified operation.
- * After calling an asynchronous operation, you can call Get Operation Status to
- * determine whether the operation has succeed, failed, or is still in progress.
- *
- * @param string $requestId The request ID. If omitted, the last request ID will be used.
- * @return Microsoft_WindowsAzure_Management_OperationStatusInstance
- * @throws Microsoft_WindowsAzure_Management_Exception
- */
- public function getOperationStatus($requestId = '')
- {
- if ($requestId == '') {
- $requestId = $this->getLastRequestId();
- }
-
- $response = $this->_performRequest(self::OP_OPERATIONS . '/' . $requestId);
- if ($response->isSuccessful()) {
- $result = $this->_parseResponse($response);
- if (!is_null($result)) {
- return new Microsoft_WindowsAzure_Management_OperationStatusInstance(
- (string)$result->ID,
- (string)$result->Status,
- ($result->Error ? (string)$result->Error->Code : ''),
- ($result->Error ? (string)$result->Error->Message : '')
- );
- }
- return null;
- } else {
- throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
- }
- }
-
-
- /**
- * The List Subscription Operations operation returns a list of create, update,
- * and delete operations that were performed on a subscription during the specified timeframe.
- * Documentation on the parameters can be found at http://msdn.microsoft.com/en-us/library/gg715318.aspx.
- *
- * @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.
- * @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.
- * @param string $objectIdFilter Returns subscription operations only for the specified object type and object ID.
- * @param string $operationResultFilter Returns subscription operations only for the specified result status, either Succeeded, Failed, or InProgress.
- * @param string $continuationToken Internal usage.
- * @return array Array of Microsoft_WindowsAzure_Management_SubscriptionOperationInstance
- * @throws Microsoft_WindowsAzure_Management_Exception
- */
- public function listSubscriptionOperations($startTime, $endTime, $objectIdFilter = null, $operationResultFilter = null, $continuationToken = null)
- {
- if ($startTime == '' || is_null($startTime)) {
- throw new Microsoft_WindowsAzure_Management_Exception('Start time should be specified.');
- }
- if ($endTime == '' || is_null($endTime)) {
- throw new Microsoft_WindowsAzure_Management_Exception('End time should be specified.');
- }
- if ($operationResultFilter != '' && !is_null($operationResultFilter)) {
- $operationResultFilter = strtolower($operationResultFilter);
- if ($operationResultFilter != 'succeeded' && $operationResultFilter != 'failed' && $operationResultFilter != 'inprogress') {
- throw new Microsoft_WindowsAzure_Management_Exception('OperationResultFilter should be succeeded|failed|inprogress.');
- }
- }
-
- $parameters = array();
- $parameters[] = 'StartTime=' . $startTime;
- $parameters[] = 'EndTime=' . $endTime;
- if ($objectIdFilter != '' && !is_null($objectIdFilter)) {
- $parameters[] = 'ObjectIdFilter=' . $objectIdFilter;
- }
- if ($operationResultFilter != '' && !is_null($operationResultFilter)) {
- $parameters[] = 'OperationResultFilter=' . ucfirst($operationResultFilter);
- }
- if ($continuationToken != '' && !is_null($continuationToken)) {
- $parameters[] = 'ContinuationToken=' . $continuationToken;
- }
-
- $response = $this->_performRequest(self::OP_OPERATIONS, '?' . implode('&', $parameters));
- if ($response->isSuccessful()) {
- $result = $this->_parseResponse($response);
- $namespaces = $result->getDocNamespaces();
- $result->registerXPathNamespace('__empty_ns', $namespaces['']);
-
- $xmlOperations = $result->xpath('//__empty_ns:SubscriptionOperation');
-
- // Create return value
- $returnValue = array();
- foreach ($xmlOperations as $xmlOperation) {
- // Create operation instance
- $operation = new Microsoft_WindowsAzure_Management_SubscriptionOperationInstance(
- $xmlOperation->OperationId,
- $xmlOperation->OperationObjectId,
- $xmlOperation->OperationName,
- array(),
- (array)$xmlOperation->OperationCaller,
- (array)$xmlOperation->OperationStatus
- );
-
- // Parse parameters
- $xmlOperation->registerXPathNamespace('__empty_ns', $namespaces['']);
- $xmlParameters = $xmlOperation->xpath('.//__empty_ns:OperationParameter');
- foreach ($xmlParameters as $xmlParameter) {
- $xmlParameterDetails = $xmlParameter->children('http://schemas.datacontract.org/2004/07/Microsoft.Samples.WindowsAzure.ServiceManagement');
- $operation->addOperationParameter((string)$xmlParameterDetails->Name, (string)$xmlParameterDetails->Value);
- }
-
- // Add to result
- $returnValue[] = $operation;
- }
-
- // More data?
- if (!is_null($result->ContinuationToken) && $result->ContinuationToken != '') {
- $returnValue = array_merge($returnValue, $this->listSubscriptionOperations($startTime, $endTime, $objectIdFilter, $operationResultFilter, (string)$result->ContinuationToken));
- }
-
- // Return
- return $returnValue;
- } else {
- throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
- }
- }
-
- /**
- * Wait for an operation to complete
- *
- * @param string $requestId The request ID. If omitted, the last request ID will be used.
- * @param int $sleepInterval Sleep interval in milliseconds.
- * @return Microsoft_WindowsAzure_Management_OperationStatusInstance
- * @throws Microsoft_WindowsAzure_Management_Exception
- */
- public function waitForOperation($requestId = '', $sleepInterval = 250)
- {
- if ($requestId == '') {
- $requestId = $this->getLastRequestId();
- }
- if ($requestId == '' || is_null($requestId)) {
- return null;
- }
- $status = $this->getOperationStatus($requestId);
- while ($status->Status == 'InProgress') {
- $status = $this->getOperationStatus($requestId);
- usleep($sleepInterval);
- }
-
- return $status;
- }
-
- /**
- * Creates a new Microsoft_WindowsAzure_Storage_Blob instance for the current account
- *
- * @param string $serviceName the service name to create a storage client for.
- * @param Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy Retry policy to use when making requests
- * @return Microsoft_WindowsAzure_Storage_Blob
- */
- public function createBlobClientForService($serviceName, Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy = null)
- {
- if ($serviceName == '' || is_null($serviceName)) {
- throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
- }
-
- $storageKeys = $this->getStorageAccountKeys($serviceName);
-
- return new Microsoft_WindowsAzure_Storage_Blob(
- Microsoft_WindowsAzure_Storage::URL_CLOUD_BLOB,
- $serviceName,
- $storageKeys[0],
- false,
- $retryPolicy
- );
- }
-
- /**
- * Creates a new Microsoft_WindowsAzure_Storage_Table instance for the current account
- *
- * @param string $serviceName the service name to create a storage client for.
- * @param Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy Retry policy to use when making requests
- * @return Microsoft_WindowsAzure_Storage_Table
- */
- public function createTableClientForService($serviceName, Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy = null)
- {
- if ($serviceName == '' || is_null($serviceName)) {
- throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
- }
-
- $storageKeys = $this->getStorageAccountKeys($serviceName);
-
- return new Microsoft_WindowsAzure_Storage_Table(
- Microsoft_WindowsAzure_Storage::URL_CLOUD_TABLE,
- $serviceName,
- $storageKeys[0],
- false,
- $retryPolicy
- );
- }
-
- /**
- * Creates a new Microsoft_WindowsAzure_Storage_Queue instance for the current account
- *
- * @param string $serviceName the service name to create a storage client for.
- * @param Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy Retry policy to use when making requests
- * @return Microsoft_WindowsAzure_Storage_Queue
- */
- public function createQueueClientForService($serviceName, Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy = null)
- {
- if ($serviceName == '' || is_null($serviceName)) {
- throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
- }
-
- $storageKeys = $this->getStorageAccountKeys($serviceName);
-
- return new Microsoft_WindowsAzure_Storage_Queue(
- Microsoft_WindowsAzure_Storage::URL_CLOUD_QUEUE,
- $serviceName,
- $storageKeys[0],
- false,
- $retryPolicy
- );
- }
-
- /**
- * The List Storage Accounts operation lists the storage accounts available under
- * the current subscription.
- *
- * @return array An array of Microsoft_WindowsAzure_Management_StorageServiceInstance
- */
- public function listStorageAccounts()
- {
- $response = $this->_performRequest(self::OP_STORAGE_ACCOUNTS);
- if ($response->isSuccessful()) {
- $result = $this->_parseResponse($response);
- if (count($result->StorageService) > 1) {
- $xmlServices = $result->StorageService;
- } else {
- $xmlServices = array($result->StorageService);
- }
-
- $services = array();
- if (!is_null($xmlServices)) {
- for ($i = 0; $i < count($xmlServices); $i++) {
- $services[] = new Microsoft_WindowsAzure_Management_StorageServiceInstance(
- (string)$xmlServices[$i]->Url,
- (string)$xmlServices[$i]->ServiceName
- );
- }
- }
- return $services;
- } else {
- throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
- }
- }
-
- /**
- * The Get Storage Account Properties operation returns the system properties for the
- * specified storage account. These properties include: the address, description, and
- * label of the storage account; and the name of the affinity group to which the service
- * belongs, or its geo-location if it is not part of an affinity group.
- *
- * @param string $serviceName The name of your service.
- * @return Microsoft_WindowsAzure_Management_StorageServiceInstance
- * @throws Microsoft_WindowsAzure_Management_Exception
- */
- public function getStorageAccountProperties($serviceName)
- {
- if ($serviceName == '' || is_null($serviceName)) {
- throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
- }
-
- $response = $this->_performRequest(self::OP_STORAGE_ACCOUNTS . '/' . $serviceName);
- if ($response->isSuccessful()) {
- $xmlService = $this->_parseResponse($response);
- if (!is_null($xmlService)) {
- return new Microsoft_WindowsAzure_Management_StorageServiceInstance(
- (string)$xmlService->Url,
- (string)$xmlService->ServiceName,
- (string)$xmlService->StorageServiceProperties->Description,
- (string)$xmlService->StorageServiceProperties->AffinityGroup,
- (string)$xmlService->StorageServiceProperties->Location,
- (string)$xmlService->StorageServiceProperties->Label
- );
- }
- return null;
- } else {
- throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
- }
- }
-
- /**
- * The Get Storage Keys operation returns the primary
- * and secondary access keys for the specified storage account.
- *
- * @param string $serviceName The name of your service.
- * @return array An array of strings
- * @throws Microsoft_WindowsAzure_Management_Exception
- */
- public function getStorageAccountKeys($serviceName)
- {
- if ($serviceName == '' || is_null($serviceName)) {
- throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
- }
-
- $response = $this->_performRequest(self::OP_STORAGE_ACCOUNTS . '/' . $serviceName . '/keys');
- if ($response->isSuccessful()) {
- $xmlService = $this->_parseResponse($response);
- if (!is_null($xmlService)) {
- return array(
- (string)$xmlService->StorageServiceKeys->Primary,
- (string)$xmlService->StorageServiceKeys->Secondary
- );
- }
- return array();
- } else {
- throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
- }
- }
-
- /**
- * The Regenerate Keys operation regenerates the primary
- * or secondary access key for the specified storage account.
- *
- * @param string $serviceName The name of your service.
- * @param string $key The key to regenerate (primary or secondary)
- * @return array An array of strings
- * @throws Microsoft_WindowsAzure_Management_Exception
- */
- public function regenerateStorageAccountKey($serviceName, $key = 'primary')
- {
- if ($serviceName == '' || is_null($serviceName)) {
- throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
- }
- $key = strtolower($key);
- if ($key != 'primary' && $key != 'secondary') {
- throw new Microsoft_WindowsAzure_Management_Exception('Key identifier should be primary|secondary.');
- }
-
- $response = $this->_performRequest(
- self::OP_STORAGE_ACCOUNTS . '/' . $serviceName . '/keys', '?action=regenerate',
- Microsoft_Http_Client::POST,
- array('Content-Type' => 'application/xml'),
- '<?xml version="1.0" encoding="utf-8"?>
- <RegenerateKeys xmlns="http://schemas.microsoft.com/windowsazure">
- <KeyType>' . ucfirst($key) . '</KeyType>
- </RegenerateKeys>');
- if ($response->isSuccessful()) {
- $xmlService = $this->_parseResponse($response);
- if (!is_null($xmlService)) {
- return array(
- (string)$xmlService->StorageServiceKeys->Primary,
- (string)$xmlService->StorageServiceKeys->Secondary
- );
- }
- return array();
- } else {
- throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
- }
- }
-
- /**
- * The List Hosted Services operation lists the hosted services available
- * under the current subscription.
- *
- * @return array An array of Microsoft_WindowsAzure_Management_HostedServiceInstance
- * @throws Microsoft_WindowsAzure_Management_Exception
- */
- public function listHostedServices()
- {
- $response = $this->_performRequest(self::OP_HOSTED_SERVICES);
- if ($response->isSuccessful()) {
- $result = $this->_parseResponse($response);
- if (count($result->HostedService) > 1) {
- $xmlServices = $result->HostedService;
- } else {
- $xmlServices = array($result->HostedService);
- }
-
- $services = array();
- if (!is_null($xmlServices)) {
- for ($i = 0; $i < count($xmlServices); $i++) {
- $services[] = new Microsoft_WindowsAzure_Management_HostedServiceInstance(
- (string)$xmlServices[$i]->Url,
- (string)$xmlServices[$i]->ServiceName
- );
- }
- }
- return $services;
- } else {
- throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
- }
- }
-
- /**
- * The Create Hosted Service operation creates a new hosted service in Windows Azure.
- *
- * @param string $serviceName A name for the hosted service that is unique to the subscription.
- * @param string $label A label for the hosted service. The label may be up to 100 characters in length.
- * @param string $description A description for the hosted service. The description may be up to 1024 characters in length.
- * @param string $location Required if AffinityGroup is not specified. The location where the hosted service will be created.
- * @param string $affinityGroup Required if Location is not specified. The name of an existing affinity group associated with this subscription.
- */
- public function createHostedService($serviceName, $label, $description = '', $location = null, $affinityGroup = null)
- {
- if ($serviceName == '' || is_null($serviceName)) {
- throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
- }
- if ($label == '' || is_null($label)) {
- throw new Microsoft_WindowsAzure_Management_Exception('Label should be specified.');
- }
- if (strlen($label) > 100) {
- throw new Microsoft_WindowsAzure_Management_Exception('Label is too long. The maximum length is 100 characters.');
- }
- if (strlen($description) > 1024) {
- throw new Microsoft_WindowsAzure_Management_Exception('Description is too long. The maximum length is 1024 characters.');
- }
- if ( (is_null($location) && is_null($affinityGroup)) || (!is_null($location) && !is_null($affinityGroup)) ) {
- throw new Microsoft_WindowsAzure_Management_Exception('Please specify a location -or- an affinity group for the service.');
- }
-
- $locationOrAffinityGroup = is_null($location)
- ? '<AffinityGroup>' . $affinityGroup . '</AffinityGroup>'
- : '<Location>' . $location . '</Location>';
-
- $response = $this->_performRequest(self::OP_HOSTED_SERVICES, '',
- Microsoft_Http_Client::POST,
- array('Content-Type' => 'application/xml; charset=utf-8'),
- '<CreateHostedService xmlns="http://schemas.microsoft.com/windowsazure"><ServiceName>' . $serviceName . '</ServiceName><Label>' . base64_encode($label) . '</Label><Description>' . $description . '</Description>' . $locationOrAffinityGroup . '</CreateHostedService>');
-
- if (!$response->isSuccessful()) {
- throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
- }
- }
-
- /**
- * The Update Hosted Service operation updates the label and/or the description for a hosted service in Windows Azure.
- *
- * @param string $serviceName A name for the hosted service that is unique to the subscription.
- * @param string $label A label for the hosted service. The label may be up to 100 characters in length.
- * @param string $description A description for the hosted service. The description may be up to 1024 characters in length.
- */
- public function updateHostedService($serviceName, $label, $description = '')
- {
- if ($serviceName == '' || is_null($serviceName)) {
- throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
- }
- if ($label == '' || is_null($label)) {
- throw new Microsoft_WindowsAzure_Management_Exception('Label should be specified.');
- }
- if (strlen($label) > 100) {
- throw new Microsoft_WindowsAzure_Management_Exception('Label is too long. The maximum length is 100 characters.');
- }
-
- $response = $this->_performRequest(self::OP_HOSTED_SERVICES . '/' . $serviceName, '',
- Microsoft_Http_Client::PUT,
- array('Content-Type' => 'application/xml; charset=utf-8'),
- '<UpdateHostedService xmlns="http://schemas.microsoft.com/windowsazure"><Label>' . base64_encode($label) . '</Label><Description>' . $description . '</Description></UpdateHostedService>');
-
- if (!$response->isSuccessful()) {
- throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
- }
- }
-
- /**
- * The Delete Hosted Service operation deletes the specified hosted service in Windows Azure.
- *
- * @param string $serviceName A name for the hosted service that is unique to the subscription.
- */
- public function deleteHostedService($serviceName)
- {
- if ($serviceName == '' || is_null($serviceName)) {
- throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
- }
-
- $response = $this->_performRequest(self::OP_HOSTED_SERVICES . '/' . $serviceName, '', Microsoft_Http_Client::DELETE);
-
- if (!$response->isSuccessful()) {
- throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
- }
- }
-
- /**
- * The Get Hosted Service Properties operation retrieves system properties
- * for the specified hosted service. These properties include the service
- * name and service type; the name of the affinity group to which the service
- * belongs, or its location if it is not part of an affinity group; and
- * optionally, information on the service's deployments.
- *
- * @param string $serviceName The name of your service.
- * @return Microsoft_WindowsAzure_Management_HostedServiceInstance
- * @throws Microsoft_WindowsAzure_Management_Exception
- */
- public function getHostedServiceProperties($serviceName)
- {
- if ($serviceName == '' || is_null($serviceName)) {
- throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
- }
-
- $response = $this->_performRequest(self::OP_HOSTED_SERVICES . '/' . $serviceName, '?embed-detail=true');
- if ($response->isSuccessful()) {
- $xmlService = $this->_parseResponse($response);
- if (!is_null($xmlService)) {
- $returnValue = new Microsoft_WindowsAzure_Management_HostedServiceInstance(
- (string)$xmlService->Url,
- (string)$xmlService->ServiceName,
- (string)$xmlService->HostedServiceProperties->Description,
- (string)$xmlService->HostedServiceProperties->AffinityGroup,
- (string)$xmlService->HostedServiceProperties->Location,
- (string)$xmlService->HostedServiceProperties->Label
- );
-
- // Deployments
- if (count($xmlService->Deployments->Deployment) > 1) {
- $xmlServices = $xmlService->Deployments->Deployment;
- } else {
- $xmlServices = array($xmlService->Deployments->Deployment);
- }
-
- $deployments = array();
- foreach ($xmlServices as $xmlDeployment) {
- $deployments[] = $this->_convertXmlElementToDeploymentInstance($xmlDeployment);
- }
- $returnValue->Deployments = $deployments;
-
- return $returnValue;
- }
- return null;
- } else {
- throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
- }
- }
- /**
- * The Create Deployment operation uploads a new service package
- * and creates a new deployment on staging or production.
- *
- * @param string $serviceName The service name
- * @param string $deploymentSlot The deployment slot (production or staging)
- * @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.
- * @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.
- * @param string $packageUrl The service configuration file for the deployment.
- * @param string $configuration A label for this deployment, up to 100 characters in length.
- * @param boolean $startDeployment Indicates whether to start the deployment immediately after it is created.
- * @param boolean $treatWarningsAsErrors Indicates whether to treat package validation warnings as errors.
- * @throws Microsoft_WindowsAzure_Management_Exception
- */
- public function createDeployment($serviceName, $deploymentSlot, $name, $label, $packageUrl, $configuration, $startDeployment = false, $treatWarningsAsErrors = false)
- {
- if ($serviceName == '' || is_null($serviceName)) {
- throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
- }
- $deploymentSlot = strtolower($deploymentSlot);
- if ($deploymentSlot != 'production' && $deploymentSlot != 'staging') {
- throw new Microsoft_WindowsAzure_Management_Exception('Deployment slot should be production|staging.');
- }
- if ($name == '' || is_null($name)) {
- throw new Microsoft_WindowsAzure_Management_Exception('Name should be specified.');
- }
- if ($label == '' || is_null($label)) {
- throw new Microsoft_WindowsAzure_Management_Exception('Label should be specified.');
- }
- if (strlen($label) > 100) {
- throw new Microsoft_WindowsAzure_Management_Exception('Label is too long. The maximum length is 100 characters.');
- }
- if ($packageUrl == '' || is_null($packageUrl)) {
- throw new Microsoft_WindowsAzure_Management_Exception('Package URL should be specified.');
- }
- if ($configuration == '' || is_null($configuration)) {
- throw new Microsoft_WindowsAzure_Management_Exception('Configuration should be specified.');
- }
-
- if (@file_exists($configuration)) {
- $configuration = utf8_decode(file_get_contents($configuration));
- }
-
- // Clean up the configuration
- $conformingConfiguration = $this->_cleanConfiguration($configuration);
-
- $operationUrl = self::OP_HOSTED_SERVICES . '/' . $serviceName . '/deploymentslots/' . $deploymentSlot;
- $response = $this->_performRequest($operationUrl, '',
- Microsoft_Http_Client::POST,
- array('Content-Type' => 'application/xml; charset=utf-8'),
- '<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>');
-
- if (!$response->isSuccessful()) {
- throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
- }
- }
-
- /**
- * The Get Deployment operation returns configuration information, status,
- * and system properties for the specified deployment.
- *
- * @param string $serviceName The service name
- * @param string $deploymentSlot The deployment slot (production or staging)
- * @return Microsoft_WindowsAzure_Management_DeploymentInstance
- * @throws Microsoft_WindowsAzure_Management_Exception
- */
- public function getDeploymentBySlot($serviceName, $deploymentSlot)
- {
- if ($serviceName == '' || is_null($serviceName)) {
- throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
- }
- $deploymentSlot = strtolower($deploymentSlot);
- if ($deploymentSlot != 'production' && $deploymentSlot != 'staging') {
- throw new Microsoft_WindowsAzure_Management_Exception('Deployment slot should be production|staging.');
- }
-
- $operationUrl = self::OP_HOSTED_SERVICES . '/' . $serviceName . '/deploymentslots/' . $deploymentSlot;
- return $this->_getDeployment($operationUrl);
- }
-
- /**
- * The Get Deployment operation returns configuration information, status,
- * and system properties for the specified deployment.
- *
- * @param string $serviceName The service name
- * @param string $deploymentId The deployment ID as listed on the Windows Azure management portal
- * @return Microsoft_WindowsAzure_Management_DeploymentInstance
- * @throws Microsoft_WindowsAzure_Management_Exception
- */
- public function getDeploymentByDeploymentId($serviceName, $deploymentId)
- {
- if ($serviceName == '' || is_null($serviceName)) {
- throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
- }
- if ($deploymentId == '' || is_null($deploymentId)) {
- throw new Microsoft_WindowsAzure_Management_Exception('Deployment ID should be specified.');
- }
-
- $operationUrl = self::OP_HOSTED_SERVICES . '/' . $serviceName . '/deployments/' . $deploymentId;
- return $this->_getDeployment($operationUrl);
- }
-
- /**
- * The Get Deployment operation returns configuration information, status,
- * and system properties for the specified deployment.
- *
- * @param string $operationUrl The operation url
- * @return Microsoft_WindowsAzure_Management_DeploymentInstance
- * @throws Microsoft_WindowsAzure_Management_Exception
- */
- protected function _getDeployment($operationUrl)
- {
- $response = $this->_performRequest($operationUrl);
- if ($response->isSuccessful()) {
- $xmlService = $this->_parseResponse($response);
-
- return $this->_convertXmlElementToDeploymentInstance($xmlService);
- } else {
- throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
- }
- }
-
- /**
- * The Swap Deployment operation initiates a virtual IP swap between
- * the staging and production deployment environments for a service.
- * If the service is currently running in the staging environment,
- * it will be swapped to the production environment. If it is running
- * in the production environment, it will be swapped to staging.
- *
- * @param string $serviceName The service name.
- * @param string $productionDeploymentName The name of the production deployment.
- * @param string $sourceDeploymentName The name of the source deployment.
- * @throws Microsoft_WindowsAzure_Management_Exception
- */
- public function swapDeployment($serviceName, $productionDeploymentName, $sourceDeploymentName)
- {
- if ($serviceName == '' || is_null($serviceName)) {
- throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
- }
- if ($productionDeploymentName == '' || is_null($productionDeploymentName)) {
- throw new Microsoft_WindowsAzure_Management_Exception('Production Deployment ID should be specified.');
- }
- if ($sourceDeploymentName == '' || is_null($sourceDeploymentName)) {
- throw new Microsoft_WindowsAzure_Management_Exception('Source Deployment ID should be specified.');
- }
-
- $operationUrl = self::OP_HOSTED_SERVICES . '/' . $serviceName;
- $response = $this->_performRequest($operationUrl, '',
- Microsoft_Http_Client::POST,
- array('Content-Type' => 'application/xml; charset=utf-8'),
- '<Swap xmlns="http://schemas.microsoft.com/windowsazure"><Production>' . $productionDeploymentName . '</Production><SourceDeployment>' . $sourceDeploymentName . '</SourceDeployment></Swap>');
-
- if (!$response->isSuccessful()) {
- throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
- }
- }
-
- /**
- * The Delete Deployment operation deletes the specified deployment.
- *
- * @param string $serviceName The service name
- * @param string $deploymentSlot The deployment slot (production or staging)
- * @throws Microsoft_WindowsAzure_Management_Exception
- */
- public function deleteDeploymentBySlot($serviceName, $deploymentSlot)
- {
- if ($serviceName == '' || is_null($serviceName)) {
- throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
- }
- $deploymentSlot = strtolower($deploymentSlot);
- if ($deploymentSlot != 'production' && $deploymentSlot != 'staging') {
- throw new Microsoft_WindowsAzure_Management_Exception('Deployment slot should be production|staging.');
- }
-
- $operationUrl = self::OP_HOSTED_SERVICES . '/' . $serviceName . '/deploymentslots/' . $deploymentSlot;
- return $this->_deleteDeployment($operationUrl);
- }
-
- /**
- * The Delete Deployment operation deletes the specified deployment.
- *
- * @param string $serviceName The service name
- * @param string $deploymentId The deployment ID as listed on the Windows Azure management portal
- * @throws Microsoft_WindowsAzure_Management_Exception
- */
- public function deleteDeploymentByDeploymentId($serviceName, $deploymentId)
- {
- if ($serviceName == '' || is_null($serviceName)) {
- throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
- }
- if ($deploymentId == '' || is_null($deploymentId)) {
- throw new Microsoft_WindowsAzure_Management_Exception('Deployment ID should be specified.');
- }
-
- $operationUrl = self::OP_HOSTED_SERVICES . '/' . $serviceName . '/deployments/' . $deploymentId;
- return $this->_deleteDeployment($operationUrl);
- }
-
- /**
- * The Delete Deployment operation deletes the specified deployment.
- *
- * @param string $operationUrl The operation url
- * @throws Microsoft_WindowsAzure_Management_Exception
- */
- protected function _deleteDeployment($operationUrl)
- {
- $response = $this->_performRequest($operationUrl, '', Microsoft_Http_Client::DELETE);
-
- if (!$response->isSuccessful()) {
- throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
- }
- }
-
- /**
- * The Update Deployment Status operation initiates a change in deployment status.
- *
- * @param string $serviceName The service name
- * @param string $deploymentSlot The deployment slot (production or staging)
- * @param string $status The deployment status (running|suspended)
- * @throws Microsoft_WindowsAzure_Management_Exception
- */
- public function updateDeploymentStatusBySlot($serviceName, $deploymentSlot, $status = 'running')
- {
- if ($serviceName == '' || is_null($serviceName)) {
- throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
- }
- $deploymentSlot = strtolower($deploymentSlot);
- if ($deploymentSlot != 'production' && $deploymentSlot != 'staging') {
- throw new Microsoft_WindowsAzure_Management_Exception('Deployment slot should be production|staging.');
- }
- $status = strtolower($status);
- if ($status != 'running' && $status != 'suspended') {
- throw new Microsoft_WindowsAzure_Management_Exception('Status should be running|suspended.');
- }
-
- $operationUrl = self::OP_HOSTED_SERVICES . '/' . $serviceName . '/deploymentslots/' . $deploymentSlot;
- return $this->_updateDeploymentStatus($operationUrl, $status);
- }
-
- /**
- * The Update Deployment Status operation initiates a change in deployment status.
- *
- * @param string $serviceName The service name
- * @param string $deploymentId The deployment ID as listed on the Windows Azure management portal
- * @param string $status The deployment status (running|suspended)
- * @throws Microsoft_WindowsAzure_Management_Exception
- */
- public function updateDeploymentStatusByDeploymentId($serviceName, $deploymentId, $status = 'running')
- {
- if ($serviceName == '' || is_null($serviceName)) {
- throw new Microsoft_WindowsAzure_Management_Exception('Service name should be specified.');
- }
- if ($deploymentId == '' || is_null($deploymentId)) {
- throw new Microsoft_WindowsAzure_Management_Exception('Deployment ID should be specified.');
- }
- $status = strtolower($status);
- if ($status != 'running' && $status != 'suspended') {
- throw new Microsoft_WindowsAzure_Management_Exception('Status should be running|suspended.');
- }
-
- $operationUrl = self::OP_HOSTED_SERVICES . '/' . $serviceName . '/deployments/' . $deploymentId;
- return $this->_updateDeploymentStatus($operationUrl, $status);
- }
-
- /**
- * The Update Deployment Status operation initiates a change in deployment status.
- *
- * @param string $operationUrl The operation url
- * @param string $status The deployment status (running|suspended)
- * @throws Microsoft_WindowsAzure_Management_Exception
- */
- protected function _updateDeploymentStatus($operationUrl, $status = 'running')
- {
- $response = $this->_performRequest($operationUrl . '/', '?comp=status',
- Microsoft_Http_Client::POST,
- array('Content-Type' => 'application/xml; charset=utf-8'),
- '<UpdateDeploymentStatus xmlns="http://schemas.microsoft.com/windowsazure"><Status>' . ucfirst($status) . '</Status></UpdateDeploymentStatus>');
-
- if (!$response->isSuccessful()) {
- throw new Microsoft_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
- }
- }
-
- /**
- * Converts an XmlElement into a Microsoft_WindowsAzure_Management_DeploymentInstance
- *
- * @param object $xmlService The XML Element
- * @return Microsoft_WindowsAzure_Management_DeploymentInstance
- * @throws Microsoft_WindowsAzure_Management_Exception
- */
- protected function _convertXmlElementToDeploymentInstance($xmlService)
- {
- if (!is_null($xmlService)) {
- $returnValue = new Microsoft_WindowsAzure_Management_DeploymentInstance(
- (string)$xmlService->Name,
- (string)$xmlService->DeploymentSlot,
- (string)$xmlService->PrivateID,
- (string)$xmlService->Label,
- (string)$xmlService->Url,
- (string)$xmlService->Configuration,
- (string)$xmlService->Status,
- (string)$xmlService->UpgradeStatus,
- (string)$xmlService->UpgradeType,
- (string)$xmlService->CurrentUpgradeDomainState,
- (string)$xmlService->CurrentUpgradeDomain,
- (string)$xmlService->UpgradeDomainCount
- );
-
- // Append role instances
- $xmlRoleInstances = $xmlService->RoleInstanceList->RoleInstance;
- if (count($xmlService->RoleInstanceList->RoleInstance) == 1) {
- $xmlRoleInstances = array($xmlService->RoleInstanceList->RoleInstance);
- }
-
- $roleInstances = array();
- if (!is_null($xmlRoleInstances)) {
- for ($i = 0; $i < count($xmlRoleInstances); $i++) {
- $roleInstances[] = array(
- 'rolename' => (string)$xmlRoleInstances[$i]->RoleName,
- 'instancename' => (string)$xmlRoleInstances[$i]->InstanceName,
- 'instancestatus' => (string)$xmlRoleInstances[$i]->InstanceStatus
- );
- }
- }
-
- $returnValue->RoleInstanceList = $roleInstances;
-
- // Append roles
- $xmlRoles = $xmlService->RoleList->Role;
- if (count($xmlService->RoleList->Role) == 1) {
- $xmlRoles = array($xmlService->RoleList->Role);
- }
-
- $roles = array();
- if (!is_null($xmlRoles)) {
- for ($i = 0; $i < count($xmlRoles); $i++) {
- $roles[] = array(
- 'rolename' => (string)$xmlRoles[$i]->RoleName,
- 'osversion' => (!is_null($xmlRoles[$i]->OsVersion) ? (string)$xmlRoles[$i]->OsVersion : (string)$xmlRoles[$i]->OperatingSystemVersion)
- );
- }
- }
- $returnValue->RoleList = $roles;
-
- return $returnValue;
- }
- return null;
- }
-
- /**
- * Updates a deployment's role instance count.
- *
- * @param string $serviceName The service name
- * @param string $deploymentSlot The deployment slot (production or staging)
- * @param string|array $roleName The role name
- * @param string|array $instanceCount The instance count
- * @throws Microsoft_Win…
Large files files are truncated, but you can click here to view the full file