Class: Staypuft::Deployment::CinderService::Equallogic::SanIpValueValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/models/staypuft/deployment/cinder_service/equallogic.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/staypuft/deployment/cinder_service/equallogic.rb', line 40

def validate_each(record, attribute, value)
  return if value.empty?

  begin
    ip_addr = IPAddr.new(value)
    ip_range = ip_addr.to_range
    if ip_range.begin == ip_range.end
      true
    else
      record.errors.add attribute, "Specify single IP address, not range"
      false
    end
  rescue
    # not IP addr
    # validating as fqdn
    if /(?=^.{1,254}$)(^(((?!-)[a-zA-Z0-9-]{1,63}(?<!-))|((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+[a-zA-Z]{2,63})$)/ =~ value
      true
    else
      record.errors.add attribute, "Invalid IP address or FQDN supplied"
      false
    end
  end
end