Statistics
| Branch: | Tag: | Revision:

one / src / oca / ruby / OpenNebula / VirtualNetwork.rb @ 6329d8b3

History | View | Annotate | Download (4.73 KB)

1
# -------------------------------------------------------------------------- #
2
# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org)             #
3
#                                                                            #
4
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
5
# not use this file except in compliance with the License. You may obtain    #
6
# a copy of the License at                                                   #
7
#                                                                            #
8
# http://www.apache.org/licenses/LICENSE-2.0                                 #
9
#                                                                            #
10
# Unless required by applicable law or agreed to in writing, software        #
11
# distributed under the License is distributed on an "AS IS" BASIS,          #
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
13
# See the License for the specific language governing permissions and        #
14
# limitations under the License.                                             #
15
#--------------------------------------------------------------------------- #
16

    
17
require 'OpenNebula/Pool'
18

    
19
module OpenNebula
20
    class VirtualNetwork < PoolElement
21
        # ---------------------------------------------------------------------
22
        # Constants and Class Methods
23
        # ---------------------------------------------------------------------
24
        VN_METHODS = {
25
            :info       => "vn.info",
26
            :allocate   => "vn.allocate",
27
            :publish    => "vn.publish",
28
            :delete     => "vn.delete",
29
            :addleases  => "vn.addleases",
30
            :rmleases   => "vn.rmleases"
31
        }
32

    
33
        # Creates a VirtualNetwork description with just its identifier
34
        # this method should be used to create plain VirtualNetwork objects.
35
        # +id+ the id of the network
36
        #
37
        # Example:
38
        #   vnet = VirtualNetwork.new(VirtualNetwork.build_xml(3),rpc_client)
39
        #
40
        def VirtualNetwork.build_xml(pe_id=nil)
41
            if pe_id
42
                vn_xml = "<VNET><ID>#{pe_id}</ID></VNET>"
43
            else
44
                vn_xml = "<VNET></VNET>"
45
            end
46

    
47
            XMLElement.build_xml(vn_xml, 'VNET')
48
        end
49

    
50
        # Class constructor
51
        def initialize(xml, client)
52
            super(xml,client)
53

    
54
            @client = client
55
        end
56

    
57
        #######################################################################
58
        # XML-RPC Methods for the Virtual Network Object
59
        #######################################################################
60

    
61
        # Retrieves the information of the given VirtualNetwork.
62
        def info()
63
            super(VN_METHODS[:info], 'VNET')
64
        end
65

    
66
        # Allocates a new VirtualNetwork in OpenNebula
67
        #
68
        # +description+ A string containing the template of the VirtualNetwork.
69
        def allocate(description)
70
            super(VN_METHODS[:allocate],description)
71
        end
72
        
73
        # Publishes the VirtualNetwork, to be used by other users
74
        def publish
75
            set_publish(true)
76
        end
77

    
78
        # Unplubishes the VirtualNetwork
79
        def unpublish
80
            set_publish(false)
81
        end
82

    
83
        # Deletes the VirtualNetwork
84
        def delete()
85
            super(VN_METHODS[:delete])
86
        end
87

    
88
        # Adds a lease to the VirtualNetwork
89
        def addleases(ip, mac = nil)
90
            return Error.new('ID not defined') if !@pe_id
91

    
92
            lease_template = "LEASES = [ IP = #{ip}"
93
            lease_template << ", MAC = #{mac}" if mac
94
            lease_template << " ]"
95

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

    
99
            return rc
100
        end
101

    
102
        # Removes a lease from the VirtualNetwork
103
        def rmleases(ip)
104
            return Error.new('ID not defined') if !@pe_id
105

    
106
            lease_template = "LEASES = [ IP = #{ip} ]"
107

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

    
111
            return rc
112
        end
113

    
114
        # ---------------------------------------------------------------------
115
        # Helpers to get VirtualNetwork information
116
        # ---------------------------------------------------------------------
117

    
118
        # Returns the group identifier
119
        # [return] _Integer_ the element's group ID
120
        def gid
121
            self['GID'].to_i
122
        end
123

    
124
    private
125
        def set_publish(published)
126
            return Error.new('ID not defined') if !@pe_id
127

    
128
            rc = @client.call(VN_METHODS[:publish], @pe_id, published)
129
            rc = nil if !OpenNebula.is_error?(rc)
130

    
131
            return rc
132
        end
133

    
134
    end
135
end