/// <summary>
 /// Adds a Network Security Group to a network interface.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.Network.INetworkSecurityGroupOperations.
 /// </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 Add Network Security Group to
 /// a 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 AddToNetworkInterface(this INetworkSecurityGroupOperations operations, string serviceName, string deploymentName, string roleName, string networkInterfaceName, NetworkSecurityGroupAddAssociationParameters parameters)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((INetworkSecurityGroupOperations)s).AddToNetworkInterfaceAsync(serviceName, deploymentName, roleName, networkInterfaceName, 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, NetworkSecurityGroupAddAssociationParameters parameters)
 {
     return operations.BeginAddingToSubnetAsync(virtualNetworkName, subnetName, parameters, CancellationToken.None);
 }
 /// <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 BeginAddingToSubnet(this INetworkSecurityGroupOperations operations, string virtualNetworkName, string subnetName, NetworkSecurityGroupAddAssociationParameters parameters)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((INetworkSecurityGroupOperations)s).BeginAddingToSubnetAsync(virtualNetworkName, subnetName, parameters);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }
 /// <summary>
 /// Adds a Network Security Group to a Role.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.Network.INetworkSecurityGroupOperations.
 /// </param>
 /// <param name='serviceName'>
 /// Required.
 /// </param>
 /// <param name='deploymentName'>
 /// Required.
 /// </param>
 /// <param name='roleName'>
 /// Required.
 /// </param>
 /// <param name='parameters'>
 /// Required. Parameters supplied to the Add Network Security Group to
 /// 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> BeginAddingToRoleAsync(this INetworkSecurityGroupOperations operations, string serviceName, string deploymentName, string roleName, NetworkSecurityGroupAddAssociationParameters parameters)
 {
     return operations.BeginAddingToRoleAsync(serviceName, deploymentName, roleName, parameters, CancellationToken.None);
 }
        public void AddAndRemoveNetworkSecurityGroupToNIC()
        {
            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();

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

                    _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 1
                        var associationParams = new NetworkSecurityGroupAddAssociationParameters(securityGroupName);
                        _testFixture.NetworkClient.NetworkSecurityGroups.AddToNetworkInterface(
                            serviceName,
                            deploymentName,
                            roleName,
                            networkInterfaceName,
                            associationParams);

                        // assert 1
                        NetworkSecurityGroupGetAssociationResponse response =
                            _testFixture.NetworkClient.NetworkSecurityGroups.GetForNetworkInterface(
                            serviceName,
                            deploymentName,
                            roleName,
                            networkInterfaceName);
                        Assert.Equal(associationParams.Name, response.Name);

                        // action 2
                        _testFixture.NetworkClient.NetworkSecurityGroups.RemoveFromNetworkInterface(
                            serviceName,
                            deploymentName,
                            roleName,
                            networkInterfaceName,
                            securityGroupName);

                        // assert 2
                        Assert.Throws<CloudException>(() =>_testFixture.NetworkClient.NetworkSecurityGroups.GetForNetworkInterface(
                                serviceName,
                                deploymentName,
                                roleName,
                                networkInterfaceName));
                    }

                    finally
                    {
                        if (hostedServiceCreated)
                        {
                            _testFixture.ComputeClient.HostedServices.DeleteAll(serviceName);
                        }
                    }
                }
            }
        }
        public void AddAndRemoveNetworkSecurityGroupToRole()
        {
            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();

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

                    _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);

                    try
                    {
                        // action 1
                        var associationParams = new NetworkSecurityGroupAddAssociationParameters(securityGroupName);
                        _testFixture.NetworkClient.NetworkSecurityGroups.AddToRole(serviceName, deploymentName, roleName,
                            associationParams);

                        // assert 1
                        NetworkSecurityGroupGetAssociationResponse response =
                            _testFixture.NetworkClient.NetworkSecurityGroups.GetForRole(serviceName, deploymentName, roleName);
                        Assert.Equal(associationParams.Name, response.Name);

                        // action 2
                        _testFixture.NetworkClient.NetworkSecurityGroups.RemoveFromRole(
                            serviceName,
                            deploymentName,
                            roleName,
                            securityGroupName);

                        // assert 2
                        Assert.Throws<CloudException>(() =>
                            _testFixture.NetworkClient.NetworkSecurityGroups.GetForRole(serviceName, deploymentName, roleName));
                    }
                    finally
                    {
                        if (storageAccountCreated)
                        {
                            _testFixture.StorageClient.StorageAccounts.Delete(storageAccountName);
                        }
                        if (hostedServiceCreated)
                        {
                            _testFixture.ComputeClient.HostedServices.DeleteAll(serviceName);
                        }
                    }
                }
            }
        }
        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();

                    NetworkSecurityGroupAddAssociationParameters parameters = new NetworkSecurityGroupAddAssociationParameters()
                    {
                        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));
                }
            }
        }