/// <summary> /// Process the command. /// </summary> public override void ExecuteCmdlet() { // Obtain the source server and database name from the given parameters. var serverName = this.Database != null ? this.Database.ServerName : this.ServerName; var databaseName = this.Database != null ? this.Database.Name : this.DatabaseName; // Get the SQL management client for the current subscription SqlManagementClient sqlManagementClient = GetCurrentSqlClient(); try { if (databaseName != null) { // Retrieve the database with the specified name RecoverableDatabaseGetResponse response = sqlManagementClient.RecoverableDatabases.Get(serverName, databaseName); this.WriteObject(response.Database); } else { // No name specified, retrieve all databases in the server RecoverableDatabaseListResponse response = sqlManagementClient.RecoverableDatabases.List(serverName); this.WriteObject(response.Databases, true); } } catch (Exception ex) { this.WriteErrorDetails(ex); } }
/// <summary> /// Returns information about a recoverable Azure SQL Database. /// </summary> /// <param name='serverName'> /// Required. The name of the Azure SQL Database Server on which the /// database was hosted. /// </param> /// <param name='databaseName'> /// Required. The name of the recoverable Azure SQL Database to be /// obtained. /// </param> /// <param name='cancellationToken'> /// Cancellation token. /// </param> /// <returns> /// Contains the response to the Get Recoverable Database request. /// </returns> public async System.Threading.Tasks.Task <Microsoft.WindowsAzure.Management.Sql.Models.RecoverableDatabaseGetResponse> GetAsync(string serverName, string databaseName, CancellationToken cancellationToken) { // Validate if (serverName == null) { throw new ArgumentNullException("serverName"); } if (databaseName == null) { throw new ArgumentNullException("databaseName"); } // Tracing bool shouldTrace = CloudContext.Configuration.Tracing.IsEnabled; string invocationId = null; if (shouldTrace) { invocationId = Tracing.NextInvocationId.ToString(); Dictionary <string, object> tracingParameters = new Dictionary <string, object>(); tracingParameters.Add("serverName", serverName); tracingParameters.Add("databaseName", databaseName); Tracing.Enter(invocationId, this, "GetAsync", tracingParameters); } // Construct URL string url = "/" + (this.Client.Credentials.SubscriptionId != null ? this.Client.Credentials.SubscriptionId.Trim() : "") + "/services/sqlservers/servers/" + serverName.Trim() + "/recoverabledatabases/" + databaseName.Trim(); 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) { Tracing.SendRequest(invocationId, httpRequest); } cancellationToken.ThrowIfCancellationRequested(); httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false); if (shouldTrace) { Tracing.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) { Tracing.Error(invocationId, ex); } throw ex; } // Create Result RecoverableDatabaseGetResponse result = null; // Deserialize Response cancellationToken.ThrowIfCancellationRequested(); string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); result = new RecoverableDatabaseGetResponse(); XDocument responseDoc = XDocument.Parse(responseContent); XElement serviceResourceElement = responseDoc.Element(XName.Get("ServiceResource", "http://schemas.microsoft.com/windowsazure")); if (serviceResourceElement != null) { RecoverableDatabase serviceResourceInstance = new RecoverableDatabase(); result.Database = serviceResourceInstance; XElement entityIdElement = serviceResourceElement.Element(XName.Get("EntityId", "http://schemas.microsoft.com/windowsazure")); if (entityIdElement != null) { string entityIdInstance = entityIdElement.Value; serviceResourceInstance.EntityId = entityIdInstance; } XElement serverNameElement = serviceResourceElement.Element(XName.Get("ServerName", "http://schemas.microsoft.com/windowsazure")); if (serverNameElement != null) { string serverNameInstance = serverNameElement.Value; serviceResourceInstance.ServerName = serverNameInstance; } XElement editionElement = serviceResourceElement.Element(XName.Get("Edition", "http://schemas.microsoft.com/windowsazure")); if (editionElement != null) { string editionInstance = editionElement.Value; serviceResourceInstance.Edition = editionInstance; } XElement lastAvailableBackupDateElement = serviceResourceElement.Element(XName.Get("LastAvailableBackupDate", "http://schemas.microsoft.com/windowsazure")); if (lastAvailableBackupDateElement != null) { DateTime lastAvailableBackupDateInstance = DateTime.Parse(lastAvailableBackupDateElement.Value, CultureInfo.InvariantCulture); serviceResourceInstance.LastAvailableBackupDate = lastAvailableBackupDateInstance; } XElement nameElement = serviceResourceElement.Element(XName.Get("Name", "http://schemas.microsoft.com/windowsazure")); if (nameElement != null) { string nameInstance = nameElement.Value; serviceResourceInstance.Name = nameInstance; } XElement typeElement = serviceResourceElement.Element(XName.Get("Type", "http://schemas.microsoft.com/windowsazure")); if (typeElement != null) { string typeInstance = typeElement.Value; serviceResourceInstance.Type = typeInstance; } XElement stateElement = serviceResourceElement.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) { Tracing.Exit(invocationId, result); } return(result); } finally { if (httpResponse != null) { httpResponse.Dispose(); } } } finally { if (httpRequest != null) { httpRequest.Dispose(); } } }