Class: OpenNebula::VirtualNetwork

Inherits:
PoolElement show all
Defined in:
lib/opennebula/virtual_network.rb

Constant Summary collapse

VN_METHODS =

Constants and Class Methods

{
    :info       => "vn.info",
    :allocate   => "vn.allocate",
    :delete     => "vn.delete",
    :add_ar     => "vn.add_ar",
    :rm_ar      => "vn.rm_ar",
    :update_ar  => "vn.update_ar",
    :chown      => "vn.chown",
    :chmod      => "vn.chmod",
    :update     => "vn.update",
    :hold       => "vn.hold",
    :release    => "vn.release",
    :rename     => "vn.rename",
    :reserve    => "vn.reserve",
    :free_ar    => "vn.free_ar",
    :lock       => "vn.lock",
    :unlock     => "vn.unlock",
    :recover    => "vn.recover"
}
VN_STATES =
%w{INIT READY LOCK_CREATE LOCK_DELETE DONE ERROR UPDATE_FAILURE}
SHORT_VN_STATES =
{
    "INIT"          => "init",
    "READY"         => "rdy",
    "LOCK_CREATE"   => "lock",
    "LOCK_DELETE"   => "lock",
    "DONE"          => "done",
    "ERROR"         => "err",
    "UPDATE_FAILURE"=> "fail"
}

Instance Attribute Summary

Attributes inherited from PoolElement

#name

Instance Method Summary collapse

Methods inherited from PoolElement

#id, new_with_id, #replace, #to_str

Methods inherited from XMLElement

#[], #add_element, #attr, build_xml, #delete_element, #each, #each_xpath, #element_xml, #has_elements?, #initialize_xml, #name, #retrieve_elements, #retrieve_xmlelements, #set_content, #template_like_str, #template_str, #template_xml, #text, #to_hash, #to_xml, #xml_nil?

Constructor Details

#initialize(xml, client) ⇒ VirtualNetwork

Class constructor



77
78
79
80
81
# File 'lib/opennebula/virtual_network.rb', line 77

def initialize(xml, client)
    LockableExt.make_lockable(self, VN_METHODS)

    super(xml,client)
end

Instance Method Details

#add_ar(ar_template) ⇒ Object

Adds Address Ranges to the VirtualNetwork



133
134
135
136
137
138
139
140
# File 'lib/opennebula/virtual_network.rb', line 133

def add_ar(ar_template)
    return Error.new('ID not defined') if !@pe_id

    rc = @client.call(VN_METHODS[:add_ar], @pe_id, ar_template)
    rc = nil if !OpenNebula.is_error?(rc)

    return rc
end

#allocate(description, cluster_id = ClusterPool::NONE_CLUSTER_ID) ⇒ Integer, OpenNebula::Error

Allocates a new VirtualNetwork in OpenNebula

Parameters:

  • description (String)

    The template of the VirtualNetwork.

  • cluster_id (Integer) (defaults to: ClusterPool::NONE_CLUSTER_ID)

    Id of the cluster, -1 to use default

Returns:



101
102
103
# File 'lib/opennebula/virtual_network.rb', line 101

def allocate(description,cluster_id=ClusterPool::NONE_CLUSTER_ID)
    super(VN_METHODS[:allocate], description, cluster_id)
end

#chmod(owner_u, owner_m, owner_a, group_u, group_m, group_a, other_u, other_m, other_a) ⇒ nil, OpenNebula::Error

Changes the virtual network permissions. Each [Integer] argument must be 1 to allow, 0 deny, -1 do not change

Returns:



275
276
277
278
279
# File 'lib/opennebula/virtual_network.rb', line 275

def chmod(owner_u, owner_m, owner_a, group_u, group_m, group_a, other_u,
        other_m, other_a)
    super(VN_METHODS[:chmod], owner_u, owner_m, owner_a, group_u,
        group_m, group_a, other_u, other_m, other_a)
end

#chmod_octet(octet) ⇒ nil, OpenNebula::Error

Changes the virtual network permissions.

Parameters:

  • octet (String)

    Permissions octed , e.g. 640

Returns:



266
267
268
# File 'lib/opennebula/virtual_network.rb', line 266

def chmod_octet(octet)
    super(VN_METHODS[:chmod], octet)
end

#chown(uid, gid) ⇒ nil, OpenNebula::Error

Changes the owner/group

Parameters:

  • uid (Integer)

    the new owner id. Set to -1 to leave the current one

  • gid (Integer)

    the new group id. Set to -1 to leave the current one

Returns:



257
258
259
# File 'lib/opennebula/virtual_network.rb', line 257

def chown(uid, gid)
    super(VN_METHODS[:chown], uid, gid)
end

#deleteObject

Deletes the VirtualNetwork



128
129
130
# File 'lib/opennebula/virtual_network.rb', line 128

def delete()
    super(VN_METHODS[:delete])
end

#free(ar_id) ⇒ Object

Removes an Address Range from the VirtualNetwork



241
242
243
244
245
246
247
248
# File 'lib/opennebula/virtual_network.rb', line 241

def free(ar_id)
    return Error.new('ID not defined') if !@pe_id

    rc = @client.call(VN_METHODS[:free_ar], @pe_id, ar_id.to_i)
    rc = nil if !OpenNebula.is_error?(rc)

    return rc
end

#gidObject

Returns the group identifier [return] Integer the element's group ID



307
308
309
# File 'lib/opennebula/virtual_network.rb', line 307

def gid
    self['GID'].to_i
end

#hold(ip, ar_id = -1)) ⇒ Object

Holds a virtual network address

Parameters:

  • ip (String)

    address to hold, if contains ":" a MAC address is assumed

  • ar_id (Integer) (defaults to: -1))

    The address range to hold the lease. If not set the lease will be held from all possible address ranges



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/opennebula/virtual_network.rb', line 166

def hold(ip, ar_id=-1)
    return Error.new('ID not defined') if !@pe_id

    addr_name = address_type(ip)

    return addr_name if OpenNebula.is_error?(addr_name)

    lease_template =  "LEASES = [ #{addr_name} = #{ip}"
    lease_template << ", AR_ID = #{ar_id}" if ar_id != -1
    lease_template << "]"

    rc = @client.call(VN_METHODS[:hold], @pe_id, lease_template)
    rc = nil if !OpenNebula.is_error?(rc)

    return rc
end

#info(decrypt = false) ⇒ Object Also known as: info!

Retrieves the information of the given VirtualNetwork.



88
89
90
# File 'lib/opennebula/virtual_network.rb', line 88

def info(decrypt = false)
    super(VN_METHODS[:info], 'VNET', decrypt)
end

#public?Boolean

Returns:

  • (Boolean)


311
312
313
314
315
316
317
# File 'lib/opennebula/virtual_network.rb', line 311

def public?
    if self['PERMISSIONS/GROUP_U'] == "1" || self['PERMISSIONS/OTHER_U'] == "1"
        true
    else
        false
    end
end

#publishObject

Publishes the VirtualNetwork, to be used by other users



118
119
120
# File 'lib/opennebula/virtual_network.rb', line 118

def publish
    set_publish(true)
end

#recover(result) ⇒ nil, OpenNebula::Error

Recovers an stuck Virtual Network

delete (2), retry (3)

Parameters:

  • result (Integer)

    Recover with failure (0), success (1),

Returns:



297
298
299
# File 'lib/opennebula/virtual_network.rb', line 297

def recover(result)
    return call(VN_METHODS[:recover], @pe_id, result)
end

#release(ip, ar_id = -1)) ⇒ Object

Releases an address on hold

Parameters:

  • ip (String)

    IP to release, if contains ":" a MAC address is assumed

  • ar_id (Integer) (defaults to: -1))

    The address range to release the lease. If not set the lease will be freed from all possible address ranges



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/opennebula/virtual_network.rb', line 187

def release(ip, ar_id=-1)
    return Error.new('ID not defined') if !@pe_id

    addr_name = address_type(ip)

    return addr_name if OpenNebula.is_error?(addr_name)

    lease_template =  "LEASES = [ #{addr_name} = #{ip}"
    lease_template << ", AR_ID = #{ar_id}" if ar_id != -1
    lease_template << "]"

    rc = @client.call(VN_METHODS[:release], @pe_id, lease_template)
    rc = nil if !OpenNebula.is_error?(rc)

    return rc
end

#rename(name) ⇒ nil, OpenNebula::Error

Renames this virtual network

Parameters:

  • name (String)

    New name for the virtual network.

Returns:



287
288
289
# File 'lib/opennebula/virtual_network.rb', line 287

def rename(name)
    return call(VN_METHODS[:rename], @pe_id, name)
end

#reserve(rname, rsize, ar_id, addr, vnet) ⇒ Integer, OpenNebula::Error

Reserve a set of addresses from this virtual network

Parameters:

  • rname (String)

    of the reservation

  • rsize (String)

    number of addresses to reserve

  • ar_id (String)

    the ar_id to make the reservation. If set to nil any address range will be used

  • addr (String)

    the first address in the reservation. If set to nil the first free address will be used

  • vnet (String)

    ID of the VNET to add the reservation to. If not set a new VNET will be created.

Returns:

  • (Integer, OpenNebula::Error)

    The reservation vnet id on success, Error otherwise



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/opennebula/virtual_network.rb', line 215

def reserve(rname, rsize, ar_id, addr, vnet)
    return Error.new('ID not defined') if !@pe_id

    rtmpl =  "SIZE       = #{rsize}\n"
    rtmpl << "NAME       = \"#{rname}\"\n" if !rname.nil?
    rtmpl << "AR_ID      = #{ar_id}\n" if !ar_id.nil?
    rtmpl << "NETWORK_ID = #{vnet}\n"  if !vnet.nil?

    if !addr.nil?
        addr_name = address_type(addr)

        return addr_name if OpenNebula.is_error?(addr_name)

        rtmpl << "#{addr_name} = #{addr}\n"
    end

    return @client.call(VN_METHODS[:reserve], @pe_id, rtmpl)
end

#reserve_with_extra(extra) ⇒ Object



234
235
236
237
238
# File 'lib/opennebula/virtual_network.rb', line 234

def reserve_with_extra(extra)
    return Error.new('ID not defined') unless @pe_id

    @client.call(VN_METHODS[:reserve], @pe_id, extra)
end

#rm_ar(ar_id, force = false) ⇒ Object

Removes an Address Range from the VirtualNetwork



143
144
145
146
147
148
149
150
# File 'lib/opennebula/virtual_network.rb', line 143

def rm_ar(ar_id, force = false)
    return Error.new('ID not defined') if !@pe_id

    rc = @client.call(VN_METHODS[:rm_ar], @pe_id, ar_id.to_i, force)
    rc = nil if !OpenNebula.is_error?(rc)

    return rc
end

#short_state_strObject

Returns the state of the Virtual Network (string value)



341
342
343
# File 'lib/opennebula/virtual_network.rb', line 341

def short_state_str
    SHORT_VN_STATES[state_str]
end

#stateObject

Returns the state of the Virtual Network (numeric value)



331
332
333
# File 'lib/opennebula/virtual_network.rb', line 331

def state
    self['STATE'].to_i
end

#state_strObject

Returns the state of the Virtual Network (string value)



336
337
338
# File 'lib/opennebula/virtual_network.rb', line 336

def state_str
    VN_STATES[state]
end

#unpublishObject

Unplubishes the VirtualNetwork



123
124
125
# File 'lib/opennebula/virtual_network.rb', line 123

def unpublish
    set_publish(false)
end

#update(new_template = nil, append = false) ⇒ nil, OpenNebula::Error

Replaces the template contents

Parameters:

  • new_template (String) (defaults to: nil)

    New template contents

  • append (true, false) (defaults to: false)

    True to append new attributes instead of replace the whole template

Returns:



113
114
115
# File 'lib/opennebula/virtual_network.rb', line 113

def update(new_template=nil, append=false)
    super(VN_METHODS[:update], new_template, append ? 1 : 0)
end

#update_ar(ar_template) ⇒ Object

Updates Address Ranges from the VirtualNetwork



153
154
155
156
157
158
159
160
# File 'lib/opennebula/virtual_network.rb', line 153

def update_ar(ar_template)
    return Error.new('ID not defined') if !@pe_id

    rc = @client.call(VN_METHODS[:update_ar], @pe_id, ar_template)
    rc = nil if !OpenNebula.is_error?(rc)

    return rc
end

#vm_idsObject

Returns three arrays with the numeric VM IDs for VMs updated, outdated (include updating) and error



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/opennebula/virtual_network.rb', line 347

def vm_ids
    updated = Array.new

    self.each('UPDATED_VMS/ID') do |id|
        updated << id.text.to_i
    end

    outdated = Array.new

    self.each('OUTDATED_VMS/ID') do |id|
        outdated << id.text.to_i
    end

    self.each('UPDATING_VMS/ID') do |id|
        outdated << id.text.to_i
    end

    error = Array.new

    self.each('ERROR_VMS/ID') do |id|
        error << id.text.to_i
    end

    [updated, outdated, error]
end

#vrouter_idsObject

Returns an array with the numeric virtual router ids



320
321
322
323
324
325
326
327
328
# File 'lib/opennebula/virtual_network.rb', line 320

def vrouter_ids
    array = Array.new

    self.each("VROUTERS/ID") do |id|
        array << id.text.to_i
    end

    return array
end