Permalink
Browse files

Ability to add IP with subnet mask.

  • Loading branch information...
1 parent fab093e commit 6816608f988013f7b35cea03d50b437973b902b8 @sibprogrammer committed Feb 23, 2013
Showing with 13 additions and 2 deletions.
  1. +2 −1 app/models/virtual_server.rb
  2. +6 −1 test/fixtures/ip_pools.yml
  3. +5 −0 test/unit/virtual_server_test.rb
@@ -10,7 +10,7 @@ class VirtualServer < ActiveRecord::Base
has_many :backups, :dependent => :destroy
has_many :bean_counters, :dependent => :destroy
- validates_format_of :ip_address, :with => /^(((\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})|\s|(([\da-fA-F]{1,4}:?)|(::)){1,8})*|auto)$/
+ validates_format_of :ip_address, :with => /^(((\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(\/\d+)?|\s|(([\da-fA-F]{1,4}:?)|(::)){1,8})*|auto)$/
validates_uniqueness_of :ip_address, :allow_blank => true
validates_uniqueness_of :identity, :scope => :hardware_server_id
validates_confirmation_of :password
@@ -323,6 +323,7 @@ def validate
ip_address.split(' ').each do |ip|
begin
+ ip, netmask = ip.split('/')
ip = IPAddr.new(ip).to_s
rescue
msg = I18n.t('activerecord.errors.models.virtual_server.attributes.ip_address.invalid')
@@ -11,4 +11,9 @@ ip_pool_2:
ip_pool_3:
id: 3
first_ip: 2001:0db8:85a3:0000:0000:8a2e:0370:7300
- last_ip: 2001:0db8:85a3:0000:0000:8a2e:0370:73a0
+ last_ip: 2001:0db8:85a3:0000:0000:8a2e:0370:73a0
+
+ip_pool_4:
+ id: 4
+ first_ip: 10.0.0.1
+ last_ip: 10.0.0.254
@@ -31,6 +31,11 @@ def setup
assert_valid @server_101
end
+ test "IPv4 address with netmask" do
+ @server_101.ip_address = "10.0.0.1/16"
+ assert_valid @server_101
+ end
+
test "Incorrect IP address assignment" do
@server_101.ip_address = "not IP address"
assert !@server_101.valid?

0 comments on commit 6816608

Please sign in to comment.