module Azure::ARM::VnetConfig

Public Instance Methods

add_subnet(subnet_name, vnet_config, subnets) click to toggle source

add new subnet into the existing virtual network ##

# File lib/azure/resource_management/vnet_config.rb, line 189
def add_subnet(subnet_name, vnet_config, subnets)
  new_subnet_prefix = nil
  vnet_address_prefix_count = 0
  vnet_address_space = vnet_config[:addressPrefixes]

  ## search for space in all the address prefixes of the virtual network ##
  while new_subnet_prefix.nil? && vnet_address_space.length > vnet_address_prefix_count
    new_subnet_prefix = new_subnet_address_prefix(
      vnet_address_space[vnet_address_prefix_count],
      subnets_list_for_specific_address_space(
        vnet_address_space[vnet_address_prefix_count], subnets
      )
    )
    vnet_address_prefix_count += 1
  end

  if new_subnet_prefix ## found space for new subnet ##
    vnet_config[:subnets].push(
      subnet(subnet_name, new_subnet_prefix)
    )
  else ## no space available in the virtual network for the new subnet ##
    raise "Unable to add subnet #{subnet_name} into the virtual network #{vnet_config[:virtualNetworkName]}, no address space available !!!"
  end

  vnet_config
end
create_vnet_config(resource_group_name, vnet_name, vnet_subnet_name) click to toggle source

virtual network configuration creation for the new vnet creation or to handle existing vnet ##

# File lib/azure/resource_management/vnet_config.rb, line 218
def create_vnet_config(resource_group_name, vnet_name, vnet_subnet_name)
  raise ArgumentError, "GatewaySubnet cannot be used as the name for --azure-vnet-subnet-name option. GatewaySubnet can only be used for virtual network gateways." if vnet_subnet_name == "GatewaySubnet"

  vnet_config = {}
  subnets = nil
  flag = true
  ## check whether user passed or default named virtual network exist or not ##
  vnet = get_vnet(resource_group_name, vnet_name)
  vnet_config[:virtualNetworkName] = vnet_name
  if vnet ## handle resources in the existing virtual network ##
    vnet_config[:addressPrefixes] = vnet_address_spaces(vnet)
    vnet_config[:subnets] = []
    subnets = subnets_list(resource_group_name, vnet_name)
    if subnets
      subnets.each do |subnet|
        flag = false if subnet.name == vnet_subnet_name ## given subnet already exist in the virtual network ##
        ## preserve the existing subnet resources ##
        vnet_config[:subnets].push(
          subnet(subnet.name, subnet_address_prefix(subnet))
        )
      end
    end
  else ## create config for new vnet ##
    vnet_config[:addressPrefixes] = [ "10.0.0.0/16" ]
    vnet_config[:subnets] = []
    vnet_config[:subnets].push(
      subnet(vnet_subnet_name, "10.0.0.0/24")
    )
    flag = false
  end

  ## given subnet does not exist, so create new one in the virtual network ##
  vnet_config = add_subnet(vnet_subnet_name, vnet_config, subnets) if flag

  vnet_config
end
divide_network(address_prefix) click to toggle source

when a address space in an existing virtual network is not used at all then divide the space into the number of subnets based on the total number of hosts that network supports ##

# File lib/azure/resource_management/vnet_config.rb, line 110
def divide_network(address_prefix)
  network_address = IPAddress(address_prefix)
  prefix = nil

  case network_address.count
  when 4097..65536
    prefix = "20"
  when 256..4096
    prefix = "24"
  end

  ## if the given network is small then do not divide it else divide using
  ## the prefix value calculated above ##
  prefix.nil? ? address_prefix : network_address.network.address.concat("/" + prefix)
end
get_vnet(resource_group_name, vnet_name) click to toggle source
# File lib/azure/resource_management/vnet_config.rb, line 38
def get_vnet(resource_group_name, vnet_name)
  network_resource_client.virtual_networks.get(resource_group_name, vnet_name)
rescue MsRestAzure::AzureOperationError => error
  if error.body
    err_json = JSON.parse(error.response.body)
    if err_json["error"]["code"] == "ResourceNotFound"
      false
    else
      raise error
    end
  end
end
in_use_network?(subnet_network, available_network) click to toggle source

check if the available_network is part of the subnet_network or vice-versa ##

# File lib/azure/resource_management/vnet_config.rb, line 127
def in_use_network?(subnet_network, available_network)
  (subnet_network.include? available_network) ||
    (available_network.include? subnet_network)
end
new_subnet_address_prefix(vnet_address_prefix, subnets) click to toggle source

calculate and return address_prefix for the new subnet to be added in the existing virtual network ##

# File lib/azure/resource_management/vnet_config.rb, line 134
def new_subnet_address_prefix(vnet_address_prefix, subnets)
  if subnets.empty? ## no subnets exist in the given address space of the virtual network, so divide the network into smaller subnets (based on the network size) and allocate space for the new subnet to be added ##
    divide_network(vnet_address_prefix)
  else ## subnets exist in vnet, calculate new address_prefix for the new subnet based on the space taken by these existing subnets under the given address space of the virtual network ##
    vnet_network_address = IPAddress(vnet_address_prefix)
    subnets = sort_subnets_by_cidr_prefix(subnets)
    available_networks_pool = []
    used_networks_pool = []
    subnets.each do |subnet|
      ## in case the larger network is not divided into smaller subnets but
      ## divided into only 1 largest subnet of the complete network size ##
      if vnet_network_address.prefix == subnet_cidr_prefix(subnet)
        break
      end

      ## add all the possible subnets (calculated using the current subnet's
      ## cidr prefix value) into the available_networks_pool ##
      available_networks_pool.push(
        vnet_network_address.subnet(subnet_cidr_prefix(subnet))
      ).flatten!.uniq! { |nwrk| [ nwrk.network.address, nwrk.prefix ].join(":") }

      ## add current subnet into the used_networks_pool ##
      used_networks_pool.push(
        IPAddress(subnet_address_prefix(subnet))
      )

      ## sort both the network pools before trimming the available_networks_pool ##
      available_networks_pool, used_networks_pool = sort_pools(
        available_networks_pool, used_networks_pool
      )

      ## trim the available_networks_pool based on the networks already
      ## allocated to the existing subnets ##
      used_networks_pool.each do |subnet_network|
        available_networks_pool.delete_if do |available_network|
          in_use_network?(subnet_network, available_network)
        end
      end

      ## sort both the network pools after trimming the available_networks_pool ##
      available_networks_pool, used_networks_pool = sort_pools(
        available_networks_pool, used_networks_pool
      )
    end

    ## space available in the vnet_address_prefix network for the new subnet ##
    if !available_networks_pool.empty? && available_networks_pool.first.network?
      available_networks_pool.first.network.address.concat("/" + available_networks_pool.first.prefix.to_s)
    else ## space not available in the vnet_address_prefix network for the new subnet ##
      nil
    end
  end
end
sort_available_networks(available_networks) click to toggle source

sort available networks pool in ascending order based on the network's IP address to allocate the network for the new subnet to be added in the existing virtual network ##

# File lib/azure/resource_management/vnet_config.rb, line 80
def sort_available_networks(available_networks)
  available_networks.sort_by { |nwrk| nwrk.network.address.split(".").map(&:to_i) }
end
sort_pools(available_networks_pool, used_networks_pool) click to toggle source

method to invoke respective sort methods for the network pools ##

# File lib/azure/resource_management/vnet_config.rb, line 103
def sort_pools(available_networks_pool, used_networks_pool)
  [sort_available_networks(available_networks_pool), sort_used_networks_by_hosts_size(used_networks_pool)]
end
sort_subnets_by_cidr_prefix(subnets) click to toggle source

sort existing subnets in ascending order based on their cidr prefix or netmask to have subnets with larger networks on the top ##

# File lib/azure/resource_management/vnet_config.rb, line 86
def sort_subnets_by_cidr_prefix(subnets)
  subnets.sort_by.with_index { |sbn, i| [subnet_address_prefix(sbn).split("/")[1].to_i, i] }
end
sort_used_networks_by_hosts_size(used_network) click to toggle source

sort used networks pool in descending order based on the number of hosts it contains, this helps to keep larger networks on top thereby eliminating more number of entries in available_networks_pool at a faster pace ##

# File lib/azure/resource_management/vnet_config.rb, line 93
def sort_used_networks_by_hosts_size(used_network)
  used_network.sort_by.with_index { |nwrk, i| [-nwrk.hosts.size, i] }
end
subnet(subnet_name, subnet_prefix) click to toggle source

single subnet body creation to be added in template ##

# File lib/azure/resource_management/vnet_config.rb, line 58
def subnet(subnet_name, subnet_prefix)
  {
    "name" => subnet_name,
    "properties" => {
      "addressPrefix" => subnet_prefix,
    },
  }
end
subnet_address_prefix(subnet) click to toggle source

return address prefix of a subnet ##

# File lib/azure/resource_management/vnet_config.rb, line 73
def subnet_address_prefix(subnet)
  subnet.address_prefix
end
subnet_cidr_prefix(subnet) click to toggle source

return the cidr prefix or netmask of the given subnet ##

# File lib/azure/resource_management/vnet_config.rb, line 98
def subnet_cidr_prefix(subnet)
  subnet_address_prefix(subnet).split("/")[1].to_i
end
subnets_list(resource_group_name, vnet_name, address_prefix = nil) click to toggle source

lists all subnets under a virtual network or lists subnets of only a particular address space ##

# File lib/azure/resource_management/vnet_config.rb, line 52
def subnets_list(resource_group_name, vnet_name, address_prefix = nil)
  list = network_resource_client.subnets.list(resource_group_name, vnet_name)
  !address_prefix.nil? && !list.empty? ? subnets_list_for_specific_address_space(address_prefix, list) : list
end
subnets_list_for_specific_address_space(address_prefix, subnets_list) click to toggle source

lists subnets of only a specific virtual network address space ##

# File lib/azure/resource_management/vnet_config.rb, line 25
def subnets_list_for_specific_address_space(address_prefix, subnets_list)
  list = []
  address_space = IPAddress(address_prefix)
  subnets_list.each do |sbn|
    subnet_address_prefix = IPAddress(sbn.address_prefix)

    ## check if the subnet belongs to this address space or not ##
    list << sbn if address_space.include? subnet_address_prefix
  end

  list
end
vnet_address_spaces(vnet) click to toggle source

return all the address prefixes under a virtual network ##

# File lib/azure/resource_management/vnet_config.rb, line 68
def vnet_address_spaces(vnet)
  vnet.address_space.address_prefixes
end