/// <summary>
 /// Initializes a new instance of the <see cref="ASRStorage" /> class with required
 /// parameters.
 /// </summary>
 /// <param name="storage">Storage object</param>
 public ASRStorage(AsrStorage storage)
 {
     this.ID             = storage.ID;
     this.Name           = storage.Name;
     this.Type           = storage.Type;
     this.FabricObjectID = storage.FabricObjectID;
     this.FabricType     = storage.FabricType;
     this.ServerId       = storage.ServerID;
 }
示例#2
0
        /// <summary>
        /// Get the list of all storages under the vault.
        /// </summary>
        /// <param name='serverId'>
        /// Required. Server Id.
        /// </param>
        /// <param name='customRequestHeaders'>
        /// Optional. Request header parameters.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The response model for the list storage operation.
        /// </returns>
        public async Task <StorageListResponse> ListAsync(string serverId, CustomRequestHeaders customRequestHeaders, CancellationToken cancellationToken)
        {
            // Validate
            if (serverId == null)
            {
                throw new ArgumentNullException("serverId");
            }

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

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

            // Construct URL
            string url = "";

            if (this.Client.Credentials.SubscriptionId != null)
            {
                url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId);
            }
            url = url + "/cloudservices/";
            url = url + Uri.EscapeDataString(this.Client.CloudServiceName);
            url = url + "/resources/";
            url = url + "WAHyperVRecoveryManager";
            url = url + "/~/";
            url = url + "HyperVRecoveryManagerVault";
            url = url + "/";
            url = url + Uri.EscapeDataString(this.Client.ResourceName);
            url = url + "/Storages";
            List <string> queryParameters = new List <string>();

            queryParameters.Add("api-version=2015-02-10");
            queryParameters.Add("ServerId=" + Uri.EscapeDataString(serverId));
            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", "application/xml");
                httpRequest.Headers.Add("x-ms-client-request-id", customRequestHeaders.ClientRequestId);
                httpRequest.Headers.Add("x-ms-version", "2013-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
                    StorageListResponse result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

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

                        XElement arrayOfStorageSequenceElement = responseDoc.Element(XName.Get("ArrayOfStorage", "http://schemas.microsoft.com/windowsazure"));
                        if (arrayOfStorageSequenceElement != null)
                        {
                            foreach (XElement arrayOfStorageElement in arrayOfStorageSequenceElement.Elements(XName.Get("Storage", "http://schemas.microsoft.com/windowsazure")))
                            {
                                AsrStorage storageInstance = new AsrStorage();
                                result.Storages.Add(storageInstance);

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

                                XElement fabricObjectIDElement = arrayOfStorageElement.Element(XName.Get("FabricObjectID", "http://schemas.microsoft.com/windowsazure"));
                                if (fabricObjectIDElement != null)
                                {
                                    string fabricObjectIDInstance = fabricObjectIDElement.Value;
                                    storageInstance.FabricObjectID = fabricObjectIDInstance;
                                }

                                XElement fabricTypeElement = arrayOfStorageElement.Element(XName.Get("FabricType", "http://schemas.microsoft.com/windowsazure"));
                                if (fabricTypeElement != null)
                                {
                                    string fabricTypeInstance = fabricTypeElement.Value;
                                    storageInstance.FabricType = fabricTypeInstance;
                                }

                                XElement serverIDElement = arrayOfStorageElement.Element(XName.Get("ServerID", "http://schemas.microsoft.com/windowsazure"));
                                if (serverIDElement != null)
                                {
                                    string serverIDInstance = serverIDElement.Value;
                                    storageInstance.ServerID = serverIDInstance;
                                }

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

                                XElement idElement = arrayOfStorageElement.Element(XName.Get("ID", "http://schemas.microsoft.com/windowsazure"));
                                if (idElement != null)
                                {
                                    string idInstance = idElement.Value;
                                    storageInstance.ID = idInstance;
                                }
                            }
                        }
                    }
                    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();
                }
            }
        }
        public void SanE2ETest()
        {
            using (UndoContext context = UndoContext.Current)
            {
                try
                {
                    context.Start();
                    var client = GetSiteRecoveryClient(CustomHttpHandler);

                    var requestHeaders = RequestHeaders;
                    requestHeaders.AgentAuthenticationHeader = GenerateAgentAuthenticationHeader(requestHeaders.ClientRequestId);

                    string containerId      = "fc1e58ee-b96a-46fe-8afe-330f7ea545a1_d6a83495-5a6a-4ceb-9dc3-2829f6719032";
                    string entityId         = "a7b4f73c-7a02-4fa2-b895-ddfcfceb0d7d";
                    string primaryServerId  = "fc1e58ee-b96a-46fe-8afe-330f7ea545a1";
                    string recoveryServerId = "e19be056-3a6b-4239-8d01-b0820bc1aeaf";

                    //var responseServer = client.Servers.List(requestHeaders);

                    //var responsePE = client.ProtectionEntity.Get(containerId, entityId, RequestHeaders);

                    var responseStoragePoolPaired = client.StoragePoolMappings.List(primaryServerId, recoveryServerId, requestHeaders);

                    var        responseStoragePrimary = client.Storages.List(primaryServerId, requestHeaders);
                    AsrStorage storagePoolPrimary     = null;
                    foreach (var storage in responseStoragePrimary.Storages)
                    {
                        if (storage.Type == "Pool")
                        {
                            if (storage.StoragePools.Count > 0)
                            {
                                storagePoolPrimary = storage;
                            }
                        }
                    }

                    var        responseStorageRecovery = client.Storages.List(recoveryServerId, requestHeaders);
                    AsrStorage storagePoolRecovery     = null;
                    foreach (var storage in responseStorageRecovery.Storages)
                    {
                        if (storage.Type == "Pool")
                        {
                            if (storage.StoragePools.Count > 0)
                            {
                                storagePoolRecovery = storage;
                            }
                        }
                    }

                    StoragePoolMappingInput storagePoolMappingInput = new StoragePoolMappingInput();
                    storagePoolMappingInput.PrimaryServerId       = primaryServerId;
                    storagePoolMappingInput.RecoveryServerId      = recoveryServerId;
                    storagePoolMappingInput.PrimaryArrayId        = storagePoolPrimary.ID;
                    storagePoolMappingInput.RecoveryArrayId       = storagePoolRecovery.ID;
                    storagePoolMappingInput.PrimaryStoragePoolId  = storagePoolPrimary.StoragePools[0].ID;
                    storagePoolMappingInput.RecoveryStoragePoolId = storagePoolRecovery.StoragePools[0].ID;
                    var responseStoragePoolPair   = client.StoragePoolMappings.Create(storagePoolMappingInput, requestHeaders);
                    var responseStoragePoolUnpair = client.StoragePoolMappings.Delete(storagePoolMappingInput, requestHeaders);

                    FailoverRequest request = new FailoverRequest();

                    // Planned Failover RG
                    request = new PlannedFailoverRequest();
                    request.ReplicationProvider = "San";
                    request.FailoverDirection   = "PrimaryToRecovery";
                    var response = client.ProtectionEntity.PlannedFailover(containerId, entityId, (PlannedFailoverRequest)request, requestHeaders);
                    ValidateResponse(response);
                    WaitForJobToComplete(client, response.Job.ID);

                    // Reverse protect RG
                    request = new ReprotectRequest();
                    request.ReplicationProvider = "San";
                    request.FailoverDirection   = "RecoveryToPrimary";
                    response = client.ProtectionEntity.Reprotect(containerId, entityId, (ReprotectRequest)request, requestHeaders);
                    ValidateResponse(response);
                    WaitForJobToComplete(client, response.Job.ID);

                    // UnPlanned Failover RG
                    request = new UnplannedFailoverRequest();
                    request.ReplicationProvider = "San";
                    request.FailoverDirection   = "RecoveryToPrimary";
                    response = client.ProtectionEntity.UnplannedFailover(containerId, entityId, (UnplannedFailoverRequest)request, requestHeaders);
                    ValidateResponse(response);
                    WaitForJobToComplete(client, response.Job.ID);

                    // Reverse protect RG
                    request = new ReprotectRequest();
                    request.ReplicationProvider = "San";
                    request.FailoverDirection   = "PrimaryToRecovery";
                    response = client.ProtectionEntity.Reprotect(containerId, entityId, (ReprotectRequest)request, requestHeaders);
                    ValidateResponse(response);
                    WaitForJobToComplete(client, response.Job.ID);

                    // Test Failover RG
                    request = new TestFailoverRequest();
                    request.ReplicationProvider = "San";
                    request.FailoverDirection   = "PrimaryToRecovery";
                    ((TestFailoverRequest)request).NetworkType = "NoNetworkAttachAsInput";
                    ((TestFailoverRequest)request).NetworkID   = "xxx";
                    response = client.ProtectionEntity.TestFailover(containerId, entityId, (TestFailoverRequest)request, requestHeaders);
                    ValidateResponse(response);
                }
                catch
                {
                    //skip
                }
            }
        }