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,259 @@
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
+ module OpenNebula
19
+
20
+ # Abstract rules of the type USER RESOURCE RIGHTS
21
+ # which are:
22
+ # USER -> #<num>
23
+ # @<num>
24
+ # ALL
25
+ # RESOURCE -> + separated list and "/{#,@}<num>|ALL"
26
+ # VM,
27
+ # HOST
28
+ # NET
29
+ # IMAGE
30
+ # USER
31
+ # TEMPLATE
32
+ # GROUP
33
+ # ACL
34
+ # RIGHTS -> + separated list
35
+ # USE
36
+ # MANAGE
37
+ # ADMIN
38
+ # CREATE
39
+ class Acl < PoolElement
40
+
41
+ USERS = {
42
+ "UID" => 0x100000000,
43
+ "GID" => 0x200000000,
44
+ "ALL" => 0x400000000
45
+ }
46
+
47
+ RESOURCES =
48
+ {
49
+ "VM" => 0x1000000000,
50
+ "HOST" => 0x2000000000,
51
+ "NET" => 0x4000000000,
52
+ "IMAGE" => 0x8000000000,
53
+ "USER" => 0x10000000000,
54
+ "TEMPLATE" => 0x20000000000,
55
+ "GROUP" => 0x40000000000,
56
+ "DATASTORE" => 0x100000000000,
57
+ "CLUSTER" => 0x200000000000,
58
+ "DOCUMENT" => 0x400000000000
59
+ }
60
+
61
+ RIGHTS =
62
+ {
63
+ "USE" => 0x1, # Auth. to use an object
64
+ "MANAGE" => 0x2, # Auth. to perform management actions
65
+ "ADMIN" => 0x4, # Auth. to perform administrative actions
66
+ "CREATE" => 0x8 # Auth. to create an object
67
+ }
68
+
69
+ # Constructor
70
+ #
71
+ # @param xml [String] must be an xml built with {#build_xml}
72
+ # @param client [Client] represents an XML-RPC connection
73
+ def initialize(xml, client)
74
+ super(xml,client)
75
+ end
76
+
77
+ # Creates an empty XML representation. It contains the id, if it is
78
+ # specified.
79
+ #
80
+ # @param pe_id [Integer] rule ID
81
+ # @param client [Client] represents an XML-RPC connection
82
+ #
83
+ # @return [String] an empty XML representation
84
+ def self.build_xml(pe_id=nil)
85
+ if pe_id
86
+ acl_xml = "<ACL><ID>#{pe_id}</ID></ACL>"
87
+ else
88
+ acl_xml = "<ACL></ACL>"
89
+ end
90
+
91
+ XMLElement.build_xml(acl_xml,'ACL')
92
+ end
93
+
94
+ # Creates a new ACL rule.
95
+ #
96
+ # @param user [String]
97
+ # A string containing a hex number, e.g. 0x100000001
98
+ # @param resource [String]
99
+ # A string containing a hex number, e.g. 0x2100000001
100
+ # @param rights [String]
101
+ # A string containing a hex number, e.g. 0x10
102
+ #
103
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
104
+ # otherwise
105
+ def allocate(user, resource, rights)
106
+ return super( AclPool::ACL_POOL_METHODS[:addrule],
107
+ user,
108
+ resource,
109
+ rights )
110
+ end
111
+
112
+ # Deletes the Acl rule
113
+ #
114
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
115
+ # otherwise
116
+ def delete()
117
+ super(AclPool::ACL_POOL_METHODS[:delrule])
118
+ end
119
+
120
+ # Does nothing, individual ACL rules info can't be retrieved from
121
+ # OpenNebula
122
+ #
123
+ # @return [nil] nil
124
+ def info()
125
+ return nil
126
+ end
127
+
128
+ # Parses a rule string, e.g. "#5 HOST+VM/@12 INFO+CREATE+DELETE"
129
+ #
130
+ # @param rule_str [String] an ACL rule in string format
131
+ #
132
+ # @return [Array] an Array containing 3 strings (hex 64b numbers),
133
+ # or OpenNebula::Error objects
134
+ def self.parse_rule(rule_str)
135
+ ret = Array.new
136
+
137
+ rule_str = rule_str.split(" ")
138
+
139
+ if rule_str.length != 3
140
+ return OpenNebula::Error.new(
141
+ "String needs three components: User, Resource, Rights")
142
+ end
143
+
144
+ ret << parse_users(rule_str[0])
145
+ ret << parse_resources(rule_str[1])
146
+ ret << parse_rights(rule_str[2])
147
+
148
+ errors=ret.map do |arg|
149
+ if OpenNebula.is_error?(arg)
150
+ arg.message
151
+ else
152
+ nil
153
+ end
154
+ end
155
+
156
+ errors.compact!
157
+
158
+ if errors.length>0
159
+ return OpenNebula::Error.new(errors.join(', '))
160
+ end
161
+
162
+ return ret
163
+ end
164
+
165
+ private
166
+
167
+ # Converts a string in the form [#<id>, @<id>, *] to a hex. number
168
+ #
169
+ # @param users [String] Users component string
170
+ #
171
+ # @return [String] A string containing a hex number
172
+ def self.parse_users(users)
173
+ begin
174
+ return calculate_ids(users).to_i.to_s(16)
175
+ rescue Exception => e
176
+ return OpenNebula::Error.new(e.message)
177
+ end
178
+ end
179
+
180
+ # Converts a resources string to a hex. number
181
+ #
182
+ # @param resources [String] Resources component string
183
+ #
184
+ # @return [String] A string containing a hex number
185
+ def self.parse_resources(resources)
186
+ begin
187
+ ret = 0
188
+ resources = resources.split("/")
189
+
190
+ if resources.size != 2
191
+ raise "Resource '#{resources}' malformed"
192
+ end
193
+
194
+ resources[0].split("+").each{ |resource|
195
+ if !RESOURCES[resource.upcase]
196
+ raise "Resource '#{resource}' does not exist"
197
+ end
198
+ ret += RESOURCES[resource.upcase]
199
+ }
200
+
201
+ ret += calculate_ids(resources[1])
202
+
203
+ return ret.to_i.to_s(16)
204
+ rescue Exception => e
205
+ return OpenNebula::Error.new(e.message)
206
+ end
207
+ end
208
+
209
+ # Converts a rights string to a hex. number
210
+ #
211
+ # @param rights [String] Rights component string
212
+ #
213
+ # @return [String] A string containing a hex number
214
+ def self.parse_rights(rights)
215
+ begin
216
+ ret = 0
217
+ rights = rights.split("+")
218
+
219
+ rights.each{ |right|
220
+ raise "Right '#{right}' does not exist" if !RIGHTS[right.upcase]
221
+
222
+ ret += RIGHTS[right.upcase]
223
+ }
224
+
225
+ return ret.to_i.to_s(16)
226
+ rescue Exception => e
227
+ return OpenNebula::Error.new(e.message)
228
+ end
229
+ end
230
+
231
+ # Calculates the numeric value for a String containing an individual
232
+ # (#<id>), group (@<id>) or all (*) ID component
233
+ #
234
+ # @param id_str [String] Rule Id string
235
+ #
236
+ # @return [Integer] the numeric value for the given id_str
237
+ def self.calculate_ids(id_str)
238
+ raise "ID string '#{id_str}' malformed" if
239
+ !id_str.match(/^([\#@]\d+|\*)$/)
240
+
241
+ value = 0
242
+
243
+ case id_str[0..0]
244
+ when "#"
245
+ value = USERS["UID"]
246
+ users_value = id_str[1..-1].to_i + value
247
+
248
+ when "@"
249
+ value = USERS["GID"]
250
+ users_value = id_str[1..-1].to_i + value
251
+
252
+ when "*"
253
+ users_value = USERS["ALL"]
254
+ end
255
+
256
+ return users_value
257
+ end
258
+ end
259
+ 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 AclPool < Pool
22
+
23
+ #######################################################################
24
+ # Constants and Class Methods
25
+ #######################################################################
26
+
27
+
28
+ ACL_POOL_METHODS = {
29
+ :info => "acl.info",
30
+ :addrule => "acl.addrule",
31
+ :delrule => "acl.delrule"
32
+ }
33
+
34
+ # Class constructor
35
+ def initialize(client)
36
+ super('ACL_POOL','ACL',client)
37
+ end
38
+
39
+ def factory(element_xml)
40
+ OpenNebula::Acl.new(element_xml, @client)
41
+ end
42
+
43
+ #######################################################################
44
+ # XML-RPC Methods
45
+ #######################################################################
46
+
47
+ # Retrieves the ACL Pool
48
+ def info()
49
+ # Retrieves all the Acls in the pool.
50
+ super(ACL_POOL_METHODS[:info])
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,247 @@
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 Cluster < PoolElement
22
+ #######################################################################
23
+ # Constants and Class Methods
24
+ #######################################################################
25
+
26
+ CLUSTER_METHODS = {
27
+ :info => "cluster.info",
28
+ :allocate => "cluster.allocate",
29
+ :delete => "cluster.delete",
30
+ :addhost => "cluster.addhost",
31
+ :delhost => "cluster.delhost",
32
+ :adddatastore => "cluster.adddatastore",
33
+ :deldatastore => "cluster.deldatastore",
34
+ :addvnet => "cluster.addvnet",
35
+ :delvnet => "cluster.delvnet",
36
+ :update => "cluster.update",
37
+ }
38
+
39
+ # Creates a Cluster description with just its identifier
40
+ # this method should be used to create plain Cluster objects.
41
+ # +id+ the id of the host
42
+ #
43
+ # Example:
44
+ # cluster = Cluster.new(Cluster.build_xml(3),rpc_client)
45
+ #
46
+ def Cluster.build_xml(pe_id=nil)
47
+ if pe_id
48
+ cluster_xml = "<CLUSTER><ID>#{pe_id}</ID></CLUSTER>"
49
+ else
50
+ cluster_xml = "<CLUSTER></CLUSTER>"
51
+ end
52
+
53
+ XMLElement.build_xml(cluster_xml,'CLUSTER')
54
+ end
55
+
56
+ # Class constructor
57
+ def initialize(xml, client)
58
+ super(xml,client)
59
+ end
60
+
61
+ #######################################################################
62
+ # XML-RPC Methods for the Cluster Object
63
+ #######################################################################
64
+
65
+ # Retrieves the information of the given Cluster.
66
+ def info()
67
+ super(CLUSTER_METHODS[:info], 'CLUSTER')
68
+ end
69
+
70
+ # Allocates a new Cluster in OpenNebula
71
+ #
72
+ # +clustername+ A string containing the name of the Cluster.
73
+ def allocate(clustername)
74
+ super(CLUSTER_METHODS[:allocate], clustername)
75
+ end
76
+
77
+ # Deletes the Cluster
78
+ def delete()
79
+ super(CLUSTER_METHODS[:delete])
80
+ end
81
+
82
+ # Adds a Host to this Cluster
83
+ # @param hid [Integer] Host ID
84
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
85
+ # otherwise
86
+ def addhost(hid)
87
+ return Error.new('ID not defined') if !@pe_id
88
+
89
+ rc = @client.call(CLUSTER_METHODS[:addhost], @pe_id, hid)
90
+ rc = nil if !OpenNebula.is_error?(rc)
91
+
92
+ return rc
93
+ end
94
+
95
+ # Deletes a Host from this Cluster
96
+ # @param hid [Integer] Host ID
97
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
98
+ # otherwise
99
+ def delhost(hid)
100
+ return Error.new('ID not defined') if !@pe_id
101
+
102
+ rc = @client.call(CLUSTER_METHODS[:delhost], @pe_id, hid)
103
+ rc = nil if !OpenNebula.is_error?(rc)
104
+
105
+ return rc
106
+ end
107
+
108
+ # Adds a Datastore to this Cluster
109
+ # @param ds_id [Integer] Datastore ID
110
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
111
+ # otherwise
112
+ def adddatastore(ds_id)
113
+ return Error.new('ID not defined') if !@pe_id
114
+
115
+ rc = @client.call(CLUSTER_METHODS[:adddatastore], @pe_id, ds_id)
116
+ rc = nil if !OpenNebula.is_error?(rc)
117
+
118
+ return rc
119
+ end
120
+
121
+ # Deletes a Datastore from this Cluster
122
+ # @param ds_id [Integer] Datastore ID
123
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
124
+ # otherwise
125
+ def deldatastore(ds_id)
126
+ return Error.new('ID not defined') if !@pe_id
127
+
128
+ rc = @client.call(CLUSTER_METHODS[:deldatastore], @pe_id, ds_id)
129
+ rc = nil if !OpenNebula.is_error?(rc)
130
+
131
+ return rc
132
+ end
133
+
134
+ # Adds a VNet to this Cluster
135
+ # @param vnet_id [Integer] VNet ID
136
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
137
+ # otherwise
138
+ def addvnet(vnet_id)
139
+ return Error.new('ID not defined') if !@pe_id
140
+
141
+ rc = @client.call(CLUSTER_METHODS[:addvnet], @pe_id, vnet_id)
142
+ rc = nil if !OpenNebula.is_error?(rc)
143
+
144
+ return rc
145
+ end
146
+
147
+ # Deletes a VNet from this Cluster
148
+ # @param vnet_id [Integer] VNet ID
149
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
150
+ # otherwise
151
+ def delvnet(vnet_id)
152
+ return Error.new('ID not defined') if !@pe_id
153
+
154
+ rc = @client.call(CLUSTER_METHODS[:delvnet], @pe_id, vnet_id)
155
+ rc = nil if !OpenNebula.is_error?(rc)
156
+
157
+ return rc
158
+ end
159
+
160
+ # Replaces the template contents
161
+ #
162
+ # @param new_template [String] New template contents
163
+ #
164
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
165
+ # otherwise
166
+ def update(new_template)
167
+ super(CLUSTER_METHODS[:update], new_template)
168
+ end
169
+
170
+ # ---------------------------------------------------------------------
171
+ # Helpers to get information
172
+ # ---------------------------------------------------------------------
173
+
174
+ # Returns whether or not the host with 'id' is part of this cluster
175
+ # @param id [Integer|Array] host ID
176
+ # @return [Boolean] true if found
177
+ def contains_host?(id)
178
+ contains_resource?('HOSTS/ID', id)
179
+ end
180
+
181
+ # Returns an array with the numeric host ids
182
+ # @return [Array<Integer>]
183
+ def host_ids
184
+ array = Array.new
185
+
186
+ self.each("HOSTS/ID") do |id|
187
+ array << id.text.to_i
188
+ end
189
+
190
+ return array
191
+ end
192
+
193
+ # Returns whether or not the datastore with 'id' is part of this cluster
194
+ # @param id [Integer|Array] datastore ID
195
+ # @return [Boolean] true if found
196
+ def contains_datastore?(id)
197
+ contains_resource?('DATASTORES/ID', id)
198
+ end
199
+
200
+ # Returns an array with the numeric datastore ids
201
+ # @return [Array<Integer>]
202
+ def datastore_ids
203
+ array = Array.new
204
+
205
+ self.each("DATASTORES/ID") do |id|
206
+ array << id.text.to_i
207
+ end
208
+
209
+ return array
210
+ end
211
+
212
+ # Returns whether or not the vnet with 'id' is part of this cluster
213
+ # @param id [Integer|Arrray] vnet ID
214
+ # @return [Boolean] true if found
215
+ def contains_vnet?(id)
216
+ contains_resource?('VNETS/ID', id)
217
+ end
218
+
219
+ # Returns an array with the numeric vnet ids
220
+ # @return [Array<Integer>]
221
+ def vnet_ids
222
+ array = Array.new
223
+
224
+ self.each("VNETS/ID") do |id|
225
+ array << id.text.to_i
226
+ end
227
+
228
+ return array
229
+ end
230
+
231
+ private
232
+
233
+ def contains_resource?(xpath, id)
234
+ id_array = retrieve_elements(xpath)
235
+
236
+ return false if id_array.nil?
237
+
238
+ id = [id] if id.class != Array
239
+
240
+ id.each { |i|
241
+ return false if !id_array.include?(i.to_s)
242
+ }
243
+
244
+ return true
245
+ end
246
+ end
247
+ end
@@ -0,0 +1,56 @@
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 ClusterPool < Pool
22
+ #######################################################################
23
+ # Constants and Class attribute accessors
24
+ #######################################################################
25
+
26
+ NONE_CLUSTER_ID = -1
27
+ DEFAULT_CLUSTER_ID = 0
28
+
29
+ CLUSTER_POOL_METHODS = {
30
+ :info => "clusterpool.info"
31
+ }
32
+
33
+ #######################################################################
34
+ # Class constructor & Pool Methods
35
+ #######################################################################
36
+
37
+ # +client+ a Client object that represents a XML-RPC connection
38
+ def initialize(client)
39
+ super('CLUSTER_POOL','CLUSTER',client)
40
+ end
41
+
42
+ # Factory method to create Cluster objects
43
+ def factory(element_xml)
44
+ OpenNebula::Cluster.new(element_xml,@client)
45
+ end
46
+
47
+ #######################################################################
48
+ # XML-RPC Methods for the Cluster Object
49
+ #######################################################################
50
+
51
+ # Retrieves all the Clusters in the pool.
52
+ def info()
53
+ super(CLUSTER_POOL_METHODS[:info])
54
+ end
55
+ end
56
+ end