Module: Staypuft::Deployment::IpCheck

Included in:
IpAddressValidator, NfsUriValidator
Defined in:
app/models/staypuft/deployment/ip_check.rb

Constant Summary collapse

NOT_RANGE_MSG =
N_("Specify single IP address, not range")
INVALID_IP_OR_FQDN_MSG =
N_("Invalid IP address or FQDN supplied")

Instance Method Summary collapse

Instance Method Details

#check_ip_or_hostname(record, attribute, value) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/models/staypuft/deployment/ip_check.rb', line 6

def check_ip_or_hostname(record, attribute, value)
  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, NOT_RANGE_MSG
      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_OR_FQDN_MSG
      false
    end
  end
end