PageRenderTime 48ms CodeModel.GetById 15ms RepoModel.GetById 1ms 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

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

  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_Win…

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