PageRenderTime 52ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

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

http://grupal.googlecode.com/
PHP | 2423 lines | 1465 code | 263 blank | 695 comment | 492 complexity | 77d2037f8277216f53ee3c19ec793e90 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0

Large files files are truncated, but you can click here to view the full 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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Service_WindowsAzure_Management_OperationStatusInstance
  24. */
  25. require_once 'Zend/Service/WindowsAzure/Management/OperationStatusInstance.php';
  26. /**
  27. * @see Zend_Service_WindowsAzure_Management_SubscriptionOperationInstance
  28. */
  29. require_once 'Zend/Service/WindowsAzure/Management/SubscriptionOperationInstance.php';
  30. /**
  31. * @see Zend_Service_WindowsAzure_Management_DeploymentInstance
  32. */
  33. require_once 'Zend/Service/WindowsAzure/Management/DeploymentInstance.php';
  34. /**
  35. * @see Zend_Service_WindowsAzure_Storage_Blob
  36. */
  37. require_once 'Zend/Service/WindowsAzure/Storage/Blob.php';
  38. /**
  39. * @see Zend_Service_WindowsAzure_Storage_Table
  40. */
  41. require_once 'Zend/Service/WindowsAzure/Storage/Table.php';
  42. /**
  43. * @see Zend_Service_WindowsAzure_Management_HostedServiceInstance
  44. */
  45. require_once 'Zend/Service/WindowsAzure/Management/HostedServiceInstance.php';
  46. /**
  47. * @see Zend_Service_WindowsAzure_Management_CertificateInstance
  48. */
  49. require_once 'Zend/Service/WindowsAzure/Management/CertificateInstance.php';
  50. /**
  51. * @see Zend_Service_WindowsAzure_Management_AffinityGroupInstance
  52. */
  53. require_once 'Zend/Service/WindowsAzure/Management/AffinityGroupInstance.php';
  54. /**
  55. * @see Zend_Service_WindowsAzure_Management_LocationInstance
  56. */
  57. require_once 'Zend/Service/WindowsAzure/Management/LocationInstance.php';
  58. /**
  59. * @see Zend_Service_WindowsAzure_Management_OperatingSystemInstance
  60. */
  61. require_once 'Zend/Service/WindowsAzure/Management/OperatingSystemInstance.php';
  62. /**
  63. * @see Zend_Service_WindowsAzure_Management_OperatingSystemFamilyInstance
  64. */
  65. require_once 'Zend/Service/WindowsAzure/Management/OperatingSystemFamilyInstance.php';
  66. /**
  67. * @category Zend
  68. * @package Zend_Service_WindowsAzure
  69. * @subpackage Management
  70. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  71. * @license http://framework.zend.com/license/new-bsd New BSD License
  72. */
  73. class Zend_Service_WindowsAzure_Management_Client
  74. {
  75. /**
  76. * Management service URL
  77. */
  78. const URL_MANAGEMENT = "https://management.core.windows.net";
  79. /**
  80. * Operations
  81. */
  82. const OP_OPERATIONS = "operations";
  83. const OP_STORAGE_ACCOUNTS = "services/storageservices";
  84. const OP_HOSTED_SERVICES = "services/hostedservices";
  85. const OP_AFFINITYGROUPS = "affinitygroups";
  86. const OP_LOCATIONS = "locations";
  87. const OP_OPERATINGSYSTEMS = "operatingsystems";
  88. const OP_OPERATINGSYSTEMFAMILIES = "operatingsystemfamilies";
  89. /**
  90. * Current API version
  91. *
  92. * @var string
  93. */
  94. protected $_apiVersion = '2011-02-25';
  95. /**
  96. * Subscription ID
  97. *
  98. * @var string
  99. */
  100. protected $_subscriptionId = '';
  101. /**
  102. * Management certificate path (.PEM)
  103. *
  104. * @var string
  105. */
  106. protected $_certificatePath = '';
  107. /**
  108. * Management certificate passphrase
  109. *
  110. * @var string
  111. */
  112. protected $_certificatePassphrase = '';
  113. /**
  114. * Zend_Http_Client channel used for communication with REST services
  115. *
  116. * @var Zend_Http_Client
  117. */
  118. protected $_httpClientChannel = null;
  119. /**
  120. * Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract instance
  121. *
  122. * @var Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract
  123. */
  124. protected $_retryPolicy = null;
  125. /**
  126. * Returns the last request ID
  127. *
  128. * @var string
  129. */
  130. protected $_lastRequestId = null;
  131. /**
  132. * Creates a new Zend_Service_WindowsAzure_Management instance
  133. *
  134. * @param string $subscriptionId Subscription ID
  135. * @param string $certificatePath Management certificate path (.PEM)
  136. * @param string $certificatePassphrase Management certificate passphrase
  137. * @param Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy Retry policy to use when making requests
  138. */
  139. public function __construct(
  140. $subscriptionId,
  141. $certificatePath,
  142. $certificatePassphrase,
  143. Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy = null
  144. ) {
  145. $this->_subscriptionId = $subscriptionId;
  146. $this->_certificatePath = $certificatePath;
  147. $this->_certificatePassphrase = $certificatePassphrase;
  148. $this->_retryPolicy = $retryPolicy;
  149. if (is_null($this->_retryPolicy)) {
  150. $this->_retryPolicy = Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract::noRetry();
  151. }
  152. // Setup default Zend_Http_Client channel
  153. $options = array(
  154. 'adapter' => 'Zend_Http_Client_Adapter_Socket',
  155. 'ssltransport' => 'ssl',
  156. 'sslcert' => $this->_certificatePath,
  157. 'sslpassphrase' => $this->_certificatePassphrase,
  158. 'sslusecontext' => true,
  159. );
  160. if (function_exists('curl_init')) {
  161. // Set cURL options if cURL is used afterwards
  162. $options['curloptions'] = array(
  163. CURLOPT_FOLLOWLOCATION => true,
  164. CURLOPT_TIMEOUT => 120,
  165. );
  166. }
  167. $this->_httpClientChannel = new Zend_Http_Client(null, $options);
  168. }
  169. /**
  170. * Set the HTTP client channel to use
  171. *
  172. * @param Zend_Http_Client_Adapter_Interface|string $adapterInstance Adapter instance or adapter class name.
  173. */
  174. public function setHttpClientChannel($adapterInstance = 'Zend_Http_Client_Adapter_Socket')
  175. {
  176. $this->_httpClientChannel->setAdapter($adapterInstance);
  177. }
  178. /**
  179. * Retrieve HTTP client channel
  180. *
  181. * @return Zend_Http_Client_Adapter_Interface
  182. */
  183. public function getHttpClientChannel()
  184. {
  185. return $this->_httpClientChannel;
  186. }
  187. /**
  188. * Returns the Windows Azure subscription ID
  189. *
  190. * @return string
  191. */
  192. public function getSubscriptionId()
  193. {
  194. return $this->_subscriptionId;
  195. }
  196. /**
  197. * Returns the last request ID.
  198. *
  199. * @return string
  200. */
  201. public function getLastRequestId()
  202. {
  203. return $this->_lastRequestId;
  204. }
  205. /**
  206. * Get base URL for creating requests
  207. *
  208. * @return string
  209. */
  210. public function getBaseUrl()
  211. {
  212. return self::URL_MANAGEMENT . '/' . $this->_subscriptionId;
  213. }
  214. /**
  215. * Perform request using Zend_Http_Client channel
  216. *
  217. * @param string $path Path
  218. * @param string $queryString Query string
  219. * @param string $httpVerb HTTP verb the request will use
  220. * @param array $headers x-ms headers to add
  221. * @param mixed $rawData Optional RAW HTTP data to be sent over the wire
  222. * @return Zend_Http_Response
  223. */
  224. protected function _performRequest(
  225. $path = '/',
  226. $queryString = '',
  227. $httpVerb = Zend_Http_Client::GET,
  228. $headers = array(),
  229. $rawData = null
  230. ) {
  231. // Clean path
  232. if (strpos($path, '/') !== 0) {
  233. $path = '/' . $path;
  234. }
  235. // Clean headers
  236. if (is_null($headers)) {
  237. $headers = array();
  238. }
  239. // Ensure cUrl will also work correctly:
  240. // - disable Content-Type if required
  241. // - disable Expect: 100 Continue
  242. if (!isset($headers["Content-Type"])) {
  243. $headers["Content-Type"] = '';
  244. }
  245. //$headers["Expect"] = '';
  246. // Add version header
  247. $headers['x-ms-version'] = $this->_apiVersion;
  248. // URL encoding
  249. $path = self::urlencode($path);
  250. $queryString = self::urlencode($queryString);
  251. // Generate URL and sign request
  252. $requestUrl = $this->getBaseUrl() . $path . $queryString;
  253. $requestHeaders = $headers;
  254. // Prepare request
  255. $this->_httpClientChannel->resetParameters(true);
  256. $this->_httpClientChannel->setUri($requestUrl);
  257. $this->_httpClientChannel->setHeaders($requestHeaders);
  258. $this->_httpClientChannel->setRawData($rawData);
  259. // Execute request
  260. $response = $this->_retryPolicy->execute(
  261. array($this->_httpClientChannel, 'request'),
  262. array($httpVerb)
  263. );
  264. // Store request id
  265. $this->_lastRequestId = $response->getHeader('x-ms-request-id');
  266. return $response;
  267. }
  268. /**
  269. * Parse result from Zend_Http_Response
  270. *
  271. * @param Zend_Http_Response $response Response from HTTP call
  272. * @return object
  273. * @throws Zend_Service_WindowsAzure_Exception
  274. */
  275. protected function _parseResponse(Zend_Http_Response $response = null)
  276. {
  277. if (is_null($response)) {
  278. require_once 'Zend/Service/WindowsAzure/Exception.php';
  279. throw new Zend_Service_WindowsAzure_Exception('Response should not be null.');
  280. }
  281. $xml = @simplexml_load_string($response->getBody());
  282. if ($xml !== false) {
  283. // Fetch all namespaces
  284. $namespaces = array_merge($xml->getNamespaces(true), $xml->getDocNamespaces(true));
  285. // Register all namespace prefixes
  286. foreach ($namespaces as $prefix => $ns) {
  287. if ($prefix != '') {
  288. $xml->registerXPathNamespace($prefix, $ns);
  289. }
  290. }
  291. }
  292. return $xml;
  293. }
  294. /**
  295. * URL encode function
  296. *
  297. * @param string $value Value to encode
  298. * @return string Encoded value
  299. */
  300. public static function urlencode($value)
  301. {
  302. return str_replace(' ', '%20', $value);
  303. }
  304. /**
  305. * Builds a query string from an array of elements
  306. *
  307. * @param array Array of elements
  308. * @return string Assembled query string
  309. */
  310. public static function createQueryStringFromArray($queryString)
  311. {
  312. return count($queryString) > 0 ? '?' . implode('&', $queryString) : '';
  313. }
  314. /**
  315. * Get error message from Zend_Http_Response
  316. *
  317. * @param Zend_Http_Response $response Repsonse
  318. * @param string $alternativeError Alternative error message
  319. * @return string
  320. */
  321. protected function _getErrorMessage(Zend_Http_Response $response, $alternativeError = 'Unknown error.')
  322. {
  323. $response = $this->_parseResponse($response);
  324. if ($response && $response->Message) {
  325. return (string)$response->Message;
  326. } else {
  327. return $alternativeError;
  328. }
  329. }
  330. /**
  331. * The Get Operation Status operation returns the status of the specified operation.
  332. * After calling an asynchronous operation, you can call Get Operation Status to
  333. * determine whether the operation has succeed, failed, or is still in progress.
  334. *
  335. * @param string $requestId The request ID. If omitted, the last request ID will be used.
  336. * @return Zend_Service_WindowsAzure_Management_OperationStatusInstance
  337. * @throws Zend_Service_WindowsAzure_Management_Exception
  338. */
  339. public function getOperationStatus($requestId = '')
  340. {
  341. if ($requestId == '') {
  342. $requestId = $this->getLastRequestId();
  343. }
  344. $response = $this->_performRequest(self::OP_OPERATIONS . '/' . $requestId);
  345. if ($response->isSuccessful()) {
  346. $result = $this->_parseResponse($response);
  347. if (!is_null($result)) {
  348. return new Zend_Service_WindowsAzure_Management_OperationStatusInstance(
  349. (string)$result->ID,
  350. (string)$result->Status,
  351. ($result->Error ? (string)$result->Error->Code : ''),
  352. ($result->Error ? (string)$result->Error->Message : '')
  353. );
  354. }
  355. return null;
  356. } else {
  357. require_once 'Zend/Service/Management/Exception.php';
  358. throw new Zend_Service_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
  359. }
  360. }
  361. /**
  362. * The List Subscription Operations operation returns a list of create, update,
  363. * and delete operations that were performed on a subscription during the specified timeframe.
  364. * Documentation on the parameters can be found at http://msdn.microsoft.com/en-us/library/gg715318.aspx.
  365. *
  366. * @param string $startTime The start of the timeframe to begin listing subscription operations in UTC format. This parameter and the $endTime parameter indicate the timeframe to retrieve subscription operations. This parameter cannot indicate a start date of more than 90 days in the past.
  367. * @param string $endTime The end of the timeframe to begin listing subscription operations in UTC format. This parameter and the $startTime parameter indicate the timeframe to retrieve subscription operations.
  368. * @param string $objectIdFilter Returns subscription operations only for the specified object type and object ID.
  369. * @param string $operationResultFilter Returns subscription operations only for the specified result status, either Succeeded, Failed, or InProgress.
  370. * @param string $continuationToken Internal usage.
  371. * @return array Array of Zend_Service_WindowsAzure_Management_SubscriptionOperationInstance
  372. * @throws Zend_Service_WindowsAzure_Management_Exception
  373. */
  374. public function listSubscriptionOperations($startTime, $endTime, $objectIdFilter = null, $operationResultFilter = null, $continuationToken = null)
  375. {
  376. if ($startTime == '' || is_null($startTime)) {
  377. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  378. throw new Zend_Service_WindowsAzure_Management_Exception('Start time should be specified.');
  379. }
  380. if ($endTime == '' || is_null($endTime)) {
  381. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  382. throw new Zend_Service_WindowsAzure_Management_Exception('End time should be specified.');
  383. }
  384. if ($operationResultFilter != '' && !is_null($operationResultFilter)) {
  385. $operationResultFilter = strtolower($operationResultFilter);
  386. if ($operationResultFilter != 'succeeded' && $operationResultFilter != 'failed' && $operationResultFilter != 'inprogress') {
  387. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  388. throw new Zend_Service_WindowsAzure_Management_Exception('OperationResultFilter should be succeeded|failed|inprogress.');
  389. }
  390. }
  391. $parameters = array();
  392. $parameters[] = 'StartTime=' . $startTime;
  393. $parameters[] = 'EndTime=' . $endTime;
  394. if ($objectIdFilter != '' && !is_null($objectIdFilter)) {
  395. $parameters[] = 'ObjectIdFilter=' . $objectIdFilter;
  396. }
  397. if ($operationResultFilter != '' && !is_null($operationResultFilter)) {
  398. $parameters[] = 'OperationResultFilter=' . ucfirst($operationResultFilter);
  399. }
  400. if ($continuationToken != '' && !is_null($continuationToken)) {
  401. $parameters[] = 'ContinuationToken=' . $continuationToken;
  402. }
  403. $response = $this->_performRequest(self::OP_OPERATIONS, '?' . implode('&', $parameters));
  404. if ($response->isSuccessful()) {
  405. $result = $this->_parseResponse($response);
  406. $namespaces = $result->getDocNamespaces();
  407. $result->registerXPathNamespace('__empty_ns', $namespaces['']);
  408. $xmlOperations = $result->xpath('//__empty_ns:SubscriptionOperation');
  409. // Create return value
  410. $returnValue = array();
  411. foreach ($xmlOperations as $xmlOperation) {
  412. // Create operation instance
  413. $operation = new Zend_Service_WindowsAzure_Management_SubscriptionOperationInstance(
  414. $xmlOperation->OperationId,
  415. $xmlOperation->OperationObjectId,
  416. $xmlOperation->OperationName,
  417. array(),
  418. (array)$xmlOperation->OperationCaller,
  419. (array)$xmlOperation->OperationStatus
  420. );
  421. // Parse parameters
  422. $xmlOperation->registerXPathNamespace('__empty_ns', $namespaces['']);
  423. $xmlParameters = $xmlOperation->xpath('.//__empty_ns:OperationParameter');
  424. foreach ($xmlParameters as $xmlParameter) {
  425. $xmlParameterDetails = $xmlParameter->children('http://schemas.datacontract.org/2004/07/Microsoft.Samples.WindowsAzure.ServiceManagement');
  426. $operation->addOperationParameter((string)$xmlParameterDetails->Name, (string)$xmlParameterDetails->Value);
  427. }
  428. // Add to result
  429. $returnValue[] = $operation;
  430. }
  431. // More data?
  432. if (!is_null($result->ContinuationToken) && $result->ContinuationToken != '') {
  433. $returnValue = array_merge($returnValue, $this->listSubscriptionOperations($startTime, $endTime, $objectIdFilter, $operationResultFilter, (string)$result->ContinuationToken));
  434. }
  435. // Return
  436. return $returnValue;
  437. } else {
  438. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  439. throw new Zend_Service_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
  440. }
  441. }
  442. /**
  443. * Wait for an operation to complete
  444. *
  445. * @param string $requestId The request ID. If omitted, the last request ID will be used.
  446. * @param int $sleepInterval Sleep interval in milliseconds.
  447. * @return Zend_Service_WindowsAzure_Management_OperationStatusInstance
  448. * @throws Zend_Service_WindowsAzure_Management_Exception
  449. */
  450. public function waitForOperation($requestId = '', $sleepInterval = 250)
  451. {
  452. if ($requestId == '') {
  453. $requestId = $this->getLastRequestId();
  454. }
  455. if ($requestId == '' || is_null($requestId)) {
  456. return null;
  457. }
  458. $status = $this->getOperationStatus($requestId);
  459. while ($status->Status == 'InProgress') {
  460. $status = $this->getOperationStatus($requestId);
  461. usleep($sleepInterval);
  462. }
  463. return $status;
  464. }
  465. /**
  466. * Creates a new Zend_Service_WindowsAzure_Storage_Blob instance for the current account
  467. *
  468. * @param string $serviceName the service name to create a storage client for.
  469. * @param Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy Retry policy to use when making requests
  470. * @return Zend_Service_WindowsAzure_Storage_Blob
  471. */
  472. public function createBlobClientForService($serviceName, Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy = null)
  473. {
  474. if ($serviceName == '' || is_null($serviceName)) {
  475. throw new Zend_Service_WindowsAzure_Management_Exception('Service name should be specified.');
  476. }
  477. $storageKeys = $this->getStorageAccountKeys($serviceName);
  478. return new Zend_Service_WindowsAzure_Storage_Blob(
  479. Zend_Service_WindowsAzure_Storage::URL_CLOUD_BLOB,
  480. $serviceName,
  481. $storageKeys[0],
  482. false,
  483. $retryPolicy
  484. );
  485. }
  486. /**
  487. * Creates a new Zend_Service_WindowsAzure_Storage_Table instance for the current account
  488. *
  489. * @param string $serviceName the service name to create a storage client for.
  490. * @param Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy Retry policy to use when making requests
  491. * @return Zend_Service_WindowsAzure_Storage_Table
  492. */
  493. public function createTableClientForService($serviceName, Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy = null)
  494. {
  495. if ($serviceName == '' || is_null($serviceName)) {
  496. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  497. throw new Zend_Service_WindowsAzure_Management_Exception('Service name should be specified.');
  498. }
  499. $storageKeys = $this->getStorageAccountKeys($serviceName);
  500. return new Zend_Service_WindowsAzure_Storage_Table(
  501. Zend_Service_WindowsAzure_Storage::URL_CLOUD_TABLE,
  502. $serviceName,
  503. $storageKeys[0],
  504. false,
  505. $retryPolicy
  506. );
  507. }
  508. /**
  509. * Creates a new Zend_Service_WindowsAzure_Storage_Queue instance for the current account
  510. *
  511. * @param string $serviceName the service name to create a storage client for.
  512. * @param Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy Retry policy to use when making requests
  513. * @return Zend_Service_WindowsAzure_Storage_Queue
  514. */
  515. public function createQueueClientForService($serviceName, Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy = null)
  516. {
  517. if ($serviceName == '' || is_null($serviceName)) {
  518. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  519. throw new Zend_Service_WindowsAzure_Management_Exception('Service name should be specified.');
  520. }
  521. $storageKeys = $this->getStorageAccountKeys($serviceName);
  522. require_once 'Zend/Service/WindowsAzure/Storage/Queue.php';
  523. return new Zend_Service_WindowsAzure_Storage_Queue(
  524. Zend_Service_WindowsAzure_Storage::URL_CLOUD_QUEUE,
  525. $serviceName,
  526. $storageKeys[0],
  527. false,
  528. $retryPolicy
  529. );
  530. }
  531. /**
  532. * The List Storage Accounts operation lists the storage accounts available under
  533. * the current subscription.
  534. *
  535. * @return array An array of Zend_Service_WindowsAzure_Management_StorageServiceInstance
  536. */
  537. public function listStorageAccounts()
  538. {
  539. $response = $this->_performRequest(self::OP_STORAGE_ACCOUNTS);
  540. if ($response->isSuccessful()) {
  541. $result = $this->_parseResponse($response);
  542. if (!$result->StorageService) {
  543. return array();
  544. }
  545. if (count($result->StorageService) > 1) {
  546. $xmlServices = $result->StorageService;
  547. } else {
  548. $xmlServices = array($result->StorageService);
  549. }
  550. $services = array();
  551. if (!is_null($xmlServices)) {
  552. for ($i = 0; $i < count($xmlServices); $i++) {
  553. $services[] = new Zend_Service_WindowsAzure_Management_StorageServiceInstance(
  554. (string)$xmlServices[$i]->Url,
  555. (string)$xmlServices[$i]->ServiceName
  556. );
  557. }
  558. }
  559. return $services;
  560. } else {
  561. throw new Zend_Service_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
  562. }
  563. }
  564. /**
  565. * The Get Storage Account Properties operation returns the system properties for the
  566. * specified storage account. These properties include: the address, description, and
  567. * label of the storage account; and the name of the affinity group to which the service
  568. * belongs, or its geo-location if it is not part of an affinity group.
  569. *
  570. * @param string $serviceName The name of your service.
  571. * @return Zend_Service_WindowsAzure_Management_StorageServiceInstance
  572. * @throws Zend_Service_WindowsAzure_Management_Exception
  573. */
  574. public function getStorageAccountProperties($serviceName)
  575. {
  576. if ($serviceName == '' || is_null($serviceName)) {
  577. throw new Zend_Service_WindowsAzure_Management_Exception('Service name should be specified.');
  578. }
  579. $response = $this->_performRequest(self::OP_STORAGE_ACCOUNTS . '/' . $serviceName);
  580. if ($response->isSuccessful()) {
  581. $xmlService = $this->_parseResponse($response);
  582. if (!is_null($xmlService)) {
  583. require_once 'Zend/Service/WindowsAzure/Management/StorageServiceInstance.php';
  584. return new Zend_Service_WindowsAzure_Management_StorageServiceInstance(
  585. (string)$xmlService->Url,
  586. (string)$xmlService->ServiceName,
  587. (string)$xmlService->StorageServiceProperties->Description,
  588. (string)$xmlService->StorageServiceProperties->AffinityGroup,
  589. (string)$xmlService->StorageServiceProperties->Location,
  590. (string)$xmlService->StorageServiceProperties->Label
  591. );
  592. }
  593. return null;
  594. } else {
  595. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  596. throw new Zend_Service_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
  597. }
  598. }
  599. /**
  600. * The Get Storage Keys operation returns the primary
  601. * and secondary access keys for the specified storage account.
  602. *
  603. * @param string $serviceName The name of your service.
  604. * @return array An array of strings
  605. * @throws Zend_Service_WindowsAzure_Management_Exception
  606. */
  607. public function getStorageAccountKeys($serviceName)
  608. {
  609. if ($serviceName == '' || is_null($serviceName)) {
  610. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  611. throw new Zend_Service_WindowsAzure_Management_Exception('Service name should be specified.');
  612. }
  613. $response = $this->_performRequest(self::OP_STORAGE_ACCOUNTS . '/' . $serviceName . '/keys');
  614. if ($response->isSuccessful()) {
  615. $xmlService = $this->_parseResponse($response);
  616. if (!is_null($xmlService)) {
  617. return array(
  618. (string)$xmlService->StorageServiceKeys->Primary,
  619. (string)$xmlService->StorageServiceKeys->Secondary
  620. );
  621. }
  622. return array();
  623. } else {
  624. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  625. throw new Zend_Service_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
  626. }
  627. }
  628. /**
  629. * The Regenerate Keys operation regenerates the primary
  630. * or secondary access key for the specified storage account.
  631. *
  632. * @param string $serviceName The name of your service.
  633. * @param string $key The key to regenerate (primary or secondary)
  634. * @return array An array of strings
  635. * @throws Zend_Service_WindowsAzure_Management_Exception
  636. */
  637. public function regenerateStorageAccountKey($serviceName, $key = 'primary')
  638. {
  639. if ($serviceName == '' || is_null($serviceName)) {
  640. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  641. throw new Zend_Service_WindowsAzure_Management_Exception('Service name should be specified.');
  642. }
  643. $key = strtolower($key);
  644. if ($key != 'primary' && $key != 'secondary') {
  645. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  646. throw new Zend_Service_WindowsAzure_Management_Exception('Key identifier should be primary|secondary.');
  647. }
  648. $response = $this->_performRequest(
  649. self::OP_STORAGE_ACCOUNTS . '/' . $serviceName . '/keys', '?action=regenerate',
  650. Zend_Http_Client::POST,
  651. array('Content-Type' => 'application/xml'),
  652. '<?xml version="1.0" encoding="utf-8"?>
  653. <RegenerateKeys xmlns="http://schemas.microsoft.com/windowsazure">
  654. <KeyType>' . ucfirst($key) . '</KeyType>
  655. </RegenerateKeys>');
  656. if ($response->isSuccessful()) {
  657. $xmlService = $this->_parseResponse($response);
  658. if (!is_null($xmlService)) {
  659. return array(
  660. (string)$xmlService->StorageServiceKeys->Primary,
  661. (string)$xmlService->StorageServiceKeys->Secondary
  662. );
  663. }
  664. return array();
  665. } else {
  666. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  667. throw new Zend_Service_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
  668. }
  669. }
  670. /**
  671. * The List Hosted Services operation lists the hosted services available
  672. * under the current subscription.
  673. *
  674. * @return array An array of Zend_Service_WindowsAzure_Management_HostedServiceInstance
  675. * @throws Zend_Service_WindowsAzure_Management_Exception
  676. */
  677. public function listHostedServices()
  678. {
  679. $response = $this->_performRequest(self::OP_HOSTED_SERVICES);
  680. if ($response->isSuccessful()) {
  681. $result = $this->_parseResponse($response);
  682. if (!$result->HostedService) {
  683. return array();
  684. }
  685. if (count($result->HostedService) > 1) {
  686. $xmlServices = $result->HostedService;
  687. } else {
  688. $xmlServices = array($result->HostedService);
  689. }
  690. $services = array();
  691. if (!is_null($xmlServices)) {
  692. for ($i = 0; $i < count($xmlServices); $i++) {
  693. $services[] = new Zend_Service_WindowsAzure_Management_HostedServiceInstance(
  694. (string)$xmlServices[$i]->Url,
  695. (string)$xmlServices[$i]->ServiceName
  696. );
  697. }
  698. }
  699. return $services;
  700. } else {
  701. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  702. throw new Zend_Service_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
  703. }
  704. }
  705. /**
  706. * The Create Hosted Service operation creates a new hosted service in Windows Azure.
  707. *
  708. * @param string $serviceName A name for the hosted service that is unique to the subscription.
  709. * @param string $label A label for the hosted service. The label may be up to 100 characters in length.
  710. * @param string $description A description for the hosted service. The description may be up to 1024 characters in length.
  711. * @param string $location Required if AffinityGroup is not specified. The location where the hosted service will be created.
  712. * @param string $affinityGroup Required if Location is not specified. The name of an existing affinity group associated with this subscription.
  713. */
  714. public function createHostedService($serviceName, $label, $description = '', $location = null, $affinityGroup = null)
  715. {
  716. if ($serviceName == '' || is_null($serviceName)) {
  717. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  718. throw new Zend_Service_WindowsAzure_Management_Exception('Service name should be specified.');
  719. }
  720. if ($label == '' || is_null($label)) {
  721. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  722. throw new Zend_Service_WindowsAzure_Management_Exception('Label should be specified.');
  723. }
  724. if (strlen($label) > 100) {
  725. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  726. throw new Zend_Service_WindowsAzure_Management_Exception('Label is too long. The maximum length is 100 characters.');
  727. }
  728. if (strlen($description) > 1024) {
  729. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  730. throw new Zend_Service_WindowsAzure_Management_Exception('Description is too long. The maximum length is 1024 characters.');
  731. }
  732. if ( (is_null($location) && is_null($affinityGroup)) || (!is_null($location) && !is_null($affinityGroup)) ) {
  733. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  734. throw new Zend_Service_WindowsAzure_Management_Exception('Please specify a location -or- an affinity group for the service.');
  735. }
  736. $locationOrAffinityGroup = is_null($location)
  737. ? '<AffinityGroup>' . $affinityGroup . '</AffinityGroup>'
  738. : '<Location>' . $location . '</Location>';
  739. $response = $this->_performRequest(self::OP_HOSTED_SERVICES, '',
  740. Zend_Http_Client::POST,
  741. array('Content-Type' => 'application/xml; charset=utf-8'),
  742. '<CreateHostedService xmlns="http://schemas.microsoft.com/windowsazure"><ServiceName>' . $serviceName . '</ServiceName><Label>' . base64_encode($label) . '</Label><Description>' . $description . '</Description>' . $locationOrAffinityGroup . '</CreateHostedService>');
  743. if (!$response->isSuccessful()) {
  744. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  745. throw new Zend_Service_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
  746. }
  747. }
  748. /**
  749. * The Update Hosted Service operation updates the label and/or the description for a hosted service in Windows Azure.
  750. *
  751. * @param string $serviceName A name for the hosted service that is unique to the subscription.
  752. * @param string $label A label for the hosted service. The label may be up to 100 characters in length.
  753. * @param string $description A description for the hosted service. The description may be up to 1024 characters in length.
  754. */
  755. public function updateHostedService($serviceName, $label, $description = '')
  756. {
  757. if ($serviceName == '' || is_null($serviceName)) {
  758. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  759. throw new Zend_Service_WindowsAzure_Management_Exception('Service name should be specified.');
  760. }
  761. if ($label == '' || is_null($label)) {
  762. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  763. throw new Zend_Service_WindowsAzure_Management_Exception('Label should be specified.');
  764. }
  765. if (strlen($label) > 100) {
  766. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  767. throw new Zend_Service_WindowsAzure_Management_Exception('Label is too long. The maximum length is 100 characters.');
  768. }
  769. $response = $this->_performRequest(self::OP_HOSTED_SERVICES . '/' . $serviceName, '',
  770. Zend_Http_Client::PUT,
  771. array('Content-Type' => 'application/xml; charset=utf-8'),
  772. '<UpdateHostedService xmlns="http://schemas.microsoft.com/windowsazure"><Label>' . base64_encode($label) . '</Label><Description>' . $description . '</Description></UpdateHostedService>');
  773. if (!$response->isSuccessful()) {
  774. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  775. throw new Zend_Service_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
  776. }
  777. }
  778. /**
  779. * The Delete Hosted Service operation deletes the specified hosted service in Windows Azure.
  780. *
  781. * @param string $serviceName A name for the hosted service that is unique to the subscription.
  782. */
  783. public function deleteHostedService($serviceName)
  784. {
  785. if ($serviceName == '' || is_null($serviceName)) {
  786. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  787. throw new Zend_Service_WindowsAzure_Management_Exception('Service name should be specified.');
  788. }
  789. $response = $this->_performRequest(self::OP_HOSTED_SERVICES . '/' . $serviceName, '', Zend_Http_Client::DELETE);
  790. if (!$response->isSuccessful()) {
  791. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  792. throw new Zend_Service_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
  793. }
  794. }
  795. /**
  796. * The Get Hosted Service Properties operation retrieves system properties
  797. * for the specified hosted service. These properties include the service
  798. * name and service type; the name of the affinity group to which the service
  799. * belongs, or its location if it is not part of an affinity group; and
  800. * optionally, information on the service's deployments.
  801. *
  802. * @param string $serviceName The name of your service.
  803. * @return Zend_Service_WindowsAzure_Management_HostedServiceInstance
  804. * @throws Zend_Service_WindowsAzure_Management_Exception
  805. */
  806. public function getHostedServiceProperties($serviceName)
  807. {
  808. if ($serviceName == '' || is_null($serviceName)) {
  809. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  810. throw new Zend_Service_WindowsAzure_Management_Exception('Service name should be specified.');
  811. }
  812. $response = $this->_performRequest(self::OP_HOSTED_SERVICES . '/' . $serviceName, '?embed-detail=true');
  813. if ($response->isSuccessful()) {
  814. $xmlService = $this->_parseResponse($response);
  815. if (!is_null($xmlService)) {
  816. $returnValue = new Zend_Service_WindowsAzure_Management_HostedServiceInstance(
  817. (string)$xmlService->Url,
  818. (string)$xmlService->ServiceName,
  819. (string)$xmlService->HostedServiceProperties->Description,
  820. (string)$xmlService->HostedServiceProperties->AffinityGroup,
  821. (string)$xmlService->HostedServiceProperties->Location,
  822. (string)$xmlService->HostedServiceProperties->Label
  823. );
  824. // Deployments
  825. if (count($xmlService->Deployments->Deployment) > 1) {
  826. $xmlServices = $xmlService->Deployments->Deployment;
  827. } else {
  828. $xmlServices = array($xmlService->Deployments->Deployment);
  829. }
  830. $deployments = array();
  831. foreach ($xmlServices as $xmlDeployment) {
  832. $deployments[] = $this->_convertXmlElementToDeploymentInstance($xmlDeployment);
  833. }
  834. $returnValue->Deployments = $deployments;
  835. return $returnValue;
  836. }
  837. return null;
  838. } else {
  839. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  840. throw new Zend_Service_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
  841. }
  842. }
  843. /**
  844. * The Create Deployment operation uploads a new service package
  845. * and creates a new deployment on staging or production.
  846. *
  847. * @param string $serviceName The service name
  848. * @param string $deploymentSlot The deployment slot (production or staging)
  849. * @param string $name The name for the deployment. The deployment ID as listed on the Windows Azure management portal must be unique among other deployments for the hosted service.
  850. * @param string $label A URL that refers to the location of the service package in the Blob service. The service package must be located in a storage account beneath the same subscription.
  851. * @param string $packageUrl The service configuration file for the deployment.
  852. * @param string $configuration A label for this deployment, up to 100 characters in length.
  853. * @param boolean $startDeployment Indicates whether to start the deployment immediately after it is created.
  854. * @param boolean $treatWarningsAsErrors Indicates whether to treat package validation warnings as errors.
  855. * @throws Zend_Service_WindowsAzure_Management_Exception
  856. */
  857. public function createDeployment($serviceName, $deploymentSlot, $name, $label, $packageUrl, $configuration, $startDeployment = false, $treatWarningsAsErrors = false)
  858. {
  859. if ($serviceName == '' || is_null($serviceName)) {
  860. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  861. throw new Zend_Service_WindowsAzure_Management_Exception('Service name should be specified.');
  862. }
  863. $deploymentSlot = strtolower($deploymentSlot);
  864. if ($deploymentSlot != 'production' && $deploymentSlot != 'staging') {
  865. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  866. throw new Zend_Service_WindowsAzure_Management_Exception('Deployment slot should be production|staging.');
  867. }
  868. if ($name == '' || is_null($name)) {
  869. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  870. throw new Zend_Service_WindowsAzure_Management_Exception('Name should be specified.');
  871. }
  872. if ($label == '' || is_null($label)) {
  873. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  874. throw new Zend_Service_WindowsAzure_Management_Exception('Label should be specified.');
  875. }
  876. if (strlen($label) > 100) {
  877. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  878. throw new Zend_Service_WindowsAzure_Management_Exception('Label is too long. The maximum length is 100 characters.');
  879. }
  880. if ($packageUrl == '' || is_null($packageUrl)) {
  881. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  882. throw new Zend_Service_WindowsAzure_Management_Exception('Package URL should be specified.');
  883. }
  884. if ($configuration == '' || is_null($configuration)) {
  885. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  886. throw new Zend_Service_WindowsAzure_Management_Exception('Configuration should be specified.');
  887. }
  888. if (@file_exists($configuration)) {
  889. $configuration = utf8_decode(file_get_contents($configuration));
  890. }
  891. // Clean up the configuration
  892. $conformingConfiguration = $this->_cleanConfiguration($configuration);
  893. $operationUrl = self::OP_HOSTED_SERVICES . '/' . $serviceName . '/deploymentslots/' . $deploymentSlot;
  894. $response = $this->_performRequest($operationUrl, '',
  895. Zend_Http_Client::POST,
  896. array('Content-Type' => 'application/xml; charset=utf-8'),
  897. '<CreateDeployment xmlns="http://schemas.microsoft.com/windowsazure"><Name>' . $name . '</Name><PackageUrl>' . $packageUrl . '</PackageUrl><Label>' . base64_encode($label) . '</Label><Configuration>' . base64_encode($conformingConfiguration) . '</Configuration><StartDeployment>' . ($startDeployment ? 'true' : 'false') . '</StartDeployment><TreatWarningsAsError>' . ($treatWarningsAsErrors ? 'true' : 'false') . '</TreatWarningsAsError></CreateDeployment>');
  898. if (!$response->isSuccessful()) {
  899. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  900. throw new Zend_Service_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
  901. }
  902. }
  903. /**
  904. * The Get Deployment operation returns configuration information, status,
  905. * and system properties for the specified deployment.
  906. *
  907. * @param string $serviceName The service name
  908. * @param string $deploymentSlot The deployment slot (production or staging)
  909. * @return Zend_Service_WindowsAzure_Management_DeploymentInstance
  910. * @throws Zend_Service_WindowsAzure_Management_Exception
  911. */
  912. public function getDeploymentBySlot($serviceName, $deploymentSlot)
  913. {
  914. if ($serviceName == '' || is_null($serviceName)) {
  915. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  916. throw new Zend_Service_WindowsAzure_Management_Exception('Service name should be specified.');
  917. }
  918. $deploymentSlot = strtolower($deploymentSlot);
  919. if ($deploymentSlot != 'production' && $deploymentSlot != 'staging') {
  920. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  921. throw new Zend_Service_WindowsAzure_Management_Exception('Deployment slot should be production|staging.');
  922. }
  923. $operationUrl = self::OP_HOSTED_SERVICES . '/' . $serviceName . '/deploymentslots/' . $deploymentSlot;
  924. return $this->_getDeployment($operationUrl);
  925. }
  926. /**
  927. * The Get Deployment operation returns configuration information, status,
  928. * and system properties for the specified deployment.
  929. *
  930. * @param string $serviceName The service name
  931. * @param string $deploymentId The deployment ID as listed on the Windows Azure management portal
  932. * @return Zend_Service_WindowsAzure_Management_DeploymentInstance
  933. * @throws Zend_Service_WindowsAzure_Management_Exception
  934. */
  935. public function getDeploymentByDeploymentId($serviceName, $deploymentId)
  936. {
  937. if ($serviceName == '' || is_null($serviceName)) {
  938. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  939. throw new Zend_Service_WindowsAzure_Management_Exception('Service name should be specified.');
  940. }
  941. if ($deploymentId == '' || is_null($deploymentId)) {
  942. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  943. throw new Zend_Service_WindowsAzure_Management_Exception('Deployment ID should be specified.');
  944. }
  945. $operationUrl = self::OP_HOSTED_SERVICES . '/' . $serviceName . '/deployments/' . $deploymentId;
  946. return $this->_getDeployment($operationUrl);
  947. }
  948. /**
  949. * The Get Deployment operation returns configuration information, status,
  950. * and system properties for the specified deployment.
  951. *
  952. * @param string $operationUrl The operation url
  953. * @return Zend_Service_WindowsAzure_Management_DeploymentInstance
  954. * @throws Zend_Service_WindowsAzure_Management_Exception
  955. */
  956. protected function _getDeployment($operationUrl)
  957. {
  958. $response = $this->_performRequest($operationUrl);
  959. if ($response->isSuccessful()) {
  960. $xmlService = $this->_parseResponse($response);
  961. return $this->_convertXmlElementToDeploymentInstance($xmlService);
  962. } else {
  963. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  964. throw new Zend_Service_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
  965. }
  966. }
  967. /**
  968. * The Swap Deployment operation initiates a virtual IP swap between
  969. * the staging and production deployment environments for a service.
  970. * If the service is currently running in the staging environment,
  971. * it will be swapped to the production environment. If it is running
  972. * in the production environment, it will be swapped to staging.
  973. *
  974. * @param string $serviceName The service name.
  975. * @param string $productionDeploymentName The name of the production deployment.
  976. * @param string $sourceDeploymentName The name of the source deployment.
  977. * @throws Zend_Service_WindowsAzure_Management_Exception
  978. */
  979. public function swapDeployment($serviceName, $productionDeploymentName, $sourceDeploymentName)
  980. {
  981. if ($serviceName == '' || is_null($serviceName)) {
  982. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  983. throw new Zend_Service_WindowsAzure_Management_Exception('Service name should be specified.');
  984. }
  985. if ($productionDeploymentName == '' || is_null($productionDeploymentName)) {
  986. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  987. throw new Zend_Service_WindowsAzure_Management_Exception('Production Deployment ID should be specified.');
  988. }
  989. if ($sourceDeploymentName == '' || is_null($sourceDeploymentName)) {
  990. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  991. throw new Zend_Service_WindowsAzure_Management_Exception('Source Deployment ID should be specified.');
  992. }
  993. $operationUrl = self::OP_HOSTED_SERVICES . '/' . $serviceName;
  994. $response = $this->_performRequest($operationUrl, '',
  995. Zend_Http_Client::POST,
  996. array('Content-Type' => 'application/xml; charset=utf-8'),
  997. '<Swap xmlns="http://schemas.microsoft.com/windowsazure"><Production>' . $productionDeploymentName . '</Production><SourceDeployment>' . $sourceDeploymentName . '</SourceDeployment></Swap>');
  998. if (!$response->isSuccessful()) {
  999. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  1000. throw new Zend_Service_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
  1001. }
  1002. }
  1003. /**
  1004. * The Delete Deployment operation deletes the specified deployment.
  1005. *
  1006. * @param string $serviceName The service name
  1007. * @param string $deploymentSlot The deployment slot (production or staging)
  1008. * @throws Zend_Service_WindowsAzure_Management_Exception
  1009. */
  1010. public function deleteDeploymentBySlot($serviceName, $deploymentSlot)
  1011. {
  1012. if ($serviceName == '' || is_null($serviceName)) {
  1013. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  1014. throw new Zend_Service_WindowsAzure_Management_Exception('Service name should be specified.');
  1015. }
  1016. $deploymentSlot = strtolower($deploymentSlot);
  1017. if ($deploymentSlot != 'production' && $deploymentSlot != 'staging') {
  1018. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  1019. throw new Zend_Service_WindowsAzure_Management_Exception('Deployment slot should be production|staging.');
  1020. }
  1021. $operationUrl = self::OP_HOSTED_SERVICES . '/' . $serviceName . '/deploymentslots/' . $deploymentSlot;
  1022. return $this->_deleteDeployment($operationUrl);
  1023. }
  1024. /**
  1025. * The Delete Deployment operation deletes the specified deployment.
  1026. *
  1027. * @param string $serviceName The service name
  1028. * @param string $deploymentId The deployment ID as listed on the Windows Azure management portal
  1029. * @throws Zend_Service_WindowsAzure_Management_Exception
  1030. */
  1031. public function deleteDeploymentByDeploymentId($serviceName, $deploymentId)
  1032. {
  1033. if ($serviceName == '' || is_null($serviceName)) {
  1034. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  1035. throw new Zend_Service_WindowsAzure_Management_Exception('Service name should be specified.');
  1036. }
  1037. if ($deploymentId == '' || is_null($deploymentId)) {
  1038. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  1039. throw new Zend_Service_WindowsAzure_Management_Exception('Deployment ID should be specified.');
  1040. }
  1041. $operationUrl = self::OP_HOSTED_SERVICES . '/' . $serviceName . '/deployments/' . $deploymentId;
  1042. return $this->_deleteDeployment($operationUrl);
  1043. }
  1044. /**
  1045. * The Delete Deployment operation deletes the specified deployment.
  1046. *
  1047. * @param string $operationUrl The operation url
  1048. * @throws Zend_Service_WindowsAzure_Management_Exception
  1049. */
  1050. protected function _deleteDeployment($operationUrl)
  1051. {
  1052. $response = $this->_performRequest($operationUrl, '', Zend_Http_Client::DELETE);
  1053. if (!$response->isSuccessful()) {
  1054. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  1055. throw new Zend_Service_WindowsAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
  1056. }
  1057. }
  1058. /**
  1059. * The Update Deployment Status operation initiates a change in deployment status.
  1060. *
  1061. * @param string $serviceName The service name
  1062. * @param string $deploymentSlot The deployment slot (production or staging)
  1063. * @param string $status The deployment status (running|suspended)
  1064. * @throws Zend_Service_WindowsAzure_Management_Exception
  1065. */
  1066. public function updateDeploymentStatusBySlot($serviceName, $deploymentSlot, $status = 'running')
  1067. {
  1068. if ($serviceName == '' || is_null($serviceName)) {
  1069. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  1070. throw new Zend_Service_WindowsAzure_Management_Exception('Service name should be specified.');
  1071. }
  1072. $deploymentSlot = strtolower($deploymentSlot);
  1073. if ($deploymentSlot != 'production' && $deploymentSlot != 'staging') {
  1074. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  1075. throw new Zend_Service_WindowsAzure_Management_Exception('Deployment slot should be production|staging.');
  1076. }
  1077. $status = strtolower($status);
  1078. if ($status != 'running' && $status != 'suspended') {
  1079. require_once 'Zend/Service/WindowsAzure/Management/Exception.php';
  1080. throw new Zend_Service_WindowsAzure_Management_Exception('Status should be running|suspended.');
  1081. }
  1082. $operationUrl = self::OP_HOSTED_SERVICES . '/' . $serviceName . '/deploymentslots/' . $deploymentSlot;
  1083. return $this->_updateDeploymentStatus($operationUrl, $status);
  1084. }
  1085. /**
  1086. * The Update…

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