public void SetIPForwardingOnRoleSucceeds()
        {
            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();
                using (NetworkTestBase _testFixture = new NetworkTestBase())
                {
                    // setup
                    bool storageAccountCreated = false;
                    bool hostedServiceCreated  = false;

                    string serviceName    = _testFixture.GenerateRandomName();
                    string deploymentName = _testFixture.GenerateRandomName();
                    string roleName       = "WebRole1";
                    string location       = _testFixture.ManagementClient.GetDefaultLocation("Storage", "Compute", "PersistentVMRole");

                    string storageAccountName = _testFixture.GenerateRandomName().ToLower();

                    _testFixture.CreateStorageAccount(location, storageAccountName, out storageAccountCreated);
                    _testFixture.SetSimpleVirtualNetwork();
                    _testFixture.CreateHostedService(location, serviceName, out hostedServiceCreated);
                    var deployment = _testFixture.CreatePaaSDeployment(
                        storageAccountName,
                        serviceName,
                        deploymentName,
                        NetworkTestConstants.OneWebOneWorkerPkgFilePath,
                        NetworkTestConstants.VnetOneWebOneWorkerCscfgFilePath,
                        startDeployment: true);

                    try
                    {
                        // action
                        var ipForwardingState = "Enabled";
                        var ipForwarding      = new IPForwardingSetParameters(ipForwardingState);

                        _testFixture.NetworkClient.IPForwarding.SetOnRole(serviceName, deploymentName, roleName,
                                                                          ipForwarding);

                        // assert
                        IPForwardingGetResponse response =
                            _testFixture.NetworkClient.IPForwarding.GetForRole(serviceName, deploymentName, roleName);
                        Assert.Equal(ipForwardingState, response.State);
                    }
                    finally
                    {
                        if (storageAccountCreated)
                        {
                            _testFixture.StorageClient.StorageAccounts.Delete(storageAccountName);
                        }
                        if (hostedServiceCreated)
                        {
                            _testFixture.ComputeClient.HostedServices.DeleteAll(serviceName);
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        public void SetIPForwardingForNetworkInterface(string serviceName, string deploymentName, string roleName, string networkInterfaceName, bool ipForwarding)
        {
            var parameters = new IPForwardingSetParameters()
            {
                State = ipForwarding ? "Enabled" : "Disabled"
            };

            client.IPForwarding.SetOnNetworkInterface(serviceName, deploymentName, roleName, networkInterfaceName, parameters);
        }
Esempio n. 3
0
        /// <summary>
        /// Sets IP Forwarding on a network interface.
        /// </summary>
        /// <param name='serviceName'>
        /// Required.
        /// </param>
        /// <param name='deploymentName'>
        /// Required.
        /// </param>
        /// <param name='roleName'>
        /// Required.
        /// </param>
        /// <param name='networkInterfaceName'>
        /// Required.
        /// </param>
        /// <param name='parameters'>
        /// Required. Parameters supplied to the Set IP Forwarding on network
        /// interface 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 also includes error
        /// information regarding the failure.
        /// </returns>
        public async Task <OperationStatusResponse> BeginSettingIPForwardingOnNetworkInterfaceAsync(string serviceName, string deploymentName, string roleName, string networkInterfaceName, IPForwardingSetParameters parameters, CancellationToken cancellationToken)
        {
            // Validate
            if (serviceName == null)
            {
                throw new ArgumentNullException("serviceName");
            }
            if (deploymentName == null)
            {
                throw new ArgumentNullException("deploymentName");
            }
            if (roleName == null)
            {
                throw new ArgumentNullException("roleName");
            }
            if (networkInterfaceName == null)
            {
                throw new ArgumentNullException("networkInterfaceName");
            }
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }
            if (parameters.State == null)
            {
                throw new ArgumentNullException("parameters.State");
            }

            // 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("roleName", roleName);
                tracingParameters.Add("networkInterfaceName", networkInterfaceName);
                tracingParameters.Add("parameters", parameters);
                TracingAdapter.Enter(invocationId, this, "BeginSettingIPForwardingOnNetworkInterfaceAsync", 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 + "/roles/";
            url = url + Uri.EscapeDataString(roleName);
            url = url + "/networkinterfaces/";
            url = url + Uri.EscapeDataString(networkInterfaceName);
            url = url + "/ipforwarding";
            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", "2016-03-01");

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

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

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

                XElement stateElement = new XElement(XName.Get("State", "http://schemas.microsoft.com/windowsazure"));
                stateElement.Value = parameters.State;
                iPForwardingElement.Add(stateElement);

                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();
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Sets IP Forwarding on a role.
        /// </summary>
        /// <param name='serviceName'>
        /// Required.
        /// </param>
        /// <param name='deploymentName'>
        /// Required.
        /// </param>
        /// <param name='roleName'>
        /// Required.
        /// </param>
        /// <param name='parameters'>
        /// Required. Parameters supplied to the Set IP Forwarding on role
        /// 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 also includes error
        /// information regarding the failure.
        /// </returns>
        public async Task <OperationStatusResponse> SetOnRoleAsync(string serviceName, string deploymentName, string roleName, IPForwardingSetParameters parameters, CancellationToken cancellationToken)
        {
            NetworkManagementClient 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("roleName", roleName);
                tracingParameters.Add("parameters", parameters);
                TracingAdapter.Enter(invocationId, this, "SetOnRoleAsync", tracingParameters);
            }

            cancellationToken.ThrowIfCancellationRequested();
            OperationStatusResponse response = await client.IPForwarding.BeginSettingIPForwardingOnRoleAsync(serviceName, deploymentName, roleName, 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);
        }
        public void SetIPForwardingOnNICSucceeds()
        {
            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();
                using (NetworkTestBase _testFixture = new NetworkTestBase())
                {
                    // setup
                    bool storageAccountCreated = false;
                    bool hostedServiceCreated  = false;

                    string serviceName          = _testFixture.GenerateRandomName();
                    string deploymentName       = _testFixture.GenerateRandomName();
                    string roleName             = _testFixture.GenerateRandomName();
                    string networkInterfaceName = _testFixture.GenerateRandomName();
                    string location             = _testFixture.ManagementClient.GetDefaultLocation("Storage", "Compute", "PersistentVMRole");
                    string virtualNetworkName   = "virtualNetworkSiteName";
                    string subnetName           = "FrontEndSubnet5";

                    string storageAccountName = _testFixture.GenerateRandomName().ToLower();

                    _testFixture.CreateStorageAccount(location, storageAccountName, out storageAccountCreated);
                    _testFixture.SetSimpleVirtualNetwork();
                    _testFixture.CreateHostedService(location, serviceName, out hostedServiceCreated);
                    _testFixture.ComputeClient.VirtualMachines.CreateDeployment(
                        serviceName,
                        _testFixture.CreateMultiNICIaaSDeploymentParameters(
                            serviceName,
                            deploymentName,
                            roleName,
                            networkInterfaceName,
                            storageAccountName,
                            virtualNetworkName,
                            subnetName));

                    try
                    {
                        // action
                        var ipForwardingState = "Enabled";
                        var ipForwarding      = new IPForwardingSetParameters(ipForwardingState);

                        _testFixture.NetworkClient.IPForwarding.SetOnNetworkInterface(
                            serviceName,
                            deploymentName,
                            roleName,
                            networkInterfaceName,
                            ipForwarding);

                        // assert
                        IPForwardingGetResponse response = _testFixture.NetworkClient.IPForwarding.GetForNetworkInterface(
                            serviceName,
                            deploymentName,
                            roleName,
                            networkInterfaceName);
                        Assert.Equal(ipForwardingState, response.State);
                    }
                    finally
                    {
                        if (hostedServiceCreated)
                        {
                            _testFixture.ComputeClient.HostedServices.DeleteAll(serviceName);
                        }
                    }
                }
            }
        }
Esempio n. 6
0
 /// <summary>
 /// Sets IP Forwarding on a network interface.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.Network.IIPForwardingOperations.
 /// </param>
 /// <param name='serviceName'>
 /// Required.
 /// </param>
 /// <param name='deploymentName'>
 /// Required.
 /// </param>
 /// <param name='roleName'>
 /// Required.
 /// </param>
 /// <param name='networkInterfaceName'>
 /// Required.
 /// </param>
 /// <param name='parameters'>
 /// Required. Parameters supplied to the Set IP Forwarding on network
 /// interface 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 also includes error
 /// information regarding the failure.
 /// </returns>
 public static OperationStatusResponse BeginSettingIPForwardingOnNetworkInterface(this IIPForwardingOperations operations, string serviceName, string deploymentName, string roleName, string networkInterfaceName, IPForwardingSetParameters parameters)
 {
     return(Task.Factory.StartNew((object s) =>
     {
         return ((IIPForwardingOperations)s).BeginSettingIPForwardingOnNetworkInterfaceAsync(serviceName, deploymentName, roleName, networkInterfaceName, parameters);
     }
                                  , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult());
 }
Esempio n. 7
0
 /// <summary>
 /// Sets IP Forwarding on a role.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.Network.IIPForwardingOperations.
 /// </param>
 /// <param name='serviceName'>
 /// Required.
 /// </param>
 /// <param name='deploymentName'>
 /// Required.
 /// </param>
 /// <param name='roleName'>
 /// Required.
 /// </param>
 /// <param name='parameters'>
 /// Required. Parameters supplied to the Set IP Forwarding on role
 /// 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 also includes error
 /// information regarding the failure.
 /// </returns>
 public static Task <OperationStatusResponse> SetOnRoleAsync(this IIPForwardingOperations operations, string serviceName, string deploymentName, string roleName, IPForwardingSetParameters parameters)
 {
     return(operations.SetOnRoleAsync(serviceName, deploymentName, roleName, parameters, CancellationToken.None));
 }
Esempio n. 8
0
 /// <summary>
 /// Sets IP Forwarding on a network interface.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.Network.IIPForwardingOperations.
 /// </param>
 /// <param name='serviceName'>
 /// Required.
 /// </param>
 /// <param name='deploymentName'>
 /// Required.
 /// </param>
 /// <param name='roleName'>
 /// Required.
 /// </param>
 /// <param name='networkInterfaceName'>
 /// Required.
 /// </param>
 /// <param name='parameters'>
 /// Required. Parameters supplied to the Set IP Forwarding on network
 /// interface 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 also includes error
 /// information regarding the failure.
 /// </returns>
 public static Task <OperationStatusResponse> BeginSettingIPForwardingOnNetworkInterfaceAsync(this IIPForwardingOperations operations, string serviceName, string deploymentName, string roleName, string networkInterfaceName, IPForwardingSetParameters parameters)
 {
     return(operations.BeginSettingIPForwardingOnNetworkInterfaceAsync(serviceName, deploymentName, roleName, networkInterfaceName, parameters, CancellationToken.None));
 }