Module: Azure::ARM::VnetConfig

Included in:
ResourceManagement::ARMInterface
Defined in:
lib/azure/resource_management/vnet_config.rb

Instance Method Summary collapse

Instance Method Details

#add_subnet(subnet_name, vnet_config, subnets) ⇒ Object

add new subnet into the existing virtual network ##



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# 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) ⇒ Object

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

Raises:

  • (ArgumentError)


218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# 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) ⇒ Object

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 ##



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# 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) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# 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) ⇒ Boolean

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

Returns:

  • (Boolean)


127
128
129
130
# 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) ⇒ Object

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



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# 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) ⇒ Object

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 ##



80
81
82
# 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) ⇒ Object

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



103
104
105
# 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) ⇒ Object

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



86
87
88
# 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) ⇒ Object

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 ##



93
94
95
# 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) ⇒ Object

single subnet body creation to be added in template ##



58
59
60
61
62
63
64
65
# 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) ⇒ Object

return address prefix of a subnet ##



73
74
75
# File 'lib/azure/resource_management/vnet_config.rb', line 73

def subnet_address_prefix(subnet)
  subnet.address_prefix
end

#subnet_cidr_prefix(subnet) ⇒ Object

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



98
99
100
# 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) ⇒ Object

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



52
53
54
55
# 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) ⇒ Object

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



25
26
27
28
29
30
31
32
33
34
35
36
# 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) ⇒ Object

return all the address prefixes under a virtual network ##



68
69
70
# File 'lib/azure/resource_management/vnet_config.rb', line 68

def vnet_address_spaces(vnet)
  vnet.address_space.address_prefixes
end