PageRenderTime 48ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/backwpup/sdk/WindowsAzure/ServiceManagement/ServiceManagementRestProxy.php

https://bitbucket.org/cesarmedrano/cesarmedrano
PHP | 1298 lines | 671 code | 129 blank | 498 comment | 13 complexity | 39f5b17f4481ec08911a5bbc7acfb426 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. * @package WindowsAzure\ServiceManagement
  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. * @link https://github.com/windowsazure/azure-sdk-for-php
  22. */
  23. namespace WindowsAzure\ServiceManagement;
  24. use WindowsAzure\Common\Internal\Resources;
  25. use WindowsAzure\Common\Internal\Validate;
  26. use WindowsAzure\Common\Internal\Utilities;
  27. use WindowsAzure\Common\Internal\RestProxy;
  28. use WindowsAzure\Common\Internal\Http\HttpCallContext;
  29. use WindowsAzure\Common\Internal\Serialization\XmlSerializer;
  30. use WindowsAzure\ServiceManagement\Internal\IServiceManagement;
  31. use WindowsAzure\ServiceManagement\Models\CreateAffinityGroupOptions;
  32. use WindowsAzure\ServiceManagement\Models\AffinityGroup;
  33. use WindowsAzure\ServiceManagement\Models\ListAffinityGroupsResult;
  34. use WindowsAzure\ServiceManagement\Models\GetAffinityGroupPropertiesResult;
  35. use WindowsAzure\ServiceManagement\Models\ListLocationsResult;
  36. use WindowsAzure\ServiceManagement\Models\StorageService;
  37. use WindowsAzure\ServiceManagement\Models\ListStorageServicesResult;
  38. use WindowsAzure\ServiceManagement\Models\GetOperationStatusResult;
  39. use WindowsAzure\ServiceManagement\Models\AsynchronousOperationResult;
  40. use WindowsAzure\ServiceManagement\Models\UpdateServiceOptions;
  41. use WindowsAzure\ServiceManagement\Models\GetStorageServicePropertiesResult;
  42. use WindowsAzure\ServiceManagement\Models\GetStorageServiceKeysResult;
  43. use WindowsAzure\ServiceManagement\Models\ListHostedServicesResult;
  44. use WindowsAzure\ServiceManagement\Models\HostedService;
  45. use WindowsAzure\ServiceManagement\Models\GetHostedServicePropertiesOptions;
  46. use WindowsAzure\ServiceManagement\Models\GetHostedServicePropertiesResult;
  47. use WindowsAzure\ServiceManagement\Models\DeploymentSlot;
  48. use WindowsAzure\ServiceManagement\Models\CreateDeploymentOptions;
  49. use WindowsAzure\ServiceManagement\Models\GetDeploymentResult;
  50. use WindowsAzure\ServiceManagement\Models\DeploymentStatus;
  51. use WindowsAzure\ServiceManagement\Models\Mode;
  52. /**
  53. * This class constructs HTTP requests and receive HTTP responses for service
  54. * management service layer.
  55. *
  56. * @category Microsoft
  57. * @package WindowsAzure\ServiceManagement
  58. * @author Azure PHP SDK <azurephpsdk@microsoft.com>
  59. * @copyright 2012 Microsoft Corporation
  60. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  61. * @version Release: @package_version@
  62. * @link https://github.com/windowsazure/azure-sdk-for-php
  63. */
  64. class ServiceManagementRestProxy extends RestProxy
  65. implements IServiceManagement
  66. {
  67. /**
  68. * @var string
  69. */
  70. private $_subscriptionId;
  71. /**
  72. * Sends an order request for the specified role instance.
  73. *
  74. * @param string $name The hosted service name.
  75. * @param string $roleName The role instance name.
  76. * @param GetDeploymentOptions $options The optional parameters.
  77. * @param string $order The order name which is used as value
  78. * for query parameter 'comp'.
  79. *
  80. * @return AsynchronousOperationResult
  81. */
  82. private function _sendRoleInstanceOrder($name, $roleName, $options, $order)
  83. {
  84. Validate::isString($name, 'name');
  85. Validate::notNullOrEmpty($name, 'name');
  86. Validate::isString($roleName, 'roleName');
  87. Validate::notNullOrEmpty($roleName, 'roleName');
  88. Validate::notNullOrEmpty($options, 'options');
  89. $context = new HttpCallContext();
  90. $context->setMethod(Resources::HTTP_POST);
  91. $context->setPath($this->_getRoleInstancePath($name, $options, $roleName));
  92. $context->addStatusCode(Resources::STATUS_ACCEPTED);
  93. $context->addQueryParameter(Resources::QP_COMP, $order);
  94. $context->addHeader(Resources::CONTENT_TYPE, Resources::XML_CONTENT_TYPE);
  95. $context->addHeader(Resources::CONTENT_LENGTH_NO_SPACE, 0);
  96. $response = $this->sendContext($context);
  97. return AsynchronousOperationResult::create($response->getHeader());
  98. }
  99. /**
  100. * Constructs URI path for given service management resource.
  101. *
  102. * @param string $serviceManagementResource The resource name.
  103. * @param string $name The service name.
  104. *
  105. * @return string
  106. */
  107. private function _getPath($serviceManagementResource, $name)
  108. {
  109. $path = $this->_subscriptionId . '/' . $serviceManagementResource;
  110. if (!is_null($name)) {
  111. $path .= '/' . $name;
  112. }
  113. return $path;
  114. }
  115. /**
  116. * Constructs URI path for locations.
  117. *
  118. * @return string
  119. */
  120. private function _getLocationPath()
  121. {
  122. return $this->_getPath('locations', null);
  123. }
  124. /**
  125. * Constructs URI path for affinity group.
  126. *
  127. * @param string $name The affinity group name.
  128. *
  129. * @return string
  130. */
  131. private function _getAffinityGroupPath($name = null)
  132. {
  133. return $this->_getPath('affinitygroups', $name);
  134. }
  135. /**
  136. * Constructs URI path for storage service.
  137. *
  138. * @param string $name The storage service name.
  139. *
  140. * @return string
  141. */
  142. private function _getStorageServicePath($name = null)
  143. {
  144. return $this->_getPath('services/storageservices', $name);
  145. }
  146. /**
  147. * Constructs URI path for hosted service.
  148. *
  149. * @param string $name The hosted service name.
  150. *
  151. * @return string
  152. */
  153. private function _getHostedServicePath($name = null)
  154. {
  155. return $this->_getPath('services/hostedservices', $name);
  156. }
  157. /**
  158. * Constructs URI path for deployment slot.
  159. *
  160. * @param string $name The hosted service name.
  161. * @param string $slot The deployment slot name.
  162. *
  163. * @return string
  164. */
  165. private function _getDeploymentPathUsingSlot($name, $slot)
  166. {
  167. $path = "services/hostedservices/$name/deploymentslots";
  168. return $this->_getPath($path, $slot);
  169. }
  170. /**
  171. * Constructs URI path for deployment slot.
  172. *
  173. * @param string $name The hosted service name.
  174. * @param string $deploymentName The deployment slot name.
  175. *
  176. * @return string
  177. */
  178. private function _getDeploymentPathUsingName($name, $deploymentName)
  179. {
  180. $path = "services/hostedservices/$name/deployments";
  181. return $this->_getPath($path, $deploymentName);
  182. }
  183. private function _getRoleInstancePath($name, $options, $roleName)
  184. {
  185. $path = $this->_getDeploymentPath($name, $options) . '/roleinstances';
  186. return "$path/$roleName";
  187. }
  188. /**
  189. * Gets the deployment URI path using the slot or name.
  190. *
  191. * @param string $name The hosted service name.
  192. * @param GetDeploymentOptions $options The optional parameters.
  193. *
  194. * @return string
  195. */
  196. private function _getDeploymentPath($name, $options)
  197. {
  198. $slot = $options->getSlot();
  199. $deploymentName = $options->getDeploymentName();
  200. $path = null;
  201. Validate::isTrue(
  202. !empty($slot) || !empty($deploymentName),
  203. Resources::INVALID_DEPLOYMENT_LOCATOR_MSG
  204. );
  205. if (!empty($slot)) {
  206. $path = $this->_getDeploymentPathUsingSlot($name, $slot);
  207. } else {
  208. $path = $this->_getDeploymentPathUsingName($name, $deploymentName);
  209. }
  210. return $path;
  211. }
  212. /**
  213. * Constructs URI path for storage service key.
  214. *
  215. * @param string $name The storage service name.
  216. *
  217. * @return string
  218. */
  219. private function _getStorageServiceKeysPath($name = null)
  220. {
  221. return $this->_getPath('services/storageservices', $name) . '/keys';
  222. }
  223. /**
  224. * Constructs URI path for operations.
  225. *
  226. * @param string $name The operation resource name.
  227. *
  228. * @return string
  229. */
  230. private function _getOperationPath($name = null)
  231. {
  232. return $this->_getPath('operations', $name);
  233. }
  234. /**
  235. * Constructs request XML including windows azure XML namesoace.
  236. *
  237. * @param array $xmlElements The XML elements associated with their values.
  238. * @param string $root The XML root name.
  239. *
  240. * @return string
  241. */
  242. private function _createRequestXml($xmlElements, $root)
  243. {
  244. $requestArray = array(
  245. Resources::XTAG_NAMESPACE => array(Resources::WA_XML_NAMESPACE => null)
  246. );
  247. foreach ($xmlElements as $tagName => $value) {
  248. if (!empty($value)) {
  249. $requestArray[$tagName] = $value;
  250. }
  251. }
  252. $properties = array(XmlSerializer::ROOT_NAME => $root);
  253. return $this->dataSerializer->serialize($requestArray, $properties);
  254. }
  255. /**
  256. * Initializes new ServiceManagementRestProxy object.
  257. *
  258. * @param IHttpClient $channel The HTTP channel.
  259. * @param string $subscriptionId The user subscription id.
  260. * @param string $uri The service URI.
  261. * @param ISerializer $dataSerializer The data serializer.
  262. */
  263. public function __construct($channel, $subscriptionId, $uri, $dataSerializer)
  264. {
  265. parent::__construct(
  266. $channel,
  267. $dataSerializer,
  268. $uri
  269. );
  270. $this->_subscriptionId = $subscriptionId;
  271. }
  272. /**
  273. * Lists the storage accounts available under the current subscription.
  274. *
  275. * @return ListStorageServicesResult
  276. *
  277. * @see http://msdn.microsoft.com/en-us/library/windowsazure/ee460787.aspx
  278. */
  279. public function listStorageServices()
  280. {
  281. $context = new HttpCallContext();
  282. $context->setMethod(Resources::HTTP_GET);
  283. $context->setPath($this->_getStorageServicePath());
  284. $context->addStatusCode(Resources::STATUS_OK);
  285. $response = $this->sendContext($context);
  286. $serialized = $this->dataSerializer->unserialize($response->getBody());
  287. return ListStorageServicesResult::create($serialized);
  288. }
  289. /**
  290. * Returns the system properties for the specified storage account.
  291. *
  292. * These properties include: the address, description, and label of the storage
  293. * account; and the name of the affinity group to which the service belongs,
  294. * or its geo-location if it is not part of an affinity group.
  295. *
  296. * @param string $name The storage account name.
  297. *
  298. * @return GetStorageServicePropertiesResult
  299. *
  300. * @see http://msdn.microsoft.com/en-us/library/windowsazure/ee460802.aspx
  301. */
  302. public function getStorageServiceProperties($name)
  303. {
  304. Validate::isString($name, 'name');
  305. Validate::notNullOrEmpty($name, 'name');
  306. $context = new HttpCallContext();
  307. $context->setMethod(Resources::HTTP_GET);
  308. $context->setPath($this->_getStorageServicePath($name));
  309. $context->addStatusCode(Resources::STATUS_OK);
  310. $response = $this->sendContext($context);
  311. $parsed = $this->dataSerializer->unserialize($response->getBody());
  312. return GetStorageServicePropertiesResult::create($parsed);
  313. }
  314. /**
  315. * Returns the primary and secondary access keys for the specified storage
  316. * account.
  317. *
  318. * @param string $name The storage account name.
  319. *
  320. * @return GetStorageServiceKeysResult
  321. *
  322. * @see http://msdn.microsoft.com/en-us/library/windowsazure/ee460785.aspx
  323. */
  324. public function getStorageServiceKeys($name)
  325. {
  326. Validate::isString($name, 'name');
  327. Validate::notNullOrEmpty($name, 'name');
  328. $context = new HttpCallContext();
  329. $context->setMethod(Resources::HTTP_GET);
  330. $context->setPath($this->_getStorageServiceKeysPath($name));
  331. $context->addStatusCode(Resources::STATUS_OK);
  332. $response = $this->sendContext($context);
  333. $parsed = $this->dataSerializer->unserialize($response->getBody());
  334. return GetStorageServiceKeysResult::create($parsed);
  335. }
  336. /**
  337. * Regenerates the primary or secondary access key for the specified storage
  338. * account.
  339. *
  340. * @param string $name The storage account name.
  341. * @param string $keyType Specifies which key to regenerate.
  342. *
  343. * @return GetStorageServiceKeysResult
  344. *
  345. * @see http://msdn.microsoft.com/en-us/library/windowsazure/ee460795.aspx
  346. */
  347. public function regenerateStorageServiceKeys($name, $keyType)
  348. {
  349. Validate::isString($name, 'name');
  350. Validate::notNullOrEmpty($name, 'name');
  351. Validate::isString($keyType, 'keyType');
  352. Validate::notNullOrEmpty($keyType, 'keyType');
  353. $body = $this->_createRequestXml(
  354. array(Resources::XTAG_KEY_TYPE => $keyType),
  355. Resources::XTAG_REGENERATE_KEYS
  356. );
  357. $context = new HttpCallContext();
  358. $context->setMethod(Resources::HTTP_POST);
  359. $context->setPath($this->_getStorageServiceKeysPath($name));
  360. $context->addStatusCode(Resources::STATUS_OK);
  361. $context->addQueryParameter(Resources::QP_ACTION, Resources::QPV_REGENERATE);
  362. $context->setBody($body);
  363. $context->addHeader(
  364. Resources::CONTENT_TYPE,
  365. Resources::XML_CONTENT_TYPE
  366. );
  367. $response = $this->sendContext($context);
  368. $parsed = $this->dataSerializer->unserialize($response->getBody());
  369. return GetStorageServiceKeysResult::create($parsed);
  370. }
  371. /**
  372. * Creates a new storage account in Windows Azure.
  373. *
  374. * In the optional parameters either location or affinity group must be provided.
  375. * Because Create Storage Account is an asynchronous operation, it always returns
  376. * status code 202 (Accepted). To determine the status code for the operation
  377. * once it is complete, call getOperationStatus API. The status code is embedded
  378. * in the response for this operation; if successful, it will be
  379. * status code 200 (OK).
  380. *
  381. * @param string $name The storage account name.
  382. * @param string $label The name for the storage account
  383. * specified as a base64-encoded string. The name may be up to 100 characters
  384. * in length. The name can be used identify the storage account for your tracking
  385. * purposes.
  386. * @param CreateServiceOptions $options The optional parameters.
  387. *
  388. * @return AsynchronousOperationResult
  389. *
  390. * @see http://msdn.microsoft.com/en-us/library/windowsazure/hh264518.aspx
  391. */
  392. public function createStorageService($name, $label, $options)
  393. {
  394. Validate::isString($name, 'name');
  395. Validate::notNullOrEmpty($name, 'name');
  396. Validate::isString($label, 'label');
  397. Validate::notNullOrEmpty($label, 'label');
  398. Validate::notNullOrEmpty($options, 'options');
  399. $affinityGroup = $options->getAffinityGroup();
  400. $location = $options->getLocation();
  401. Validate::isTrue(
  402. !empty($location) || !empty($affinityGroup),
  403. Resources::INVALID_CREATE_SERVICE_OPTIONS_MSG
  404. );
  405. $storageService = new StorageService();
  406. $storageService->setName($name);
  407. $storageService->setLabel($label);
  408. $storageService->setLocation($options->getLocation());
  409. $storageService->setAffinityGroup($options->getAffinityGroup());
  410. $storageService->setDescription($options->getDescription());
  411. $storageService->addSerializationProperty(
  412. XmlSerializer::ROOT_NAME,
  413. Resources::XTAG_CREATE_STORAGE_SERVICE_INPUT
  414. );
  415. $context = new HttpCallContext();
  416. $context->setMethod(Resources::HTTP_POST);
  417. $context->setPath($this->_getStorageServicePath());
  418. $context->addStatusCode(Resources::STATUS_ACCEPTED);
  419. $context->setBody($storageService->serialize($this->dataSerializer));
  420. $context->addHeader(
  421. Resources::CONTENT_TYPE,
  422. Resources::XML_CONTENT_TYPE
  423. );
  424. $response = $this->sendContext($context);
  425. return AsynchronousOperationResult::create($response->getHeader());
  426. }
  427. /**
  428. * Deletes the specified storage account from Windows Azure.
  429. *
  430. * @param string $name The storage account name.
  431. *
  432. * @return none
  433. *
  434. * @see http://msdn.microsoft.com/en-us/library/windowsazure/hh264517.aspx
  435. */
  436. public function deleteStorageService($name)
  437. {
  438. Validate::isString($name, 'name');
  439. Validate::notNullOrEmpty($name, 'name');
  440. $context = new HttpCallContext();
  441. $context->setMethod(Resources::HTTP_DELETE);
  442. $context->setPath($this->_getStorageServicePath($name));
  443. $context->addStatusCode(Resources::STATUS_OK);
  444. $this->sendContext($context);
  445. }
  446. /**
  447. * Updates the label and/or the description for a storage account in Windows
  448. * Azure.
  449. *
  450. * @param string $name The storage account name.
  451. * @param UpdateServiceOptions $options The optional parameters.
  452. *
  453. * @return none
  454. *
  455. * @see http://msdn.microsoft.com/en-us/library/windowsazure/hh264516.aspx
  456. */
  457. public function updateStorageService($name, $options)
  458. {
  459. Validate::isString($name, 'name');
  460. Validate::notNullOrEmpty($name, 'name');
  461. $label = $options->getLabel();
  462. $description = $options->getDescription();
  463. Validate::isTrue(
  464. !empty($label) || !empty($description),
  465. Resources::INVALID_UPDATE_SERVICE_OPTIONS_MSG
  466. );
  467. $storageService = new StorageService();
  468. $storageService->setLabel($options->getLabel());
  469. $storageService->setDescription($options->getDescription());
  470. $storageService->addSerializationProperty(
  471. XmlSerializer::ROOT_NAME,
  472. Resources::XTAG_UPDATE_STORAGE_SERVICE_INPUT
  473. );
  474. $context = new HttpCallContext();
  475. $context->setMethod(Resources::HTTP_PUT);
  476. $context->setPath($this->_getStorageServicePath($name));
  477. $context->addStatusCode(Resources::STATUS_OK);
  478. $context->setBody($storageService->serialize($this->dataSerializer));
  479. $context->addHeader(
  480. Resources::CONTENT_TYPE,
  481. Resources::XML_CONTENT_TYPE
  482. );
  483. $this->sendContext($context);
  484. }
  485. /**
  486. * Lists the affinity groups associated with the specified subscription.
  487. *
  488. * @return ListAffinityGroupsResult
  489. *
  490. * @see http://msdn.microsoft.com/en-us/library/windowsazure/ee460797.aspx
  491. */
  492. public function listAffinityGroups()
  493. {
  494. $context = new HttpCallContext();
  495. $context->setMethod(Resources::HTTP_GET);
  496. $context->setPath($this->_getAffinityGroupPath());
  497. $context->addStatusCode(Resources::STATUS_OK);
  498. $response = $this->sendContext($context);
  499. $serialized = $this->dataSerializer->unserialize($response->getBody());
  500. return ListAffinityGroupsResult::create($serialized);
  501. }
  502. /**
  503. * Creates a new affinity group for the specified subscription.
  504. *
  505. * @param string $name The affinity group name.
  506. * @param string $label The base-64 encoded name for the
  507. * affinity group. The name can be up to 100 characters in length.
  508. * @param string $location The data center location where the
  509. * affinity group will be created. To list available locations, use the
  510. * listLocations API.
  511. * @param CreateAffinityGroupOptions $options The optional parameters.
  512. *
  513. * @return none
  514. *
  515. * @see http://msdn.microsoft.com/en-us/library/windowsazure/gg715317.aspx
  516. */
  517. public function createAffinityGroup($name, $label, $location, $options = null)
  518. {
  519. Validate::isString($name, 'name');
  520. Validate::notNullOrEmpty($name, 'name');
  521. Validate::isString($label, 'label');
  522. Validate::notNullOrEmpty($label, 'label');
  523. Validate::isString($location, 'location');
  524. Validate::notNullOrEmpty($location, 'location');
  525. if (is_null($options)) {
  526. $options = new CreateAffinityGroupOptions();
  527. }
  528. $affinityGroup = new AffinityGroup();
  529. $affinityGroup->setName($name);
  530. $affinityGroup->setLabel($label);
  531. $affinityGroup->setLocation($location);
  532. $affinityGroup->setDescription($options->getDescription());
  533. $affinityGroup->addSerializationProperty(
  534. XmlSerializer::ROOT_NAME,
  535. Resources::XTAG_CREATE_AFFINITY_GROUP
  536. );
  537. $context = new HttpCallContext();
  538. $context->setMethod(Resources::HTTP_POST);
  539. $context->setPath($this->_getAffinityGroupPath());
  540. $context->addStatusCode(Resources::STATUS_CREATED);
  541. $context->setBody($affinityGroup->serialize($this->dataSerializer));
  542. $context->addHeader(
  543. Resources::CONTENT_TYPE,
  544. Resources::XML_CONTENT_TYPE
  545. );
  546. $this->sendContext($context);
  547. }
  548. /**
  549. * Deletes an affinity group in the specified subscription.
  550. *
  551. * @param string $name The affinity group name.
  552. *
  553. * @return none
  554. *
  555. * @see http://msdn.microsoft.com/en-us/library/windowsazure/gg715314.aspx
  556. */
  557. public function deleteAffinityGroup($name)
  558. {
  559. Validate::isString($name, 'name');
  560. Validate::notNullOrEmpty($name, 'name');
  561. $context = new HttpCallContext();
  562. $context->setMethod(Resources::HTTP_DELETE);
  563. $context->setPath($this->_getAffinityGroupPath($name));
  564. $context->addStatusCode(Resources::STATUS_OK);
  565. $this->sendContext($context);
  566. }
  567. /**
  568. * Updates the label and/or the description for an affinity group for the
  569. * specified subscription.
  570. *
  571. * @param string $name The affinity group name.
  572. * @param string $label The affinity group label.
  573. * @param CreateAffinityGroupOptions $options The optional parameters.
  574. *
  575. * @return none
  576. *
  577. * @see http://msdn.microsoft.com/en-us/library/windowsazure/gg715316.aspx
  578. */
  579. public function updateAffinityGroup($name, $label, $options = null)
  580. {
  581. Validate::isString($name, 'name');
  582. Validate::notNullOrEmpty($name, 'name');
  583. Validate::isString($label, 'label');
  584. Validate::notNullOrEmpty($label, 'label');
  585. if (is_null($options)) {
  586. $options = new CreateAffinityGroupOptions();
  587. }
  588. $affinityGroup = new AffinityGroup();
  589. $affinityGroup->setLabel($label);
  590. $affinityGroup->setDescription($options->getDescription());
  591. $affinityGroup->addSerializationProperty(
  592. XmlSerializer::ROOT_NAME,
  593. Resources::XTAG_UPDATE_AFFINITY_GROUP
  594. );
  595. $context = new HttpCallContext();
  596. $context->setMethod(Resources::HTTP_PUT);
  597. $context->setPath($this->_getAffinityGroupPath($name));
  598. $context->addStatusCode(Resources::STATUS_OK);
  599. $context->setBody($affinityGroup->serialize($this->dataSerializer));
  600. $context->addHeader(
  601. Resources::CONTENT_TYPE,
  602. Resources::XML_CONTENT_TYPE
  603. );
  604. $this->sendContext($context);
  605. }
  606. /**
  607. * Returns the system properties associated with the specified affinity group.
  608. *
  609. * @param string $name The affinity group name.
  610. *
  611. * @return GetAffinityGroupPropertiesResult
  612. *
  613. * @see http://msdn.microsoft.com/en-us/library/windowsazure/ee460789.aspx
  614. */
  615. public function getAffinityGroupProperties($name)
  616. {
  617. Validate::isString($name, 'name');
  618. Validate::notNullOrEmpty($name, 'name');
  619. $context = new HttpCallContext();
  620. $context->setMethod(Resources::HTTP_GET);
  621. $context->setPath($this->_getAffinityGroupPath($name));
  622. $context->addStatusCode(Resources::STATUS_OK);
  623. $response = $this->sendContext($context);
  624. $parsed = $this->dataSerializer->unserialize($response->getBody());
  625. return GetAffinityGroupPropertiesResult::create($parsed);
  626. }
  627. /**
  628. * Lists all of the data center locations that are valid for your subscription.
  629. *
  630. * @return ListLocationsResult
  631. *
  632. * @see http://msdn.microsoft.com/en-us/library/windowsazure/gg441293.aspx
  633. */
  634. public function listLocations()
  635. {
  636. $context = new HttpCallContext();
  637. $context->setMethod(Resources::HTTP_GET);
  638. $context->setPath($this->_getLocationPath());
  639. $context->addStatusCode(Resources::STATUS_OK);
  640. $response = $this->sendContext($context);
  641. $serialized = $this->dataSerializer->unserialize($response->getBody());
  642. return ListLocationsResult::create($serialized);
  643. }
  644. /**
  645. * Returns the status of the specified operation. After calling an asynchronous
  646. * operation, you can call Get Operation Status to determine whether the
  647. * operation has succeeded, failed, or is still in progress.
  648. *
  649. * @param AsynchronousOperationResult $requestInfo The request information for
  650. * the REST call you want to track.
  651. *
  652. * @return GetOperationStatusResult
  653. *
  654. * @see http://msdn.microsoft.com/en-us/library/windowsazure/ee460783.aspx
  655. */
  656. public function getOperationStatus($requestInfo)
  657. {
  658. Validate::notNullOrEmpty($requestInfo, 'requestInfo');
  659. Validate::notNullOrEmpty($requestInfo->getrequestId(), 'requestId');
  660. $context = new HttpCallContext();
  661. $context->setMethod(Resources::HTTP_GET);
  662. $context->setPath($this->_getOperationPath($requestInfo->getrequestId()));
  663. $context->addStatusCode(Resources::STATUS_OK);
  664. $response = $this->sendContext($context);
  665. $serialized = $this->dataSerializer->unserialize($response->getBody());
  666. return GetOperationStatusResult::create($serialized);
  667. }
  668. /**
  669. * Lists the hosted services available under the current subscription.
  670. *
  671. * @return ListHostedServicesResult
  672. *
  673. * @see http://msdn.microsoft.com/en-us/library/windowsazure/ee460781.aspx
  674. */
  675. public function listHostedServices()
  676. {
  677. $context = new HttpCallContext();
  678. $context->setMethod(Resources::HTTP_GET);
  679. $context->setPath($this->_getHostedServicePath());
  680. $context->addStatusCode(Resources::STATUS_OK);
  681. $response = $this->sendContext($context);
  682. $serialized = $this->dataSerializer->unserialize($response->getBody());
  683. return ListHostedServicesResult::create($serialized);
  684. }
  685. /**
  686. * Creates a new hosted service in Windows Azure.
  687. *
  688. * @param string $name The name for the hosted service
  689. * that is unique within Windows Azure. This name is the DNS prefix name and can
  690. * be used to access the hosted service.
  691. * @param string $label The name for the hosted service
  692. * that is base-64 encoded. The name can be used identify the storage account for
  693. * your tracking purposes.
  694. * @param CreateServiceOptions $options The optional parameters.
  695. *
  696. * @return none
  697. *
  698. * @see http://msdn.microsoft.com/en-us/library/windowsazure/gg441304.aspx
  699. */
  700. public function createHostedService($name, $label, $options)
  701. {
  702. Validate::isString($name, 'name');
  703. Validate::notNullOrEmpty($name, 'name');
  704. Validate::isString($label, 'label');
  705. Validate::notNullOrEmpty($label, 'label');
  706. Validate::notNullOrEmpty($options, 'options');
  707. // User have to set affinity group or location.
  708. $affinityGroup = $options->getAffinityGroup();
  709. $location = $options->getLocation();
  710. Validate::isTrue(
  711. !empty($location) || !empty($affinityGroup),
  712. Resources::INVALID_CREATE_SERVICE_OPTIONS_MSG
  713. );
  714. $hostedService = new HostedService();
  715. $hostedService->setName($name);
  716. $hostedService->setLabel($label);
  717. $hostedService->setLocation($options->getLocation());
  718. $hostedService->setAffinityGroup($options->getAffinityGroup());
  719. $hostedService->setDescription($options->getDescription());
  720. $hostedService->addSerializationProperty(
  721. XmlSerializer::ROOT_NAME,
  722. Resources::XTAG_CREATE_HOSTED_SERVICE
  723. );
  724. $context = new HttpCallContext();
  725. $context->setMethod(Resources::HTTP_POST);
  726. $context->setPath($this->_getHostedServicePath());
  727. $context->addStatusCode(Resources::STATUS_CREATED);
  728. $context->setBody($hostedService->serialize($this->dataSerializer));
  729. $context->addHeader(
  730. Resources::CONTENT_TYPE,
  731. Resources::XML_CONTENT_TYPE
  732. );
  733. $this->sendContext($context);
  734. }
  735. /**
  736. * updates the label and/or the description for a hosted service in Windows
  737. * Azure.
  738. *
  739. * @param string $name The name for the hosted service that is
  740. * unique within Windows Azure.
  741. * @param UpdateServiceOptions $options The optional parameters.
  742. *
  743. * @return none
  744. *
  745. * @see http://msdn.microsoft.com/en-us/library/windowsazure/gg441303.aspx
  746. */
  747. public function updateHostedService($name, $options)
  748. {
  749. Validate::isString($name, 'name');
  750. Validate::notNullOrEmpty($name, 'name');
  751. Validate::notNullOrEmpty($options, 'options');
  752. $label = $options->getLabel();
  753. $description = $options->getDescription();
  754. Validate::isTrue(
  755. !empty($label) || !empty($description),
  756. Resources::INVALID_UPDATE_SERVICE_OPTIONS_MSG
  757. );
  758. $hostedService = new HostedService();
  759. $hostedService->setLabel($options->getLabel());
  760. $hostedService->setDescription($options->getDescription());
  761. $hostedService->addSerializationProperty(
  762. XmlSerializer::ROOT_NAME,
  763. Resources::XTAG_UPDATE_HOSTED_SERVICE
  764. );
  765. $context = new HttpCallContext();
  766. $context->setMethod(Resources::HTTP_PUT);
  767. $context->setPath($this->_getHostedServicePath($name));
  768. $context->addStatusCode(Resources::STATUS_OK);
  769. $context->setBody($hostedService->serialize($this->dataSerializer));
  770. $context->addHeader(
  771. Resources::CONTENT_TYPE,
  772. Resources::XML_CONTENT_TYPE
  773. );
  774. $this->sendContext($context);
  775. }
  776. /**
  777. * Deletes the specified hosted service from Windows Azure.
  778. *
  779. * Before you can delete a hosted service, you must delete any deployments it
  780. * has. Attempting to delete a hosted service that has deployments results in
  781. * an error. You can call the deleteDeployment API to delete a hosted service's
  782. * deployments.
  783. *
  784. * @param string $name The name for the hosted service.
  785. *
  786. * @return none
  787. *
  788. * @see http://msdn.microsoft.com/en-us/library/windowsazure/gg441305.aspx
  789. */
  790. public function deleteHostedService($name)
  791. {
  792. Validate::isString($name, 'name');
  793. Validate::notNullOrEmpty($name, 'name');
  794. $context = new HttpCallContext();
  795. $context->setMethod(Resources::HTTP_DELETE);
  796. $context->setPath($this->_getHostedServicePath($name));
  797. $context->addStatusCode(Resources::STATUS_OK);
  798. $this->sendContext($context);
  799. }
  800. /**
  801. * Retrieves system properties for the specified hosted service. These properties
  802. * include the service name and service type; the name of the affinity group to
  803. * which the service belongs, or its location if it is not part of an affinity
  804. * group; and optionally, information on the service's deployments.
  805. *
  806. * @param string $name The name for the hosted
  807. * service.
  808. * @param GetHostedServicePropertiesOptions $options The optional parameters.
  809. *
  810. * @return GetHostedServicePropertiesResult
  811. *
  812. * @see http://msdn.microsoft.com/en-us/library/windowsazure/ee460806.aspx
  813. */
  814. public function getHostedServiceProperties($name, $options = null)
  815. {
  816. Validate::isString($name, 'name');
  817. Validate::notNullOrEmpty($name, 'name');
  818. if (is_null($options)) {
  819. $options = new GetHostedServicePropertiesOptions();
  820. }
  821. $context = new HttpCallContext();
  822. $context->setMethod(Resources::HTTP_GET);
  823. $context->setPath($this->_getHostedServicePath($name));
  824. $context->addStatusCode(Resources::STATUS_OK);
  825. $context->addQueryParameter(
  826. Resources::QP_EMBED_DETAIL,
  827. Utilities::booleanToString($options->getEmbedDetail())
  828. );
  829. $response = $this->sendContext($context);
  830. $parsed = $this->dataSerializer->unserialize($response->getBody());
  831. return GetHostedServicePropertiesResult::create($parsed);
  832. }
  833. /**
  834. * Uploads a new service package and creates a new deployment on staging or
  835. * production.
  836. *
  837. * The createDeployment API is an asynchronous operation. To determine whether
  838. * the management service has finished processing the request, call
  839. * getOperationStatus API.
  840. *
  841. * @param string $name The name for the hosted service
  842. * that is unique within Windows Azure.
  843. * @param string $deploymentName The name for the deployment.
  844. * The deployment name must be unique among other deployments for the hosted
  845. * service.
  846. * @param string $slot The name of the deployment slot
  847. * This can be "production" or "staging".
  848. * @param string $packageUrl The URL that refers to the
  849. * location of the service package in the Blob service. The service package can
  850. * be located in a storage account beneath the same subscription.
  851. * @param string $configuration The base-64 encoded service
  852. * configuration file for the deployment.
  853. * @param string $label The name for the hosted service
  854. * that is base-64 encoded. The name can be up to 100 characters in length. It is
  855. * recommended that the label be unique within the subscription. The name can be
  856. * used identify the hosted service for your tracking purposes.
  857. * @param CreateDeploymentOptions $options The optional parameters.
  858. *
  859. * @return AsynchronousOperationResult
  860. *
  861. * @see http://msdn.microsoft.com/en-us/library/windowsazure/ee460813.aspx
  862. */
  863. public function createDeployment(
  864. $name,
  865. $deploymentName,
  866. $slot,
  867. $packageUrl,
  868. $configuration,
  869. $label,
  870. $options = null
  871. ) {
  872. Validate::isString($name, 'name');
  873. Validate::notNullOrEmpty($name, 'name');
  874. Validate::isString($deploymentName, 'deploymentName');
  875. Validate::notNullOrEmpty($deploymentName, 'deploymentName');
  876. Validate::isString($slot, 'slot');
  877. Validate::notNullOrEmpty($slot, 'slot');
  878. Validate::isTrue(
  879. DeploymentSlot::isValid($slot),
  880. sprintf(Resources::INVALID_SLOT, $slot)
  881. );
  882. Validate::isString($packageUrl, 'packageUrl');
  883. Validate::notNullOrEmpty($packageUrl, 'packageUrl');
  884. Validate::isString($configuration, 'configuration');
  885. Validate::notNullOrEmpty($configuration, 'configuration');
  886. Validate::isString($label, 'label');
  887. Validate::notNullOrEmpty($label, 'label');
  888. if (is_null($options)) {
  889. $options = new CreateDeploymentOptions();
  890. }
  891. $startDeployment = Utilities::booleanToString(
  892. $options->getStartDeployment()
  893. );
  894. $treatWarningsAsErrors = Utilities::booleanToString(
  895. $options->getTreatWarningsAsErrors()
  896. );
  897. $xmlElements = array(
  898. Resources::XTAG_NAME => $deploymentName,
  899. Resources::XTAG_PACKAGE_URL => $packageUrl,
  900. Resources::XTAG_LABEL => $label,
  901. Resources::XTAG_CONFIGURATION => $configuration,
  902. Resources::XTAG_START_DEPLOYMENT => $startDeployment,
  903. Resources::XTAG_TREAT_WARNINGS_AS_ERROR => $treatWarningsAsErrors
  904. );
  905. $requestXml = $this->_createRequestXml(
  906. $xmlElements,
  907. Resources::XTAG_CREATE_DEPLOYMENT
  908. );
  909. $context = new HttpCallContext();
  910. $context->setMethod(Resources::HTTP_POST);
  911. $context->setPath($this->_getDeploymentPathUsingSlot($name, $slot));
  912. $context->addStatusCode(Resources::STATUS_ACCEPTED);
  913. $context->setBody($requestXml);
  914. $context->addHeader(
  915. Resources::CONTENT_TYPE,
  916. Resources::XML_CONTENT_TYPE
  917. );
  918. $response = $this->sendContext($context);
  919. return AsynchronousOperationResult::create($response->getHeader());
  920. }
  921. /**
  922. * Returns configuration information, status, and system properties for a
  923. * deployment.
  924. *
  925. * The getDeployment API can be used to retrieve information for a specific
  926. * deployment or for all deployments in the staging or production environment.
  927. * If you want to retrieve information about a specific deployment, you must
  928. * first get the unique name for the deployment. This unique name is part of the
  929. * response when you make a request to get all deployments in an environment.
  930. *
  931. * @param string $name The hosted service name.
  932. * @param GetDeploymentOptions $options The optional parameters.
  933. *
  934. * @return GetDeploymentResult
  935. *
  936. * @see http://msdn.microsoft.com/en-us/library/windowsazure/ee460804.aspx
  937. */
  938. public function getDeployment($name, $options)
  939. {
  940. Validate::isString($name, 'name');
  941. Validate::notNullOrEmpty($name, 'name');
  942. Validate::notNullOrEmpty($options, 'options');
  943. $context = new HttpCallContext();
  944. $path = $this->_getDeploymentPath($name, $options);
  945. $context->setMethod(Resources::HTTP_GET);
  946. $context->setPath($path);
  947. $context->addStatusCode(Resources::STATUS_OK);
  948. $response = $this->sendContext($context);
  949. $parsed = $this->dataSerializer->unserialize($response->getBody());
  950. return GetDeploymentResult::create($parsed);
  951. }
  952. /**
  953. * Initiates a virtual IP swap between the staging and production deployment
  954. * environments for a service. If the service is currently running in the staging
  955. * environment, it will be swapped to the production environment. If it is
  956. * running in the production environment, it will be swapped to staging.
  957. *
  958. * You can swap VIPs only if the number of endpoints specified by the service
  959. * definition is identical for both deployments. For example, if you add an HTTPS
  960. * endpoint to a web role that previously exposed only an HTTP endpoint, you
  961. * cannot upgrade your service using a VIP swap; you must delete your production
  962. * deployment and redeploy instead. You can obtain information about endpoints
  963. * that are used by using the Get Deployment operation.
  964. *
  965. * @param string $name The hosted service name.
  966. * @param string $source The name of the source deployment.
  967. * @param string $destination The name of the destination deployment.
  968. *
  969. * @return AsynchronousOperationResult
  970. *
  971. * @see http://msdn.microsoft.com/en-us/library/windowsazure/ee460814.aspx
  972. */
  973. public function swapDeployment($name, $source, $destination)
  974. {
  975. Validate::isString($name, 'name');
  976. Validate::notNullOrEmpty($name, 'name');
  977. Validate::isString($destination, 'destination');
  978. Validate::notNullOrEmpty($destination, 'destination');
  979. Validate::isString($source, 'source');
  980. Validate::notNullOrEmpty($source, 'source');
  981. $xmlElements = array(
  982. Resources::XTAG_PRODUCTION => $destination,
  983. Resources::XTAG_SOURCE_DEPLOYMENT => $source
  984. );
  985. $body = $this->_createRequestXml($xmlElements, Resources::XTAG_SWAP);
  986. $context = new HttpCallContext();
  987. $context->setMethod(Resources::HTTP_POST);
  988. $context->setPath($this->_getHostedServicePath($name));
  989. $context->addStatusCode(Resources::STATUS_ACCEPTED);
  990. $context->setBody($body);
  991. $context->addHeader(
  992. Resources::CONTENT_TYPE,
  993. Resources::XML_CONTENT_TYPE
  994. );
  995. $response = $this->sendContext($context);
  996. return AsynchronousOperationResult::create($response->getHeader());
  997. }
  998. /**
  999. * Deletes the specified deployment.
  1000. *
  1001. * Note that you can delete a deployment either by specifying the deployment
  1002. * environment (staging or production), or by specifying the deployment's unique
  1003. * name.
  1004. *
  1005. * @param string $name The hosted service name.
  1006. * @param GetDeploymentOptions $options The optional parameters.
  1007. *
  1008. * @return AsynchronousOperationResult
  1009. *
  1010. * @see http://msdn.microsoft.com/en-us/library/windowsazure/ee460815.aspx
  1011. */
  1012. public function deleteDeployment($name, $options)
  1013. {
  1014. Validate::isString($name, 'name');
  1015. Validate::notNullOrEmpty($name, 'name');
  1016. Validate::notNullOrEmpty($options, 'options');
  1017. $context = new HttpCallContext();
  1018. $path = $this->_getDeploymentPath($name, $options);
  1019. $context->setMethod(Resources::HTTP_DELETE);
  1020. $context->setPath($path);
  1021. $context->addStatusCode(Resources::STATUS_ACCEPTED);
  1022. $response = $this->sendContext($context);
  1023. return AsynchronousOperationResult::create($response->getHeader());
  1024. }
  1025. /**
  1026. * Initiates a change to the deployment configuration.
  1027. *
  1028. * Note that you can change a deployment's configuration either by specifying the
  1029. * deployment environment (staging or production), or by specifying the
  1030. * deployment's unique name.
  1031. *
  1032. * @param string $name The hosted service
  1033. * name.
  1034. * @param string|resource $configuration The configuration
  1035. * file contents or file stream,
  1036. * @param ChangeDeploymentConfigurationOptions $options The optional
  1037. * parameters.
  1038. *
  1039. * @return AsynchronousOperationResult
  1040. *
  1041. * @see http://msdn.microsoft.com/en-us/library/windowsazure/ee460809.aspx
  1042. */
  1043. public function changeDeploymentConfiguration($name, $configuration, $options)
  1044. {
  1045. Validate::isString($name, 'name');
  1046. Validate::notNullOrEmpty($name, 'name');
  1047. Validate::isString($configuration, 'configuration');
  1048. Validate::notNullOrEmpty($configuration, 'configuration');
  1049. Validate::notNullOrEmpty($options, 'options');
  1050. $configuration = is_resource($configuration)
  1051. ? stream_get_contents($configuration) : $configuration;
  1052. $configuration = base64_encode($configuration);
  1053. $warningsTreatment = Utilities::booleanToString(
  1054. $options->getTreatWarningsAsErrors()
  1055. );
  1056. $xmlElements = array(
  1057. Resources::XTAG_CONFIGURATION => $configuration,
  1058. Resources::XTAG_TREAT_WARNINGS_AS_ERROR => $warningsTreatment,
  1059. Resources::XTAG_MODE => $options->getMode()
  1060. );
  1061. $body = $this->_createRequestXml(
  1062. $xmlElements,
  1063. Resources::XTAG_CHANGE_CONFIGURATION
  1064. );
  1065. $context = new HttpCallContext();
  1066. $context->setMethod(Resources::HTTP_POST);
  1067. $context->setPath($this->_getDeploymentPath($name, $options) . '/');
  1068. $context->addStatusCode(Resources::STATUS_ACCEPTED);
  1069. $context->addQueryParameter(
  1070. Resources::QP_COMP,
  1071. Resources::QPV_CONFIG
  1072. );
  1073. $context->setBody($body);
  1074. $context->addHeader(
  1075. Resources::CONTENT_TYPE,
  1076. Resources::XML_CONTENT_TYPE
  1077. );
  1078. assert(Utilities::endsWith($context->getPath(), '/'));
  1079. $response = $this->sendContext($context);
  1080. return AsynchronousOperationResult::create($response->getHeader());
  1081. }
  1082. /**
  1083. * Initiates a change in deployment status.
  1084. *
  1085. * Note that you can change deployment status either by specifying the deployment
  1086. * environment (staging or production), or by specifying the deployment's unique
  1087. * name.
  1088. *
  1089. * @param string $name The hosted service name.
  1090. * @param string $status The change to initiate to the
  1091. * deployment status.
  1092. * Possible values include Running or Suspended.
  1093. * @param GetDeploymentOptions $options The optional parameters.
  1094. *
  1095. * @return AsynchronousOperationResult
  1096. *
  1097. * @see http://msdn.microsoft.com/en-us/library/windowsazure/ee460808.aspx
  1098. */
  1099. public function updateDeploymentStatus($name, $status, $options)
  1100. {
  1101. Validate::isString($name, 'name');
  1102. Validate::notNullOrEmpty($name, 'name');
  1103. Validate::isTrue(
  1104. DeploymentStatus::isValid($status),
  1105. Resources::INVALID_DEPLOYMENT_STATUS_MSG
  1106. );
  1107. Validate::notNullOrEmpty($options, 'options');
  1108. $body = $this->_createRequestXml(
  1109. array(Resources::XTAG_STATUS => $status),
  1110. Resources::XTAG_UPDATE_DEPLOYMENT_STATUS
  1111. );
  1112. $context = new HttpCallContext();
  1113. $context->setMethod(Resources::HTTP_POST);
  1114. $context->setPath($this->_getDeploymentPath($name, $options) . '/');
  1115. $context->addStatusCode(Resources::STATUS_ACCEPTED);
  1116. $context->addQueryParameter(
  1117. Resources::QP_COMP,
  1118. Resources::QPV_STATUS
  1119. );
  1120. $context->setBody($body);
  1121. $context->addHeader(
  1122. Resources::CONTENT_TYPE,
  1123. Resources::XML_CONTENT_TYPE
  1124. );
  1125. assert(Utilities::endsWith($context->getPath(), '/'));
  1126. $response = $this->sendContext($context);
  1127. return AsynchronousOperationResult::create($response->getHeader());
  1128. }
  1129. /**
  1130. * Initiates an upgrade to a deployment.
  1131. *
  1132. * Note that you can upgrade a deployment either by specifying the deployment
  1133. * environment (staging or production), or by specifying the deployment's unique
  1134. * name.
  1135. *
  1136. * @param string $name The hosted service name.
  1137. * @param string $mode The type of upgrade to initiate
  1138. * If not specified the default value is Auto. If set to Manual,
  1139. * walkUpgradeDomain API must be called to apply the update. If set to Auto, the
  1140. * Windows Azure platform will automatically apply the update to each Upgrade
  1141. * Domain in sequence.
  1142. * @param string $packageUrl The URL that refers to the
  1143. * location of the service package in the Blob service. The service package can
  1144. * be located in a storage account beneath the same subscription.
  1145. * @param string $configuration The base-64 encoded service
  1146. * configuration file for the deployment.
  1147. * @param string $label The name for the hosted service
  1148. * that is base-64 encoded. The name may be up to 100 characters in length.
  1149. * @param boolean $force Specifies whether the rollback
  1150. * should proceed even when it will cause local data to be lost from some role
  1151. * instances. True if the rollback should proceed; otherwise false if the
  1152. * rollback should fail.
  1153. * @param UpgradeDeploymentOptions $options The optional parameters.
  1154. *
  1155. * @return AsynchronousOperationResult
  1156. *
  1157. * @see http://msdn.microsoft.com/en-us/library/windowsazure/ee460793.aspx
  1158. */
  1159. public function upgradeDeployment(
  1160. $name,
  1161. $mode,
  1162. $packageUrl,
  1163. $configuration,
  1164. $label,
  1165. $force,
  1166. $options
  1167. ) {
  1168. Validate::isString($name, 'name');
  1169. Validate::notNullOrEmpty($