[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[Ovirt-devel] [PATCH 3/5] API for Hardware Pools



- List and filter HW pools by name
- CRUD of individual HW pool
- Hosts and storgae pools for a HW pool are accessible as nested resources, e.g.
  /ovirt/hardware_pools/1/hosts?hostname=myhost.example.com
---
 .../controllers/rest/hardware_pools_controller.rb  |   85 ++++++++++++++++++++
 wui/src/config/routes.rb                           |    4 +
 2 files changed, 89 insertions(+), 0 deletions(-)
 create mode 100644 wui/src/app/controllers/rest/hardware_pools_controller.rb

diff --git a/wui/src/app/controllers/rest/hardware_pools_controller.rb b/wui/src/app/controllers/rest/hardware_pools_controller.rb
new file mode 100644
index 0000000..49a8fcc
--- /dev/null
+++ b/wui/src/app/controllers/rest/hardware_pools_controller.rb
@@ -0,0 +1,85 @@
+class Rest::HardwarePoolsController < ApplicationController
+    OPTS = {
+        :include => [ :storage_pools, :hosts, :quota ]
+    }
+
+    EQ_ATTRIBUTES = [ :name ]
+
+    def index
+        conditions = []
+        EQ_ATTRIBUTES.each do |attr|
+            if params[attr]
+                conditions << "#{attr} = :#{attr}"
+            end
+        end
+
+        @pools = HardwarePool.find(:all,
+                    :conditions => [conditions.join(" and "), params],
+                    :order => "id")
+
+        respond_to do |format|
+            format.xml { render :xml => @pools.to_xml(OPTS) }
+        end
+    end
+
+    def show
+        @pool = HardwarePool.find(params[:id])
+        respond_to do |format|
+            format.xml { render :xml => @pool.to_xml(OPTS) }
+        end
+    end
+
+    def create
+        # FIXME: Why can't I just use HardwarePool.create here ?
+        # Shouldn't that find the parent_id param ?
+        @parent = Pool.find(params[:hardware_pool][:parent_id])
+        @pool = HardwarePool.new(params[:hardware_pool])
+        # FIXME: How do we check for errors here ?
+        @pool.create_with_parent(@parent)
+        respond_to do |format|
+            format.xml  { render :xml => @pool.to_xml(OPTS),
+                :status => :created,
+                :location => rest_hardware_pool_url(@pool) }
+        end
+    end
+
+    def update
+        # We allow updating direct attributes of the HW pool and moving
+        # hosts/storage into it here.
+        @pool = HardwarePool.find(params[:id])
+        [:hosts, :storage_pools].each do |k|
+            objs = params[:hardware_pool].delete(k)
+            ids = objs.reject{ |obj| obj[:hardware_pool_id] == @pool.id}.
+                collect{ |obj| obj[:id] }
+            if ids.size > 0
+                if k == :hosts
+                    # FIXME: Why does move_hosts need an explicit pool_id ?
+                    @pool.move_hosts(ids, @pool.id)
+                else
+                    @pool.move_storage(ids, @pool.id)
+                end
+            end
+        end
+
+        @pool.update_attributes(params[:hardware_pool])
+        respond_to do |format|
+            if @pool.save
+                format.xml  { render :xml => @pool.to_xml(OPTS),
+                    :status => :created,
+                    :location => rest_hardware_pool_url(@pool) }
+            else
+                format.xml  { render :xml => @pool.errors,
+                    :status => :unprocessable_entity }
+            end
+        end
+    end
+
+    def destroy
+        @pool = HardwarePool.find(params[:id])
+        @pool.destroy
+        respond_to do |format|
+            format.xml  { head :ok }
+        end
+    end
+
+end
diff --git a/wui/src/config/routes.rb b/wui/src/config/routes.rb
index 993a885..322d6f9 100644
--- a/wui/src/config/routes.rb
+++ b/wui/src/config/routes.rb
@@ -22,6 +22,10 @@ ActionController::Routing::Routes.draw do |map|
     map.namespace(:rest) do |rest|
         rest.resources :hosts
         rest.resources :storage_pools
+        rest.resources :hardware_pools do |pools|
+            pools.resources :hosts
+            pools.resources :storage_pools
+        end
     end
 
 
-- 
1.5.5.1


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]