Module: Fog::Proxmox::IpHelper

Defined in:
lib/fog/proxmox/helpers/ip_helper.rb

Overview

module IpHelper mixins

Constant Summary collapse

CIDRv4_PREFIX =
'([0-9]|[1-2][0-9]|3[0-2])'
CIDRv4_PREFIX_REGEXP =
/^#{CIDRv4_PREFIX}$/
IPv4_SRC =
"#{Resolv::IPv4::Regex.source.delete_suffix('\z').delete_prefix('\A')}"
CIDRv4_REGEXP =
Regexp.new("\\A(#{IPv4_SRC})(\/#{CIDRv4_PREFIX})?\\z")
CIDRv6_PREFIX =
'(\d+)'
CIDRv6_PREFIX_REGEXP =
/^#{CIDRv6_PREFIX}$/xi
IPv6_SRC =
"#{Resolv::IPv6::Regex_8Hex.source.delete_suffix('\z').delete_prefix('\A')}"
CIDRv6_REGEXP =
Regexp.new("\\A(#{IPv6_SRC})(\/#{CIDRv6_PREFIX})?\\z", Regexp::EXTENDED | Regexp::IGNORECASE)

Class Method Summary collapse

Class Method Details

.cidr6?(ip) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/fog/proxmox/helpers/ip_helper.rb', line 41

def self.cidr6?(ip)
  CIDRv6_REGEXP.match?(ip)
end

.cidr6_prefix?(prefix) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/fog/proxmox/helpers/ip_helper.rb', line 69

def self.cidr6_prefix?(prefix)
  CIDRv6_PREFIX_REGEXP.match?(prefix) && prefix.to_i >= 0 && prefix.to_i <= 128
end

.cidr?(ip) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/fog/proxmox/helpers/ip_helper.rb', line 37

def self.cidr?(ip)
  CIDRv4_REGEXP.match?(ip)
end

.cidr_prefix?(prefix) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/fog/proxmox/helpers/ip_helper.rb', line 65

def self.cidr_prefix?(prefix)
  CIDRv4_PREFIX_REGEXP.match?(prefix)
end

.ip(ip) ⇒ Object



73
74
75
76
77
# File 'lib/fog/proxmox/helpers/ip_helper.rb', line 73

def self.ip(ip)
  if cidr = CIDRv4_REGEXP.match(ip)
    cidr[1]
  end
end

.ip6(ip) ⇒ Object



79
80
81
82
83
# File 'lib/fog/proxmox/helpers/ip_helper.rb', line 79

def self.ip6(ip)
  if cidr = CIDRv6_REGEXP.match(ip)
    cidr[1]
  end
end

.ip6?(ip) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/fog/proxmox/helpers/ip_helper.rb', line 61

def self.ip6?(ip)
  Resolv::IPv6::Regex.match?(ip)
end

.ip?(ip) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/fog/proxmox/helpers/ip_helper.rb', line 57

def self.ip?(ip)
  Resolv::IPv4::Regex.match?(ip)
end

.prefix(ip) ⇒ Object



45
46
47
48
49
# File 'lib/fog/proxmox/helpers/ip_helper.rb', line 45

def self.prefix(ip)
  if cidr = CIDRv4_REGEXP.match(ip)
    cidr[7]
  end
end

.prefix6(ip) ⇒ Object



51
52
53
54
55
# File 'lib/fog/proxmox/helpers/ip_helper.rb', line 51

def self.prefix6(ip)
  if cidr = CIDRv6_REGEXP.match(ip)
    cidr[3]
  end
end

.to_cidr(ip, prefix = nil) ⇒ Object



85
86
87
88
89
90
# File 'lib/fog/proxmox/helpers/ip_helper.rb', line 85

def self.to_cidr(ip,prefix = nil)
  return nil unless self.ip?(ip) && (!prefix || self.cidr_prefix?(prefix))
  cidr = "#{ip}"
  cidr += "/#{prefix}" if self.cidr_prefix?(prefix)
  cidr
end

.to_cidr6(ip, prefix = nil) ⇒ Object



92
93
94
95
96
97
# File 'lib/fog/proxmox/helpers/ip_helper.rb', line 92

def self.to_cidr6(ip,prefix = nil)
  return nil unless self.ip6?(ip) && (!prefix || self.cidr6_prefix?(prefix))
  cidr = "#{ip}"
  cidr += "/#{prefix}" if self.cidr6_prefix?(prefix)
  cidr
end