lib/dcmgr/models/network.rb in wakame-vdc-dcmgr-10.12.0 vs lib/dcmgr/models/network.rb in wakame-vdc-dcmgr-11.06.0

- old
+ new

@@ -12,17 +12,32 @@ Fixnum :prefix, :null=>false, :default=>24, :unsigned=>true String :domain_name String :dns_server String :dhcp_server String :metadata_server - Fixnum :vlan_lease_id, :null=>false + Fixnum :metadata_server_port + Fixnum :bandwidth #in Mbit/s + Fixnum :vlan_lease_id, :null=>false, :default=>0 + Fixnum :nat_network_id Text :description + index :nat_network_id end with_timestamps - one_to_many :ip_lease + module IpLeaseMethods + def add_reserved(ipaddr, description=nil) + model.create(:network_id=>model_object.id, + :ipv4=>ipaddr, + :alloc_type=>IpLease::TYPE_RESERVED, + :description=>description) + end + end + one_to_many :ip_lease, :extend=>IpLeaseMethods many_to_one :vlan_lease + + many_to_one :nat_network, :key => :nat_network_id, :class => self + one_to_many :inside_networks, :key => :nat_network_id, :class => self def validate super # validate ipv4 syntax @@ -39,12 +54,36 @@ def to_hash h = super h.delete(:vlan_lease_id) h.merge({ + :bandwidth_mark=>self[:id], :description=>description.to_s, - :vlan_id => vlan_lease.tag_id, + :vlan_id => vlan_lease.nil? ? 0 : vlan_lease.tag_id, }) + end + + def before_destroy + #Make sure no other networks are natted to this one + Network.filter(:nat_network_id => self[:id]).each { |n| + n.nat_network_id = nil + n.save + } + + #Delete all reserved ipleases in this network + self.ip_lease_dataset.filter(:alloc_type => IpLease::TYPE_RESERVED).each { |i| + i.destroy + } + + super + end + + def to_api_document + to_hash + end + + def nat_network + Network.find(:id => self.nat_network_id) end def ipaddress IPAddress::IPv4.new("#{self.ipv4_gw}/#{self.prefix}") end