Example #1
0
        /// <summary>
        /// Returns a collection of databases that has been dropped but can
        /// still be restored from a specified server.
        /// </summary>
        /// <param name='serverName'>
        /// Required. The name of the Azure SQL Database Server to query for
        /// dropped databases that can still be restored.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// Contains the response to the List Restorable Dropped Databases
        /// request.
        /// </returns>
        public async Task <RestorableDroppedDatabaseListResponse> ListAsync(string serverName, CancellationToken cancellationToken)
        {
            // Validate
            if (serverName == null)
            {
                throw new ArgumentNullException("serverName");
            }

            // Tracing
            bool   shouldTrace  = TracingAdapter.IsEnabled;
            string invocationId = null;

            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("serverName", serverName);
                TracingAdapter.Enter(invocationId, this, "ListAsync", tracingParameters);
            }

            // Construct URL
            string url     = "/" + (this.Client.Credentials.SubscriptionId == null ? "" : Uri.EscapeDataString(this.Client.Credentials.SubscriptionId)) + "/services/sqlservers/servers/" + Uri.EscapeDataString(serverName) + "/restorabledroppeddatabases?contentview=generic";
            string baseUrl = this.Client.BaseUri.AbsoluteUri;

            // Trim '/' character from the end of baseUrl and beginning of url.
            if (baseUrl[baseUrl.Length - 1] == '/')
            {
                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
            }
            if (url[0] == '/')
            {
                url = url.Substring(1);
            }
            url = baseUrl + "/" + url;
            url = url.Replace(" ", "%20");

            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;

            try
            {
                httpRequest            = new HttpRequestMessage();
                httpRequest.Method     = HttpMethod.Get;
                httpRequest.RequestUri = new Uri(url);

                // Set Headers
                httpRequest.Headers.Add("x-ms-version", "2012-03-01");

                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        TracingAdapter.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                    if (shouldTrace)
                    {
                        TracingAdapter.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            TracingAdapter.Error(invocationId, ex);
                        }
                        throw ex;
                    }

                    // Create Result
                    RestorableDroppedDatabaseListResponse result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                        result = new RestorableDroppedDatabaseListResponse();
                        XDocument responseDoc = XDocument.Parse(responseContent);

                        XElement serviceResourcesSequenceElement = responseDoc.Element(XName.Get("ServiceResources", "http://schemas.microsoft.com/windowsazure"));
                        if (serviceResourcesSequenceElement != null)
                        {
                            foreach (XElement serviceResourcesElement in serviceResourcesSequenceElement.Elements(XName.Get("ServiceResource", "http://schemas.microsoft.com/windowsazure")))
                            {
                                RestorableDroppedDatabase serviceResourceInstance = new RestorableDroppedDatabase();
                                result.Databases.Add(serviceResourceInstance);

                                XElement entityIdElement = serviceResourcesElement.Element(XName.Get("EntityId", "http://schemas.microsoft.com/windowsazure"));
                                if (entityIdElement != null)
                                {
                                    string entityIdInstance = entityIdElement.Value;
                                    serviceResourceInstance.EntityId = entityIdInstance;
                                }

                                XElement serverNameElement = serviceResourcesElement.Element(XName.Get("ServerName", "http://schemas.microsoft.com/windowsazure"));
                                if (serverNameElement != null)
                                {
                                    string serverNameInstance = serverNameElement.Value;
                                    serviceResourceInstance.ServerName = serverNameInstance;
                                }

                                XElement editionElement = serviceResourcesElement.Element(XName.Get("Edition", "http://schemas.microsoft.com/windowsazure"));
                                if (editionElement != null)
                                {
                                    string editionInstance = editionElement.Value;
                                    serviceResourceInstance.Edition = editionInstance;
                                }

                                XElement maxSizeBytesElement = serviceResourcesElement.Element(XName.Get("MaxSizeBytes", "http://schemas.microsoft.com/windowsazure"));
                                if (maxSizeBytesElement != null)
                                {
                                    long maxSizeBytesInstance = long.Parse(maxSizeBytesElement.Value, CultureInfo.InvariantCulture);
                                    serviceResourceInstance.MaximumDatabaseSizeInBytes = maxSizeBytesInstance;
                                }

                                XElement creationDateElement = serviceResourcesElement.Element(XName.Get("CreationDate", "http://schemas.microsoft.com/windowsazure"));
                                if (creationDateElement != null)
                                {
                                    DateTime creationDateInstance = DateTime.Parse(creationDateElement.Value, CultureInfo.InvariantCulture);
                                    serviceResourceInstance.CreationDate = creationDateInstance;
                                }

                                XElement deletionDateElement = serviceResourcesElement.Element(XName.Get("DeletionDate", "http://schemas.microsoft.com/windowsazure"));
                                if (deletionDateElement != null)
                                {
                                    DateTime deletionDateInstance = DateTime.Parse(deletionDateElement.Value, CultureInfo.InvariantCulture);
                                    serviceResourceInstance.DeletionDate = deletionDateInstance;
                                }

                                XElement recoveryPeriodStartDateElement = serviceResourcesElement.Element(XName.Get("RecoveryPeriodStartDate", "http://schemas.microsoft.com/windowsazure"));
                                if (recoveryPeriodStartDateElement != null && !string.IsNullOrEmpty(recoveryPeriodStartDateElement.Value))
                                {
                                    DateTime recoveryPeriodStartDateInstance = DateTime.Parse(recoveryPeriodStartDateElement.Value, CultureInfo.InvariantCulture);
                                    serviceResourceInstance.RecoveryPeriodStartDate = recoveryPeriodStartDateInstance;
                                }

                                XElement nameElement = serviceResourcesElement.Element(XName.Get("Name", "http://schemas.microsoft.com/windowsazure"));
                                if (nameElement != null)
                                {
                                    string nameInstance = nameElement.Value;
                                    serviceResourceInstance.Name = nameInstance;
                                }

                                XElement typeElement = serviceResourcesElement.Element(XName.Get("Type", "http://schemas.microsoft.com/windowsazure"));
                                if (typeElement != null)
                                {
                                    string typeInstance = typeElement.Value;
                                    serviceResourceInstance.Type = typeInstance;
                                }

                                XElement stateElement = serviceResourcesElement.Element(XName.Get("State", "http://schemas.microsoft.com/windowsazure"));
                                if (stateElement != null)
                                {
                                    string stateInstance = stateElement.Value;
                                    serviceResourceInstance.State = stateInstance;
                                }
                            }
                        }
                    }
                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }

                    if (shouldTrace)
                    {
                        TracingAdapter.Exit(invocationId, result);
                    }
                    return(result);
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }
 /// <summary>
 /// Given a <see cref="RestorableDroppedDatabaseListResponse"/> this will create and return an array of <see cref="RestorableDroppedDatabase"/>
 /// object with the fields filled in.
 /// </summary>
 /// <param name="response">The response to turn into an array of <see cref="RestorableDroppedDatabase"/> objects</param>
 /// <returns>An array of <see cref="RestorableDroppedDatabase"/> objects.</returns>
 private RestorableDroppedDatabase[] CreateRestorableDroppedDatabaseFromResponse(RestorableDroppedDatabaseListResponse response)
 {
     return response.Databases.Select(db => this.CreateRestorableDroppedDatabaseFromResponse(
         db.EntityId,
         db.Name,
         db.ServerName,
         db.Edition,
         db.MaximumDatabaseSizeInBytes,
         db.CreationDate,
         db.DeletionDate,
         db.RecoveryPeriodStartDate)).ToArray();
 }