[Ovirt-devel] [PATCH 6/6] Sample client code that shows how to use the API

David Lutterkort dlutter at redhat.com
Thu Aug 14 23:43:52 UTC 2008


Signed-off-by: David Lutterkort <dlutter at redhat.com>
---
 wui/client/README             |   14 ++++++
 wui/client/examples/script.rb |   99 +++++++++++++++++++++++++++++++++++++++++
 wui/client/lib/ovirt.rb       |   63 ++++++++++++++++++++++++++
 3 files changed, 176 insertions(+), 0 deletions(-)
 create mode 100644 wui/client/README
 create mode 100755 wui/client/examples/script.rb
 create mode 100644 wui/client/lib/ovirt.rb

diff --git a/wui/client/README b/wui/client/README
new file mode 100644
index 0000000..4bdce27
--- /dev/null
+++ b/wui/client/README
@@ -0,0 +1,14 @@
+This is a very simple client library for accessing the OVirt API from Ruby.
+
+The file examples/script.rb contains a script that shows how this is done
+in some detail.
+
+You must have ActiveResource installed, e.g. with 'yum install
+rubygem-activeresource'
+
+The server is specified with a URL of the form
+  http://USER:PASSWORD@HOST/ovirt
+
+This requires that the server is configured to allow HTTP authentication,
+since there are no mechanisms in the API to forward krb5 tickets. You can
+also try and connect to Mongrel running on HOST:3000 directly.
diff --git a/wui/client/examples/script.rb b/wui/client/examples/script.rb
new file mode 100755
index 0000000..2103ad7
--- /dev/null
+++ b/wui/client/examples/script.rb
@@ -0,0 +1,99 @@
+#! /usr/bin/ruby
+
+# Sample script that shows how to use the OVirt API
+
+require 'pp'
+require 'rubygems'
+require 'activeresource'
+require 'optparse'
+
+require 'ovirt'
+
+def move_random_host(hosts, pool)
+    host = hosts[rand(hosts.size)]
+    puts "Move #{host.hostname} to #{pool.name}"
+    pool.hosts << host
+    pool.save
+end
+
+def element_path(obj)
+    "[#{obj.class.element_path(obj.id)}]"
+end
+
+def print_pool(pool)
+    puts "\n\nPool #{pool.name}: #{pool.hosts.size} hosts, #{pool.storage_pools.size} storage pools #{element_path(pool)} "
+    puts "=" * 75
+    pool.hosts.each do |h|
+        printf "%-36s %s\n", h.hostname, element_path(h)
+    end
+    pool.storage_pools.each do |sp|
+        type = sp.nfs? ? "NFS" : "iSCSI"
+        printf "%-5s %-30s %s\n", type, sp.label, element_path(sp)
+    end
+    puts "-" * 75
+end
+
+# Plumbing so we can find the OVirt server
+# "http://ovirt.watzmann.net:3000/ovirt/rest"
+PROGNAME=File::basename($0)
+OVirt::Base.site = ENV["OVIRT_SERVER"]
+opts = OptionParser.new("#{PROGNAME} GLOBAL_OPTS")
+opts.separator("  Run some things against an OVirt server. The server is specified with")
+opts.separator("  the -s option as a URL of the form http://USER:PASSWORD@SERVER/ovirt")
+opts.separator("")
+opts.separator "Global options:"
+opts.on("-s", "--server=URL", "The OVirt server. Since there is no auth\n" +
+        "#{" "*37}yet, must be the mongrel server port.\n" +
+        "#{" "*37}Overrides env var OVIRT_SERVER") do |val|
+    OVirt::Base.site = val
+end
+
+opts.order(ARGV)
+
+unless OVirt::Base.site
+    $stderr.puts <<EOF
+You must specify the OVirt server to connect to, either with the
+--server option or through the OVIRT_SERVER environment variable
+EOF
+    exit 1
+end
+
+# Get a single host by name
+host = OVirt::Host.find_by_hostname("node3.priv.ovirt.org")
+puts "#{host.uuid} has id #{host.id}"
+
+# What's in the default pool
+defpool = OVirt::HardwarePool.default_pool
+print_pool(defpool)
+
+# Create a new hardware pool
+mypool = OVirt::HardwarePool.find_by_path("/default/mypool")
+unless mypool
+    puts "Create mypool"
+    mypool = OVirt::HardwarePool.create( { :parent_id => defpool.id,
+                                             :name => "mypool" } )
+end
+
+# Move some hosts around
+puts
+if defpool.hosts.size > 1
+    move_random_host(defpool.hosts, mypool)
+elsif mypool.hosts.size > 0
+    move_random_host(mypool.hosts, defpool)
+end
+
+# Delete all storage pools for mypool and add a new one
+mypool.storage_pools.each do |sp|
+    puts "Delete storage pool #{sp.id}"
+    sp.destroy
+end
+
+storage_pool = OVirt::StoragePool.create( { :storage_type => "NFS",
+                                            :hardware_pool_id => mypool.id,
+                                            :ip_addr => "192.168.122.50",
+                                            :export_path => "/exports/pool1" } )
+puts "Created storage pool #{storage_pool.id}"
+
+# For some reason, mypool.reload doesn't work here
+mypool = OVirt::HardwarePool.find_by_path("/default/mypool")
+print_pool(mypool)
diff --git a/wui/client/lib/ovirt.rb b/wui/client/lib/ovirt.rb
new file mode 100644
index 0000000..48739f4
--- /dev/null
+++ b/wui/client/lib/ovirt.rb
@@ -0,0 +1,63 @@
+require 'pp'
+require 'rubygems'
+require 'activeresource'
+
+module OVirt
+    class Base < ActiveResource::Base ; end
+
+    class HardwarePool < Base
+        def self.find_by_path(path)
+            find(:first, :params => { :path => path })
+        end
+
+        def self.default_pool
+            find(:first, :params => { :path => "/default" })
+        end
+    end
+
+    class StoragePool < Base
+        def iscsi?
+            attributes["type"] == "IscsiStoragePool"
+        end
+
+        def nfs?
+            attributes["type"] == "NfsStoragePool"
+        end
+
+        def label
+            if iscsi?
+                "#{ip_addr}:#{port}:#{target}"
+            elsif nfs?
+                "#{ip_addr}:#{export_path}"
+            else
+                raise "Unknown type #{attributes["type"]}"
+            end
+        end
+    end
+
+    class IscsiStoragePool < StoragePool
+        def initialize(attributes = {})
+            super(attributes.update( "type" => "IscsiStoragePool" ))
+        end
+    end
+
+    class NfsStoragePool < StoragePool
+        def initialize(attributes = {})
+            super(attributes.update( "type" => "NfsStoragePool" ))
+        end
+    end
+
+    class Host < Base
+        def self.find_by_uuid(uuid)
+            find(:first, :params => { :uuid => uuid })
+        end
+
+        def self.find_by_hostname(hostname)
+            find(:first, :params => { :hostname => hostname })
+        end
+
+        def hardware_pool
+            HardwarePool.find(hardware_pool_id)
+        end
+    end
+end
-- 
1.5.5.1




More information about the ovirt-devel mailing list