opennebula-oca 3.8.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,147 @@
1
+ # -------------------------------------------------------------------------- #
2
+ # Copyright 2002-2012, 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
+
18
+ require 'OpenNebula/Pool'
19
+
20
+ module OpenNebula
21
+ class Datastore < PoolElement
22
+ #######################################################################
23
+ # Constants and Class Methods
24
+ #######################################################################
25
+
26
+ DATASTORE_METHODS = {
27
+ :info => "datastore.info",
28
+ :allocate => "datastore.allocate",
29
+ :delete => "datastore.delete",
30
+ :update => "datastore.update",
31
+ :chown => "datastore.chown",
32
+ :chmod => "datastore.chmod"
33
+ }
34
+
35
+ # Creates a Datastore description with just its identifier
36
+ # this method should be used to create plain Datastore objects.
37
+ # +id+ the id of the user
38
+ #
39
+ # Example:
40
+ # datastore = Datastore.new(Datastore.build_xml(3),rpc_client)
41
+ #
42
+ def Datastore.build_xml(pe_id=nil)
43
+ if pe_id
44
+ datastore_xml = "<DATASTORE><ID>#{pe_id}</ID></DATASTORE>"
45
+ else
46
+ datastore_xml = "<DATASTORE></DATASTORE>"
47
+ end
48
+
49
+ XMLElement.build_xml(datastore_xml,'DATASTORE')
50
+ end
51
+
52
+ # Class constructor
53
+ def initialize(xml, client)
54
+ super(xml,client)
55
+ end
56
+
57
+ #######################################################################
58
+ # XML-RPC Methods for the Datastore Object
59
+ #######################################################################
60
+
61
+ # Retrieves the information of the given Datastore.
62
+ def info()
63
+ super(DATASTORE_METHODS[:info], 'DATASTORE')
64
+ end
65
+
66
+ # Allocates a new Datastore in OpenNebula
67
+ #
68
+ # @param description [String] The template of the Datastore.
69
+ # @param cluster_id [Integer] Id of the cluster
70
+ #
71
+ # @return [Integer, OpenNebula::Error] the new ID in case of
72
+ # success, error otherwise
73
+ def allocate(description, cluster_id=ClusterPool::NONE_CLUSTER_ID)
74
+ super(DATASTORE_METHODS[:allocate], description, cluster_id)
75
+ end
76
+
77
+ # Deletes the Datastore
78
+ def delete()
79
+ super(DATASTORE_METHODS[:delete])
80
+ end
81
+
82
+ # Replaces the template contents
83
+ #
84
+ # @param new_template [String] New template contents
85
+ #
86
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
87
+ # otherwise
88
+ def update(new_template)
89
+ super(DATASTORE_METHODS[:update], new_template)
90
+ end
91
+
92
+ # Changes the owner/group
93
+ #
94
+ # @param uid [Integer] the new owner id. Set to -1 to leave the current one
95
+ # @param gid [Integer] the new group id. Set to -1 to leave the current one
96
+ #
97
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
98
+ # otherwise
99
+ def chown(uid, gid)
100
+ super(DATASTORE_METHODS[:chown], uid, gid)
101
+ end
102
+
103
+ # Changes the datastore permissions.
104
+ #
105
+ # @param octet [String] Permissions octed , e.g. 640
106
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
107
+ # otherwise
108
+ def chmod_octet(octet)
109
+ super(DATASTORE_METHODS[:chmod], octet)
110
+ end
111
+
112
+ # Changes the datastore permissions.
113
+ # Each [Integer] argument must be 1 to allow, 0 deny, -1 do not change
114
+ #
115
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
116
+ # otherwise
117
+ def chmod(owner_u, owner_m, owner_a, group_u, group_m, group_a, other_u,
118
+ other_m, other_a)
119
+ super(DATASTORE_METHODS[:chmod], owner_u, owner_m, owner_a, group_u,
120
+ group_m, group_a, other_u, other_m, other_a)
121
+ end
122
+
123
+ # ---------------------------------------------------------------------
124
+ # Helpers to get information
125
+ # ---------------------------------------------------------------------
126
+
127
+ # Returns whether or not the image with id 'id' is part of this datastore
128
+ def contains(id)
129
+ #This doesn't work in ruby 1.8.5
130
+ #return self["DATASTORE/ID[.=#{uid}]"] != nil
131
+
132
+ id_array = retrieve_elements('IMAGES/ID')
133
+ return id_array != nil && id_array.include?(uid.to_s)
134
+ end
135
+
136
+ # Returns an array with the numeric image ids
137
+ def img_ids
138
+ array = Array.new
139
+
140
+ self.each("IMAGES/ID") do |id|
141
+ array << id.text.to_i
142
+ end
143
+
144
+ return array
145
+ end
146
+ end
147
+ end
@@ -0,0 +1,53 @@
1
+ # -------------------------------------------------------------------------- #
2
+ # Copyright 2002-2012, 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
+
18
+ require 'OpenNebula/Pool'
19
+
20
+ module OpenNebula
21
+ class DatastorePool < Pool
22
+ #######################################################################
23
+ # Constants and Class attribute accessors
24
+ #######################################################################
25
+
26
+ DATASTORE_POOL_METHODS = {
27
+ :info => "datastorepool.info"
28
+ }
29
+
30
+ #######################################################################
31
+ # Class constructor & Pool Methods
32
+ #######################################################################
33
+
34
+ # +client+ a Client object that represents a XML-RPC connection
35
+ def initialize(client)
36
+ super('DATASTORE_POOL','DATASTORE',client)
37
+ end
38
+
39
+ # Factory method to create User objects
40
+ def factory(element_xml)
41
+ OpenNebula::Group.new(element_xml,@client)
42
+ end
43
+
44
+ #######################################################################
45
+ # XML-RPC Methods for the User Object
46
+ #######################################################################
47
+
48
+ # Retrieves all the Groups in the pool.
49
+ def info()
50
+ super(DATASTORE_POOL_METHODS[:info])
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,248 @@
1
+ # -------------------------------------------------------------------------- #
2
+ # Copyright 2002-2012, 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
+
21
+ # All subclasses must define the DOCUMENT_TYPE constant.
22
+ #
23
+ # @example
24
+ # require 'OpenNebula/Document'
25
+ #
26
+ # module OpenNebula
27
+ # class CustomObject < Document
28
+ #
29
+ # DOCUMENT_TYPE = 400
30
+ #
31
+ # end
32
+ # end
33
+ class Document < PoolElement
34
+
35
+ #######################################################################
36
+ # Constants and Class Methods
37
+ #######################################################################
38
+
39
+ DOCUMENT_METHODS = {
40
+ :allocate => "document.allocate",
41
+ :delete => "document.delete",
42
+ :info => "document.info",
43
+ :update => "document.update",
44
+ :chown => "document.chown",
45
+ :chmod => "document.chmod",
46
+ :clone => "document.clone",
47
+ }
48
+
49
+ # Creates a Document Object description with just its identifier
50
+ # this method should be used to create plain Document objects.
51
+ # @param [Integer] pe_id the id of the object
52
+ #
53
+ # @return [Nokogiri::XML::Node, REXML::Element] the empty xml
54
+ def Document.build_xml(pe_id=nil)
55
+ if pe_id
56
+ obj_xml = "<DOCUMENT><ID>#{pe_id}</ID></DOCUMENT>"
57
+ else
58
+ obj_xml = "<DOCUMENT></DOCUMENT>"
59
+ end
60
+
61
+ XMLElement.build_xml(obj_xml,'DOCUMENT')
62
+ end
63
+
64
+ # Class constructor
65
+ #
66
+ # @param [Nokogiri::XML::Node, REXML::Element] xml string
67
+ # created by the build_xml() method
68
+ # @param [OpenNebula::Client] client the xml-rpc client
69
+ #
70
+ # @return [Document] the new object
71
+ #
72
+ # @example
73
+ # doc = Document.new(Document.build_xml(3),rpc_client)
74
+ def initialize(xml, client)
75
+ super(xml,client)
76
+ end
77
+
78
+ #######################################################################
79
+ # XML-RPC Methods for the Document Object
80
+ #######################################################################
81
+
82
+ # Retrieves the information of the given Document.
83
+ #
84
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
85
+ # otherwise
86
+ def info()
87
+ rc = super(DOCUMENT_METHODS[:info], 'DOCUMENT')
88
+
89
+ if !OpenNebula.is_error?(rc) && self['TYPE'].to_i != document_type
90
+ return OpenNebula::Error.new("[DocumentInfo] Error getting document [#{@pe_id}].")
91
+ end
92
+
93
+ return rc
94
+ end
95
+
96
+ # Allocates a new Document in OpenNebula
97
+ #
98
+ # @param description [String] The contents of the Document.
99
+ #
100
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
101
+ # otherwise
102
+ def allocate(description)
103
+ super(DOCUMENT_METHODS[:allocate], description, document_type)
104
+ end
105
+
106
+ # Deletes the Document
107
+ #
108
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
109
+ # otherwise
110
+ def delete()
111
+ rc = check_type()
112
+ return rc if OpenNebula.is_error?(rc)
113
+
114
+ return call(DOCUMENT_METHODS[:delete], @pe_id)
115
+ end
116
+
117
+ # Replaces the template contents
118
+ #
119
+ # @param [String] new_template new template contents
120
+ #
121
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
122
+ # otherwise
123
+ def update(new_template)
124
+ rc = check_type()
125
+ return rc if OpenNebula.is_error?(rc)
126
+
127
+ super(DOCUMENT_METHODS[:update], new_template)
128
+ end
129
+
130
+ # Changes the owner/group
131
+ #
132
+ # @param [Integer] uid the new owner id. Set to -1 to leave the current one
133
+ # @param [Integer] gid the new group id. Set to -1 to leave the current one
134
+ #
135
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
136
+ # otherwise
137
+ def chown(uid, gid)
138
+ rc = check_type()
139
+ return rc if OpenNebula.is_error?(rc)
140
+
141
+ super(DOCUMENT_METHODS[:chown], uid, gid)
142
+ end
143
+
144
+ # Changes the Document permissions.
145
+ #
146
+ # @param octet [String] Permissions octed , e.g. 640
147
+ #
148
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
149
+ # otherwise
150
+ def chmod_octet(octet)
151
+ rc = check_type()
152
+ return rc if OpenNebula.is_error?(rc)
153
+
154
+ super(DOCUMENT_METHODS[:chmod], octet)
155
+ end
156
+
157
+ # Changes the Document permissions.
158
+ # Each [Integer] argument must be 1 to allow, 0 deny, -1 do not change
159
+ #
160
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
161
+ # otherwise
162
+ def chmod(owner_u, owner_m, owner_a, group_u, group_m, group_a, other_u,
163
+ other_m, other_a)
164
+ rc = check_type()
165
+ return rc if OpenNebula.is_error?(rc)
166
+
167
+ super(DOCUMENT_METHODS[:chmod], owner_u, owner_m, owner_a, group_u,
168
+ group_m, group_a, other_u, other_m, other_a)
169
+ end
170
+
171
+ # Clones this Document into a new one
172
+ #
173
+ # @param name [String] Name for the new Document.
174
+ #
175
+ # @return [Integer, OpenNebula::Error] The new Document ID in case
176
+ # of success, Error otherwise
177
+ def clone(name)
178
+ rc = check_type()
179
+ return rc if OpenNebula.is_error?(rc)
180
+
181
+ return Error.new('ID not defined') if !@pe_id
182
+
183
+ rc = @client.call(DOCUMENT_METHODS[:clone], @pe_id, name)
184
+
185
+ return rc
186
+ end
187
+
188
+ #######################################################################
189
+ # Helpers to get Document information
190
+ #######################################################################
191
+
192
+ # Returns the group identifier
193
+ # @return [Integer] the element's group ID
194
+ def gid
195
+ self['GID'].to_i
196
+ end
197
+
198
+ # Returns the owner user ID
199
+ # @return [Integer] the element's owner user ID
200
+ def owner_id
201
+ self['UID'].to_i
202
+ end
203
+
204
+ # Returns true if the GROUP_U permission bit is set
205
+ # @return [true, false] true if the GROUP_U permission bit is set
206
+ def public?
207
+ if self['PERMISSIONS/GROUP_U'] == "1" || self['PERMISSIONS/OTHER_U'] == "1"
208
+ true
209
+ else
210
+ false
211
+ end
212
+ end
213
+
214
+ def document_type
215
+ self.class::DOCUMENT_TYPE
216
+ end
217
+
218
+ private
219
+
220
+ def set_publish(published)
221
+ group_u = published ? 1 : 0
222
+
223
+ chmod(-1, -1, -1, group_u, -1, -1, -1, -1, -1)
224
+ end
225
+
226
+ def check_type()
227
+ type = self['TYPE']
228
+
229
+ if type.nil? && @pe_id
230
+ rc = @client.call(DOCUMENT_METHODS[:info], @pe_id)
231
+
232
+ return rc if OpenNebula.is_error?(rc)
233
+
234
+ xmldoc = XMLElement.new
235
+ xmldoc.initialize_xml(rc, 'DOCUMENT')
236
+
237
+ type = xmldoc['TYPE']
238
+ end
239
+
240
+ if !type.nil? && type.to_i != document_type
241
+ return OpenNebula::Error.new(
242
+ "[DocumentInfo] Error getting document [#{@pe_id}].")
243
+ end
244
+
245
+ return nil
246
+ end
247
+ end
248
+ end
@@ -0,0 +1,129 @@
1
+ # -------------------------------------------------------------------------- #
2
+ # Copyright 2002-2012, 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 'json'
18
+
19
+ module OpenNebula
20
+ class DocumentJSON < Document
21
+
22
+ TEMPLATE_TAG = "BODY"
23
+
24
+ # Allocate a new Document containing the json inside the TEMPLATE
25
+ #
26
+ # @param [String] template_json json to be inserted in the TEMPLATE
27
+ # of the new resource
28
+ # @param [String, nil] name name of the object, this value will be
29
+ # processed by the OpenNebula core
30
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
31
+ # otherwise
32
+ #
33
+ def allocate(template_json, name=nil)
34
+ text = build_template_xml(template_json, name)
35
+
36
+ super(text)
37
+ end
38
+
39
+ # Retrieves the information of the Service and all its Nodes.
40
+ #
41
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
42
+ # otherwise
43
+ #
44
+ def info
45
+ rc = super
46
+ if OpenNebula.is_error?(rc)
47
+ return rc
48
+ end
49
+
50
+ load_body
51
+ end
52
+
53
+ # Updates the current state of this Service in the OpenNebula DB
54
+ #
55
+ # @params [String, nil] template_json string to be inserted in the
56
+ # template. If nil @body will be used instead
57
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
58
+ # otherwise
59
+ #
60
+ def update(template_json=nil)
61
+ template_json ||= @body.to_json
62
+
63
+ text = build_template_xml(template_json)
64
+
65
+ super(text)
66
+ end
67
+
68
+ # Generates a json representing the object
69
+ #
70
+ # @param [true, false] pretty_generate
71
+ # @return [String] json representing the object
72
+ #
73
+ def to_json(pretty_generate=true)
74
+ hash = self.to_hash
75
+
76
+ body = hash['DOCUMENT']['TEMPLATE']["#{TEMPLATE_TAG}"]
77
+ if body
78
+ body_hash = JSON.parse(body)
79
+ hash['DOCUMENT']['TEMPLATE']["#{TEMPLATE_TAG}"] = body_hash
80
+ end
81
+
82
+ if pretty_generate
83
+ JSON.pretty_generate hash
84
+ else
85
+ hash.to_json
86
+ end
87
+ end
88
+
89
+
90
+ # Fill the @body hash with the values of the template
91
+ def load_body
92
+ body_str = self["TEMPLATE/#{TEMPLATE_TAG}"]
93
+
94
+ if body_str
95
+ begin
96
+ @body = JSON.parse(body_str)
97
+ rescue JSON::JSONError
98
+ return OpenNebula::Error.new($!)
99
+ end
100
+ end
101
+
102
+ return nil
103
+ end
104
+
105
+ private
106
+
107
+ # Build an xml string incluiding the provided json
108
+ #
109
+ # @param [String] template_json The template to be inserted
110
+ # @param [String, nil] name The string to be inserted as name
111
+ # @return [String] The xml containing the json
112
+ #
113
+ def build_template_xml(template_json, name=nil)
114
+ template_json ||= ""
115
+
116
+ text = "<TEMPLATE>"
117
+
118
+ text << "<NAME>#{name}</NAME>" if name
119
+
120
+ text << "<#{TEMPLATE_TAG}>"
121
+ text << "<![CDATA[#{template_json}]]>"
122
+ text << "</#{TEMPLATE_TAG}>"
123
+
124
+ text << "</TEMPLATE>"
125
+
126
+ text
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,97 @@
1
+ # -------------------------------------------------------------------------- #
2
+ # Copyright 2002-2012, 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
+
21
+ # All subclasses must define the DOCUMENT_TYPE constant
22
+ # and the factory method.
23
+ #
24
+ # @example
25
+ # require 'OpenNebula/DocumentPool'
26
+ #
27
+ # module OpenNebula
28
+ # class CustomObjectPool < DocumentPool
29
+ #
30
+ # DOCUMENT_TYPE = 400
31
+ #
32
+ # def factory(element_xml)
33
+ # OpenNebula::CustomObject.new(element_xml, @client)
34
+ # end
35
+ # end
36
+ # end
37
+ class DocumentPool < Pool
38
+
39
+ #######################################################################
40
+ # Constants and Class attribute accessors
41
+ #######################################################################
42
+
43
+ DOCUMENT_POOL_METHODS = {
44
+ :info => "documentpool.info"
45
+ }
46
+
47
+ #######################################################################
48
+ # Class constructor & Pool Methods
49
+ #######################################################################
50
+
51
+ # Class constructor
52
+ #
53
+ # @param [OpenNebula::Client] client the xml-rpc client
54
+ # @param [Integer] user_id the filter flag, see
55
+ # http://opennebula.org/documentation:rel3.6:api
56
+ #
57
+ # @return [DocumentPool] the new object
58
+ def initialize(client, user_id=-1)
59
+ super('DOCUMENT_POOL','DOCUMENT',client)
60
+
61
+ @user_id = user_id
62
+ end
63
+
64
+ #######################################################################
65
+ # XML-RPC Methods for the Document Object
66
+ #######################################################################
67
+
68
+ # Retrieves all or part of the Documents in the pool.
69
+ #
70
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
71
+ # otherwise
72
+ def info(*args)
73
+ case args.size
74
+ when 0
75
+ info_filter(DOCUMENT_POOL_METHODS[:info],@user_id,-1,-1, document_type)
76
+ when 3
77
+ info_filter(DOCUMENT_POOL_METHODS[:info],args[0],args[1],args[2], document_type)
78
+ end
79
+ end
80
+
81
+ def info_all()
82
+ return super(DOCUMENT_POOL_METHODS[:info], document_type)
83
+ end
84
+
85
+ def info_mine()
86
+ return super(DOCUMENT_POOL_METHODS[:info], document_type)
87
+ end
88
+
89
+ def info_group()
90
+ return super(DOCUMENT_POOL_METHODS[:info], document_type)
91
+ end
92
+
93
+ def document_type
94
+ self.class::DOCUMENT_TYPE
95
+ end
96
+ end
97
+ end