From: Michal Fojtik <[email protected]>

Since VolumeConfiguration is now generated using 1..1000,
this patch will give user ability to create VolumeConfiguration
and store it into database.

Signed-off-by: Michal fojtik <[email protected]>
---
 .../lib/cimi/collections/volume_configurations.rb  | 25 ++++++
 server/lib/cimi/models/volume_configuration.rb     | 88 ++++++++++++++++------
 server/lib/db.rb                                   |  1 +
 server/lib/db/provider.rb                          |  5 ++
 server/lib/db/volume_configuration.rb              | 12 +++
 server/support/cimi/volume_config.json             |  7 ++
 server/support/cimi/volume_config.xml              |  6 ++
 7 files changed, 122 insertions(+), 22 deletions(-)
 create mode 100644 server/lib/db/volume_configuration.rb
 create mode 100644 server/support/cimi/volume_config.json
 create mode 100644 server/support/cimi/volume_config.xml

diff --git a/server/lib/cimi/collections/volume_configurations.rb 
b/server/lib/cimi/collections/volume_configurations.rb
index 91ad6b0..5895209 100644
--- a/server/lib/cimi/collections/volume_configurations.rb
+++ b/server/lib/cimi/collections/volume_configurations.rb
@@ -41,6 +41,31 @@ module CIMI::Collections
           end
         end
       end
+
+      operation :create, :with_capability => :create_storage_volume do
+        description "Create new VolumeConfiguration"
+        control do
+          if grab_content_type(request.content_type, request.body) == :json
+            new_config = 
CIMI::Model::VolumeConfiguration.create_from_json(request.body.read, self)
+          else
+            new_config = 
CIMI::Model::VolumeConfiguration.create_from_xml(request.body.read, self)
+          end
+          headers_for_create new_config
+          respond_to do |format|
+            format.json { new_config.to_json }
+            format.xml { new_config.to_xml }
+          end
+        end
+      end
+
+      operation :destroy, :with_capability => :destroy_storage_volume do
+        description "Delete a specified VolumeConfiguration"
+        control do
+          CIMI::Model::VolumeConfiguration.delete!(params[:id], self)
+          no_content_with_status(200)
+        end
+      end
+
     end
 
   end
diff --git a/server/lib/cimi/models/volume_configuration.rb 
b/server/lib/cimi/models/volume_configuration.rb
index 383aac3..4a8373e 100644
--- a/server/lib/cimi/models/volume_configuration.rb
+++ b/server/lib/cimi/models/volume_configuration.rb
@@ -25,36 +25,80 @@ class CIMI::Model::VolumeConfiguration < CIMI::Model::Base
     scalar :rel, :href
   end
 
+  def self.create_from_json(body, context)
+    json = JSON.parse(body)
+    new_config = current_db.volume_configurations.new(
+      :name => json['name'],
+      :description => json['description'],
+      :format => json['format'],
+      :capacity => json['capacity'],
+      :ent_properties => json['properties'].to_json,
+      :be_kind => 'volume_configuration',
+      :be_id => ''
+    )
+    new_config.save!
+    from_db(new_config, context)
+  end
+
+  def self.create_from_xml(body, context)
+    xml = XmlSimple.xml_in(body)
+    xml['property'] ||= []
+    new_config = current_db.volume_configurations.new(
+      :name => xml['name'].first,
+      :description => xml['description'].first,
+      :format => xml['format'].first,
+      :capacity => xml['capacity'].first,
+      :ent_properties => xml['property'].inject({}) { |r, p| 
r[p['key']]=p['content']; r },
+      :be_kind => 'volume_configuration',
+      :be_id => ''
+    )
+    new_config.save!
+    from_db(new_config, context)
+  end
+
+  def self.delete!(id, context)
+    current_db.volume_configurations.first(:id => id).destroy
+  end
+
   def self.find(id, context)
-    volume_configs = []
-    if id == :all
-      #ec2 ebs volumes can 1gb..1tb
-      (1..1000).each do |size|
-        volume_configs << create(size, context)
+    if id==:all
+      if context.driver.respond_to? :volume_configurations
+        context.driver.volume_configurations(context.credentials, 
{:env=>context})
+      else
+        Deltacloud::Database::VolumeConfiguration.all(
+          'provider.driver' => driver_symbol.to_s,
+          'provider.url' => current_provider
+        ).map { |t| from_db(t, context) }
       end
     else
-      volume_configs << create(id, context)
-      return volume_configs.first
+      if context.driver.respond_to? :volume_configuration
+        context.driver.volume_configuration(context.credentials, id, 
:env=>context)
+      else
+        config = Deltacloud::Database::VolumeConfiguration.first(
+          'provider.driver' => driver_symbol.to_s,
+          'provider.url' => current_provider,
+          :id => id
+        )
+        raise CIMI::Model::NotFound unless config
+        from_db(config, context)
+      end
     end
-    return volume_configs
   end
 
-
-  def self.all(context); find(:all, context); end
-
   private
 
-  def self.create(size, context)
-    size_kib = context.to_kibibyte(size, "GB")
-    self.new( {
-                :id => context.volume_configuration_url(size),
-                :name => "volume-#{size}",
-                :description => "Volume configuration with #{size_kib} 
kibibytes",
-                :created => Time.now.xmlschema,
-                :capacity => size_kib,
-                :supports_snapshots => "true"
-                # FIXME :guest_interface => "NFS"
-            } )
+  def self.from_db(model, context)
+    self.new(
+      :id => context.volume_configuration_url(model.id),
+      :name => model.name,
+      :description => model.description,
+      :format => model.format,
+      :capacity => context.to_kibibyte(model.capacity, "GB"),
+      :property => model.ent_properties,
+      :operations => [
+        { :href => context.destroy_volume_configuration_url(model.id), :rel => 
'http://schemas.dmtf.org/cimi/1/action/delete' }
+      ]
+    )
   end
 
 end
diff --git a/server/lib/db.rb b/server/lib/db.rb
index 9564b79..f359664 100644
--- a/server/lib/db.rb
+++ b/server/lib/db.rb
@@ -10,6 +10,7 @@ module Deltacloud
     require_relative './db/entity'
     require_relative './db/machine_template'
     require_relative './db/address_template'
+    require_relative './db/volume_configuration'
   end
 
   DATABASE_LOCATION = ENV['DATABASE_LOCATION'] || File.join('/', 'var', 'tmp', 
"deltacloud-mock-#{ENV['USER']}", 'db.sqlite')
diff --git a/server/lib/db/provider.rb b/server/lib/db/provider.rb
index 789bf3e..310d007 100644
--- a/server/lib/db/provider.rb
+++ b/server/lib/db/provider.rb
@@ -11,6 +11,7 @@ module Deltacloud
       has n, :entities
       has n, :machine_templates
       has n, :address_templates
+      has n, :volume_configurations
 
       # This is a workaround for strange bug in Fedora MRI:
       #
@@ -22,6 +23,10 @@ module Deltacloud
         AddressTemplate.all(:provider_id => self.id)
       end
 
+      def volume_configurations
+        VolumeConfiguration.all(:provider_id => self.id)
+      end
+
       def entities
         Entity.all(:provider_id => self.id)
       end
diff --git a/server/lib/db/volume_configuration.rb 
b/server/lib/db/volume_configuration.rb
new file mode 100644
index 0000000..58aa624
--- /dev/null
+++ b/server/lib/db/volume_configuration.rb
@@ -0,0 +1,12 @@
+module Deltacloud
+  module Database
+
+    class VolumeConfiguration < Entity
+      belongs_to :provider
+
+      property :format, String
+      property :capacity, String
+    end
+
+  end
+end
diff --git a/server/support/cimi/volume_config.json 
b/server/support/cimi/volume_config.json
new file mode 100644
index 0000000..d0fb0ab
--- /dev/null
+++ b/server/support/cimi/volume_config.json
@@ -0,0 +1,7 @@
+{
+  "resourceURI": "http://schemas.dmtf.org/cimi/1/VolumeConfigurationCreate";,
+  "name": "myVolumeConfig1",
+  "description": "1 GB volume configuration",
+  "format": "qcow2",
+  "capacity": "1"
+}
diff --git a/server/support/cimi/volume_config.xml 
b/server/support/cimi/volume_config.xml
new file mode 100644
index 0000000..1f6942d
--- /dev/null
+++ b/server/support/cimi/volume_config.xml
@@ -0,0 +1,6 @@
+<VolumeConfigurationCreate>
+  <name>SampleVolumeConfiguration1</name>
+  <description>This is sample 1GB volume configuration</description>
+  <format>qcow2</format>
+  <capacity>1</capacity>
+</VolumeConfigurationCreate>
-- 
1.8.0.2

Reply via email to