tinyshare /vendor/Microsoft/SqlAzure/Management/Client.php

Language PHP Lines 565
MD5 Hash 9fca9f7dc4006d1ccf53822f97bd3fa6 Estimated Cost $7,311 (why?)
Repository https://bitbucket.org/ktos/tinyshare View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
<?php
/**
 * Copyright (c) 2009 - 2011, RealDolmen
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of RealDolmen nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY RealDolmen ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL RealDolmen BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * @category   Microsoft
 * @package    Microsoft_WindowsAzure
 * @subpackage Management
 * @copyright  Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
 * @license    http://phpazure.codeplex.com/license
 * @version    $Id: Storage.php 51671 2010-09-30 08:33:45Z unknown $
 */

/**
 * @see Microsoft_AutoLoader
 */
require_once dirname(__FILE__) . '/../../AutoLoader.php';

/**
 * @category   Microsoft
 * @package    Microsoft_SqlAzure
 * @subpackage Management
 * @copyright  Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
 * @license    http://phpazure.codeplex.com/license
 */
class Microsoft_SqlAzure_Management_Client
{
	/**
	 * Management service URL
	 */
	const URL_MANAGEMENT        = "https://management.database.windows.net:8443";
	
	/**
	 * Operations
	 */
	const OP_OPERATIONS                = "operations";
	const OP_SERVERS                   = "servers";
	const OP_FIREWALLRULES             = "firewallrules";

	/**
	 * Current API version
	 * 
	 * @var string
	 */
	protected $_apiVersion = '1.0';
	
	/**
	 * Subscription ID
	 *
	 * @var string
	 */
	protected $_subscriptionId = '';
	
	/**
	 * Management certificate path (.PEM)
	 *
	 * @var string
	 */
	protected $_certificatePath = '';
	
	/**
	 * Management certificate passphrase
	 *
	 * @var string
	 */
	protected $_certificatePassphrase = '';
	
	/**
	 * Microsoft_Http_Client channel used for communication with REST services
	 * 
	 * @var Microsoft_Http_Client
	 */
	protected $_httpClientChannel = null;	

	/**
	 * Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract instance
	 * 
	 * @var Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract
	 */
	protected $_retryPolicy = null;
	
	/**
	 * Returns the last request ID
	 * 
	 * @var string
	 */
	protected $_lastRequestId = null;
	
	/**
	 * Creates a new Microsoft_SqlAzure_Management_Client instance
	 * 
	 * @param string $subscriptionId Subscription ID
	 * @param string $certificatePath Management certificate path (.PEM)
	 * @param string $certificatePassphrase Management certificate passphrase
     * @param Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy Retry policy to use when making requests
	 */
	public function __construct(
		$subscriptionId,
		$certificatePath,
		$certificatePassphrase,
		Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy = null
	) {
		$this->_subscriptionId = $subscriptionId;
		$this->_certificatePath = $certificatePath;
		$this->_certificatePassphrase = $certificatePassphrase;
		
		$this->_retryPolicy = $retryPolicy;
		if (is_null($this->_retryPolicy)) {
		    $this->_retryPolicy = Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract::noRetry();
		}
		
		// Setup default Microsoft_Http_Client channel
		$options = array(
		    'adapter'       => 'Microsoft_Http_Client_Adapter_Socket',
		    'ssltransport'  => 'ssl',
			'sslcert'       => $this->_certificatePath,
			'sslpassphrase' => $this->_certificatePassphrase,
			'sslusecontext' => true,
		);
		if (function_exists('curl_init')) {
			// Set cURL options if cURL is used afterwards
			$options['curloptions'] = array(
					CURLOPT_FOLLOWLOCATION => true,
					CURLOPT_TIMEOUT => 120,
			);
		}
		$this->_httpClientChannel = new Microsoft_Http_Client(null, $options);
	}
	
	/**
	 * Set the HTTP client channel to use
	 * 
	 * @param Microsoft_Http_Client_Adapter_Interface|string $adapterInstance Adapter instance or adapter class name.
	 */
	public function setHttpClientChannel($adapterInstance = 'Microsoft_Http_Client_Adapter_Socket')
	{
		$this->_httpClientChannel->setAdapter($adapterInstance);
	}
	
    /**
     * Retrieve HTTP client channel
     * 
     * @return Microsoft_Http_Client_Adapter_Interface
     */
    public function getHttpClientChannel()
    {
        return $this->_httpClientChannel;
    }
	
	/**
	 * Returns the Windows Azure subscription ID
	 * 
	 * @return string
	 */
	public function getSubscriptionId()
	{
		return $this->_subscriptionId;
	}
	
	/**
	 * Returns the last request ID.
	 * 
	 * @return string
	 */
	public function getLastRequestId()
	{
		return $this->_lastRequestId;
	}
	
	/**
	 * Get base URL for creating requests
	 *
	 * @return string
	 */
	public function getBaseUrl()
	{
		return self::URL_MANAGEMENT . '/' . $this->_subscriptionId;
	}
	
	/**
	 * Perform request using Microsoft_Http_Client channel
	 *
	 * @param string $path Path
	 * @param array $query Query parameters
	 * @param string $httpVerb HTTP verb the request will use
	 * @param array $headers x-ms headers to add
	 * @param mixed $rawData Optional RAW HTTP data to be sent over the wire
	 * @return Microsoft_Http_Response
	 */
	protected function _performRequest(
		$path = '/',
		$query = array(),
		$httpVerb = Microsoft_Http_Client::GET,
		$headers = array(),
		$rawData = null
	) {
	    // Clean path
		if (strpos($path, '/') !== 0) {
			$path = '/' . $path;
		}
			
		// Clean headers
		if (is_null($headers)) {
		    $headers = array();
		}
		
		// Ensure cUrl will also work correctly:
		//  - disable Content-Type if required
		//  - disable Expect: 100 Continue
		if (!isset($headers["Content-Type"])) {
			$headers["Content-Type"] = '';
		}
		//$headers["Expect"] = '';

		// Add version header
		$headers['x-ms-version'] = $this->_apiVersion;

		// Generate URL and sign request
		$requestUrl = $this->getBaseUrl() . rawurlencode($path);
		$requestHeaders = $headers;
		if (count($query) > 0) {
			$queryString = '';
			foreach ($query as $key => $value) {
				$queryString .= ($queryString ? '&' : '?') . rawurlencode($key) . '=' . rawurlencode($value);
			}			
			$requestUrl .= $queryString;
		}

		// Prepare request 
		$this->_httpClientChannel->resetParameters(true);
		$this->_httpClientChannel->setUri($requestUrl);
		$this->_httpClientChannel->setHeaders($requestHeaders);
		$this->_httpClientChannel->setRawData($rawData);

		// Execute request
		$response = $this->_retryPolicy->execute(
		    array($this->_httpClientChannel, 'request'),
		    array($httpVerb)
		);
		
		// Store request id
		$this->_lastRequestId = $response->getHeader('x-ms-request-id');
		
		return $response;
	}
	
	/** 
	 * Parse result from Microsoft_Http_Response
	 *
	 * @param Microsoft_Http_Response $response Response from HTTP call
	 * @return object
	 * @throws Microsoft_WindowsAzure_Exception
	 */
	protected function _parseResponse(Microsoft_Http_Response $response = null)
	{
		if (is_null($response)) {
			throw new Microsoft_SqlAzure_Exception('Response should not be null.');
		}
		
        $xml = @simplexml_load_string($response->getBody());
        
        if ($xml !== false) {
            // Fetch all namespaces 
            $namespaces = array_merge($xml->getNamespaces(true), $xml->getDocNamespaces(true)); 
            
            // Register all namespace prefixes
            foreach ($namespaces as $prefix => $ns) { 
                if ($prefix != '') {
                    $xml->registerXPathNamespace($prefix, $ns);
                } 
            } 
        }
        
        return $xml;
	}
    
	/**
	 * Get error message from Microsoft_Http_Response
	 *
	 * @param Microsoft_Http_Response $response Repsonse
	 * @param string $alternativeError Alternative error message
	 * @return string
	 */
	protected function _getErrorMessage(Microsoft_Http_Response $response, $alternativeError = 'Unknown error.')
	{
		$response = $this->_parseResponse($response);
		if ($response && $response->Message) {
			return (string)$response->Message;
		} else {
			return $alternativeError;
		}
	}
	
	/**
	 * The Create Server operation adds a new SQL Azure server to a subscription.
	 * 
	 * @param string $administratorLogin Administrator login.
	 * @param string $administratorPassword Administrator password.
	 * @param string $location Location of the server.
	 * @return Microsoft_SqlAzure_Management_ServerInstance Server information.
	 * @throws Microsoft_SqlAzure_Management_Exception
	 */
	public function createServer($administratorLogin, $administratorPassword, $location)
	{
		if ($administratorLogin == '' || is_null($administratorLogin)) {
    		throw new Microsoft_SqlAzure_Management_Exception('Administrator login should be specified.');
    	}
		if ($administratorPassword == '' || is_null($administratorPassword)) {
    		throw new Microsoft_SqlAzure_Management_Exception('Administrator password should be specified.');
    	}
    	if (is_null($location) && is_null($affinityGroup)) {
    		throw new Microsoft_SqlAzure_Management_Exception('Please specify a location for the server.');
    	}
    	
        $response = $this->_performRequest(self::OP_SERVERS, array(),
    		Microsoft_Http_Client::POST,
    		array('Content-Type' => 'application/xml; charset=utf-8'),
    		'<Server xmlns="http://schemas.microsoft.com/sqlazure/2010/12/"><AdministratorLogin>' . $administratorLogin . '</AdministratorLogin><AdministratorLoginPassword>' . $administratorPassword . '</AdministratorLoginPassword><Location>' . $location . '</Location></Server>');
 	
    	if ($response->isSuccessful()) {
			$xml = $this->_parseResponse($response);
			
			return new Microsoft_SqlAzure_Management_ServerInstance(
				(string)$xml,
				$administratorLogin,
				$location
			);
    	} else {
			throw new Microsoft_SqlAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
		}	
	}
	
	/**
	 * The Get Servers operation enumerates SQL Azure servers that are provisioned for a subscription.
	 * 
	 * @return array An array of Microsoft_SqlAzure_Management_ServerInstance.
	 * @throws Microsoft_SqlAzure_Management_Exception
	 */
	public function listServers()
	{
        $response = $this->_performRequest(self::OP_SERVERS);
 	
    	if ($response->isSuccessful()) {
			$xml = $this->_parseResponse($response);
			$xmlServices = null;
			
    		if (!$xml->Server) {
				return array();
			}
		    if (count($xml->Server) > 1) {
    		    $xmlServices = $xml->Server;
    		} else {
    		    $xmlServices = array($xml->Server);
    		}
    		
			$services = array();
			if (!is_null($xmlServices)) {				
				for ($i = 0; $i < count($xmlServices); $i++) {
					$services[] = new Microsoft_SqlAzure_Management_ServerInstance(
					    (string)$xmlServices[$i]->Name,
					    (string)$xmlServices[$i]->AdministratorLogin,
					    (string)$xmlServices[$i]->Location
					);
				}
			}
			return $services;
    	} else {
			throw new Microsoft_SqlAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
		}
	}
	
	/**
	 * The Drop Server operation drops a SQL Azure server from a subscription.
	 * 
	 * @param string $serverName Server to drop.
	 * @throws Microsoft_SqlAzure_Management_Exception
	 */
	public function dropServer($serverName)
	{
		if ($serverName == '' || is_null($serverName)) {
    		throw new Microsoft_SqlAzure_Management_Exception('Server name should be specified.');
    	}
    	
        $response = $this->_performRequest(self::OP_SERVERS . '/' . $serverName, array(), Microsoft_Http_Client::DELETE);

    	if (!$response->isSuccessful()) {
			throw new Microsoft_SqlAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
		}	
	}
	
	/**
	 * The Set Server Administrator Password operation sets the administrative password of a SQL Azure server for a subscription.
	 * 
	 * @param string $serverName Server to set password for.
	 * @param string $administratorPassword Administrator password.
	 * @throws Microsoft_SqlAzure_Management_Exception
	 */
	public function setAdministratorPassword($serverName, $administratorPassword)
	{
		if ($serverName == '' || is_null($serverName)) {
    		throw new Microsoft_SqlAzure_Management_Exception('Server name should be specified.');
    	}
		if ($administratorPassword == '' || is_null($administratorPassword)) {
    		throw new Microsoft_SqlAzure_Management_Exception('Administrator password should be specified.');
    	}
    	
        $response = $this->_performRequest(self::OP_SERVERS . '/' . $serverName, array('op' => 'ResetPassword'),
    		Microsoft_Http_Client::POST,
    		array('Content-Type' => 'application/xml; charset=utf-8'),
    		'<AdministratorLoginPassword xmlns="http://schemas.microsoft.com/sqlazure/2010/12/">' . $administratorPassword . '</AdministratorLoginPassword>');
    		
    	if (!$response->isSuccessful()) {
			throw new Microsoft_SqlAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
		}	
	}
	
	/**
	 * The Set Server Firewall Rule operation updates an existing firewall rule or adds a new firewall rule for a SQL Azure server that belongs to a subscription.
	 * 
	 * @param string $serverName Server name.
	 * @param string $ruleName Firewall rule name.
	 * @param string $startIpAddress Start IP address.
	 * @param string $endIpAddress End IP address.
	 * @return Microsoft_SqlAzure_Management_FirewallRuleInstance
	 * @throws Microsoft_SqlAzure_Management_Exception
	 */
	public function createFirewallRule($serverName, $ruleName, $startIpAddress, $endIpAddress)
	{
		if ($serverName == '' || is_null($serverName)) {
    		throw new Microsoft_SqlAzure_Management_Exception('Server name should be specified.');
    	}
		if ($ruleName == '' || is_null($ruleName)) {
    		throw new Microsoft_SqlAzure_Management_Exception('Rule name should be specified.');
    	}
		if ($startIpAddress == '' || is_null($startIpAddress) || !filter_var($startIpAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
    		throw new Microsoft_SqlAzure_Management_Exception('Start IP address should be specified.');
    	}
		if ($endIpAddress == '' || is_null($endIpAddress) || !filter_var($endIpAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
    		throw new Microsoft_SqlAzure_Management_Exception('End IP address should be specified.');
    	}
    	
        $response = $this->_performRequest(self::OP_SERVERS . '/' . $serverName . '/' . self::OP_FIREWALLRULES . '/' . $ruleName, array(),
    		Microsoft_Http_Client::PUT,
    		array('Content-Type' => 'application/xml; charset=utf-8'),
    		'<FirewallRule xmlns="http://schemas.microsoft.com/sqlazure/2010/12/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schemas.microsoft.com/sqlazure/2010/12/ FirewallRule.xsd"><StartIpAddress>' . $startIpAddress . '</StartIpAddress><EndIpAddress>' . $endIpAddress . '</EndIpAddress></FirewallRule>');

    	if ($response->isSuccessful()) {
    		return new Microsoft_SqlAzure_Management_FirewallRuleInstance(
    			$ruleName,
    			$startIpAddress,
    			$endIpAddress
    		);
    	} else {
			throw new Microsoft_SqlAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
		}
	}
	
	/**
	 * The Get Server Firewall Rules operation retrieves a list of all the firewall rules for a SQL Azure server that belongs to a subscription.
	 * 
	 * @param string $serverName Server name.
	 * @return Array of Microsoft_SqlAzure_Management_FirewallRuleInstance.
	 * @throws Microsoft_SqlAzure_Management_Exception
	 */
	public function listFirewallRules($serverName)
	{
		if ($serverName == '' || is_null($serverName)) {
    		throw new Microsoft_SqlAzure_Management_Exception('Server name should be specified.');
    	}
    	
	    $response = $this->_performRequest(self::OP_SERVERS . '/' . $serverName . '/' . self::OP_FIREWALLRULES);
 	
    	if ($response->isSuccessful()) {
			$xml = $this->_parseResponse($response);
			$xmlServices = null;
			
    		if (!$xml->FirewallRule) {
				return array();
			}
		    if (count($xml->FirewallRule) > 1) {
    		    $xmlServices = $xml->FirewallRule;
    		} else {
    		    $xmlServices = array($xml->FirewallRule);
    		}
    		
			$services = array();
			if (!is_null($xmlServices)) {				
				for ($i = 0; $i < count($xmlServices); $i++) {
					$services[] = new Microsoft_SqlAzure_Management_FirewallRuleInstance(
					    (string)$xmlServices[$i]->Name,
					    (string)$xmlServices[$i]->StartIpAddress,
					    (string)$xmlServices[$i]->EndIpAddress
					);
				}
			}
			return $services;
    	} else {
			throw new Microsoft_SqlAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
		}		
	}
	
	/**
	 * The Delete Server Firewall Rule operation deletes a firewall rule from a SQL Azure server that belongs to a subscription.
	 * 
	 * @param string $serverName Server name.
	 * @param string $ruleName Rule name.
	 * @throws Microsoft_SqlAzure_Management_Exception
	 */
	public function deleteFirewallRule($serverName, $ruleName)
	{
		if ($serverName == '' || is_null($serverName)) {
    		throw new Microsoft_SqlAzure_Management_Exception('Server name should be specified.');
    	}
		if ($ruleName == '' || is_null($ruleName)) {
    		throw new Microsoft_SqlAzure_Management_Exception('Rule name should be specified.');
    	}
    	
        $response = $this->_performRequest(self::OP_SERVERS . '/' . $serverName . '/' . self::OP_FIREWALLRULES . '/' . $ruleName, array(),
    		Microsoft_Http_Client::DELETE);

    	if (!$response->isSuccessful()) {
			throw new Microsoft_SqlAzure_Management_Exception($this->_getErrorMessage($response, 'Resource could not be accessed.'));
		}
	}
	
	/**
	 * Creates a firewall rule for Microsoft Services. This is required if access to SQL Azure is required from other services like Windows Azure.
	 * 
	 * @param string $serverName Server name.
	 * @param boolean $allowAccess Allow access from other Microsoft Services?
	 * @throws Microsoft_SqlAzure_Management_Exception
	 */
	public function createFirewallRuleForMicrosoftServices($serverName, $allowAccess)
	{
		if ($allowAccess) {
			$this->createFirewallRule($serverName, 'MicrosoftServices', '0.0.0.0', '0.0.0.0');
		} else {
			$this->deleteFirewallRule($serverName, 'MicrosoftServices');
		}
	}
	
}
Back to Top