示例#1
0
        protected override void OnProcessRecord()
        {
            ServiceManagementProfile.Initialize();

            var parameters = new LoadBalancerCreateParameters
            {
                Name = this.InternalLoadBalancerName,
                FrontendIPConfiguration = new FrontendIPConfiguration
                {
                    Type       = FrontendIPConfigurationType.Private,
                    SubnetName = this.SubnetName,
                    StaticVirtualNetworkIPAddress = this.StaticVNetIPAddress == null ? null
                                                  : this.StaticVNetIPAddress.ToString()
                }
            };

            ExecuteClientActionNewSM(null,
                                     CommandRuntime.ToString(),
                                     () =>
            {
                var deploymentName = this.ComputeClient.Deployments.GetBySlot(
                    this.ServiceName,
                    DeploymentSlot.Production).Name;

                return(this.ComputeClient.LoadBalancers.Create(
                           this.ServiceName,
                           deploymentName,
                           parameters));
            });
        }
示例#2
0
        protected PSArgument[] CreateLoadBalancerCreateParameters()
        {
            string serviceName    = string.Empty;
            string deploymentName = string.Empty;
            LoadBalancerCreateParameters parameters = new LoadBalancerCreateParameters();

            return(ConvertFromObjectsToArguments(new string[] { "ServiceName", "DeploymentName", "Parameters" }, new object[] { serviceName, deploymentName, parameters }));
        }
示例#3
0
        protected void ExecuteLoadBalancerCreateMethod(object[] invokeMethodInputParameters)
        {
            string serviceName    = (string)ParseParameter(invokeMethodInputParameters[0]);
            string deploymentName = (string)ParseParameter(invokeMethodInputParameters[1]);
            LoadBalancerCreateParameters parameters = (LoadBalancerCreateParameters)ParseParameter(invokeMethodInputParameters[2]);

            var result = LoadBalancerClient.Create(serviceName, deploymentName, parameters);

            WriteObject(result);
        }
示例#4
0
 /// <summary>
 /// Add an internal load balancer to a an existing deployment. When
 /// used by an input endpoint, the internal load balancer will provide
 /// an additional private VIP that can be used for load balancing to
 /// the roles in this deployment.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.Compute.ILoadBalancerOperations.
 /// </param>
 /// <param name='serviceName'>
 /// Required. The name of the service.
 /// </param>
 /// <param name='deploymentName'>
 /// Required. The name of the deployment.
 /// </param>
 /// <param name='parameters'>
 /// Required. Parameters supplied to the Create Load Balancer operation.
 /// </param>
 /// <returns>
 /// The response body contains the status of the specified asynchronous
 /// operation, indicating whether it has succeeded, is inprogress, or
 /// has failed. Note that this status is distinct from the HTTP status
 /// code returned for the Get Operation Status operation itself. If
 /// the asynchronous operation succeeded, the response body includes
 /// the HTTP status code for the successful request. If the
 /// asynchronous operation failed, the response body includes the HTTP
 /// status code for the failed request and error information regarding
 /// the failure.
 /// </returns>
 public static OperationStatusResponse BeginCreating(this ILoadBalancerOperations operations, string serviceName, string deploymentName, LoadBalancerCreateParameters parameters)
 {
     return(Task.Factory.StartNew((object s) =>
     {
         return ((ILoadBalancerOperations)s).BeginCreatingAsync(serviceName, deploymentName, parameters);
     }
                                  , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult());
 }
示例#5
0
 /// <summary>
 /// Add an internal load balancer to a an existing deployment. When
 /// used by an input endpoint, the internal load balancer will provide
 /// an additional private VIP that can be used for load balancing to
 /// the roles in this deployment.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.Compute.ILoadBalancerOperations.
 /// </param>
 /// <param name='serviceName'>
 /// Required. The name of the service.
 /// </param>
 /// <param name='deploymentName'>
 /// Required. The name of the deployment.
 /// </param>
 /// <param name='parameters'>
 /// Required. Parameters supplied to the Create Load Balancer operation.
 /// </param>
 /// <returns>
 /// The response body contains the status of the specified asynchronous
 /// operation, indicating whether it has succeeded, is inprogress, or
 /// has failed. Note that this status is distinct from the HTTP status
 /// code returned for the Get Operation Status operation itself. If
 /// the asynchronous operation succeeded, the response body includes
 /// the HTTP status code for the successful request. If the
 /// asynchronous operation failed, the response body includes the HTTP
 /// status code for the failed request and error information regarding
 /// the failure.
 /// </returns>
 public static Task <OperationStatusResponse> CreateAsync(this ILoadBalancerOperations operations, string serviceName, string deploymentName, LoadBalancerCreateParameters parameters)
 {
     return(operations.CreateAsync(serviceName, deploymentName, parameters, CancellationToken.None));
 }
        /// <summary>
        /// Add an internal load balancer to a an existing deployment. When
        /// used by an input endpoint, the internal load balancer will provide
        /// an additional private VIP that can be used for load balancing to
        /// the roles in this deployment.
        /// </summary>
        /// <param name='serviceName'>
        /// Required. The name of the service.
        /// </param>
        /// <param name='deploymentName'>
        /// Required. The name of the deployment.
        /// </param>
        /// <param name='parameters'>
        /// Required. Parameters supplied to the Create Load Balancer operation.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The response body contains the status of the specified asynchronous
        /// operation, indicating whether it has succeeded, is inprogress, or
        /// has failed. Note that this status is distinct from the HTTP status
        /// code returned for the Get Operation Status operation itself. If
        /// the asynchronous operation succeeded, the response body includes
        /// the HTTP status code for the successful request. If the
        /// asynchronous operation failed, the response body includes the HTTP
        /// status code for the failed request and error information regarding
        /// the failure.
        /// </returns>
        public async Task <OperationStatusResponse> BeginCreatingAsync(string serviceName, string deploymentName, LoadBalancerCreateParameters parameters, CancellationToken cancellationToken)
        {
            // Validate
            if (serviceName == null)
            {
                throw new ArgumentNullException("serviceName");
            }
            if (deploymentName == null)
            {
                throw new ArgumentNullException("deploymentName");
            }
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }

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

            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("serviceName", serviceName);
                tracingParameters.Add("deploymentName", deploymentName);
                tracingParameters.Add("parameters", parameters);
                TracingAdapter.Enter(invocationId, this, "BeginCreatingAsync", tracingParameters);
            }

            // Construct URL
            string url = "";

            url = url + "/";
            if (this.Client.Credentials.SubscriptionId != null)
            {
                url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId);
            }
            url = url + "/services/hostedservices/";
            url = url + Uri.EscapeDataString(serviceName);
            url = url + "/deployments/";
            url = url + Uri.EscapeDataString(deploymentName);
            url = url + "/loadbalancers";
            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", "2015-04-01");

                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);

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

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

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

                if (parameters.FrontendIPConfiguration != null)
                {
                    XElement frontendIpConfigurationElement = new XElement(XName.Get("FrontendIpConfiguration", "http://schemas.microsoft.com/windowsazure"));
                    loadBalancerElement.Add(frontendIpConfigurationElement);

                    if (parameters.FrontendIPConfiguration.Type != null)
                    {
                        XElement typeElement = new XElement(XName.Get("Type", "http://schemas.microsoft.com/windowsazure"));
                        typeElement.Value = parameters.FrontendIPConfiguration.Type;
                        frontendIpConfigurationElement.Add(typeElement);
                    }

                    if (parameters.FrontendIPConfiguration.SubnetName != null)
                    {
                        XElement subnetNameElement = new XElement(XName.Get("SubnetName", "http://schemas.microsoft.com/windowsazure"));
                        subnetNameElement.Value = parameters.FrontendIPConfiguration.SubnetName;
                        frontendIpConfigurationElement.Add(subnetNameElement);
                    }

                    if (parameters.FrontendIPConfiguration.StaticVirtualNetworkIPAddress != null)
                    {
                        XElement staticVirtualNetworkIPAddressElement = new XElement(XName.Get("StaticVirtualNetworkIPAddress", "http://schemas.microsoft.com/windowsazure"));
                        staticVirtualNetworkIPAddressElement.Value = parameters.FrontendIPConfiguration.StaticVirtualNetworkIPAddress;
                        frontendIpConfigurationElement.Add(staticVirtualNetworkIPAddressElement);
                    }
                }

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

                    // Create Result
                    OperationStatusResponse result = null;
                    // Deserialize Response
                    result            = new OperationStatusResponse();
                    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();
                }
            }
        }
        /// <summary>
        /// Add an internal load balancer to a an existing deployment. When
        /// used by an input endpoint, the internal load balancer will provide
        /// an additional private VIP that can be used for load balancing to
        /// the roles in this deployment.
        /// </summary>
        /// <param name='serviceName'>
        /// Required. The name of the service.
        /// </param>
        /// <param name='deploymentName'>
        /// Required. The name of the deployment.
        /// </param>
        /// <param name='parameters'>
        /// Required. Parameters supplied to the Create Load Balancer operation.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The response body contains the status of the specified asynchronous
        /// operation, indicating whether it has succeeded, is inprogress, or
        /// has failed. Note that this status is distinct from the HTTP status
        /// code returned for the Get Operation Status operation itself. If
        /// the asynchronous operation succeeded, the response body includes
        /// the HTTP status code for the successful request. If the
        /// asynchronous operation failed, the response body includes the HTTP
        /// status code for the failed request and error information regarding
        /// the failure.
        /// </returns>
        public async Task <OperationStatusResponse> CreateAsync(string serviceName, string deploymentName, LoadBalancerCreateParameters parameters, CancellationToken cancellationToken)
        {
            ComputeManagementClient client = this.Client;
            bool   shouldTrace             = TracingAdapter.IsEnabled;
            string invocationId            = null;

            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("serviceName", serviceName);
                tracingParameters.Add("deploymentName", deploymentName);
                tracingParameters.Add("parameters", parameters);
                TracingAdapter.Enter(invocationId, this, "CreateAsync", tracingParameters);
            }

            cancellationToken.ThrowIfCancellationRequested();
            OperationStatusResponse response = await client.LoadBalancers.BeginCreatingAsync(serviceName, deploymentName, parameters, cancellationToken).ConfigureAwait(false);

            if (response.Status == OperationStatus.Succeeded)
            {
                return(response);
            }
            cancellationToken.ThrowIfCancellationRequested();
            OperationStatusResponse result = await client.GetOperationStatusAsync(response.RequestId, cancellationToken).ConfigureAwait(false);

            int delayInSeconds = 30;

            if (client.LongRunningOperationInitialTimeout >= 0)
            {
                delayInSeconds = client.LongRunningOperationInitialTimeout;
            }
            while (result.Status == OperationStatus.InProgress)
            {
                cancellationToken.ThrowIfCancellationRequested();
                await TaskEx.Delay(delayInSeconds * 1000, cancellationToken).ConfigureAwait(false);

                cancellationToken.ThrowIfCancellationRequested();
                result = await client.GetOperationStatusAsync(response.RequestId, cancellationToken).ConfigureAwait(false);

                delayInSeconds = 30;
                if (client.LongRunningOperationRetryTimeout >= 0)
                {
                    delayInSeconds = client.LongRunningOperationRetryTimeout;
                }
            }

            if (shouldTrace)
            {
                TracingAdapter.Exit(invocationId, result);
            }

            if (result.Status != OperationStatus.Succeeded)
            {
                if (result.Error != null)
                {
                    CloudException ex = new CloudException(result.Error.Code + " : " + result.Error.Message);
                    ex.Error         = new CloudError();
                    ex.Error.Code    = result.Error.Code;
                    ex.Error.Message = result.Error.Message;
                    if (shouldTrace)
                    {
                        TracingAdapter.Error(invocationId, ex);
                    }
                    throw ex;
                }
                else
                {
                    CloudException ex = new CloudException("");
                    if (shouldTrace)
                    {
                        TracingAdapter.Error(invocationId, ex);
                    }
                    throw ex;
                }
            }

            return(result);
        }
        protected override void OnProcessRecord()
        {
            ServiceManagementProfile.Initialize();

            var parameters = new LoadBalancerCreateParameters
            {
                Name = this.InternalLoadBalancerName,
                FrontendIPConfiguration = new FrontendIPConfiguration
                {
                    Type = FrontendIPConfigurationType.Private,
                    SubnetName = this.SubnetName,
                    StaticVirtualNetworkIPAddress = this.StaticVNetIPAddress == null ? null
                                                  : this.StaticVNetIPAddress.ToString()
                }
            };

            ExecuteClientActionNewSM(null,
                CommandRuntime.ToString(),
                () =>
                {
                    var deploymentName = this.ComputeClient.Deployments.GetBySlot(
                        this.ServiceName,
                        DeploymentSlot.Production).Name;

                    return this.ComputeClient.LoadBalancers.Create(
                        this.ServiceName,
                        deploymentName,
                        parameters);
                });
        }