Class: Staypuft::Deployment::NovaService::NetworkRangesValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Defined in:
app/models/staypuft/deployment/nova_service.rb

Instance Method Summary collapse

Instance Method Details

#validate(record) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/staypuft/deployment/nova_service.rb', line 13

def validate(record)
  valid_ranges = []
  [:public_floating_range, :private_fixed_range].each do |range_param|
    begin
      unless (range_str = record.send(range_param)).empty?
        ip_addr = IPAddr.new(range_str)
        ip_range = ip_addr.to_range
        if ip_range.begin == ip_range.end
          record.errors[range_param] << "Specify address range, not single value"
        else
          valid_ranges << [range_param, ip_addr]
        end
      end
    rescue
      record.errors[range_param] << "Invalid Network Range Format"
    end
    # don't validate conflicts unless both ranges otherwise passed validation
    if valid_ranges.size == 2
      valid_ranges.each_with_index do |param_and_ip, index|
        this_param_name = param_and_ip[0]
        this_ip_range = param_and_ip[1].to_range

        other_param_name = valid_ranges[(index+1)%2][0]
        other_ip_addr = valid_ranges[(index+1)%2][1]
        ["begin", "end"].each do |action|
          if (other_ip_addr===this_ip_range.send(action))
            record.errors[this_param_name] << "Range #{action} #{this_ip_range.begin} overlaps with range for #{other_param_name.to_s.humanize}"
          end
        end
      end
    end
  end
  unless record.private_fixed_range.empty?
    if record.network_size < 4
      record.errors[:private_fixed_range] << "Fixed range is too small. Specify CIDR for network size #{record.min_fixed_range_cidr} or larger"
    end
  end
end