/// <summary> /// This method makes a callback to an AsyncResponseParser and takes a service management request and certificate and sends a request to /// the management endpoint /// </summary> public Task <WebResponse> MakeASyncRequest(ServiceManagementRequest serviceManagementRequest, ServiceManager.AsyncResponseParser parser, ServiceManager.AsyncResponseException error) { if (ServicePointManager.ServerCertificateValidationCallback == null) { ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, errors) => true; } ServicePointManager.DefaultConnectionLimit = 40; HttpWebRequest request = BuildAzureHttpRequest(serviceManagementRequest); return(Task <WebResponse> .Factory .FromAsync(request.BeginGetResponse, request.EndGetResponse, null) .ContinueWith <WebResponse>(task => { if (task.Exception != null) { if (task.Exception.InnerException.GetType() == typeof(WebException)) { error((WebException)task.Exception.InnerException); } return null; } var response = (HttpWebResponse)task.Result; parser(response); return response; })); }
/// <summary> /// Builds an Azure request which is then sent to the Management Portal /// </summary> private HttpWebRequest BuildAzureHttpRequest(ServiceManagementRequest serviceManagementRequest) { string optionalData = serviceManagementRequest.OptionalData != null ? "/" + serviceManagementRequest.OptionalData : String.Empty; string operationId = serviceManagementRequest.OperationId == null ? "" : "/" + serviceManagementRequest.OperationId; string serviceType = serviceManagementRequest.ServiceType == null ? "" : "/" + serviceManagementRequest.ServiceType; string requestUriString = String.Format("{0}/{1}", serviceManagementRequest.BaseUri, serviceManagementRequest.SubscriptionId); var requestUri = new Uri(requestUriString + serviceType + operationId + optionalData); Builder.SetUri(requestUri); //var request = (HttpWebRequest) WebRequest.Create(requestUri); if (serviceManagementRequest.Certificate == null && !serviceManagementRequest.RequestWithoutCertificate) { throw new ApplicationException("unable to send management request without valid certificate"); } if (serviceManagementRequest.Certificate != null) { Builder.AddCertificate(serviceManagementRequest.Certificate);//request.ClientCertificates.Add(serviceManagementRequest.Certificate); } foreach (var item in serviceManagementRequest.AdditionalHeaders) { if (!Builder.HttpHeaderExists(item.Key)) { Builder.AddHeader(item.Key, item.Value);//request.Headers.Add(item.Key, item.Value); } } Builder.SetMethod(serviceManagementRequest.HttpVerb ?? "GET"); //request.Method = serviceManagementRequest.HttpVerb ?? "GET"; Builder.SetContentType(serviceManagementRequest.ContentType ?? "application/xml"); //request.ContentType = serviceManagementRequest.ContentType ?? "application/xml"; Builder.SetBody(serviceManagementRequest.Body); return(Builder.Create()); }
/// <summary> /// Executes the request and waits for a response from the Service Management API /// Control is delegated back to the calling class when the reponse comes back which releases the WaitHandle /// </summary> public virtual void Execute() { _exception = null; var serviceManagementRequest = new ServiceManagementRequest { BaseUri = BaseRequestUri + (IsManagement ? ":8443" : ""), HttpVerb = HttpVerb, OptionalData = HttpCommand, ServiceType = ServiceType, OperationId = OperationId, SubscriptionId = SubscriptionId, Body = CreatePayload(), Certificate = Certificate, AdditionalHeaders = AdditionalHeaders, ContentType = ContentType, Accept = Accept, RequestWithoutCertificate = !(UseCertificate.HasValue && UseCertificate.Value) }; CurrentQueryManager = CurrentQueryManager ?? new QueryManager(); CurrentQueryManager.MakeASyncRequest(serviceManagementRequest, ResponseCallback, ErrorResponseCallback); // wait for up to 30 minutes - if a deployment takes longer than that ... it's probably HPC! SitAndWait.WaitOne(200000); if (_lastFailureResponse != null) { throw new FluentManagementException(_lastFailureResponse, "ServiceCommand"); } if (_exception != null) { throw new FluentManagementWebException(_exception as WebException); } }
public void CreateWebRequest_returns_request_with_xml_contenttype() { var certificate = new X509Certificate2(); var request = ServiceManagementRequest.CreateWebRequest(new Uri("http://foo.bar.baz"), certificate); Assert.AreEqual("application/xml", request.ContentType); }
public void CreateWebRequest_returns_request_with_xmsversion_header() { var certificate = new X509Certificate2(); var request = ServiceManagementRequest.CreateWebRequest(new Uri("http://foo.bar.baz"), certificate); Assert.IsNotNull(request.Headers.Get("x-ms-version")); Assert.AreEqual("2014-06-01", request.Headers.Get("x-ms-version")); }
/// <summary> /// Executes the request and waits for a response from the Service Management API /// Control is delegated back to the calling class when the reponse comes back which releases the WaitHandle /// </summary> public virtual void Execute() { var serviceManagementRequest = new ServiceManagementRequest { BaseUri = BaseRequestUri, HttpVerb = HttpVerb, OptionalData = HttpCommand, ServiceType = ServiceType, OperationId = OperationId, SubscriptionId = SubscriptionId, Body = CreateXmlPayload(), Certificate = Certificate, AdditionalHeaders = AdditionalHeaders, ContentType = ContentType, RequestWithoutCertificate = !(UseCertificate.HasValue && UseCertificate.Value) }; CurrentQueryManager = CurrentQueryManager ?? new QueryManager(); CurrentQueryManager.MakeASyncRequest(serviceManagementRequest, ResponseCallback, ErrorResponseCallback); // wait for up to 30 minutes - if a deployment takes longer than that ... it's probably HPC! SitAndWait.WaitOne(200000); }
public static RoleModel GetRoleDetails([NotNull] string subscriptionId, [NotNull] string thumbprint) { RoleModel roleInfo = null; var certificate = CertificateFactory.GetStoreCertificate(thumbprint); if (certificate == null) { return(null); } var hostedServiceNames = ServiceManagementRequest.GetHostedServiceNames(subscriptionId, certificate); if (hostedServiceNames.Count <= 0) { return(null); } foreach (var hostedServiceName in hostedServiceNames) { if (string.IsNullOrEmpty(hostedServiceName)) { continue; } var data = ServiceManagementRequest.GetHostedService(hostedServiceName, subscriptionId, certificate); if (data == null) { continue; } var deploymentLocation = data.Element(XName.Get("HostedServiceProperties", ServiceManagementRequest.ANS)) .Element(XName.Get("Location", ServiceManagementRequest.ANS)).Value; var deploymentXElements = data.Elements(XName.Get("Deployments", ServiceManagementRequest.ANS)) .Elements(XName.Get("Deployment", ServiceManagementRequest.ANS)) .ToList(); if (deploymentXElements.Count <= 0) { continue; } foreach ( var deploymentSlotName in from deployment in deploymentXElements where deployment != null let currentDeploymentId = deployment.Element(XName.Get("PrivateID", ServiceManagementRequest.ANS)).Value where currentDeploymentId == RoleEnvironment.DeploymentId select deployment.Element(XName.Get("DeploymentSlot", ServiceManagementRequest.ANS)).Value ) { roleInfo = new RoleModel() { RoleName = hostedServiceName, DeploymentSlot = deploymentSlotName, Location = deploymentLocation }; break; } //Exit as soon as the role is found if (roleInfo != null) { break; } } return(roleInfo); }
public void MakeASyncRequest(ServiceManagementRequest serviceManagementRequest, ServiceManager.AsyncResponseParser parser) { MakeASyncRequest(serviceManagementRequest, parser, null); }