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"
}

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PoolElement

#id, #name, new_with_id, #to_str

Methods inherited from XMLElement

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

Constructor Details

#initialize(xml, client) ⇒ VirtualNetwork

Class constructor



61
62
63
# File 'lib/opennebula/virtual_network.rb', line 61

def initialize(xml, client)
    super(xml,client)
end

Class Method Details

.build_xml(pe_id = nil) ⇒ Object

Creates a VirtualNetwork description with just its identifier this method should be used to create plain VirtualNetwork objects. id the id of the network

Example:

vnet = VirtualNetwork.new(VirtualNetwork.build_xml(3),rpc_client)


50
51
52
53
54
55
56
57
58
# File 'lib/opennebula/virtual_network.rb', line 50

def VirtualNetwork.build_xml(pe_id=nil)
    if pe_id
        vn_xml = "<VNET><ID>#{pe_id}</ID></VNET>"
    else
        vn_xml = "<VNET></VNET>"
    end

    XMLElement.build_xml(vn_xml, 'VNET')
end

Instance Method Details

#add_ar(ar_template) ⇒ Object

Adds Address Ranges to the VirtualNetwork



115
116
117
118
119
120
121
122
# File 'lib/opennebula/virtual_network.rb', line 115

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

#addleases(ip, mac = nil) ⇒ Object

Deprecated.

use #add_ar

Simulates old addleases call



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/opennebula/virtual_network.rb', line 126

def addleases(ip, mac=nil)
    self.info

    ar_id = self.retrieve_elements("AR_POOL/AR[IP='#{ip}' and SIZE='1']/AR_ID")

    if !ar_id.nil?
        return Error.new("IP Address Range found with IP #{ip}")
    end

    template = 'AR = [ '
    template << 'TYPE = "IP4"'
    template << ', IP = "' << ip << '"' if ip
    template << ', MAC = "' << mac << '"' if mac
    template << ', SIZE = 1 ]'

    add_ar(template)
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

Returns:



83
84
85
# File 'lib/opennebula/virtual_network.rb', line 83

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:



293
294
295
296
297
# File 'lib/opennebula/virtual_network.rb', line 293

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:



284
285
286
# File 'lib/opennebula/virtual_network.rb', line 284

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:



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

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

#deleteObject

Deletes the VirtualNetwork



110
111
112
# File 'lib/opennebula/virtual_network.rb', line 110

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

#free(ar_id) ⇒ Object

Removes an Address Range from the VirtualNetwork



259
260
261
262
263
264
265
266
# File 'lib/opennebula/virtual_network.rb', line 259

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



315
316
317
# File 'lib/opennebula/virtual_network.rb', line 315

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



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

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

    if ip.include?':'
        addr_name = "MAC"
    else
        addr_name = "IP"
    end

    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

#infoObject Also known as: info!

Retrieves the information of the given VirtualNetwork.



70
71
72
# File 'lib/opennebula/virtual_network.rb', line 70

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

#public?Boolean

Returns:

  • (Boolean)


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

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



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

def publish
    set_publish(true)
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



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/opennebula/virtual_network.rb', line 207

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

    if ip.include?':'
        addr_name = "MAC"
    else
        addr_name = "IP"
    end

    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:



305
306
307
# File 'lib/opennebula/virtual_network.rb', line 305

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



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/opennebula/virtual_network.rb', line 237

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?
        if addr.include?':'
            addr_name = "MAC"
        else
            addr_name = "IP"
        end

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

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

#rm_ar(ar_id) ⇒ Object

Removes an Address Range from the VirtualNetwork



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

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

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

    return rc
end

#rmleases(ip) ⇒ Object

Deprecated.

use ##rm_ar

Simulates old rmleases call



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/opennebula/virtual_network.rb', line 156

def rmleases(ip)
    self.info

    ar_id = self.retrieve_elements("AR_POOL/AR[IP='#{ip}' and SIZE='1']/AR_ID")

    if !ar_id
        Error.new("No single IP Address Range found with IP #{ip}")
    elsif ar_id.size > 1
        Error.new("More than one Address Range found with IP #{ip} use rmar")
    else
        rm_ar(ar_id[0])
    end
end

#unpublishObject

Unplubishes the VirtualNetwork



105
106
107
# File 'lib/opennebula/virtual_network.rb', line 105

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:



95
96
97
# File 'lib/opennebula/virtual_network.rb', line 95

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



171
172
173
174
175
176
177
178
# File 'lib/opennebula/virtual_network.rb', line 171

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