Class: IPAddr

Inherits:
Object
  • Object
show all
Defined in:
lib/azure/core/utility.rb

Overview

Code validate private/public IP acceptable ranges.

Constant Summary collapse

PRIVATE_RANGES =
[
  IPAddr.new('10.0.0.0/8'),
  IPAddr.new('172.16.0.0/12'),
  IPAddr.new('192.168.0.0/16')
]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.address_prefix(ip, cidr) ⇒ Object



234
235
236
# File 'lib/azure/core/utility.rb', line 234

def address_prefix(ip, cidr)
  ip + '/' + cidr.to_s
end

.valid?(ip) ⇒ Boolean

Returns:

  • (Boolean)


238
239
240
# File 'lib/azure/core/utility.rb', line 238

def valid?(ip)
  (IPAddr.new(ip) rescue nil).nil?
end

.validate_address_space(ip) ⇒ Object



226
227
228
229
230
231
232
# File 'lib/azure/core/utility.rb', line 226

def validate_address_space(ip)
  if ip.split('/').size != 2
    raise "Cidr is invalid for IP #{ip}."
  elsif valid?(ip)
    raise "Address space '#{ip}' is invalid."
  end
end

.validate_ip_and_prefix(ip, cidr) ⇒ Object



216
217
218
219
220
221
222
223
224
# File 'lib/azure/core/utility.rb', line 216

def validate_ip_and_prefix(ip, cidr)
  if cidr.to_s.empty?
    raise "Cidr is missing for IP '#{ip}'."
  elsif valid?(ip)
    raise "Ip address '#{ip}' is invalid."
  elsif !IPAddr.new(ip).private?
    raise "Ip Address #{ip} must be private."
  end
end

Instance Method Details

#private?Boolean

Returns:

  • (Boolean)


203
204
205
206
207
208
209
# File 'lib/azure/core/utility.rb', line 203

def private?
  return false unless self.ipv4?
  PRIVATE_RANGES.each do |ipr|
    return true if ipr.include?(self)
  end
  false
end

#public?Boolean

Returns:

  • (Boolean)


211
212
213
# File 'lib/azure/core/utility.rb', line 211

def public?
  !private?
end