public void AddToRestoreDatabaseOperations(RestoreDatabaseOperation restoreDatabaseOperation)
 {
     base.AddObject("RestoreDatabaseOperations", restoreDatabaseOperation);
 }
 public static RestoreDatabaseOperation CreateRestoreDatabaseOperation(global::System.Guid requestID, global::System.DateTime sourceDatabaseCreationDate)
 {
     RestoreDatabaseOperation restoreDatabaseOperation = new RestoreDatabaseOperation();
     restoreDatabaseOperation.RequestID = requestID;
     restoreDatabaseOperation.SourceDatabaseCreationDate = sourceDatabaseCreationDate;
     return restoreDatabaseOperation;
 }
Esempio n. 3
0
        /// <summary>
        /// Execute the command.
        /// </summary>
        public override void ExecuteCmdlet()
        {
            // Obtain the name of the source database / source restorable dropped database from the parameters
            this.SourceDatabaseName =
                this.SourceDatabase != null ? this.SourceDatabase.Name :
                this.SourceRestorableDroppedDatabase != null ? this.SourceRestorableDroppedDatabase.Name :
                this.SourceDatabaseName;

            // Obtain the deletion date of the source restorable dropped database from the parameters
            DateTime?sourceDatabaseDeletionDate = null;

            if (this.SourceRestorableDroppedDatabase != null)
            {
                sourceDatabaseDeletionDate = this.SourceRestorableDroppedDatabase.DeletionDate;
            }
            else if (this.RestorableDropped.IsPresent)
            {
                sourceDatabaseDeletionDate = this.SourceDatabaseDeletionDate;
            }

            // Normalize the deletion date and point in time to UTC, if given
            if (sourceDatabaseDeletionDate != null)
            {
                sourceDatabaseDeletionDate = CmdletCommon.NormalizeToUtc(sourceDatabaseDeletionDate.Value);
            }

            if (this.PointInTime != null)
            {
                this.PointInTime = CmdletCommon.NormalizeToUtc(this.PointInTime.Value);
            }

            IServerDataServiceContext connectionContext = null;

            // If a database object was piped in, use its connection context...
            if (this.SourceDatabase != null)
            {
                connectionContext = this.SourceDatabase.Context;
            }
            else if (this.SourceRestorableDroppedDatabase != null)
            {
                connectionContext = this.SourceRestorableDroppedDatabase.Context;
            }

            // ... but only if it's a cert auth context. Otherwise, create a cert auth context using this.ServerName or the server name of the SQL auth context.
            if (!(connectionContext is ServerDataServiceCertAuth))
            {
                string serverName = this.SourceServerName ?? connectionContext.ServerName;

                connectionContext = ServerDataServiceCertAuth.Create(serverName, Profile, Profile.Context.Subscription);
            }

            string clientRequestId = connectionContext.ClientRequestId;

            try
            {
                RestoreDatabaseOperation operation = connectionContext.RestoreDatabase(
                    this.SourceDatabaseName,
                    sourceDatabaseDeletionDate,
                    this.TargetServerName,
                    this.TargetDatabaseName,
                    this.PointInTime);

                this.WriteObject(operation);
            }
            catch (Exception ex)
            {
                SqlDatabaseExceptionHandler.WriteErrorDetails(
                    this,
                    clientRequestId,
                    ex);
            }
        }
 public static RestoreDatabaseOperation CreateRestoreDatabaseOperation(global::System.Guid requestID)
 {
     RestoreDatabaseOperation restoreDatabaseOperation = new RestoreDatabaseOperation();
     restoreDatabaseOperation.RequestID = requestID;
     return restoreDatabaseOperation;
 }
        /// <summary>
        /// Issues a restore request for an Azure SQL Database.
        /// </summary>
        /// <param name='sourceServerName'>
        /// Required. The name of the Azure SQL Database Server where the
        /// source database is, or was, hosted.
        /// </param>
        /// <param name='parameters'>
        /// Required. Additional parameters for the Create Restore Database
        /// Operation request.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// Contains the response to the Create Restore Database Operation
        /// request.
        /// </returns>
        public async System.Threading.Tasks.Task <Microsoft.WindowsAzure.Management.Sql.Models.RestoreDatabaseOperationCreateResponse> CreateAsync(string sourceServerName, RestoreDatabaseOperationCreateParameters parameters, CancellationToken cancellationToken)
        {
            // Validate
            if (sourceServerName == null)
            {
                throw new ArgumentNullException("sourceServerName");
            }
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }
            if (parameters.SourceDatabaseName == null)
            {
                throw new ArgumentNullException("parameters.SourceDatabaseName");
            }
            if (parameters.TargetDatabaseName == null)
            {
                throw new ArgumentNullException("parameters.TargetDatabaseName");
            }

            // 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("sourceServerName", sourceServerName);
                tracingParameters.Add("parameters", parameters);
                Tracing.Enter(invocationId, this, "CreateAsync", tracingParameters);
            }

            // Construct URL
            string url     = "/" + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId) + "/services/sqlservers/servers/" + Uri.EscapeDataString(sourceServerName) + "/restoredatabaseoperations";
            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.Post;
                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);

                // Serialize Request
                string    requestContent = null;
                XDocument requestDoc     = new XDocument();

                XElement serviceResourceElement = new XElement(XName.Get("ServiceResource", "http://schemas.microsoft.com/windowsazure"));
                requestDoc.Add(serviceResourceElement);

                XElement sourceDatabaseNameElement = new XElement(XName.Get("SourceDatabaseName", "http://schemas.microsoft.com/windowsazure"));
                sourceDatabaseNameElement.Value = parameters.SourceDatabaseName;
                serviceResourceElement.Add(sourceDatabaseNameElement);

                if (parameters.SourceDatabaseDeletionDate != null)
                {
                    XElement sourceDatabaseDeletionDateElement = new XElement(XName.Get("SourceDatabaseDeletionDate", "http://schemas.microsoft.com/windowsazure"));
                    sourceDatabaseDeletionDateElement.Value = string.Format(CultureInfo.InvariantCulture, "{0:O}", parameters.SourceDatabaseDeletionDate.Value.ToUniversalTime());
                    serviceResourceElement.Add(sourceDatabaseDeletionDateElement);
                }

                if (parameters.TargetServerName != null)
                {
                    XElement targetServerNameElement = new XElement(XName.Get("TargetServerName", "http://schemas.microsoft.com/windowsazure"));
                    targetServerNameElement.Value = parameters.TargetServerName;
                    serviceResourceElement.Add(targetServerNameElement);
                }

                XElement targetDatabaseNameElement = new XElement(XName.Get("TargetDatabaseName", "http://schemas.microsoft.com/windowsazure"));
                targetDatabaseNameElement.Value = parameters.TargetDatabaseName;
                serviceResourceElement.Add(targetDatabaseNameElement);

                if (parameters.PointInTime != null)
                {
                    XElement targetUtcPointInTimeElement = new XElement(XName.Get("TargetUtcPointInTime", "http://schemas.microsoft.com/windowsazure"));
                    targetUtcPointInTimeElement.Value = string.Format(CultureInfo.InvariantCulture, "{0:O}", parameters.PointInTime.Value.ToUniversalTime());
                    serviceResourceElement.Add(targetUtcPointInTimeElement);
                }

                requestContent      = requestDoc.ToString();
                httpRequest.Content = new StringContent(requestContent, Encoding.UTF8);
                httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/xml");

                // 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.Created)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, requestContent, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            Tracing.Error(invocationId, ex);
                        }
                        throw ex;
                    }

                    // Create Result
                    RestoreDatabaseOperationCreateResponse result = null;
                    // Deserialize Response
                    cancellationToken.ThrowIfCancellationRequested();
                    string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

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

                    XElement serviceResourceElement2 = responseDoc.Element(XName.Get("ServiceResource", "http://schemas.microsoft.com/windowsazure"));
                    if (serviceResourceElement2 != null)
                    {
                        RestoreDatabaseOperation serviceResourceInstance = new RestoreDatabaseOperation();
                        result.Operation = serviceResourceInstance;

                        XElement requestIDElement = serviceResourceElement2.Element(XName.Get("RequestID", "http://schemas.microsoft.com/windowsazure"));
                        if (requestIDElement != null)
                        {
                            string requestIDInstance = requestIDElement.Value;
                            serviceResourceInstance.Id = requestIDInstance;
                        }

                        XElement sourceDatabaseNameElement2 = serviceResourceElement2.Element(XName.Get("SourceDatabaseName", "http://schemas.microsoft.com/windowsazure"));
                        if (sourceDatabaseNameElement2 != null)
                        {
                            string sourceDatabaseNameInstance = sourceDatabaseNameElement2.Value;
                            serviceResourceInstance.SourceDatabaseName = sourceDatabaseNameInstance;
                        }

                        XElement sourceDatabaseDeletionDateElement2 = serviceResourceElement2.Element(XName.Get("SourceDatabaseDeletionDate", "http://schemas.microsoft.com/windowsazure"));
                        if (sourceDatabaseDeletionDateElement2 != null && string.IsNullOrEmpty(sourceDatabaseDeletionDateElement2.Value) == false)
                        {
                            DateTime sourceDatabaseDeletionDateInstance = DateTime.Parse(sourceDatabaseDeletionDateElement2.Value, CultureInfo.InvariantCulture);
                            serviceResourceInstance.SourceDatabaseDeletionDate = sourceDatabaseDeletionDateInstance;
                        }

                        XElement targetServerNameElement2 = serviceResourceElement2.Element(XName.Get("TargetServerName", "http://schemas.microsoft.com/windowsazure"));
                        if (targetServerNameElement2 != null)
                        {
                            string targetServerNameInstance = targetServerNameElement2.Value;
                            serviceResourceInstance.TargetServerName = targetServerNameInstance;
                        }

                        XElement targetDatabaseNameElement2 = serviceResourceElement2.Element(XName.Get("TargetDatabaseName", "http://schemas.microsoft.com/windowsazure"));
                        if (targetDatabaseNameElement2 != null)
                        {
                            string targetDatabaseNameInstance = targetDatabaseNameElement2.Value;
                            serviceResourceInstance.TargetDatabaseName = targetDatabaseNameInstance;
                        }

                        XElement targetUtcPointInTimeElement2 = serviceResourceElement2.Element(XName.Get("TargetUtcPointInTime", "http://schemas.microsoft.com/windowsazure"));
                        if (targetUtcPointInTimeElement2 != null)
                        {
                            DateTime targetUtcPointInTimeInstance = DateTime.Parse(targetUtcPointInTimeElement2.Value, CultureInfo.InvariantCulture);
                            serviceResourceInstance.PointInTime = targetUtcPointInTimeInstance;
                        }
                    }

                    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();
                }
            }
        }