PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/Client/CloudSubscription.php

http://github.com/WindowsAzure/azure-sdk-for-php
PHP | 169 lines | 77 code | 19 blank | 73 comment | 10 complexity | e88f1aa804d3d2e5257fbf506ac4561b MD5 | raw file
  1. <?php
  2. /**
  3. * LICENSE: Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. * http://www.apache.org/licenses/LICENSE-2.0.
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. *
  14. * PHP version 5
  15. *
  16. * @category Microsoft
  17. *
  18. * @author Azure PHP SDK <azurephpsdk@microsoft.com>
  19. * @copyright 2012 Microsoft Corporation
  20. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  21. *
  22. * @link https://github.com/windowsazure/azure-sdk-for-php
  23. */
  24. namespace Client;
  25. use WindowsAzure\Common\ServicesBuilder;
  26. use WindowsAzure\ServiceManagement\Models\AsynchronousOperationResult;
  27. use WindowsAzure\ServiceManagement\Models\Location;
  28. use WindowsAzure\ServiceManagement\Models\OperationStatus;
  29. use WindowsAzure\ServiceManagement\Models\CreateServiceOptions;
  30. /**
  31. * Encapsulates Windows Azure subscription basic operations.
  32. *
  33. * @category Microsoft
  34. *
  35. * @author Azure PHP SDK <azurephpsdk@microsoft.com>
  36. * @copyright 2012 Microsoft Corporation
  37. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  38. *
  39. * @version Release: 0.5.0_2016-11
  40. *
  41. * @link https://github.com/windowsazure/azure-sdk-for-php
  42. */
  43. class CloudSubscription
  44. {
  45. /**
  46. * @var IServiceManagementProxy
  47. */
  48. private $_proxy;
  49. const SYNCHRONOUS = 'Sync';
  50. const ASYNCHRONOUS = 'Async';
  51. /**
  52. * Initializes new CloudSubscription object using the provided parameters.
  53. *
  54. * @param string $subscriptionId The Windows Azure subscription id.
  55. * @param string $certificatePath The registered certificate.
  56. */
  57. public function __construct($subscriptionId, $certificatePath)
  58. {
  59. $connectionString = "SubscriptionID=$subscriptionId;CertificatePath=$certificatePath";
  60. $this->_proxy = ServicesBuilder::getInstance()->createServiceManagementService($connectionString);
  61. }
  62. /**
  63. * Checks if a storage service exists or not.
  64. *
  65. * @param string $name Storage service name.
  66. *
  67. * @return bool
  68. */
  69. public function storageServiceExists($name)
  70. {
  71. $result = $this->_proxy->listStorageServices();
  72. $storageServices = $result->getStorageServices();
  73. foreach ($storageServices as $storageService) {
  74. if ($storageService->getName() == $name) {
  75. return true;
  76. }
  77. }
  78. return false;
  79. }
  80. /**
  81. * Creates a storage service if it does not exist and waits until it is created.
  82. *
  83. * @param string $name The storage service name.
  84. * @param string $execType The execution type for this call.
  85. * @param Location $location The storage service location. By default East US.
  86. *
  87. * @return CloudStorageService
  88. */
  89. public function createStorageService(
  90. $name,
  91. $execType = self:: SYNCHRONOUS,
  92. $location = 'East US'
  93. ) {
  94. $cloudStorageService = null;
  95. if (!$this->storageServiceExists($name)) {
  96. $options = new CreateServiceOptions();
  97. $options->setLocation($location);
  98. $result = $this->_proxy->createStorageService(
  99. $name,
  100. base64_encode($name),
  101. $options
  102. );
  103. if ($execType == self::SYNCHRONOUS) {
  104. $this->_blockUntilAsyncFinish($result);
  105. }
  106. $newStorageService = true;
  107. }
  108. $keys = $this->_proxy->getStorageServiceKeys($name);
  109. $properties = $this->_proxy->getStorageServiceProperties($name);
  110. $cloudStorageService = new CloudStorageService(
  111. $name,
  112. $keys->getPrimary(),
  113. $properties->getStorageService()->getBlobEndpointUri(),
  114. $properties->getStorageService()->getQueueEndpointUri(),
  115. $properties->getStorageService()->getTableEndpointUri()
  116. );
  117. return $cloudStorageService;
  118. }
  119. /**
  120. * Blocks asynchronous operation until it succeeds. Throws exception if the
  121. * operation failed.
  122. *
  123. * @param AsynchronousOperationResult $requestInfo The asynchronous operation request AsynchronousOperationResult.
  124. *
  125. * @throws \WindowsAzure\Common\ServiceException
  126. */
  127. private function _blockUntilAsyncFinish(AsynchronousOperationResult $requestInfo)
  128. {
  129. $status = null;
  130. do {
  131. sleep(5);
  132. $result = $this->_proxy->getOperationStatus($requestInfo);
  133. $status = $result->getStatus();
  134. } while (OperationStatus::IN_PROGRESS == $status);
  135. if (OperationStatus::SUCCEEDED != $status) {
  136. throw $result->getError();
  137. }
  138. }
  139. /**
  140. * Deletes a storage service from subscription.
  141. *
  142. * @param string $name The storage service name.
  143. */
  144. public function deleteStorageService($name)
  145. {
  146. if ($this->storageServiceExists($name)) {
  147. $this->_proxy->deleteStorageService($name);
  148. }
  149. }
  150. }