incubator-deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mandr...@redhat.com
Subject [PATCH] Adds create blob and delete blob operations for blobstore API:
Date Thu, 25 Nov 2010 12:32:35 GMT
From: marios <marios@redhat.com>

 * Functionality is added only for Amazon driver (ie S3) for now - Rackspace, Azure to follow.
 * Adds appropriate haml for creating blob over html form
 * Uses the sinatra :method_override hack for Delete operation over HTML form (using post).
---
 server/lib/deltacloud/base_driver/base_driver.rb |    9 ++++
 server/lib/deltacloud/drivers/ec2/ec2_driver.rb  |   22 +++++++++++
 server/server.rb                                 |   44 ++++++++++++++++++---
 server/views/blobs/new.html.haml                 |   10 +++++
 server/views/blobs/show.html.haml                |   36 ++++++++++-------
 server/views/buckets/show.html.haml              |    5 ++-
 6 files changed, 103 insertions(+), 23 deletions(-)
 create mode 100644 server/views/blobs/new.html.haml

diff --git a/server/lib/deltacloud/base_driver/base_driver.rb b/server/lib/deltacloud/base_driver/base_driver.rb
index 61ebc21..00a8a8e 100644
--- a/server/lib/deltacloud/base_driver/base_driver.rb
+++ b/server/lib/deltacloud/base_driver/base_driver.rb
@@ -214,6 +214,15 @@ module Deltacloud
 
     def blob_data(credentials, bucket_id, blob_id, opts)
     end
+  
+    def create_blob(credentials, bucket_id, blob_id, blob_data, opts=nil)
+      
+    end
+    
+    def delete_blob(credentials, bucket_id, blob_id, opts=nil)
+      
+    end
+ 
 
     def filter_on(collection, attribute, opts)
       return collection if opts.nil?
diff --git a/server/lib/deltacloud/drivers/ec2/ec2_driver.rb b/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
index 9b37069..26faf73 100644
--- a/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
+++ b/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
@@ -402,6 +402,28 @@ class EC2Driver < Deltacloud::BaseDriver
     end
   end
 
+  def create_blob(credentials, bucket_id, blob_id, data = nil, opts = nil)
+    s3_client = s3_client(credentials)
+    #data is a construct with the temporary file created by server @.tempfile
+    #also file[:type] will give us the content-type
+    res = s3_client.interface.put(bucket_id, blob_id, data.tempfile, {"Content-Type" =>
data[:type]})
+    Blob.new( { :id => blob_id,
+                :bucket => bucket_id,
+                :content_length => data.tempfile.length,
+                :content_type => data[:type],
+                :last_modified => ''
+              }
+            )
+    #create a new Blob object and return that
+    #why isn't the content-type set correctly?
+  end
+  
+  def delete_blob(credentials, bucket_id, blob_id, opts=nil)
+    s3_client = s3_client(credentials)
+    s3_client.interface.delete(bucket_id, blob_id)
+  end
+
+  
   private
 
   def new_client(credentials)
diff --git a/server/server.rb b/server/server.rb
index af8bc09..2affde1 100644
--- a/server/server.rb
+++ b/server/server.rb
@@ -366,6 +366,35 @@ collection :keys do
 
 end
 
+#get html form for creating a new blob
+get '/api/buckets/:bucket/new_blob' do
+  @bucket_id = params[:bucket]
+  respond_to do |format|
+    format.html {haml :"blobs/new"}
+  end
+end
+
+#create a new blob
+post '/api/buckets/:bucket' do
+  bucket_id = params[:bucket]
+  blob_id = params['blob_id']
+  blob_data = params['blob_data']
+  @blob = driver.create_blob(credentials, bucket_id, blob_id, blob_data )
+  respond_to do |format|
+    format.html { haml :"blobs/show"}
+    format.xml { haml :"blobs/show" }
+  end
+end
+
+#delete a blob
+delete '/api/buckets/:bucket/:blob' do
+  bucket_id = params[:bucket]
+  blob_id = params[:blob]
+  driver.delete_blob(credentials, bucket_id, blob_id)
+  redirect(bucket_url(bucket_id))
+end
+
+#Get a particular blob's particulars (not actual blob data)
 get '/api/buckets/:bucket/:blob' do
   @blob = driver.blob(credentials, { :id => params[:blob], 'bucket' => params[:bucket]})
   if @blob
@@ -379,13 +408,7 @@ get '/api/buckets/:bucket/:blob' do
   end
 end
 
-get '/api/buckets/new' do
-  respond_to do |format|
-    format.html { haml :"buckets/new" }
-  end
-end
-
-
+#get the content of a particular blob
 get '/api/buckets/:bucket/:blob/content' do
   @blob = driver.blob(credentials, { :id => params[:blob], 'bucket' => params[:bucket]})
   params['content_length'] = @blob.content_length
@@ -393,6 +416,13 @@ get '/api/buckets/:bucket/:blob/content' do
   BlobStream.call(env, credentials, params)
 end
 
+#Get html form for creating a new bucket
+get '/api/buckets/new' do
+  respond_to do |format|
+    format.html { haml :"buckets/new" }
+  end
+end
+
 collection :buckets do
   description "Cloud Storage buckets - aka buckets|directories|folders"
 
diff --git a/server/views/blobs/new.html.haml b/server/views/blobs/new.html.haml
new file mode 100644
index 0000000..efc32ec
--- /dev/null
+++ b/server/views/blobs/new.html.haml
@@ -0,0 +1,10 @@
+%h1 New Blob
+
+%form{ :action => buckets_url + '/' + @bucket_id, :method => :post, :enctype =>
'multipart/form-data'}
+  %label
+    Blob Name:
+    %input{ :name => 'blob_id', :size => 512}/
+    Blob Data:
+    %input{ :type => "file", :name => 'blob_data',  :size => 50}/
+    %br
+  %input{ :type => :submit, :name => "commit", :value => "create"}/
diff --git a/server/views/blobs/show.html.haml b/server/views/blobs/show.html.haml
index 66c9fb1..e5c2cee 100644
--- a/server/views/blobs/show.html.haml
+++ b/server/views/blobs/show.html.haml
@@ -3,18 +3,24 @@
   = @blob.id
 
 %dl
-  %dt bucket
-  %dd
-    = @blob.bucket
-  %dt Content_Length
-  %dd
-    = @blob.content_length
-  %dt Content_Type
-  %dd
-    = @blob.content_type
-  %dt Last_Modified
-  %dd
-    = @blob.last_modified
-  %dt Content
-  %dd
-    =link_to 'Blob content', bucket_url(@blob.bucket) + '/' + @blob.id + '/content'
+  %di
+    %dt Bucket
+    %dd
+      = @blob.bucket
+    %dt Content_Length
+    %dd
+      = @blob.content_length
+    %dt Content_Type
+    %dd
+      = @blob.content_type
+    %dt Last_Modified
+    %dd
+      = @blob.last_modified
+    %dt Content
+    %dd
+      =link_to 'Blob content', bucket_url(@blob.bucket) + '/' + @blob.id + '/content'
+    %dt Delete this Blob
+    %dd
+      %form{ :action => bucket_url(@blob.bucket) + '/' + @blob.id , :method => :post
}
+        %input{ :type => "hidden", :name => "_method", :value => "delete"}
+        %input{ :type => :submit, :value => "Delete"}/
diff --git a/server/views/buckets/show.html.haml b/server/views/buckets/show.html.haml
index d704867..5b5457a 100644
--- a/server/views/buckets/show.html.haml
+++ b/server/views/buckets/show.html.haml
@@ -14,6 +14,9 @@
     %dd
       -@bucket.blob_list.each do |blob|
         = link_to blob, bucket_url(@bucket.name) + '/' + blob
-    %dt delete bucket (must be empty)
+    %dt Delete bucket (must be empty)
     %dd
       =link_to 'Delete', destroy_bucket_url(@bucket.name), :class => 'delete'
+    %dt Create a new blob
+    %dd
+      =link_to 'Create Blob', bucket_url(@bucket.name) + '/new_blob'
\ No newline at end of file
-- 
1.7.2.3


Mime
View raw message