Class: VLAN

Inherits:
Object
  • Object
show all
Defined in:
models/VLANManager.rb

Overview

Table of VLAN IDs ranges Model Class

Defined Under Namespace

Classes: NoAvailavleVLANsPoolsLeftException, NoFreeVLANsLeftException

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all_with_metaArray<Hash>

Returns all VLAN pools as Hash

Returns:

See Also:



91
92
93
94
95
# File 'models/VLANManager.rb', line 91

def self.all_with_meta
  VLAN.all.map do | v |
    v.hash_with_meta
  end
end

.available_poolVLAN

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_vlansTrueClass

Checking if any free VLANs left in Pool Will raise StandardError if no left

Returns:

  • (TrueClass)

Raises:



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_metaHash

Returns VLAN as hash and adds some meta data Adds :leased key with amount of leased IDs

Returns:



100
101
102
# File 'models/VLANManager.rb', line 100

def hash_with_meta
  to_hash.merge(leased: VLANLease.where(pool_id: id).count)
end

#hash_with_meta_and_leasesHash

Returns #hash_with_meta with leases objects

Returns:

See Also:



107
108
109
# File 'models/VLANManager.rb', line 107

def hash_with_meta_and_leases
  hash_with_meta.merge(leases: VLANLease.all_with_meta(id))
end

#lease(name, owner = 0, group = 0) ⇒ Integer

Note:

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

Parameters:

  • name (String)
    • New VNet Name

  • owner (Integer) (defaults to: 0)
    • OpenNebula::User ID. New VNet would be bind to that user

  • group (Integer) (defaults to: 0)
    • OpenNebula::Group ID. New VNet would be bind to that group

Returns:

  • (Integer)
    • New OpenNebula::VirtualNetwork ID



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

#leasesArray<VLANLease>

Returns all existing lease records

Returns:



113
114
115
# File 'models/VLANManager.rb', line 113

def leases
  VLANLease.where(pool_id: id).all
end

#next_idInteger | NilClass

Returns VLAN ID would be assigned with next lease Also saves it to local variable to reduce transactions

Returns:

  • (Integer | NilClass)
    • either ID or nil if no free VLANs left



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)

Parameters:

  • vlan_id (Integer)
    • VLAN ID to reserve

  • vn (Integer) (defaults to: nil)
    • Made for manual record creation, vn to lease to

Returns:

  • (TrueClass)
    • returns true if it worked out, or raises Exception if not



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