Class: Wire::NetworksValidation

Inherits:
ValidationBase show all
Defined in:
lib/wire/model/network_validation.rb

Overview

Run validations on network model part

Instance Attribute Summary

Attributes inherited from ValidationBase

#errors

Instance Method Summary collapse

Methods inherited from ValidationBase

#initialize, #mark, #objects_attached_to_zones?

Constructor Details

This class inherits a constructor from Wire::ValidationBase

Instance Method Details

#check_network_ip_ranges(dhcp_data, network, network_name) ⇒ Object

check ip ranges of dhcp_data and given network with name network_name



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/wire/model/network_validation.rb', line 112

def check_network_ip_ranges(dhcp_data, network, network_name)
  network_ip = IPAddr.new(network)

  # check both starting/ending ip range
  { :start => IPAddr.new(dhcp_data[:start]),
    :end   => IPAddr.new(dhcp_data[:end]) }.each do |type, data|
    mark("Network dhcp #{type} ip #{dhcp_data[type]} is not within network range" \
         "#{network} of network #{network_name}",
         'network', network_name) unless data.in_range_of?(network_ip)
  end
rescue => e
  mark("Network dhcp ip range is not valid: #{e}", 'network', network_name)
end

#dhcp_address_ranges_valid?Boolean

ensures that if a network has dhcp set, its :start/:end address ranges are within the address range of network, and a hostip is given (for dnsmasq to udp-listen on it) rubocop:disable CyclomaticComplexity

Returns:

  • (Boolean)


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/wire/model/network_validation.rb', line 85

def dhcp_address_ranges_valid?
  @project.get_element('networks').each do |network_name, network_data|
    network = network_data[:network]
    dhcp_data = network_data[:dhcp]
    next unless network && dhcp_data

    # do we have a host-ip on this bridge?
    host_ip = network_data[:hostip]
    if !host_ip
      mark("Network #{network_name} wants dhcp, but does not include a hostip.",
           'network', network_name)
      return false
    else
      if !dhcp_data[:start] || !dhcp_data[:end]
        mark("Network #{network_name} wants dhcp, but does not include an " \
             'address range. Set :start, :end.',
             'network', network_name)
        return false
      else
        check_network_ip_ranges(dhcp_data, network, network_name)
      end
    end
  end
end

#duplicate_networks_found?Boolean

ensures that all network ranges are unique

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
52
53
# File 'lib/wire/model/network_validation.rb', line 43

def duplicate_networks_found?
  dup_map = {}
  @project.get_element('networks').each do |network_name, network_data|
    nw = network_data[:network]
    dupe_name = dup_map[nw]

    mark("Network range #{nw} used in more than one network (#{dupe_name})",
         'network', network_name) if dupe_name
    dup_map.store nw, network_name
  end
end

#missing_network_def_found?Boolean

ensures that all networks have their network range defined

Returns:

  • (Boolean)


56
57
58
59
60
61
62
# File 'lib/wire/model/network_validation.rb', line 56

def missing_network_def_found?
  @project.get_element('networks').each do |network_name, network_data|
    nw = network_data[:network]
    mark("Network #{network_name} has no network ip range.",
         'network', network_name) unless nw
  end
end

#networks_attached_to_zones?Boolean

ensures that all networks are attached to a zone

Returns:

  • (Boolean)


24
25
26
# File 'lib/wire/model/network_validation.rb', line 24

def networks_attached_to_zones?
  objects_attached_to_zones? 'networks'
end

#networks_names_too_long?Boolean

ensures that networks with names > 6 chars have a short name defined, and short names are 6 chars. max.

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
39
40
# File 'lib/wire/model/network_validation.rb', line 30

def networks_names_too_long?
  @project.get_element('networks').each do |network_name, network_data|
    b_short_name_ok = (network_data[:shortname] && network_data[:shortname].size <= 6)

    mark("Network name #{network_name} too long, please define a :shortname with 6 chars. max.",
         'network', network_name) if network_name.size > 6 && !b_short_name_ok
    mark("Network short name of network #{network_name} too long, please define a :shortname " \
         'with 6 chars. max.',
         'network', network_name) if network_data[:shortname] && !b_short_name_ok
  end
end

#nonmatching_hostips_found?Boolean

ensures that all networks with hostips have their hostip within the network range of its network, i.e. 10.10.1.1 for 10.10.1.0/24

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/wire/model/network_validation.rb', line 66

def nonmatching_hostips_found?
  @project.get_element('networks').each do |network_name, network_data|
    network = network_data[:network]
    host_ip = network_data[:hostip]
    next unless network && host_ip

    host_ip_ip = IPAddr.new(host_ip)
    network_ip = IPAddr.new(network)

    mark("Network Host ip #{host_ip} is not within network range" \
      "#{network} of network #{network_name}", 'network', network_name) unless
        host_ip_ip.in_range_of?(network_ip)
  end
end

#run_validationsObject

run validation steps on network elements returns:

  • nil, results in errors of ValidationBase



14
15
16
17
18
19
20
21
# File 'lib/wire/model/network_validation.rb', line 14

def run_validations
  networks_attached_to_zones?
  duplicate_networks_found?
  missing_network_def_found?
  nonmatching_hostips_found?
  dhcp_address_ranges_valid?
  networks_names_too_long?
end