/// <summary>
 /// The Put VirtualNetworkGatewayConnectionSharedKey operation sets the
 /// virtual network gateway connection shared key for passed virtual
 /// network gateway connection in the specified resource group through
 /// Network resource provider.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Network.IVirtualNetworkGatewayConnectionOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The name of the resource group.
 /// </param>
 /// <param name='virtualNetworkGatewayConnectionName'>
 /// Required. The virtual network gateway connection name.
 /// </param>
 /// <param name='parameters'>
 /// Required. Parameters supplied to the Begin Create or update Virtual
 /// Network Gateway connection shared key operation through Network
 /// resource provider.
 /// </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<AzureAsyncOperationResponse> SetSharedKeyAsync(this IVirtualNetworkGatewayConnectionOperations operations, string resourceGroupName, string virtualNetworkGatewayConnectionName, ConnectionSharedKey parameters)
 {
     return operations.SetSharedKeyAsync(resourceGroupName, virtualNetworkGatewayConnectionName, parameters, CancellationToken.None);
 }
        public void VirtualNetworkGatewayConnectionSharedKeyOperationsApisTest()
        {
            var handler = new RecordedDelegatingHandler { StatusCodeToReturn = HttpStatusCode.OK };

            using (var context = UndoContext.Current)
            {
                context.Start();

                var resourcesClient = ResourcesManagementTestUtilities.GetResourceManagementClientWithHandler(handler);
                var networkResourceProviderClient = NetworkManagementTestUtilities.GetNetworkResourceProviderClient(handler);

                var location = NetworkManagementTestUtilities.GetResourceLocation(resourcesClient, "Microsoft.Network/connections");

                string resourceGroupName = TestUtilities.GenerateName("csmrg");
                resourcesClient.ResourceGroups.CreateOrUpdate(resourceGroupName,
                    new ResourceGroup
                    {
                        Location = location
                    });


                // 1. SetVirtualNetworkGatewayConnectionSharedKey API

                // Pre-requsite:- CreateVirtualNetworkGatewayConnection first

                // Create VirtualNetworkGateway1

                // a. Create PublicIPAddress(Gateway Ip) using Put PublicIpAddress API
                string publicIpName = TestUtilities.GenerateName();
                string domainNameLabel = TestUtilities.GenerateName();

                var nic1publicIp = TestHelper.CreateDefaultPublicIpAddress(publicIpName, resourceGroupName, domainNameLabel, location, networkResourceProviderClient);
                Console.WriteLine("PublicIPAddress(Gateway Ip) :{0}", nic1publicIp.Id);

                // b. Create Virtual Network using Put VirtualNetwork API
                string vnetName = TestUtilities.GenerateName();
                string subnetName = "GatewaySubnet";

                var virtualNetwork = TestHelper.CreateVirtualNetwork(vnetName, subnetName, resourceGroupName, location, networkResourceProviderClient);

                var getSubnetResponse = networkResourceProviderClient.Subnets.Get(resourceGroupName, vnetName, subnetName);
                Console.WriteLine("Virtual Network GatewaySubnet Id: {0}", getSubnetResponse.Subnet.Id);

                // c. CreateVirtualNetworkGateway API
                string virtualNetworkGatewayName = TestUtilities.GenerateName();
                string ipConfigName = TestUtilities.GenerateName();

                var virtualNetworkGateway = new VirtualNetworkGateway()
                {
                    Location = location,
                    Name = virtualNetworkGatewayName,
                    Tags = new Dictionary<string, string>()
                        {
                           {"key","value"}
                        },
                    EnableBgp = false,
                    GatewayType = VirtualNetworkGatewayType.Vpn,
                    VpnType = VpnType.RouteBased,
                    IpConfigurations = new List<VirtualNetworkGatewayIpConfiguration>()
                    {
                        new VirtualNetworkGatewayIpConfiguration()
                        {
                             Name = ipConfigName,
                             PrivateIpAllocationMethod = IpAllocationMethod.Dynamic,
                             PublicIpAddress = new ResourceId()
                             {
                                 Id = nic1publicIp.Id
                             },
                             Subnet = new ResourceId()
                             {
                                 Id = getSubnetResponse.Subnet.Id
                             }
                        }
                    }
                };

                var putVirtualNetworkGatewayResponse = networkResourceProviderClient.VirtualNetworkGateways.CreateOrUpdate(resourceGroupName, virtualNetworkGatewayName, virtualNetworkGateway);
                Assert.Equal(HttpStatusCode.OK, putVirtualNetworkGatewayResponse.StatusCode);
                Assert.Equal("Succeeded", putVirtualNetworkGatewayResponse.Status);
                var getVirtualNetworkGatewayResponse = networkResourceProviderClient.VirtualNetworkGateways.Get(resourceGroupName, virtualNetworkGatewayName);

                // Create LocalNetworkGateway2
                string localNetworkGatewayName = TestUtilities.GenerateName();
                string gatewayIp = "192.168.3.4";

                var localNetworkGateway = new LocalNetworkGateway()
                {
                    Location = location,
                    Name = localNetworkGatewayName,
                    Tags = new Dictionary<string, string>()
                        {
                           {"test","value"}
                        },
                    GatewayIpAddress = gatewayIp,
                    LocalNetworkAddressSpace = new AddressSpace()
                    {
                        AddressPrefixes = new List<string>()
                        {
                            "192.168.0.0/16",
                        }
                    }
                };

                var putLocalNetworkGatewayResponse = networkResourceProviderClient.LocalNetworkGateways.CreateOrUpdate(resourceGroupName, localNetworkGatewayName, localNetworkGateway);
                Assert.Equal(HttpStatusCode.OK, putLocalNetworkGatewayResponse.StatusCode);
                Assert.Equal("Succeeded", putLocalNetworkGatewayResponse.Status);
                var getLocalNetworkGatewayResponse = networkResourceProviderClient.LocalNetworkGateways.Get(resourceGroupName, localNetworkGatewayName);
                getLocalNetworkGatewayResponse.LocalNetworkGateway.Location = location;

                // CreaetVirtualNetworkGatewayConnection API
                string VirtualNetworkGatewayConnectionName = TestUtilities.GenerateName();
                var virtualNetworkGatewayConneciton = new VirtualNetworkGatewayConnection()
                {
                    Location = location,
                    Name = VirtualNetworkGatewayConnectionName,
                    VirtualNetworkGateway1 = getVirtualNetworkGatewayResponse.VirtualNetworkGateway,
                    LocalNetworkGateway2 = getLocalNetworkGatewayResponse.LocalNetworkGateway,
                    ConnectionType = VirtualNetworkGatewayConnectionType.IPsec,
                    RoutingWeight = 3,
                    SharedKey = "abc"
                };
                var putVirtualNetworkGatewayConnectionResponse = networkResourceProviderClient.VirtualNetworkGatewayConnections.CreateOrUpdate(resourceGroupName, VirtualNetworkGatewayConnectionName, virtualNetworkGatewayConneciton);
                Assert.Equal(HttpStatusCode.OK, putVirtualNetworkGatewayConnectionResponse.StatusCode);
                Assert.Equal("Succeeded", putVirtualNetworkGatewayConnectionResponse.Status);

                // SetVirtualNetworkGatewayConnectionSharedKey API on created connection above:- virtualNetworkGatewayConneciton
                string connectionSharedKeyName = VirtualNetworkGatewayConnectionName;
                var connectionSharedKey = new ConnectionSharedKey()
                {
                    Value = "TestSharedKeyValue"
                };

                var putConnectionSharedKeyResponse = networkResourceProviderClient.VirtualNetworkGatewayConnections.SetSharedKey(resourceGroupName, connectionSharedKeyName, connectionSharedKey);
                Assert.Equal(HttpStatusCode.OK, putConnectionSharedKeyResponse.StatusCode);
                Assert.Equal("Succeeded", putConnectionSharedKeyResponse.Status);

                // 2. GetVirtualNetworkGatewayConnectionSharedKey API
                var getconnectionSharedKeyResponse = networkResourceProviderClient.VirtualNetworkGatewayConnections.GetSharedKey(resourceGroupName, connectionSharedKeyName);
                Assert.Equal(HttpStatusCode.OK, getconnectionSharedKeyResponse.StatusCode);
                Console.WriteLine("ConnectionSharedKey details:- Value: {0}", getconnectionSharedKeyResponse.Value);

                // 3A. VirtualNetworkGatewayConnectionResetSharedKey API
                var connectionResetSharedKey = new ConnectionResetSharedKey()
                {
                    KeyLength = 50
                };
                var resetConnectionResetSharedKeyResponse = networkResourceProviderClient.VirtualNetworkGatewayConnections.ResetSharedKey(resourceGroupName, connectionSharedKeyName, connectionResetSharedKey);
                Assert.Equal(HttpStatusCode.OK, resetConnectionResetSharedKeyResponse.StatusCode);
                Assert.Equal("Succeeded", resetConnectionResetSharedKeyResponse.Status);

                // 3B. GetVirtualNetworkGatewayConnectionSharedKey API after VirtualNetworkGatewayConnectionResetSharedKey API was called
                getconnectionSharedKeyResponse = networkResourceProviderClient.VirtualNetworkGatewayConnections.GetSharedKey(resourceGroupName, connectionSharedKeyName);
                Assert.Equal(HttpStatusCode.OK, getconnectionSharedKeyResponse.StatusCode);
                Console.WriteLine("ConnectionSharedKey details:- Value: {0}", getconnectionSharedKeyResponse.Value);
            }
        }
 /// <summary>
 /// The Put VirtualNetworkGatewayConnectionSharedKey operation sets the
 /// virtual network gateway connection shared key for passed virtual
 /// network gateway connection in the specified resource group through
 /// Network resource provider.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Network.IVirtualNetworkGatewayConnectionOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The name of the resource group.
 /// </param>
 /// <param name='virtualNetworkGatewayConnectionName'>
 /// Required. The virtual network gateway connection name.
 /// </param>
 /// <param name='parameters'>
 /// Required. Parameters supplied to the Begin Create or update Virtual
 /// Network Gateway connection shared key operation through Network
 /// resource provider.
 /// </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 AzureAsyncOperationResponse SetSharedKey(this IVirtualNetworkGatewayConnectionOperations operations, string resourceGroupName, string virtualNetworkGatewayConnectionName, ConnectionSharedKey parameters)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((IVirtualNetworkGatewayConnectionOperations)s).SetSharedKeyAsync(resourceGroupName, virtualNetworkGatewayConnectionName, parameters);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }