Revision c03fcc70 src/onedb/onedb_live.rb

View differences:

src/onedb/onedb_live.rb
183 183
            delete("history", "vid = #{vm.id}", false)
184 184
        end
185 185
    end
186

  
187
    def check_expr(object, expr)
188
        reg = /^(?<xpath>.+?)(?<operator>=|!=|>=|<=|>|<)(?<value>.*?)$/
189
        parsed = expr.match(reg)
190

  
191
        raise "Expression malformed: '#{expr}'" unless parsed
192

  
193
        val = object[parsed[:xpath]]
194
        return false if !val
195

  
196
        p_val = parsed[:value].strip
197
        val.strip!
198

  
199
        res = false
200

  
201
        res = case parsed[:operator]
202
        when '='
203
            val == p_val
204
        when '!='
205
            val != p_val
206
        when '<'
207
            val.to_i < p_val.to_i
208
        when '>'
209
            val.to_i > p_val.to_i
210
        when '<='
211
            val.to_i <= p_val.to_i
212
        when '>='
213
            val.to_i >= p_val.to_i
214
        end
215

  
216
        res
217
    end
218

  
219
    def change_body(object, xpath, value, options = {})
220
        case (object||'').downcase.strip.to_sym
221
        when :vm
222
            table = 'vm_pool'
223
            object = OpenNebula::VirtualMachinePool.new(client)
224
            federate = false
225

  
226
        when :host
227
            table = 'host_pool'
228
            object = OpenNebula::HostPool.new(client)
229
            federate = false
230

  
231
        when :vnet
232
            table = 'network_pool'
233
            object = OpenNebula::VirtualNetworkPool.new(client)
234
            federate = false
235

  
236
        when :image
237
            table = 'image_pool'
238
            object = OpenNebula::ImagePool.new(client)
239
            federate = false
240

  
241
        when :cluster
242
            table = 'cluster_pool'
243
            object = OpenNebula::ClusterPool.new(client)
244
            federate = false
245

  
246
        when :document
247
            table = 'document_pool'
248
            object = OpenNebula::DocumentPool.new(client)
249
            federate = false
250

  
251
        when :group
252
            table = 'group_pool'
253
            object = OpenNebula::GroupPool.new(client)
254
            federate = true
255

  
256
        when :marketplace
257
            table = 'marketplace_pool'
258
            object = OpenNebula::MarketPlacePool.new(client)
259
            federate = true
260

  
261
        when :marketplaceapp
262
            table = 'marketplaceapp_pool'
263
            object = OpenNebula::MarketPlaceAppPool.new(client)
264
            federate = true
265

  
266
        when :secgroup
267
            table = 'secgroup_pool'
268
            object = OpenNebula::SecurityGroupPool.new(client)
269
            federate = false
270

  
271
        when :template
272
            table = 'template_pool'
273
            object = OpenNebula::TemplatePool.new(client)
274
            federate = false
275

  
276
        when :vrouter
277
            table = 'vrouter_pool'
278
            object = OpenNebula::VirtualRouterPool.new(client)
279
            federate = false
280

  
281
        when :zone
282
            table = 'zone_pool'
283
            object = OpenNebula::ZonePool.new(client)
284
            federate = true
285

  
286
        else
287
            raise "Object type '#{object}' not supported"
288
        end
289

  
290
        if !value && !options[:delete]
291
            raise "A value or --delete should specified"
292
        end
293

  
294
        object.info
295

  
296
        object.each do |o|
297
            if options[:id]
298
                next unless o.id.to_s.strip == options[:id].to_s
299
            elsif options[:xpath]
300
                next unless o[options[:xpath]]
301
            elsif options[:expr]
302
                next unless check_expr(o, options[:expr])
303
            end
304

  
305
            o.info
306
            doc = Nokogiri::XML(o.to_xml, nil, NOKOGIRI_ENCODING) do |c|
307
                c.default_xml.noblanks
308
            end
309

  
310
            doc.xpath(xpath).each do |e|
311
                if options[:delete]
312
                    e.remove
313
                else
314
                    e.content = value
315
                end
316
            end
317

  
318
            xml = doc.root.to_xml
319

  
320
            if options[:dry]
321
                puts xml
322
            else
323
                update_body(table, xml, "oid = #{o.id}", federate)
324
            end
325
        end
326
    end
186 327
end
328

  

Also available in: Unified diff