示例#1
0
        public SubnetInner GetAksSubnet(VirtualNetworkInner virtualNetwork)
        {
            var aksSubnet = virtualNetwork
                            .Subnets
                            .Where(subnet => subnet.Name == VIRTUAL_NETWORK_AKS_SUBNET_NAME)
                            .FirstOrDefault();

            return(aksSubnet);
        }
        /// <summary>
        /// Create a virtual network.
        /// </summary>
        /// <param name="name">Name of the virtual network.</param>
        /// <param name="resourceGroup">Name of the resource group.</param>
        /// <returns>a async Task</returns>
        public async Task <VirtualNetworkInner> Create(
            string name,
            string resourceGroup,
            List <string> ipAddresses,
            List <string> dnsServers)
        {
            VirtualNetworkInner vnet = new VirtualNetworkInner()
            {
                Location     = Region.USWest.ToString(),
                AddressSpace = new AddressSpace()
                {
                    AddressPrefixes = ipAddresses
                },
                DhcpOptions = new DhcpOptions()
                {
                    DnsServers = dnsServers
                }
            };

            return(await this.client.VirtualNetworks.CreateOrUpdateAsync(resourceGroup, name, vnet));
        }
示例#3
0
        protected async Task <VirtualNetworkInner> CreateOrRetrieveNetworkAsync(string resourceGroupName, string vnetName, string subnetName)
        {
            VirtualNetworkInner vnet = null;

            // attempt to retrieve an existing vnet
            try
            {
                Console.Write("Checking the existence of vnet '{0}' in resource group '{1}'...", vnetName, resourceGroupName);
                var vNetResponse = await NetworkManagementClient.VirtualNetworks.GetWithHttpMessagesAsync(resourceGroupName, vnetName).ConfigureAwait(false);

                Console.WriteLine("done");

                vnet = vNetResponse.Body;
            }
            catch (Exception e)
            {
                VerifyExpectedException <CloudException>(e, HttpStatusCode.NotFound, (ex) => { return(ex.Response.StatusCode); });
            }

            // create one if we must
            if (vnet == null)
            {
                try
                {
                    Console.Write("Creating vnet '{0}' in resource group '{1}'...", vnetName, resourceGroupName);
                    var vNetResponse = await NetworkManagementClient.VirtualNetworks.CreateOrUpdateWithHttpMessagesAsync(
                        resourceGroupName,
                        vnetName,
                        CreateVNetParameters(vnetName, SampleConstants.VNetAddressSpace, subnetName, SampleConstants.VNetSubnetAddressSpace))
                                       .ConfigureAwait(false);

                    Console.WriteLine("done.");

                    vnet = vNetResponse.Body;
                }
                catch (Exception e)
                {
                    Console.WriteLine("Unexpected exception encountered creating the virtual network: {0}", e.Message);
                    throw;
                }
            }

            // attempt to retrieve the specified subnet; create if not existing
            var subnetEntry = new SubnetInner()
            {
                Name             = subnetName,
                ServiceEndpoints = new List <ServiceEndpointPropertiesFormat> {
                    new ServiceEndpointPropertiesFormat("Microsoft.KeyVault")
                },
                AddressPrefix = SampleConstants.VNetSubnetAddressSpace
            };

            bool hasMatchingSubnet = false;

            if (vnet.Subnets.Count > 0)
            {
                // enumerate existing subnets and look for a match.
                // Invoking vnet.Subnets.Contains() will only return true
                // for a complete match of all the properties of the subnet,
                // some of which we can't know beforehand.
                for (var subnetIt = vnet.Subnets.GetEnumerator();
                     subnetIt.MoveNext();
                     )
                {
                    hasMatchingSubnet |= subnetIt.Current.Name.Equals(subnetEntry.Name, StringComparison.InvariantCultureIgnoreCase);
                    if (hasMatchingSubnet)
                    {
                        break;
                    }
                }
            }

            if (!hasMatchingSubnet)
            {
                try
                {
                    Console.Write("Creating subnet '{0}' in resource group '{1}'...", subnetEntry.Name, resourceGroupName);
                    var subnetResponse = await NetworkManagementClient.Subnets.CreateOrUpdateWithHttpMessagesAsync(
                        resourceGroupName,
                        vnet.Name,
                        subnetEntry.Name,
                        subnetEntry)
                                         .ConfigureAwait(false);

                    Console.WriteLine("done.");

                    // retrieve the updated vnet
                    Console.Write("Retrieving the updated vnet '{0}' in resource group '{1}'...", vnetName, resourceGroupName);
                    var vNetResponse = await NetworkManagementClient.VirtualNetworks.GetWithHttpMessagesAsync(resourceGroupName, vnetName).ConfigureAwait(false);

                    Console.WriteLine("done");

                    vnet = vNetResponse.Body;
                }
                catch (Exception e)
                {
                    Console.WriteLine("Unexpected exception encountered updating the virtual network: {0}", e.Message);
                    throw;
                }
            }

            return(vnet);
        }
 /// <summary>
 /// Creates or updates a virtual network in the specified resource group.
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='resourceGroupName'>
 /// The name of the resource group.
 /// </param>
 /// <param name='virtualNetworkName'>
 /// The name of the virtual network.
 /// </param>
 /// <param name='parameters'>
 /// Parameters supplied to the create or update virtual network operation.
 /// </param>
 /// <param name='cancellationToken'>
 /// The cancellation token.
 /// </param>
 public static async Task <VirtualNetworkInner> CreateOrUpdateAsync(this IVirtualNetworksOperations operations, string resourceGroupName, string virtualNetworkName, VirtualNetworkInner parameters, CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var _result = await operations.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, virtualNetworkName, parameters, null, cancellationToken).ConfigureAwait(false))
     {
         return(_result.Body);
     }
 }
示例#5
0
        public async Task <VirtualNetworkInner> CreateVirtualNetworkAsync(
            IResourceGroup resourceGroup,
            NetworkSecurityGroupInner networkSecurityGroup,
            string virtualNetworkName,
            RouteTableInner routeTable          = null,
            IDictionary <string, string> tags   = null,
            CancellationToken cancellationToken = default
            )
        {
            try {
                tags = tags ?? new Dictionary <string, string>();

                Log.Information($"Creating Azure Virtual Network: {virtualNetworkName} ...");

                // Define Virtual Network
                var virtualNetworkDefinition = new VirtualNetworkInner {
                    Location = resourceGroup.RegionName,
                    Tags     = tags,

                    AddressSpace = new AddressSpace {
                        AddressPrefixes = new List <string>()
                        {
                            VIRTUAL_NETWORK_ADDRESS_PREFIXES
                        }
                    },
                    Subnets = new List <SubnetInner> {
                        new SubnetInner()
                        {
                            Name                 = VIRTUAL_NETWORK_AKS_SUBNET_NAME,
                            AddressPrefix        = VIRTUAL_NETWORK_AKS_SUBNET_ADDRESS_PREFIXES,
                            NetworkSecurityGroup = new SubResource {
                                Id = networkSecurityGroup.Id
                            }
                        }
                    }
                };

                if (null != routeTable)
                {
                    virtualNetworkDefinition.Subnets[0].RouteTable = new SubResource {
                        Id = routeTable.Id
                    };
                }

                virtualNetworkDefinition.Validate();

                var virtualNetwork = await _networkManagementClient
                                     .VirtualNetworks
                                     .CreateOrUpdateAsync(
                    resourceGroup.Name,
                    virtualNetworkName,
                    virtualNetworkDefinition,
                    cancellationToken
                    );

                Log.Information($"Created Azure Virtual Network: {virtualNetworkName}");

                return(virtualNetwork);
            }
            catch (Exception ex) {
                Log.Error(ex, $"Failed to create Azure Virtual Network: {virtualNetworkName}");
                throw;
            }
        }