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)


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

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

.cidr6_prefix?(prefix) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.cidr?(ip) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.cidr_prefix?(prefix) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.ip(ip) ⇒ Object



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

def self.ip(ip)
  return unless cidr = CIDRv4_REGEXP.match(ip)

  cidr[1]
end

.ip6(ip) ⇒ Object



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

def self.ip6(ip)
  return unless cidr = CIDRv6_REGEXP.match(ip)

  cidr[1]
end

.ip6?(ip) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.ip?(ip) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.prefix(ip) ⇒ Object



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

def self.prefix(ip)
  return unless cidr = CIDRv4_REGEXP.match(ip)

  cidr[7]
end

.prefix6(ip) ⇒ Object



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

def self.prefix6(ip)
  return unless cidr = CIDRv6_REGEXP.match(ip)

  cidr[3]
end

.to_cidr(ip, prefix = nil) ⇒ Object



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

def self.to_cidr(ip, prefix = nil)
  return nil unless ip?(ip) && (!prefix || cidr_prefix?(prefix))

  cidr = "#{ip}"
  cidr += "/#{prefix}" if cidr_prefix?(prefix)
  cidr
end

.to_cidr6(ip, prefix = nil) ⇒ Object



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

def self.to_cidr6(ip, prefix = nil)
  return nil unless ip6?(ip) && (!prefix || cidr6_prefix?(prefix))

  cidr = "#{ip}"
  cidr += "/#{prefix}" if cidr6_prefix?(prefix)
  cidr
end