deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@redhat.com
Subject [PATCH core 2/2] CIMI: Added MachineAdmin model operations
Date Wed, 30 Nov 2011 15:20:40 GMT
From: Michal Fojtik <mfojtik@redhat.com>


Signed-off-by: Michal fojtik <mfojtik@redhat.com>
---
 server/lib/cimi/model/machine_admin.rb |   27 +++++++++++++++++
 server/lib/cimi/server.rb              |   49 ++++++++++++++++++++++++++++++-
 2 files changed, 74 insertions(+), 2 deletions(-)

diff --git a/server/lib/cimi/model/machine_admin.rb b/server/lib/cimi/model/machine_admin.rb
index 4f73223..fa27ca4 100644
--- a/server/lib/cimi/model/machine_admin.rb
+++ b/server/lib/cimi/model/machine_admin.rb
@@ -25,4 +25,31 @@ class CIMI::Model::MachineAdmin < CIMI::Model::Base
     scalar :rel, :href
   end
 
+  def self.find(id, context)
+    if id == :all
+      keys = context.driver.keys(context.credentials)
+      keys.map { |key| from_key(key, context) }
+    else
+      key = context.driver.key(context.credentials, :id => id)
+      from_key(key, context)
+    end
+  end
+
+  def self.create_from_xml(body, context)
+    machine_admin = MachineAdmin.from_xml(body)
+    key = context.driver.create_key(context.credentials, :key_name => machine_admin.name)
+    from_key(key, context)
+  end
+
+  private
+
+  def self.from_key(key, context)
+    self.new(
+      :name => key.id,
+      :username => key.username,
+      :password => key.fingerprint,
+      :key => key.pem_rsa_key
+    )
+  end
+
 end
diff --git a/server/lib/cimi/server.rb b/server/lib/cimi/server.rb
index 1e85d0f..a7fd778 100644
--- a/server/lib/cimi/server.rb
+++ b/server/lib/cimi/server.rb
@@ -136,7 +136,6 @@ global_collection :machine_images do
 
   operation :show do
     description "Show specific machine image."
-    with_capability :image
     param :id,          :string,    :required
     control do
       machine_image = MachineImage.find(params[:id], self)
@@ -149,6 +148,53 @@ global_collection :machine_images do
 
 end
 
+global_collection :machine_admins do
+  description 'Machine Admin entity'
+
+  operation :index do
+    description "List all machine admins"
+    with_capability :keys
+    control do
+      machine_admins = MachineAdmin.all(self)
+      respond_to do |format|
+        format.xml { machine_admins.to_xml_cimi_collection(self) }
+        format.json { machine_admins.to_json_cimi_collection(self) }
+      end
+    end
+  end
+
+  operation :show do
+    description "Show specific machine admin"
+    param :id,          :string,    :required
+    with_capability :key
+    control do
+      machine_admin = MachineAdmin.find(params[:id], self)
+      respond_to do |format|
+        format.xml { machine_admin.to_xml }
+        format.json { machine_admin.to_json }
+      end
+    end
+  end
+
+  operation :create do
+    description "Show specific machine admin"
+    with_capability :create_key
+    control do
+      if request.content_type.end_with?("+json")
+        new_admin = MachineAdmin.create_from_json(request.body.read, self)
+      else
+        new_admin = MachineAdmin.create_from_xml(request.body.read, self)
+      end
+      status 201 # Created
+      respond_to do |format|
+        format.json { new_admin.to_json }
+        format.xml { new_admin.to_xml }
+      end
+    end
+  end
+
+end
+
 global_collection :machines do
   description 'List all machine'
 
@@ -165,7 +211,6 @@ global_collection :machines do
 
   operation :show do
     description "Show specific machine."
-    with_capability :instance
     param :id,          :string,    :required
     control do
       machine = Machine.find(params[:id], self)
-- 
1.7.4.4


Mime
View raw message