PageRenderTime 73ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

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

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