Class: VLAN
- Inherits:
-
Object
- Object
- VLAN
- Defined in:
- models/VLANManager.rb
Overview
Table of VLAN IDs ranges Model Class
Defined Under Namespace
Classes: NoAvailavleVLANsPoolsLeftException, NoFreeVLANsLeftException
Class Method Summary collapse
-
.all_with_meta ⇒ Array<Hash>
Returns all VLAN pools as Hash.
-
.available_pool ⇒ VLAN
Returns first VLANs pool with available VLAN or raises VLAN::NoAvailavleVLANsPoolsLeftException.
Instance Method Summary collapse
-
#check_free_vlans ⇒ TrueClass
Checking if any free VLANs left in Pool Will raise StandardError if no left.
-
#hash_with_meta ⇒ Hash
Returns VLAN as hash and adds some meta data Adds :leased key with amount of leased IDs.
-
#hash_with_meta_and_leases ⇒ Hash
Returns #hash_with_meta with leases objects.
-
#lease(name, owner = 0, group = 0) ⇒ Integer
Create new VNet and lease record with VLAN ID from this Pool.
-
#leases ⇒ Array<VLANLease>
Returns all existing lease records.
-
#next_id ⇒ Integer | NilClass
Returns VLAN ID would be assigned with next lease Also saves it to local variable to reduce transactions.
-
#reserve(vlan_id, vn = nil) ⇒ TrueClass
Reserve VLAN ID(so it won't be used for new VNets).
-
#to_json(*args) ⇒ Object
Needed for serialization.
Class Method Details
.all_with_meta ⇒ Array<Hash>
Returns all VLAN pools as Hash
91 92 93 94 95 |
# File 'models/VLANManager.rb', line 91 def self. VLAN.all.map do | v | v. end end |
.available_pool ⇒ VLAN
Returns first VLANs pool with available VLAN or raises VLAN::NoAvailavleVLANsPoolsLeftException
79 80 81 82 83 84 85 86 |
# File 'models/VLANManager.rb', line 79 def self.available_pool VLAN.all.each do | vlan | return vlan if vlan.check_free_vlans rescue VLAN::NoFreeVLANsLeftException next end raise VLAN::NoAvailavleVLANsPoolsLeftException.new end |
Instance Method Details
#check_free_vlans ⇒ TrueClass
Checking if any free VLANs left in Pool Will raise StandardError if no left
127 128 129 130 131 |
# File 'models/VLANManager.rb', line 127 def check_free_vlans raise VLAN::NoFreeVLANsLeftException.new(id) if next_id.nil? true end |
#hash_with_meta ⇒ Hash
Returns VLAN as hash and adds some meta data Adds :leased key with amount of leased IDs
100 101 102 |
# File 'models/VLANManager.rb', line 100 def to_hash.merge(leased: VLANLease.where(pool_id: id).count) end |
#hash_with_meta_and_leases ⇒ Hash
Returns #hash_with_meta with leases objects
107 108 109 |
# File 'models/VLANManager.rb', line 107 def .merge(leases: VLANLease.(id)) end |
#lease(name, owner = 0, group = 0) ⇒ Integer
Must be exported and accessible $client object with OpenNebula::Client in order to make method working Otherwise will raise `info': undefined method `call' for nil:NilClass (NoMethodError) on template
Create new VNet and lease record with VLAN ID from this Pool
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'models/VLANManager.rb', line 140 def lease name, owner = 0, group = 0 template = IONe::Settings['VNETS_TEMPLATES'][type] if template.nil? then raise StandardError.new("No Template for VN_MAD #{type} configured") else template = OpenNebula::VNTemplate.new_with_id(template.to_i, $client) end rc = template.info! if OpenNebula.is_error? rc then raise rc end check_free_vlans rc = template.instantiate( name, { VLAN_ID: @next_id, PHYDEV: "vlan#{@next_id}pd", BRIDGE: "vlan#{@next_id}pd" }.to_one_template ) if OpenNebula.is_error? rc then raise rc end VLANLease.insert(vn: rc, id: @next_id, pool_id: id) vnet = OpenNebula::VirtualNetwork.new_with_id(rc, $client) vnet.chown(owner, group) vnet.id end |
#leases ⇒ Array<VLANLease>
Returns all existing lease records
113 114 115 |
# File 'models/VLANManager.rb', line 113 def leases VLANLease.where(pool_id: id).all end |
#next_id ⇒ Integer | NilClass
Returns VLAN ID would be assigned with next lease Also saves it to local variable to reduce transactions
120 121 122 |
# File 'models/VLANManager.rb', line 120 def next_id @next_id = ((start...(start + size)).to_a - leases.map { |l| l.id }).first end |
#reserve(vlan_id, vn = nil) ⇒ TrueClass
Reserve VLAN ID(so it won't be used for new VNets)
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'models/VLANManager.rb', line 178 def reserve vlan_id, vn = nil unless (start...(start + size)).include? vlan_id then raise StandardError.new("Requested VLAN ID isn't a part of this Pool(#{id})") end lease = if vn.nil? then VLANLease.where(id: vlan_id, pool_id: id).first else VLANLease.where(vn: vn, id: vlan_id, pool_id: id).first end unless lease.nil? then raise StandardError.new("Requested VLAN ID is already leased") end unless vn.nil? then VLANLease.insert(vn: vn, id: vlan_id, pool_id: id) else VLANLease.insert(id: vlan_id, pool_id: id) end true end |
#to_json(*args) ⇒ Object
Needed for serialization
217 218 219 |
# File 'models/VLANManager.rb', line 217 def to_json *args @values.without(:key).to_json(*args) end |