incubator-deltacloud-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mar...@apache.org
Subject svn commit: r1134368 - in /incubator/deltacloud/trunk/server: ./ lib/deltacloud/drivers/azure/ lib/deltacloud/drivers/ec2/ lib/deltacloud/drivers/mock/ lib/deltacloud/drivers/rackspace/ lib/deltacloud/helpers/
Date Fri, 10 Jun 2011 16:15:07 GMT
Author: marios
Date: Fri Jun 10 16:15:06 2011
New Revision: 1134368

URL: http://svn.apache.org/viewvc?rev=1134368&view=rev
Log:
Tidying up of repetitive code into helper: extraction of blob
metadata headers and renaming of the metadata header prefix
('X-Deltacloud-Blobmeta-KEY:VAL' ==> 'x-amz-meta-KEY:VAL')
are now in a BlobHelper module (in /helpers/blob_stream.rb

Modified:
    incubator/deltacloud/trunk/server/lib/deltacloud/drivers/azure/azure_driver.rb
    incubator/deltacloud/trunk/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
    incubator/deltacloud/trunk/server/lib/deltacloud/drivers/mock/mock_driver.rb
    incubator/deltacloud/trunk/server/lib/deltacloud/drivers/rackspace/rackspace_driver.rb
    incubator/deltacloud/trunk/server/lib/deltacloud/helpers/blob_stream.rb
    incubator/deltacloud/trunk/server/server.rb

Modified: incubator/deltacloud/trunk/server/lib/deltacloud/drivers/azure/azure_driver.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/lib/deltacloud/drivers/azure/azure_driver.rb?rev=1134368&r1=1134367&r2=1134368&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/lib/deltacloud/drivers/azure/azure_driver.rb (original)
+++ incubator/deltacloud/trunk/server/lib/deltacloud/drivers/azure/azure_driver.rb Fri Jun
10 16:15:06 2011
@@ -105,7 +105,7 @@ class AzureDriver < Deltacloud::BaseDriv
   def create_blob(credentials, bucket_id, blob_id, blob_data, opts={})
     azure_connect(credentials)
     #insert azure-specific header for user metadata ... x-ms-meta-kEY = VALUE
-    opts.gsub_keys("HTTP_X_Deltacloud_Blobmeta_", "x-ms-meta-")
+    BlobHelper::rename_metadata_headers(opts, "x-ms-meta-")
     safely do
       #get a handle to the bucket in order to put there
       the_bucket = WAZ::Blobs::Container.find(bucket_id)
@@ -116,7 +116,7 @@ class AzureDriver < Deltacloud::BaseDriv
                 :content_lengh => blob_data[:tempfile].length,
                 :content_type => blob_data[:type],
                 :last_modified => '',
-                :user_metadata => opts.gsub_keys('x-ms-meta-','')
+                :user_metadata => opts.gsub_keys(/x-ms-meta-/,'')
             } )
   end
 
@@ -144,7 +144,7 @@ class AzureDriver < Deltacloud::BaseDriv
     end
     user_meta = {}
     all_meta.inject({}){|result_hash, (k,v)| user_meta[k]=v if k.to_s.match(/x_ms_meta/i)}
-    user_meta.gsub_keys('x_ms_meta_','')
+    user_meta.gsub_keys(/x_ms_meta_/,'')
   end
 
 #-
@@ -153,7 +153,7 @@ class AzureDriver < Deltacloud::BaseDriv
   def update_blob_metadata(credentials, opts={})
     azure_connect(credentials)
     meta_hash = opts['meta_hash']
-    meta_hash.gsub_keys("HTTP_X_Deltacloud_Blobmeta_", "x-ms-meta-")
+    BlobHelper::rename_metadata_headers(meta_hash, "x-ms-meta-")
     safely do
       the_blob = WAZ::Blobs::Container.find(opts['bucket'])[opts[:id]]
       the_blob.put_metadata!(meta_hash)
@@ -189,7 +189,7 @@ class AzureDriver < Deltacloud::BaseDriv
     blob_metadata = {}
     waz_blob.metadata.inject({}) { |result_hash, (k,v)| blob_metadata[k]=v if k.to_s.match(/x_ms_meta/i)}
     #strip off the x_ms_meta_ from each key
-    blob_metadata.gsub_keys('x_ms_meta_', '')
+    blob_metadata.gsub_keys(/x_ms_meta_/, '')
     Blob.new({   :id => waz_blob.name,
                  :bucket => bucket,
                  :content_length => waz_blob.metadata[:content_length],

Modified: incubator/deltacloud/trunk/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/lib/deltacloud/drivers/ec2/ec2_driver.rb?rev=1134368&r1=1134367&r2=1134368&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/lib/deltacloud/drivers/ec2/ec2_driver.rb (original)
+++ incubator/deltacloud/trunk/server/lib/deltacloud/drivers/ec2/ec2_driver.rb Fri Jun 10
16:15:06 2011
@@ -410,7 +410,7 @@ module Deltacloud
           # File stream needs to be reopened in binary mode for whatever reason
           file = File::open(data[:tempfile].path, 'rb')
           #insert ec2-specific header for user metadata ... x-amz-meta-KEY = VALUE
-          opts.gsub_keys('HTTP_X_Deltacloud_Blobmeta_', 'x-amz-meta-')
+          BlobHelper::rename_metadata_headers(opts, 'x-amz-meta-')
           opts["Content-Type"] = data[:type]
           safely do
             res = s3_client.interface.put(bucket_id,
@@ -453,13 +453,11 @@ module Deltacloud
         def blob_stream_connection(params)
           #canonicalise metadata:
           #http://docs.amazonwebservices.com/AmazonS3/latest/dev/index.html?RESTAuthentication.html
-          metadata = params[:metadata]
+          metadata = params[:metadata] || {}
           signature_meta_string = ""
-          unless metadata.nil?
-            metadata.gsub_keys('HTTP[-_]X[-_]Deltacloud[-_]Blobmeta[-_]', 'x-amz-meta-')
-            keys_array = metadata.keys.sort!
-            keys_array.each {|k| signature_meta_string << "#{k}:#{metadata[k]}\n"}
-          end
+          BlobHelper::rename_metadata_headers(metadata, 'x-amz-meta-')
+          keys_array = metadata.keys.sort!
+          keys_array.each {|k| signature_meta_string << "#{k}:#{metadata[k]}\n"}
           provider = "https://#{endpoint_for_service(:s3)}"
           uri = URI.parse(provider)
           http = Net::HTTP.new("#{params[:bucket]}.#{uri.host}", uri.port )
@@ -476,9 +474,7 @@ module Deltacloud
           request['Content-Length'] = params[:content_length]
           request['Authorization'] = "AWS #{params[:user]}:#{auth_string}"
           request['Expect'] = "100-continue"
-          unless metadata.nil?
-            metadata.each{|k,v| request[k] = v}
-          end
+          metadata.each{|k,v| request[k] = v}
           return http, request
         end
 

Modified: incubator/deltacloud/trunk/server/lib/deltacloud/drivers/mock/mock_driver.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/lib/deltacloud/drivers/mock/mock_driver.rb?rev=1134368&r1=1134367&r2=1134368&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/lib/deltacloud/drivers/mock/mock_driver.rb (original)
+++ incubator/deltacloud/trunk/server/lib/deltacloud/drivers/mock/mock_driver.rb Fri Jun 10
16:15:06 2011
@@ -428,16 +428,15 @@ class MockDriver < Deltacloud::BaseDrive
 # Create blob
 #--
   def create_blob(credentials, bucket_id, blob_id, blob_data, opts={})
-    check_credentials(credentials)
-      blob_meta = {}
-      opts.inject({}){|result, (k,v)| blob_meta[k] = v if k.match(/X[_-]Deltacloud[_-]Blobmeta[_-]/i)}
#select{|k,v| k.match(/X[_-]Deltacloud[_-]Blobmeta[_-]/i)}
-       blob = {
+      check_credentials(credentials)
+      blob_meta = BlobHelper::extract_blob_metadata_hash(opts)
+      blob = {
       :id => blob_id,
       :bucket => bucket_id,
       :content_length => blob_data[:tempfile].length,
       :content_type => blob_data[:type],
       :last_modified => Time.now,
-      :user_metadata => blob_meta.gsub_keys('X_Deltacloud_Blobmeta_', ''),
+      :user_metadata => BlobHelper::rename_metadata_headers(blob_meta, ''),
       :content => blob_data[:tempfile].read
     }
     File.open( File::join("#{@storage_root}", "buckets", "blobs", "#{blob_id}.yml"), 'w'
) {|b| YAML.dump( blob, b )}
@@ -482,7 +481,7 @@ class MockDriver < Deltacloud::BaseDrive
     safely do
       blob = YAML.load_file(blobfile)
       return false unless blob
-      blob[:user_metadata] = opts['meta_hash'].gsub_keys('HTTP[-_]X[-_]Deltacloud[-_]Blobmeta[-_]',
'')
+      blob[:user_metadata] = BlobHelper::rename_metadata_headers(opts['meta_hash'], '')
       File.open(File::join("#{@storage_root}", "buckets", "blobs", "#{opts[:id]}.yml"), 'w'
) {|b| YAML.dump( blob, b )}
     end
   end

Modified: incubator/deltacloud/trunk/server/lib/deltacloud/drivers/rackspace/rackspace_driver.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/lib/deltacloud/drivers/rackspace/rackspace_driver.rb?rev=1134368&r1=1134367&r2=1134368&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/lib/deltacloud/drivers/rackspace/rackspace_driver.rb
(original)
+++ incubator/deltacloud/trunk/server/lib/deltacloud/drivers/rackspace/rackspace_driver.rb
Fri Jun 10 16:15:06 2011
@@ -262,7 +262,7 @@ class RackspaceDriver < Deltacloud::Base
   def create_blob(credentials, bucket_id, blob_id, blob_data, opts={})
     cf = cloudfiles_client(credentials)
     #insert ec2-specific header for user metadata ... X-Object-Meta-KEY = VALUE
-    opts.gsub_keys("HTTP_X_Deltacloud_Blobmeta_", "X-Object-Meta-")
+    BlobHelper::rename_metadata_headers(opts, "X-Object-Meta-)"
     opts['Content-Type'] = blob_data[:type]
     object = nil
     safely do
@@ -311,7 +311,7 @@ class RackspaceDriver < Deltacloud::Base
     cf = cloudfiles_client(credentials)
     meta_hash = opts['meta_hash']
     #the set_metadata method actually places the 'X-Object-Meta-' prefix for us:
-    meta_hash.gsub_keys('HTTP_X_Deltacloud_Blobmeta_', '')
+    BlobHelper::rename_metadata_headers(meta_hash, '')
     safely do
       blob = cf.container(opts['bucket']).object(opts[:id])
       blob.set_metadata(meta_hash)

Modified: incubator/deltacloud/trunk/server/lib/deltacloud/helpers/blob_stream.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/lib/deltacloud/helpers/blob_stream.rb?rev=1134368&r1=1134367&r2=1134368&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/lib/deltacloud/helpers/blob_stream.rb (original)
+++ incubator/deltacloud/trunk/server/lib/deltacloud/helpers/blob_stream.rb Fri Jun 10 16:15:06
2011
@@ -65,8 +65,7 @@ end
 
 class Hash
 
-  def gsub_keys(pattern, replacement)
-    rgx_pattern = Regexp.compile(pattern, true)
+  def gsub_keys(rgx_pattern, replacement)
     remove = []
     self.each_key do |key|
       if key.to_s.match(rgx_pattern)
@@ -81,6 +80,23 @@ class Hash
 
 end
 
+module BlobHelper
+
+  def self.extract_blob_metadata_hash(env_hash)
+    meta_array = env_hash.select{|k,v| k.match(/^HTTP[-_]X[-_]Deltacloud[-_]Blobmeta[-_]/i)}
+    metadata = meta_array.inject({}){ |result, array| result[array.first.upcase] = array.last;
result}
+    metadata
+  end
+
+DELTACLOUD_BLOBMETA_HEADER = /HTTP[-_]X[-_]Deltacloud[-_]Blobmeta[-_]/i
+
+  #e.g. from HTTP-X-Deltacloud-Blobmeta-FOO:BAR to amz-meta-FOO:BAR
+  def self.rename_metadata_headers(metadata, rename_to)
+    metadata.gsub_keys(DELTACLOUD_BLOBMETA_HEADER, rename_to)
+  end
+
+end
+
 #Monkey patch for streaming blobs:
 # Normally a client will upload a blob to deltacloud and thin will put
 # this into a tempfile. Then deltacloud would stream up to the provider:
@@ -156,8 +172,7 @@ class BlobStreamIO
     user, password = parse_credentials(request.env['HTTP_AUTHORIZATION'])
     content_type = request.env['CONTENT_TYPE'] || ""
     #deal with blob_metadata: (X-Deltacloud-Blobmeta-name: value)
-    meta_array = request.env.select{|k,v| k.match(/^HTTP[-_]X[-_]Deltacloud[-_]Blobmeta[-_]/i)}
-    user_meta = meta_array.inject({}){ |result, array| result[array.first.upcase] = array.last;
result}
+    user_meta = BlobHelper::extract_blob_metadata_hash(request.env)
     @content_length = request.env['CONTENT_LENGTH']
     @http, provider_request = driver.blob_stream_connection({:user=>user,
        :password=>password, :bucket=>bucket, :blob=>blob, :metadata=> user_meta,

Modified: incubator/deltacloud/trunk/server/server.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/server.rb?rev=1134368&r1=1134367&r2=1134368&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/server.rb (original)
+++ incubator/deltacloud/trunk/server/server.rb Fri Jun 10 16:15:06 2011
@@ -727,8 +727,7 @@ put "#{Sinatra::UrlForHelper::DEFAULT_UR
     temp_file.flush
     content_type = env['CONTENT_TYPE'] || ""
     blob_data = {:tempfile => temp_file, :type => content_type}
-    meta_array = request.env.select{|k,v| k.match(/^HTTP[-_]X[-_]Deltacloud[-_]Blobmeta[-_]/i)}
-    user_meta = meta_array.inject({}){ |result, array| result[array.first.upcase] = array.last;
result}
+    user_meta = BlobHelper::extract_blob_metadata_hash(request.env)
     @blob = driver.create_blob(credentials, bucket_id, blob_id, blob_data, user_meta)
     temp_file.delete
     respond_to do |format|
@@ -788,8 +787,7 @@ end
 
 #update blob metadata
 post "#{Sinatra::UrlForHelper::DEFAULT_URI_PREFIX}/buckets/:bucket/:blob" do
-  meta_hash = {}
-  request.env.inject({}){|current, (k,v)| meta_hash[k] = v if k.match(/^HTTP[-_]X[-_]Deltacloud[-_]Blobmeta[-_]/i)}
+  meta_hash = BlobHelper::extract_blob_metadata_hash(request.env)
   success = driver.update_blob_metadata(credentials, {'bucket'=>params[:bucket], :id =>params[:blob],
'meta_hash' => meta_hash})
   if(success)
     meta_hash.each do |k,v|



Mime
View raw message