incubator-deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mar...@redhat.com
Subject [PATCH 2/2] Updates command line client for working with buckets and blobs (create,destroy,blob_data)
Date Tue, 29 Mar 2011 19:39:28 GMT
From: marios <marios@redhat.com>

---
 client/bin/deltacloudc              |   43 +++++++++++++++++++++++++++++++---
 client/lib/client_bucket_methods.rb |    4 ++-
 client/lib/deltacloud.rb            |   12 ++++++++-
 3 files changed, 52 insertions(+), 7 deletions(-)

diff --git a/client/bin/deltacloudc b/client/bin/deltacloudc
index 04b383a..5284362 100755
--- a/client/bin/deltacloudc
+++ b/client/bin/deltacloudc
@@ -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[:operation]
 
   # 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
 
@@ -192,6 +212,21 @@ if options[:collection] and options[:operation]
     puts format(instance)
     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')
diff --git a/client/lib/client_bucket_methods.rb b/client/lib/client_bucket_methods.rb
index 3a2e12b..cc7dcff 100644
--- a/client/lib/client_bucket_methods.rb
+++ b/client/lib/client_bucket_methods.rb
@@ -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
diff --git a/client/lib/deltacloud.rb b/client/lib/deltacloud.rb
index 1095163..05e0072 100644
--- a/client/lib/deltacloud.rb
+++ b/client/lib/deltacloud.rb
@@ -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
-- 
1.7.3.4


Mime
View raw message