/// <summary>
        /// Queries by ID.
        /// </summary>
        private void GetById()
        {
            ProtectionContainerResponse protectionContainerResponse =
                RecoveryServicesClient.GetAzureSiteRecoveryProtectionContainer(this.Id);

            this.WriteProtectionContainer(protectionContainerResponse.ProtectionContainer);
        }
        /// <summary>
        /// Get the protected container by Id.
        /// </summary>
        /// <param name='protectionContainerId'>
        /// Required. Protection Container ID.
        /// </param>
        /// <param name='customRequestHeaders'>
        /// Optional. Request header parameters.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The response model for the Protection Container object.
        /// </returns>
        public async System.Threading.Tasks.Task <Microsoft.WindowsAzure.Management.SiteRecovery.Models.ProtectionContainerResponse> GetAsync(string protectionContainerId, CustomRequestHeaders customRequestHeaders, CancellationToken cancellationToken)
        {
            // Validate
            if (protectionContainerId == null)
            {
                throw new ArgumentNullException("protectionContainerId");
            }

            // 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("protectionContainerId", protectionContainerId);
                tracingParameters.Add("customRequestHeaders", customRequestHeaders);
                Tracing.Enter(invocationId, this, "GetAsync", tracingParameters);
            }

            // Construct URL
            string url = (this.Client.Credentials.SubscriptionId != null ? this.Client.Credentials.SubscriptionId.Trim() : "") + "/cloudservices/" + this.Client.CloudServiceName.Trim() + "/resources/WAHyperVRecoveryManager/~/HyperVRecoveryManagerVault/" + this.Client.ResourceName.Trim() + "/ProtectionContainers/" + protectionContainerId.Trim() + "?";

            url = url + "api-version=2014-10-27";
            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)
                    {
                        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
                    ProtectionContainerResponse result = null;
                    // Deserialize Response
                    cancellationToken.ThrowIfCancellationRequested();
                    string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

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

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

                        XElement serverIdElement = serviceResourceElement.Element(XName.Get("ServerId", "http://schemas.microsoft.com/windowsazure"));
                        if (serverIdElement != null)
                        {
                            string serverIdInstance = serverIdElement.Value;
                            serviceResourceInstance.ServerId = serverIdInstance;
                        }

                        XElement fabricObjectIdElement = serviceResourceElement.Element(XName.Get("FabricObjectId", "http://schemas.microsoft.com/windowsazure"));
                        if (fabricObjectIdElement != null)
                        {
                            string fabricObjectIdInstance = fabricObjectIdElement.Value;
                            serviceResourceInstance.FabricObjectId = fabricObjectIdInstance;
                        }

                        XElement configurationStatusElement = serviceResourceElement.Element(XName.Get("ConfigurationStatus", "http://schemas.microsoft.com/windowsazure"));
                        if (configurationStatusElement != null)
                        {
                            string configurationStatusInstance = configurationStatusElement.Value;
                            serviceResourceInstance.ConfigurationStatus = configurationStatusInstance;
                        }

                        XElement pairedToElement = serviceResourceElement.Element(XName.Get("PairedTo", "http://schemas.microsoft.com/windowsazure"));
                        if (pairedToElement != null)
                        {
                            string pairedToInstance = pairedToElement.Value;
                            serviceResourceInstance.PairedTo = pairedToInstance;
                        }

                        XElement roleElement = serviceResourceElement.Element(XName.Get("Role", "http://schemas.microsoft.com/windowsazure"));
                        if (roleElement != null)
                        {
                            string roleInstance = roleElement.Value;
                            serviceResourceInstance.Role = roleInstance;
                        }

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

                        XElement idElement = serviceResourceElement.Element(XName.Get("ID", "http://schemas.microsoft.com/windowsazure"));
                        if (idElement != null)
                        {
                            string idInstance = idElement.Value;
                            serviceResourceInstance.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)
                    {
                        Tracing.Exit(invocationId, result);
                    }
                    return(result);
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }
예제 #3
0
        /// <summary>
        /// Get the protected container by Id.
        /// </summary>
        /// <param name='protectionContainerId'>
        /// Required. Protection Container ID.
        /// </param>
        /// <param name='customRequestHeaders'>
        /// Optional. Request header parameters.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The response model for the Protection Container object.
        /// </returns>
        public async Task <ProtectionContainerResponse> GetAsync(string protectionContainerId, CustomRequestHeaders customRequestHeaders, CancellationToken cancellationToken)
        {
            // Validate
            if (protectionContainerId == null)
            {
                throw new ArgumentNullException("protectionContainerId");
            }

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

            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("protectionContainerId", protectionContainerId);
                tracingParameters.Add("customRequestHeaders", customRequestHeaders);
                TracingAdapter.Enter(invocationId, this, "GetAsync", 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 + "/ProtectionContainers/";
            url = url + Uri.EscapeDataString(protectionContainerId);
            List <string> queryParameters = new List <string>();

            queryParameters.Add("api-version=2015-04-10");
            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
                    ProtectionContainerResponse result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

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

                        XElement protectionContainerElement = responseDoc.Element(XName.Get("ProtectionContainer", "http://schemas.microsoft.com/windowsazure"));
                        if (protectionContainerElement != null)
                        {
                            ProtectionContainer protectionContainerInstance = new ProtectionContainer();
                            result.ProtectionContainer = protectionContainerInstance;

                            XElement serverIdElement = protectionContainerElement.Element(XName.Get("ServerId", "http://schemas.microsoft.com/windowsazure"));
                            if (serverIdElement != null)
                            {
                                string serverIdInstance = serverIdElement.Value;
                                protectionContainerInstance.ServerId = serverIdInstance;
                            }

                            XElement fabricObjectIdElement = protectionContainerElement.Element(XName.Get("FabricObjectId", "http://schemas.microsoft.com/windowsazure"));
                            if (fabricObjectIdElement != null)
                            {
                                string fabricObjectIdInstance = fabricObjectIdElement.Value;
                                protectionContainerInstance.FabricObjectId = fabricObjectIdInstance;
                            }

                            XElement roleElement = protectionContainerElement.Element(XName.Get("Role", "http://schemas.microsoft.com/windowsazure"));
                            if (roleElement != null)
                            {
                                string roleInstance = roleElement.Value;
                                protectionContainerInstance.Role = roleInstance;
                            }

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

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

                            XElement availableProtectionProfilesSequenceElement = protectionContainerElement.Element(XName.Get("AvailableProtectionProfiles", "http://schemas.microsoft.com/windowsazure"));
                            if (availableProtectionProfilesSequenceElement != null)
                            {
                                foreach (XElement availableProtectionProfilesElement in availableProtectionProfilesSequenceElement.Elements(XName.Get("ProtectionProfile", "http://schemas.microsoft.com/windowsazure")))
                                {
                                    ProtectionProfile protectionProfileInstance = new ProtectionProfile();
                                    protectionContainerInstance.AvailableProtectionProfiles.Add(protectionProfileInstance);

                                    XElement protectedEntityCountElement = availableProtectionProfilesElement.Element(XName.Get("ProtectedEntityCount", "http://schemas.microsoft.com/windowsazure"));
                                    if (protectedEntityCountElement != null)
                                    {
                                        int protectedEntityCountInstance = int.Parse(protectedEntityCountElement.Value, CultureInfo.InvariantCulture);
                                        protectionProfileInstance.ProtectedEntityCount = protectedEntityCountInstance;
                                    }

                                    XElement replicationProviderElement = availableProtectionProfilesElement.Element(XName.Get("ReplicationProvider", "http://schemas.microsoft.com/windowsazure"));
                                    if (replicationProviderElement != null)
                                    {
                                        string replicationProviderInstance = replicationProviderElement.Value;
                                        protectionProfileInstance.ReplicationProvider = replicationProviderInstance;
                                    }

                                    XElement replicationProviderSettingElement = availableProtectionProfilesElement.Element(XName.Get("ReplicationProviderSetting", "http://schemas.microsoft.com/windowsazure"));
                                    if (replicationProviderSettingElement != null)
                                    {
                                        string replicationProviderSettingInstance = replicationProviderSettingElement.Value;
                                        protectionProfileInstance.ReplicationProviderSetting = replicationProviderSettingInstance;
                                    }

                                    XElement canDissociateElement = availableProtectionProfilesElement.Element(XName.Get("CanDissociate", "http://schemas.microsoft.com/windowsazure"));
                                    if (canDissociateElement != null)
                                    {
                                        bool canDissociateInstance = bool.Parse(canDissociateElement.Value);
                                        protectionProfileInstance.CanDissociate = canDissociateInstance;
                                    }

                                    XElement associationDetailSequenceElement = availableProtectionProfilesElement.Element(XName.Get("AssociationDetail", "http://schemas.microsoft.com/windowsazure"));
                                    if (associationDetailSequenceElement != null)
                                    {
                                        foreach (XElement associationDetailElement in associationDetailSequenceElement.Elements(XName.Get("ProtectionProfileAssociationDetails", "http://schemas.microsoft.com/windowsazure")))
                                        {
                                            ProtectionProfileAssociationDetails protectionProfileAssociationDetailsInstance = new ProtectionProfileAssociationDetails();
                                            protectionProfileInstance.AssociationDetail.Add(protectionProfileAssociationDetailsInstance);

                                            XElement primaryProtectionContainerIdElement = associationDetailElement.Element(XName.Get("PrimaryProtectionContainerId", "http://schemas.microsoft.com/windowsazure"));
                                            if (primaryProtectionContainerIdElement != null)
                                            {
                                                string primaryProtectionContainerIdInstance = primaryProtectionContainerIdElement.Value;
                                                protectionProfileAssociationDetailsInstance.PrimaryProtectionContainerId = primaryProtectionContainerIdInstance;
                                            }

                                            XElement recoveryProtectionContainerIdElement = associationDetailElement.Element(XName.Get("RecoveryProtectionContainerId", "http://schemas.microsoft.com/windowsazure"));
                                            if (recoveryProtectionContainerIdElement != null)
                                            {
                                                string recoveryProtectionContainerIdInstance = recoveryProtectionContainerIdElement.Value;
                                                protectionProfileAssociationDetailsInstance.RecoveryProtectionContainerId = recoveryProtectionContainerIdInstance;
                                            }

                                            XElement associationStatusElement = associationDetailElement.Element(XName.Get("AssociationStatus", "http://schemas.microsoft.com/windowsazure"));
                                            if (associationStatusElement != null)
                                            {
                                                string associationStatusInstance = associationStatusElement.Value;
                                                protectionProfileAssociationDetailsInstance.AssociationStatus = associationStatusInstance;
                                            }
                                        }
                                    }

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

                                    XElement idElement = availableProtectionProfilesElement.Element(XName.Get("ID", "http://schemas.microsoft.com/windowsazure"));
                                    if (idElement != null)
                                    {
                                        string idInstance = idElement.Value;
                                        protectionProfileInstance.ID = idInstance;
                                    }
                                }
                            }

                            XElement nameElement2 = protectionContainerElement.Element(XName.Get("Name", "http://schemas.microsoft.com/windowsazure"));
                            if (nameElement2 != null)
                            {
                                string nameInstance2 = nameElement2.Value;
                                protectionContainerInstance.Name = nameInstance2;
                            }

                            XElement idElement2 = protectionContainerElement.Element(XName.Get("ID", "http://schemas.microsoft.com/windowsazure"));
                            if (idElement2 != null)
                            {
                                string idInstance2 = idElement2.Value;
                                protectionContainerInstance.ID = idInstance2;
                            }
                        }
                    }
                    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();
                }
            }
        }