You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltacloud.apache.org by ma...@apache.org on 2011/04/01 19:49:11 UTC

svn commit: r1087832 - in /incubator/deltacloud/trunk/client: bin/deltacloudc lib/client_bucket_methods.rb lib/deltacloud.rb

Author: marios
Date: Fri Apr  1 17:49:11 2011
New Revision: 1087832

URL: http://svn.apache.org/viewvc?rev=1087832&view=rev
Log:
Updates command line client for working with buckets and blobs (create,destroy,blob_data)

Modified:
    incubator/deltacloud/trunk/client/bin/deltacloudc
    incubator/deltacloud/trunk/client/lib/client_bucket_methods.rb
    incubator/deltacloud/trunk/client/lib/deltacloud.rb

Modified: incubator/deltacloud/trunk/client/bin/deltacloudc
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/client/bin/deltacloudc?rev=1087832&r1=1087831&r2=1087832&view=diff
==============================================================================
--- incubator/deltacloud/trunk/client/bin/deltacloudc (original)
+++ incubator/deltacloud/trunk/client/bin/deltacloudc Fri Apr  1 17:49:11 2011
@@ -70,6 +70,13 @@ BANNER
   opts.on( '-h', '--help', 'Display this screen' ) { puts @optparse; Kernel.exit! }
   opts.on( '-v', '--version', 'Display API version' ) { options[:version]=true }
   opts.on( '-V', '--verbose', 'Print verbose messages' ) { options[:verbose]=true }
+  opts.on( '-f', '--file-path PATH', 'local path for new blob data') {|path| options[:file_path]=path }
+  opts.on( '-m', '--blob-metadata k1=v1, k2=v2', 'Comma seperated k=v pairs for blob metadata (for create operation)') do |meta|
+                    blob_meta = {}
+                    meta.gsub!(/ /,"")
+                    meta.scan(/(\w+)=(\w+)/).map {|k,v| blob_meta[k] = v }
+                    options[:blob_metadata] = blob_meta
+                end
 end
 
 def invalid_usage(error_msg='')
@@ -120,7 +127,10 @@ end
 if options[:list] and options[:collection]
   #deal with blobs again - bypass 'normal' docs procedure
   if options[:collection] =~ /blob/i
-    puts "show : Get details of a specified blob in a specified bucket (GET /api/buckets/:bucket/:blob)"
+    puts "create \t\t: Create a new blob in a specified bucket (POST /api/buckets/:bucket)"
+    puts "destroy \t: Delete a specified blob in a specified bucket (DELETE /api/buckets/:bucket/:blob)"
+    puts "show \t\t: Get details of a specified blob in a specified bucket (GET /api/buckets/:bucket/:blob)"
+    puts "data \t\t: Get the contents of a specified blob - the blob data content itself"
     exit(0)
   end
   doc = client.documentation(options[:collection])
@@ -160,14 +170,24 @@ if options[:collection] and options[:ope
 
   # If collection is set and requested operation is 'show' just 'singularize'
   # collection name and print item with specified id (-i parameter)
-
   #Blobs are a special case so deal with first -
-  if options[:operation].eql?('show') && options[:collection] =~ (/blob/i)
+  if options[:collection] =~ (/blob/i)
     invalid_usage("Please specify the bucket for this blob using the -b option") unless options[:bucket_id]
     invalid_usage("Missing blob ID, please specify with -i option") unless options[:id]
     params = {}
     params.merge!(:id => options[:id], 'bucket' => options[:bucket_id])
-    puts format(client.send( options[:collection], params))
+    params.merge!('metadata'=>options[:blob_metadata]) unless options[:blob_metadata].nil?
+    case options[:operation]
+        when 'show' then puts format(client.send( options[:collection], params))
+        when 'data' then puts (client.blob_data(params))
+        when 'create' then
+            invalid_usage("Specify the location of the new blob data (full local path) using -f option") unless options[:file_path]
+            params.merge!('file_path'=>options[:file_path])
+            blob = client.create_blob(params)
+            puts format(blob)
+        when 'destroy' then client.destroy_blob(params)
+        else invalid_usage("Please specify a valid operation for the blob collection - try -l to see available operations")
+    end
     exit(0)
   end
 
@@ -193,6 +213,21 @@ if options[:collection] and options[:ope
     exit(0)
   end
 
+  #Create and Destroy a bucket - require the bucket id:
+  if options[:collection].eql?('buckets')
+    if options[:operation].eql?('create')
+        invalid_usage("Please specify an id for the new bucket with -i") unless options[:id]
+        bucket = client.create_bucket('id'=>options[:id])
+        puts format(bucket)
+    elsif options[:operation].eql?('destroy')
+        invalid_usage("Please specify the bucket you wish to destroy with -i") unless options[:id]
+        client.destroy_bucket('id'=>options[:id])
+    else
+        invalid_usage("Please specify a valid operation on buckets - use -l to see valid operations")
+    end
+    exit(0)
+  end
+
   # All other operations above collections is done there:
   if options[:collection].eql?('instances')
     instance = client.instance(options[:id])

Modified: incubator/deltacloud/trunk/client/lib/client_bucket_methods.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/client/lib/client_bucket_methods.rb?rev=1087832&r1=1087831&r2=1087832&view=diff
==============================================================================
--- incubator/deltacloud/trunk/client/lib/client_bucket_methods.rb (original)
+++ incubator/deltacloud/trunk/client/lib/client_bucket_methods.rb Fri Apr  1 17:49:11 2011
@@ -11,13 +11,14 @@ module ClientBucketMethods
   def destroy_bucket(params)
     #actually response here is 204 - no content - so nothing returned to client?
     request(:delete, "#{api_uri.to_s}/buckets/#{params['id']}") do |response|
+      handle_backend_error(response) if response.code!=204
       response
     end
   end
 
   def create_blob(params)
     blob = nil
-    resource = RestClient::Resource.new("#{api_uri.to_s}/buckets/#{params[:bucket]}", :open_timeout => 10, :timeout => 45)
+    resource = RestClient::Resource.new("#{api_uri.to_s}/buckets/#{params['bucket']}", :open_timeout => 10, :timeout => 45)
     headers = default_headers.merge(extended_headers)
     unless params['metadata'].nil?
       metadata_headers = {}
@@ -36,6 +37,7 @@ module ClientBucketMethods
 
   def destroy_blob(params)
     request(:delete, "#{api_uri.to_s}/buckets/#{params['bucket']}/#{params[:id]}") do |response|
+      handle_backend_error(response) if response.code!=204
       response
     end
   end

Modified: incubator/deltacloud/trunk/client/lib/deltacloud.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/client/lib/deltacloud.rb?rev=1087832&r1=1087831&r2=1087832&view=diff
==============================================================================
--- incubator/deltacloud/trunk/client/lib/deltacloud.rb (original)
+++ incubator/deltacloud/trunk/client/lib/deltacloud.rb Fri Apr  1 17:49:11 2011
@@ -211,10 +211,8 @@ module DeltaCloud
       params.merge!({ :initial_state => (item/'state').text.sanitize }) if (item/'state').length > 0
 
       obj = base_object.new(params)
-
       # Traverse across XML document and deal with elements
       item.xpath('./*').each do |attribute|
-
         # Do a link for elements which are links to other REST models
         if self.entry_points.keys.include?(:"#{attribute.name}s")
           obj.add_link!(attribute.name, attribute['id']) && next unless (attribute.name == 'bucket' && item.name == 'blob')
@@ -243,6 +241,16 @@ module DeltaCloud
           next
         end
 
+        #deal with blob metadata
+        if(attribute.name == 'user_metadata')
+          meta = {}
+          attribute.children.select {|x| x.name=="entry" }.each  do |element|
+            value = element.content.gsub!(/(\n) +/,'')
+            meta[element['key']] = value
+          end
+          obj.add_collection!(attribute.name, meta.inspect) && next
+        end
+
         # Deal with collections like public-addresses, private-addresses
         if (attribute/'./*').length > 0
           obj.add_collection!(attribute.name, (attribute/'*').collect { |value| value.text }) && next