PageRenderTime 62ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/Zend/Service/WindowsAzure/Management/Client.php

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