Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (4.79 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 Template < PoolElement
21
        # ---------------------------------------------------------------------
22
        # Constants and Class Methods
23
        # ---------------------------------------------------------------------
24
        TEMPLATE_METHODS = {
25
            :allocate    => "template.allocate",
26
            :info        => "template.info",
27
            :update      => "template.update",
28
            :rmattr      => "template.rmattr",
29
            :publish     => "template.publish",
30
            :delete      => "template.delete"
31
        }
32

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

    
47
            XMLElement.build_xml(obj_xml,'VMTEMPLATE')
48
        end
49

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

    
56
            @client = client
57
        end
58

    
59
        # ---------------------------------------------------------------------
60
        # XML-RPC Methods for the Template Object
61
        # ---------------------------------------------------------------------
62
        
63
        # Retrieves the information of the given Template.
64
        def info()
65
            super(TEMPLATE_METHODS[:info], 'VMTEMPLATE')
66
        end
67

    
68
        # Allocates a new Template in OpenNebula
69
        #
70
        # +templatename+ A string containing the name of the Template.
71
        def allocate(templatename)
72
            super(TEMPLATE_METHODS[:allocate], templatename)
73
        end
74

    
75
        # Deletes the Template
76
        def delete()
77
            super(TEMPLATE_METHODS[:delete])
78
        end
79

    
80
        # Modifies a template attribute
81
        #
82
        # +name+ Name of the attribute to be changed
83
        #
84
        # +value+ New value for the attribute
85
        def update(name, value)
86
            super(TEMPLATE_METHODS[:update], name, value)
87
        end
88

    
89
        # Deletes a template attribute
90
        #
91
        # +name+ Name of the attribute to be deleted
92
        def remove_attr(name)
93
            do_rm_attr(name)
94
        end
95

    
96
        # Publishes the Template, to be used by other users
97
        def publish
98
            set_publish(true)
99
        end
100

    
101
        # Unplubishes the Image
102
        def unpublish
103
            set_publish(false)
104
        end
105

    
106
        # ---------------------------------------------------------------------
107
        # Helpers to get Template information
108
        # ---------------------------------------------------------------------
109

    
110
        # Returns the group identifier
111
        # [return] _Integer_ the element's group ID
112
        def gid
113
            self['GID'].to_i
114
        end
115

    
116
    private
117

    
118
        def set_publish(published)
119
            return Error.new('ID not defined') if !@pe_id
120

    
121
            rc = @client.call(TEMPLATE_METHODS[:publish], @pe_id, published)
122
            rc = nil if !OpenNebula.is_error?(rc)
123

    
124
            return rc
125
        end
126

    
127
        def do_rm_attr(name)
128
            return Error.new('ID not defined') if !@pe_id
129

    
130
            rc = @client.call(TEMPLATE_METHODS[:rmattr], @pe_id, name)
131
            rc = nil if !OpenNebula.is_error?(rc)
132

    
133
            return rc
134
        end
135
    end
136
end