/// <summary>
        /// Lists all the recovery points of the given item protected by your
        /// Recovery Services Vault according to the query filter supplied in
        /// the arguments.
        /// </summary>
        /// <param name='resourceGroupName'>
        /// Required. Resource group name of your recovery services vault.
        /// </param>
        /// <param name='resourceName'>
        /// Required. Name of your recovery services vault.
        /// </param>
        /// <param name='customRequestHeaders'>
        /// Optional. Request header parameters.
        /// </param>
        /// <param name='fabricName'>
        /// Optional. Fabric name of the protected item.
        /// </param>
        /// <param name='containerName'>
        /// Optional. Name of the container where the protected item belongs to.
        /// </param>
        /// <param name='protectedItemName'>
        /// Optional. Name of the protected item whose recovery points are to
        /// be fetched.
        /// </param>
        /// <param name='queryFilter'>
        /// Optional. Query parameters for listing recovery points.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// List of recovery points as returned by the service with the list
        /// recovery points call.
        /// </returns>
        public async Task <RecoveryPointListResponse> ListAsync(string resourceGroupName, string resourceName, CustomRequestHeaders customRequestHeaders, string fabricName, string containerName, string protectedItemName, RecoveryPointQueryParameters queryFilter, CancellationToken cancellationToken)
        {
            // Validate
            if (resourceGroupName == null)
            {
                throw new ArgumentNullException("resourceGroupName");
            }
            if (resourceName == null)
            {
                throw new ArgumentNullException("resourceName");
            }

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

            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("resourceGroupName", resourceGroupName);
                tracingParameters.Add("resourceName", resourceName);
                tracingParameters.Add("customRequestHeaders", customRequestHeaders);
                tracingParameters.Add("fabricName", fabricName);
                tracingParameters.Add("containerName", containerName);
                tracingParameters.Add("protectedItemName", protectedItemName);
                tracingParameters.Add("queryFilter", queryFilter);
                TracingAdapter.Enter(invocationId, this, "ListAsync", tracingParameters);
            }

            // Construct URL
            string url = "";

            url = url + "/subscriptions/";
            if (this.Client.Credentials.SubscriptionId != null)
            {
                url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId);
            }
            url = url + "/resourceGroups/";
            url = url + Uri.EscapeDataString(resourceGroupName);
            url = url + "/providers/";
            if (this.Client.ResourceNamespace != null)
            {
                url = url + Uri.EscapeDataString(this.Client.ResourceNamespace);
            }
            url = url + "/";
            url = url + "vaults";
            url = url + "/";
            url = url + Uri.EscapeDataString(resourceName);
            url = url + "/backupFabrics/";
            if (fabricName != null)
            {
                url = url + Uri.EscapeDataString(fabricName);
            }
            url = url + "/protectionContainers/";
            if (containerName != null)
            {
                url = url + Uri.EscapeDataString(containerName);
            }
            url = url + "/protectedItems/";
            if (protectedItemName != null)
            {
                url = url + Uri.EscapeDataString(protectedItemName);
            }
            url = url + "/recoveryPoints";
            List <string> queryParameters = new List <string>();

            queryParameters.Add("api-version=2016-05-01");
            List <string> odataFilter = new List <string>();

            if (queryFilter != null && queryFilter.StartDate != null)
            {
                odataFilter.Add("startDate eq '" + Uri.EscapeDataString(queryFilter.StartDate) + "'");
            }
            if (queryFilter != null && queryFilter.EndDate != null)
            {
                odataFilter.Add("endDate eq '" + Uri.EscapeDataString(queryFilter.EndDate) + "'");
            }
            if (odataFilter.Count > 0)
            {
                queryParameters.Add("$filter=" + string.Join(" and ", odataFilter));
            }
            if (queryParameters.Count > 0)
            {
                url = url + "?" + string.Join("&", queryParameters);
            }
            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("Accept-Language", customRequestHeaders.Culture);
                httpRequest.Headers.Add("x-ms-client-request-id", customRequestHeaders.ClientRequestId);

                // 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
                    RecoveryPointListResponse result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                        result = new RecoveryPointListResponse();
                        JToken responseDoc = null;
                        if (string.IsNullOrEmpty(responseContent) == false)
                        {
                            responseDoc = JToken.Parse(responseContent);
                        }

                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            RecoveryPointResourceList recoveryPointListInstance = new RecoveryPointResourceList();
                            result.RecoveryPointList = recoveryPointListInstance;

                            JToken valueArray = responseDoc["value"];
                            if (valueArray != null && valueArray.Type != JTokenType.Null)
                            {
                                foreach (JToken valueValue in ((JArray)valueArray))
                                {
                                    RecoveryPointResource recoveryPointResourceInstance = new RecoveryPointResource();
                                    recoveryPointListInstance.RecoveryPoints.Add(recoveryPointResourceInstance);

                                    JToken propertiesValue = valueValue["properties"];
                                    if (propertiesValue != null && propertiesValue.Type != JTokenType.Null)
                                    {
                                        string typeName = ((string)propertiesValue["objectType"]);
                                        if (typeName == "IaasVMRecoveryPoint")
                                        {
                                            RecoveryPoint recoveryPointInstance = new RecoveryPoint();

                                            JToken recoveryPointTypeValue = propertiesValue["recoveryPointType"];
                                            if (recoveryPointTypeValue != null && recoveryPointTypeValue.Type != JTokenType.Null)
                                            {
                                                string recoveryPointTypeInstance = ((string)recoveryPointTypeValue);
                                                recoveryPointInstance.RecoveryPointType = recoveryPointTypeInstance;
                                            }

                                            JToken recoveryPointTimeValue = propertiesValue["recoveryPointTime"];
                                            if (recoveryPointTimeValue != null && recoveryPointTimeValue.Type != JTokenType.Null)
                                            {
                                                string recoveryPointTimeInstance = ((string)recoveryPointTimeValue);
                                                recoveryPointInstance.RecoveryPointTime = recoveryPointTimeInstance;
                                            }

                                            JToken recoveryPointAdditionalInfoValue = propertiesValue["recoveryPointAdditionalInfo"];
                                            if (recoveryPointAdditionalInfoValue != null && recoveryPointAdditionalInfoValue.Type != JTokenType.Null)
                                            {
                                                string recoveryPointAdditionalInfoInstance = ((string)recoveryPointAdditionalInfoValue);
                                                recoveryPointInstance.RecoveryPointAdditionalInfo = recoveryPointAdditionalInfoInstance;
                                            }

                                            JToken sourceVMStorageTypeValue = propertiesValue["sourceVMStorageType"];
                                            if (sourceVMStorageTypeValue != null && sourceVMStorageTypeValue.Type != JTokenType.Null)
                                            {
                                                string sourceVMStorageTypeInstance = ((string)sourceVMStorageTypeValue);
                                                recoveryPointInstance.SourceVMStorageType = sourceVMStorageTypeInstance;
                                            }
                                            recoveryPointResourceInstance.Properties = recoveryPointInstance;
                                        }
                                        if (typeName == "GenericRecoveryPoint")
                                        {
                                            GenericRecoveryPoint genericRecoveryPointInstance = new GenericRecoveryPoint();

                                            JToken recoveryPointTypeValue2 = propertiesValue["recoveryPointType"];
                                            if (recoveryPointTypeValue2 != null && recoveryPointTypeValue2.Type != JTokenType.Null)
                                            {
                                                string recoveryPointTypeInstance2 = ((string)recoveryPointTypeValue2);
                                                genericRecoveryPointInstance.RecoveryPointType = recoveryPointTypeInstance2;
                                            }

                                            JToken recoveryPointTimeValue2 = propertiesValue["recoveryPointTime"];
                                            if (recoveryPointTimeValue2 != null && recoveryPointTimeValue2.Type != JTokenType.Null)
                                            {
                                                string recoveryPointTimeInstance2 = ((string)recoveryPointTimeValue2);
                                                genericRecoveryPointInstance.RecoveryPointTime = recoveryPointTimeInstance2;
                                            }

                                            JToken recoveryPointAdditionalInfoValue2 = propertiesValue["recoveryPointAdditionalInfo"];
                                            if (recoveryPointAdditionalInfoValue2 != null && recoveryPointAdditionalInfoValue2.Type != JTokenType.Null)
                                            {
                                                string recoveryPointAdditionalInfoInstance2 = ((string)recoveryPointAdditionalInfoValue2);
                                                genericRecoveryPointInstance.RecoveryPointAdditionalInfo = recoveryPointAdditionalInfoInstance2;
                                            }
                                            recoveryPointResourceInstance.Properties = genericRecoveryPointInstance;
                                        }
                                    }

                                    JToken idValue = valueValue["id"];
                                    if (idValue != null && idValue.Type != JTokenType.Null)
                                    {
                                        string idInstance = ((string)idValue);
                                        recoveryPointResourceInstance.Id = idInstance;
                                    }

                                    JToken nameValue = valueValue["name"];
                                    if (nameValue != null && nameValue.Type != JTokenType.Null)
                                    {
                                        string nameInstance = ((string)nameValue);
                                        recoveryPointResourceInstance.Name = nameInstance;
                                    }

                                    JToken typeValue = valueValue["type"];
                                    if (typeValue != null && typeValue.Type != JTokenType.Null)
                                    {
                                        string typeInstance = ((string)typeValue);
                                        recoveryPointResourceInstance.Type = typeInstance;
                                    }

                                    JToken locationValue = valueValue["location"];
                                    if (locationValue != null && locationValue.Type != JTokenType.Null)
                                    {
                                        string locationInstance = ((string)locationValue);
                                        recoveryPointResourceInstance.Location = locationInstance;
                                    }

                                    JToken tagsSequenceElement = ((JToken)valueValue["tags"]);
                                    if (tagsSequenceElement != null && tagsSequenceElement.Type != JTokenType.Null)
                                    {
                                        foreach (JProperty property in tagsSequenceElement)
                                        {
                                            string tagsKey   = ((string)property.Name);
                                            string tagsValue = ((string)property.Value);
                                            recoveryPointResourceInstance.Tags.Add(tagsKey, tagsValue);
                                        }
                                    }

                                    JToken eTagValue = valueValue["eTag"];
                                    if (eTagValue != null && eTagValue.Type != JTokenType.Null)
                                    {
                                        string eTagInstance = ((string)eTagValue);
                                        recoveryPointResourceInstance.ETag = eTagInstance;
                                    }
                                }
                            }

                            JToken nextLinkValue = responseDoc["nextLink"];
                            if (nextLinkValue != null && nextLinkValue.Type != JTokenType.Null)
                            {
                                string nextLinkInstance = ((string)nextLinkValue);
                                recoveryPointListInstance.NextLink = nextLinkInstance;
                            }
                        }
                    }
                    result.StatusCode = statusCode;

                    if (shouldTrace)
                    {
                        TracingAdapter.Exit(invocationId, result);
                    }
                    return(result);
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }
Ejemplo n.º 2
0
        public string RestoreProtectedItem(string rsVaultRgName, string rsVaultName, string containerUri, string protectedItemUri, string sourceResourceId, string storageAccountId, RecoveryPointResource recoveryPointResource)
        {
            string fabricName = CommonTestHelper.GetSetting(TestConstants.ProviderTypeAzureIaasVM);

            RecoveryPoint recoveryPoint = (RecoveryPoint)recoveryPointResource.Properties;

            TriggerRestoreRequest restoreRequest = new TriggerRestoreRequest();

            restoreRequest.Item = new RestoreRequestResource();
            IaasVMRestoreRequest iaasVmRestoreRequest = new IaasVMRestoreRequest();

            if (recoveryPoint.KeyAndSecret != null &&
                recoveryPoint.KeyAndSecret.BekDetails != null &&
                recoveryPoint.KeyAndSecret.KekDetails != null)
            {
                iaasVmRestoreRequest.EncryptionDetails = new EncryptionDetails();
                iaasVmRestoreRequest.EncryptionDetails.EncryptionEnabled = true;
                iaasVmRestoreRequest.EncryptionDetails.KekUrl            = recoveryPoint.KeyAndSecret.KekDetails.KeyUrl;
                iaasVmRestoreRequest.EncryptionDetails.KekVaultId        = recoveryPoint.KeyAndSecret.KekDetails.KeyVaultId;
                iaasVmRestoreRequest.EncryptionDetails.SecretKeyUrl      = recoveryPoint.KeyAndSecret.BekDetails.SecretUrl;
                iaasVmRestoreRequest.EncryptionDetails.SecretKeyVaultId  = recoveryPoint.KeyAndSecret.BekDetails.SecretVaultId;
            }
            iaasVmRestoreRequest.RecoveryPointId  = recoveryPointResource.Name;
            iaasVmRestoreRequest.RecoveryType     = RecoveryType.RestoreDisks.ToString();
            iaasVmRestoreRequest.SourceResourceId = sourceResourceId;
            iaasVmRestoreRequest.StorageAccountId = storageAccountId;
            restoreRequest.Item.Properties        = iaasVmRestoreRequest;

            var response = Client.Restores.TriggerRestore(rsVaultRgName, rsVaultName, CommonTestHelper.GetCustomRequestHeaders(),
                                                          fabricName, containerUri, protectedItemUri, recoveryPointResource.Name, restoreRequest);

            Assert.NotNull(response);
            Assert.Equal(HttpStatusCode.Accepted, response.StatusCode);
            Assert.NotNull(response.Location);
            Assert.NotNull(response.AzureAsyncOperation);
            Assert.NotNull(response.RetryAfter);

            var operationResponse = Client.ProtectedItems.GetProtectedItemOperationResultByURLAsync(response.Location, CommonTestHelper.GetCustomRequestHeaders());

            while (operationResponse.Result.StatusCode == HttpStatusCode.Accepted)
            {
                if (HttpMockServer.Mode == HttpRecorderMode.Record)
                {
                    System.Threading.Thread.Sleep(5 * 1000);
                }
                operationResponse = Client.ProtectedItems.GetProtectedItemOperationResultByURLAsync(response.Location, CommonTestHelper.GetCustomRequestHeaders());
            }

            var operationStatusResponse = Client.GetOperationStatusByURLAsync(response.AzureAsyncOperation, CommonTestHelper.GetCustomRequestHeaders());
            var operationJobResponse    = (OperationStatusJobExtendedInfo)operationStatusResponse.Result.OperationStatus.Properties;

            Assert.NotNull(operationJobResponse.JobId);

            return(operationJobResponse.JobId);
        }