You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@deltacloud.apache.org by ma...@redhat.com on 2011/01/19 23:25:56 UTC

User metadata for EC2 blobs - first cut

As it says on the tin - this is a first cut at user metadata for ec2. This is not a full solution (no rackspace,azure yet) - mainly for any initial thoughts/comments. One issue is the EC2 API doesn't offer any obvious way for updating metadata without re-uploading the blob (i.e. you can only put the metadata when you create a blob). So it might not be possible to offer CRUD for metadata - rather they are just a property of the blob (sorry if this is obvious, i had been expecting to be able to update/delete individual metadata - this may be possible for the other providers haven't investigated yet), 

marios

Re: User metadata for EC2 blobs - first cut

Posted by "marios@redhat.com" <ma...@redhat.com>.
Hi, pls ignore as this patch has been succeeded by a newer version - Fri 
21 Jan 'user metadata for blob creation - S3, Cloudfiles, Azure'

marios


On 20/01/11 00:25, marios@redhat.com wrote:
> As it says on the tin - this is a first cut at user metadata for ec2. This is not a full solution (no rackspace,azure yet) - mainly for any initial thoughts/comments. One issue is the EC2 API doesn't offer any obvious way for updating metadata without re-uploading the blob (i.e. you can only put the metadata when you create a blob). So it might not be possible to offer CRUD for metadata - rather they are just a property of the blob (sorry if this is obvious, i had been expecting to be able to update/delete individual metadata - this may be possible for the other providers haven't investigated yet),
>
> marios


[PATCH] Adds user defined metadata for ec2. This is NOT a general or complete solution (no rackspace/azure blobs yet) and not intended to be pushed - mainly for comments (e.g. format of xml, use of javascript in the html form for blob creation)

Posted by ma...@redhat.com.
From: marios <ma...@redhat.com>

---
 server/lib/deltacloud/drivers/ec2/ec2_driver.rb |    6 ++-
 server/lib/deltacloud/models/blob.rb            |    1 +
 server/public/javascripts/application.js        |   35 +++++++++++++++++++++++
 server/server.rb                                |   11 ++++++-
 server/views/blobs/new.html.haml                |   18 +++++++++++-
 server/views/blobs/show.html.haml               |    3 ++
 server/views/blobs/show.xml.haml                |    4 ++-
 7 files changed, 73 insertions(+), 5 deletions(-)

diff --git a/server/lib/deltacloud/drivers/ec2/ec2_driver.rb b/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
index 7a4b394..29674e5 100644
--- a/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
+++ b/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
@@ -361,11 +361,12 @@ module Deltacloud
           res = nil
           # File stream needs to be reopened in binary mode for whatever reason
           file = File::open(data[:tempfile].path, 'rb')
+          opts["Content-Type"] = data[:type]
           safely do
             res = s3_client.interface.put(bucket_id, 
                                         blob_id, 
                                         file, 
-                                        {"Content-Type" => data[:type]})
+                                        opts)
           end
           #create a new Blob object and return that
           Blob.new( { :id => blob_id,
@@ -568,7 +569,8 @@ module Deltacloud
             :bucket => s3_object.bucket.name.to_s,
             :content_length => s3_object.headers['content-length'],
             :content_type => s3_object.headers['content-type'],
-            :last_modified => s3_object.last_modified
+            :last_modified => s3_object.last_modified,
+            :user_metadata => s3_object.meta_headers
           )  
         end
 
diff --git a/server/lib/deltacloud/models/blob.rb b/server/lib/deltacloud/models/blob.rb
index dfa67fe..0bf473b 100644
--- a/server/lib/deltacloud/models/blob.rb
+++ b/server/lib/deltacloud/models/blob.rb
@@ -24,5 +24,6 @@ class Blob < BaseModel
   attr_accessor :content_type
   attr_accessor :last_modified
   attr_accessor :content
+  attr_accessor :user_metadata
 
 end
diff --git a/server/public/javascripts/application.js b/server/public/javascripts/application.js
index 5ee0f7f..95c9bc2 100644
--- a/server/public/javascripts/application.js
+++ b/server/public/javascripts/application.js
@@ -16,3 +16,38 @@ $(document).ready(function() {
   }
 
 })
+
+function more_fields()
+{
+	//increment the hidden input that captures how many meta_data are passed
+	var meta_params = document.getElementsByName('meta_params')
+    current_number_params = eval(meta_params[0].value)+1
+	meta_params[0].value = current_number_params
+    var new_meta = document.getElementById('metadata_holder').cloneNode(true);
+    new_meta.id = 'metadata_holder' + current_number_params;
+    new_meta.style.display = 'block';
+    var nodes = new_meta.childNodes;
+    for (var i=0;i < nodes.length;i++) {
+        var theName = nodes[i].name;
+        if (theName)
+          nodes[i].name = theName + current_number_params;
+    }
+    var insertHere = document.getElementById('metadata_holder');
+    insertHere.parentNode.insertBefore(new_meta,insertHere);
+}
+
+function less_fields()
+{
+    var meta_params = document.getElementsByName('meta_params')
+	current_val = eval(meta_params[0].value)
+	if (current_val == 0)
+	{
+		return;
+	}
+	else
+	{
+		var theDiv = document.getElementById('metadata_holder'+current_val)
+		theDiv.parentNode.removeChild(theDiv)
+		meta_params[0].value = eval(current_val)-1
+	}
+}
diff --git a/server/server.rb b/server/server.rb
index 8c3b72c..a2a13cc 100644
--- a/server/server.rb
+++ b/server/server.rb
@@ -589,7 +589,16 @@ 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 )
+  max = params[:meta_params].to_i
+  user_meta = {}
+  (1..max).each do |i|
+    key = params[:"meta_name#{i}"]
+#very ec2 specific for now, will move this logic into the drivers eventually
+    key = "x-amz-meta-#{key}"
+    value = params[:"meta_value#{i}"]
+    user_meta[key] = value
+  end
+  @blob = driver.create_blob(credentials, bucket_id, blob_id, blob_data, user_meta)
   respond_to do |format|
     format.html { haml :"blobs/show"}
     format.xml { haml :"blobs/show" }
diff --git a/server/views/blobs/new.html.haml b/server/views/blobs/new.html.haml
index feb94e5..a9817d5 100644
--- a/server/views/blobs/new.html.haml
+++ b/server/views/blobs/new.html.haml
@@ -4,7 +4,23 @@
   %label
     Blob Name:
     %input{ :name => 'blob_id', :size => 512}/
+  %label
     Blob Data:
-    %input{ :type => "file", :name => 'blob_data',  :size => 50}/
     %br
+    %input{ :type => "file", :name => 'blob_data', :size => 50}/
+    %br
+    %br
+  %input{ :type => "hidden", :name => "meta_params", :value => "0"}
+  %a{ :href => "javascript:;", :onclick => "more_fields();"} Add Metadata
+  %div{ :id => "metadata_holder", :style => "display: none;"}
+    %label
+      Metadata Name:
+    %input{ :type => "text", :name => "meta_name", :value => ""}/
+    %label
+      Metadata Value:
+    %input{ :type => "text", :name => "meta_value", :value => ""}/
+  %br
+  %a{ :href => "javascript:;", :onclick => "less_fields();"} Less Metadata
+  %br
+  %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 e5c2cee..29025c0 100644
--- a/server/views/blobs/show.html.haml
+++ b/server/views/blobs/show.html.haml
@@ -19,6 +19,9 @@
     %dt Content
     %dd
       =link_to 'Blob content', bucket_url(@blob.bucket) + '/' + @blob.id + '/content'
+    %dt User_Metadata
+    %dd
+      = @blob.user_metadata.inspect
     %dt Delete this Blob
     %dd
       %form{ :action => bucket_url(@blob.bucket) + '/' + @blob.id , :method => :post }
diff --git a/server/views/blobs/show.xml.haml b/server/views/blobs/show.xml.haml
index bc499ca..1b9ea55 100644
--- a/server/views/blobs/show.xml.haml
+++ b/server/views/blobs/show.xml.haml
@@ -1,7 +1,9 @@
 !!! XML
 %blob{:href => bucket_url(@blob.bucket) + '/' + @blob.id, :id => @blob.id}
-  - @blob.attributes.select{ |attr| attr!=:id}.each do |attribute|
+  - @blob.attributes.select{ |attr| (attr!=:id && attr!=:user_metadata) }.each do |attribute|
     - unless attribute == :content
       - haml_tag(attribute, :<) do
         - haml_concat @blob.send(attribute)
+  - @blob.user_metadata.each do |k, v|
+    %user_metadata{:name => k, :value => v}
   %content{:href => bucket_url(@blob.bucket) + '/' + @blob.id + '/content'}
-- 
1.7.3.4