From: Maros Zatko <mzatko(a)redhat.com>
---
src/lib/warehouse_client.rb | 163 +++++++++++++++++++++++++++++++++++++++++++
1 files changed, 163 insertions(+), 0 deletions(-)
create mode 100644 src/lib/warehouse_client.rb
diff --git a/src/lib/warehouse_client.rb b/src/lib/warehouse_client.rb
new file mode 100644
index 0000000..55affdf
--- /dev/null
+++ b/src/lib/warehouse_client.rb
@@ -0,0 +1,163 @@
+require 'rubygems'
+
+require 'rest-client'
+require 'nokogiri'
+
+#TODO: perform iwhd version-dependent URI mapping
+
+module Warehouse
+
+ class BucketObject
+ attr_reader :key
+
+ def initialize(connection, key, bucket)
+ @connection = connection
+ @key = key
+ @bucket = bucket
+ @path = "/#{@bucket.name}/#{(a)key}"
+ end
+
+ def self.create(connection, key, bucket, body, attrs = {})
+ obj = new(connection, key, bucket)
+ obj.set_body(body)
+ obj.set_attrs(attrs)
+ obj
+ end
+
+ def body
+ @connection.do_request @path, :plain => true
+ end
+
+ def set_body(body)
+ @connection.do_request @path, :content => body, :method => :put
+ end
+
+ def attr_list
+ result = @connection.do_request @path, :content => 'op=parts', :method
=> :post
+ return result.xpath('/object/object_attr/(a)name').to_a.map {|item|
item.value}
+ end
+
+ def attrs(list)
+ attrs = {}
+ list.each do |att|
+ attrs[att] = (@connection.do_request("#{@path}/#{att}", :plain =>
true) rescue nil)
+ end
+ attrs
+ end
+
+ def attr(name)
+ attrs([name]).first
+ end
+
+ def set_attrs(hash)
+ hash.each do |name, content|
+ set_attr(name, content)
+ end
+ end
+
+ def set_attr(name, content)
+ path = "#{@path}/#{name}"
+ @connection.do_request path, :content => content, :method => :put
+ end
+
+ def delete!
+ @connection.do_request @path, :method => :delete
+ true
+ end
+
+ end
+
+ class Bucket
+ attr_accessor :name
+
+ def initialize(name, connection)
+ @name = name
+ @connection = connection
+ end
+
+ def to_s
+ "Bucket: #{@name}"
+ end
+
+ def object_names
+ result = @connection.do_request "/#{@name}"
+ result.xpath('/objects/object').map do |obj|
+ obj.at_xpath('./key/text()').to_s
+ end
+ end
+
+ def objects
+ object_names.map do |name|
+ object(name)
+ end
+ end
+
+ def object(key)
+ BucketObject.new @connection, key, self
+ end
+
+ def create_object(key, body, attrs)
+ BucketObject.create(@connection, key, self, body, attrs)
+ end
+ end
+
+ class Connection
+ attr_accessor :uri
+
+ def initialize(uri)
+ @uri = uri
+ end
+
+ def do_request(path = '', opts={})
+ method = opts[:method] || :get
+ content = opts[:content] || ''
+ plain = opts[:plain] || false
+
+ #$stderr.puts "Connetion.do_request #{method} @ #{uri}#{path}"
+
+ result = RestClient.get @uri + path, :accept => '*/xml' if method ==
:get
+ result = RestClient.put @uri + path, content if method == :put #:accept =>
'*/xml' if method == :put
+ result = RestClient.post @uri + path, content if method == :post
+ result = RestClient.delete @uri + path if method == :delete
+
+ return Nokogiri::XML result unless plain
+ return result
+ end
+
+ end
+
+ class Client
+
+ def initialize(uri)
+ @connection = Connection.new(uri)
+ end
+
+ def create_bucket(bucket)
+ @connection.do_request "/#{bucket}", :method => :put
+ Bucket.new(bucket, @connection)
+ rescue RestClient::ExceptionWithResponse => e
+# puts "Error #{e.http_code.to_s}"
+ raise
+ end
+
+ def bucket(bucket)
+# result = @connection.do_request "/#{bucket}"
+ Bucket.new bucket, @connection
+ end
+
+ def buckets
+ @connection.do_request.xpath('/api/link[(a)rel="bucket"]').map do
|obj|
+ obj.at_xpath('./(a)href').to_s.gsub(/.*\//, '')
+ end
+ end
+
+ def get_iwhd_version
+ result =
@connection.do_request.at_xpath('/api[@service="image_warehouse"]/@version')
+ # raise "Found more than one <api> tag" if r.size > 1
+ raise "Response does not contain <api> tag or version information"
if result == nil
+ return result.value
+ end
+
+ end
+
+end
--
1.7.4