PageRenderTime 32ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/Microsoft/WindowsAzure/Management/Client.php

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