/// <summary>
 /// Adds a Network Security Group to a subnet.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.Network.INetworkSecurityGroupOperations.
 /// </param>
 /// <param name='virtualNetworkName'>
 /// Required.
 /// </param>
 /// <param name='subnetName'>
 /// Required.
 /// </param>
 /// <param name='parameters'>
 /// Required. Parameters supplied to the Add Network Security Group to
 /// subnet 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 AddToSubnet(this INetworkSecurityGroupOperations operations, string virtualNetworkName, string subnetName, NetworkSecurityGroupAddToSubnetParameters parameters)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((INetworkSecurityGroupOperations)s).AddToSubnetAsync(virtualNetworkName, subnetName, parameters);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }
 /// <summary>
 /// Adds a Network Security Group to a subnet.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.Network.INetworkSecurityGroupOperations.
 /// </param>
 /// <param name='virtualNetworkName'>
 /// Required.
 /// </param>
 /// <param name='subnetName'>
 /// Required.
 /// </param>
 /// <param name='parameters'>
 /// Required. Parameters supplied to the Add Network Security Group to
 /// subnet 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> BeginAddingToSubnetAsync(this INetworkSecurityGroupOperations operations, string virtualNetworkName, string subnetName, NetworkSecurityGroupAddToSubnetParameters parameters)
 {
     return operations.BeginAddingToSubnetAsync(virtualNetworkName, subnetName, parameters, CancellationToken.None);
 }
        public void AddAndRemoveNetworkSecurityGroupToSubnet()
        {
            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();
                using (NetworkTestBase _testFixture = new NetworkTestBase())
                {
                    //setup

                    // create Network Security Group
                    string securityGroupName = _testFixture.GenerateRandomNetworkSecurityGroupName();
                    string securityGroupLabel = _testFixture.GenerateRandomName();
                    string securityGroupLocation = "North Central US";

                    _testFixture.CreateNetworkSecurityGroup(securityGroupName, securityGroupLabel, securityGroupLocation);

                    // create vnet with subnet
                    string vnetName = "virtualNetworkSiteName";
                    string subnetName = "FrontEndSubnet5";
                    _testFixture.SetSimpleVirtualNetwork();

                    NetworkSecurityGroupAddToSubnetParameters parameters = new NetworkSecurityGroupAddToSubnetParameters()
                    {
                        Name = securityGroupName
                    };

                    // action
                    _testFixture.NetworkClient.NetworkSecurityGroups.AddToSubnet(vnetName, subnetName, parameters);
                    var listNetworkResponse = _testFixture.NetworkClient.Networks.List();

                    // assert
                    var getResponse = _testFixture.NetworkClient.NetworkSecurityGroups.GetForSubnet(vnetName, subnetName);
                    Assert.Equal(securityGroupName, getResponse.Name);
                    Assert.Equal(listNetworkResponse.VirtualNetworkSites.First(vnet =>
                        vnetName.Equals(vnet.Name)).Subnets.First(subnet =>
                        subnetName.Equals(subnet.Name))
                        .NetworkSecurityGroup, securityGroupName);

                    // action
                    _testFixture.NetworkClient.NetworkSecurityGroups.RemoveFromSubnet(vnetName, subnetName, securityGroupName);


                    // assert
                    Assert.Throws<CloudException>(() => _testFixture.NetworkClient.NetworkSecurityGroups.GetForSubnet(vnetName, subnetName));
                }
            }
        }