PageRenderTime 60ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/profiles/acquia/modules/azure/phpazure/library/Microsoft/WindowsAzure/Management/Client.php

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