From: marios <[email protected]>
---
client/bin/deltacloudc | 21 +++++++++++++++++++++
client/lib/deltacloud.rb | 18 ++++++++++++++----
client/lib/plain_formatter.rb | 23 ++++++++++++++++++-----
3 files changed, 53 insertions(+), 9 deletions(-)
diff --git a/client/bin/deltacloudc b/client/bin/deltacloudc
index 1fe7874..04b383a 100755
--- a/client/bin/deltacloudc
+++ b/client/bin/deltacloudc
@@ -60,6 +60,7 @@ Options:
BANNER
opts.on( '-i', '--id ID', 'ID for operation') { |id| options[:id] = id }
opts.on( '-d', '--image-id ID', 'Image ID') { |id| options[:image_id] = id }
+ opts.on( '-b', '--bucket-id ID', 'Bucket ID') {|id| options[:bucket_id] = id
}
opts.on( '-a', '--arch ARCH', 'Architecture (x86, x86_64)') { |id|
options[:architecture] = id }
opts.on( '-p', '--hardware-profile HARDWARE_PROFILE', 'Hardware Profile') {
|id| options[:hwp_id] = id }
opts.on( '-n', '--name NAME', 'Name (for instance eg.)') { |name|
options[:name] = name }
@@ -102,6 +103,8 @@ collections = client.entry_points.keys
# Exclude collection which don't have methods in client library yet
collections.delete(:instance_states)
+#add blob collection if buckets is present
+collections << :blob if collections.include?(:buckets)
# If list parameter passed print out available collection
# with API documentation
@@ -115,6 +118,11 @@ end
# If collection parameter is present and user requested list
# print all operation defined for collection with API documentation
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)"
+ exit(0)
+ end
doc = client.documentation(options[:collection])
doc.operations.each do |c|
puts sprintf("%-20s: %s", c.operation, c.description)
@@ -131,6 +139,8 @@ end
# Do same if 'index' operation is set
if options[:collection] and ( options[:operation].nil? or
options[:operation].eql?('index') )
invalid_usage("Unknown collection: #{options[:collection]}") unless
collections.include?(options[:collection].to_sym)
+#cannot list blobs - can only show a specific blob
+ invalid_usage("You must specify a particular blob with -i and a particular
bucket with -b") if options[:collection] =~ (/blob/i)
params = {}
params.merge!(:architecture => options[:architecture]) if
options[:architecture]
params.merge!(:id => options[:id]) if options[:id]
@@ -150,6 +160,17 @@ 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)
+ 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))
+ exit(0)
+ end
+
if options[:operation].eql?('show')
invalid_usage("Missing ID, must be provided with --id") unless options[:id]
puts format(client.send(options[:collection].gsub(/s$/, ''), options[:id]))
diff --git a/client/lib/deltacloud.rb b/client/lib/deltacloud.rb
index 292305d..7f9ec9c 100644
--- a/client/lib/deltacloud.rb
+++ b/client/lib/deltacloud.rb
@@ -146,7 +146,6 @@ module DeltaCloud
# Define methods based on 'rel' attribute in entry point
# Two methods are declared: 'images' and 'image'
def declare_entry_points_methods(entry_points)
-
API.instance_eval do
entry_points.keys.select {|k| [:instance_states].include?(k)==false
}.each do |model|
@@ -167,8 +166,20 @@ module DeltaCloud
self.send(model.to_s.singularize.to_sym, $1)
end
+ end
+
+ #define methods for blobs:
+ if(entry_points.include?(:buckets))
+ define_method :"blob" do |*args|
+ bucket = args[0]["bucket"]
+ blob = args[0][:id]
+ request(:get, "#{entry_points[:buckets]}/#{bucket}/#{blob}") do
|response|
+ base_object("blob", response)
+ end
end
end
+
+ end
end
def base_object_collection(model, response)
@@ -179,7 +190,6 @@ module DeltaCloud
# Add default attributes [id and href] to class
def base_object(model, response)
-
c = DeltaCloud.add_class("#{model}",
DeltaCloud::guess_model_type(response))
xml_to_class(c,
Nokogiri::XML(response).xpath("#{model.to_s.singularize}").first)
end
@@ -201,9 +211,10 @@ module DeltaCloud
# 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
+ obj.add_link!(attribute.name, attribute['id']) && next unless
(attribute.name == 'bucket' && item.name == 'blob')
end
# Do a HWP property for hardware profile properties
@@ -242,7 +253,6 @@ module DeltaCloud
# Anything else is treaten as text object
obj.add_text!(attribute.name, attribute.text.convert)
end
-
return obj
end
diff --git a/client/lib/plain_formatter.rb b/client/lib/plain_formatter.rb
index ca29f3d..45e7310 100644
--- a/client/lib/plain_formatter.rb
+++ b/client/lib/plain_formatter.rb
@@ -96,11 +96,24 @@ module DeltaCloud
class Bucket < Base
def format
- sprintf("%-50s | %-50s | %-10s | %-1000s",
- @obj.id[0,50],
- @obj.name[0,50],
- @obj.size ? @obj.size.to_s[0,10] : "",
- @obj.blob_list ? @obj.blob_list[0,1000] : ""
+ sprintf("%-s | %-s | %-s | %-s",
+ @obj.id,
+ @obj.name,
+ @obj.size ? @obj.size : "0",
+ @obj.blob_list ? @obj.blob_list : ""
+ )
+ end
+ end
+
+ class Blob < Base
+ def format
+ sprintf("%-s | %-s | %-d | %-s | %-s | %-s " ,
+ @obj.id,
+ @obj.bucket,
+ @obj.content_length,
+ @obj.content_type,
+ @obj.last_modified,
+ @obj.user_metadata
)
end
end
--
1.7.3.4