On 12/13, Tomas Sedovic wrote:
> ACK, the patchset fixes https://issues.apache.org/jira/browse/DTACLOUD-354 
> for me.

Thanks Tomas! Pushed.

  -- Michal

> 
> Thanks!
> 
> ----- Original Message -----
> From: [email protected]
> To: [email protected]
> Sent: Thursday, December 13, 2012 11:43:17 AM
> Subject: [PATCH core 1/4] Core: Added .to_hash method to all Deltacloud models
> 
> From: Michal Fojtik <[email protected]>
> 
> * .to_hash produce Ruby Hash that can be converted to JSON
> * This patch makes 'crack' library obsolete
> * NOTE: This patch might break JSON backward compatibility.
> 
> Signed-off-by: Michal fojtik <[email protected]>
> ---
>  server/deltacloud-core.gemspec                     |  1 -
>  server/lib/cimi/server.rb                          |  1 -
>  server/lib/deltacloud/collections/addresses.rb     |  2 +-
>  server/lib/deltacloud/collections/buckets.rb       | 14 ++++----
>  server/lib/deltacloud/collections/firewalls.rb     |  4 +--
>  server/lib/deltacloud/collections/images.rb        |  2 +-
>  server/lib/deltacloud/collections/instances.rb     |  8 ++++-
>  server/lib/deltacloud/collections/keys.rb          |  2 +-
>  .../lib/deltacloud/collections/load_balancers.rb   |  8 ++---
>  .../lib/deltacloud/collections/storage_volumes.rb  |  6 ++--
>  server/lib/deltacloud/helpers/deltacloud_helper.rb | 38 +++++++++++++--------
>  server/lib/deltacloud/models/address.rb            |  9 +++++
>  server/lib/deltacloud/models/blob.rb               | 12 +++++++
>  server/lib/deltacloud/models/bucket.rb             |  9 +++++
>  server/lib/deltacloud/models/firewall.rb           | 14 +++++++-
>  server/lib/deltacloud/models/firewall_rule.rb      | 14 ++++++++
>  server/lib/deltacloud/models/hardware_profile.rb   | 13 ++++++++
>  server/lib/deltacloud/models/image.rb              | 14 ++++++++
>  server/lib/deltacloud/models/instance.rb           | 39 
> +++++++++++++++++++++-
>  server/lib/deltacloud/models/instance_address.rb   |  9 +++++
>  server/lib/deltacloud/models/key.rb                | 15 +++++++++
>  server/lib/deltacloud/models/load_balancer.rb      | 20 +++++++++++
>  server/lib/deltacloud/models/metric.rb             | 15 +++++++++
>  server/lib/deltacloud/models/provider.rb           |  8 +++++
>  server/lib/deltacloud/models/realm.rb              |  8 +++++
>  server/lib/deltacloud/models/storage_snapshot.rb   |  9 +++++
>  server/lib/deltacloud/models/storage_volume.rb     | 18 ++++++++++
>  server/lib/deltacloud/server.rb                    |  3 +-
>  server/lib/deltacloud_rack.rb                      |  1 -
>  server/tests/deltacloud/deltacloud_helper_test.rb  |  4 ---
>  server/tests/deltacloud/server_test.rb             |  1 +
>  31 files changed, 277 insertions(+), 44 deletions(-)
> 
> diff --git a/server/deltacloud-core.gemspec b/server/deltacloud-core.gemspec
> index ff57683..1c36e2f 100644
> --- a/server/deltacloud-core.gemspec
> +++ b/server/deltacloud-core.gemspec
> @@ -70,7 +70,6 @@ Gem::Specification.new do |s|
>    s.add_dependency('haml', '>= 2.2.17')
>    s.add_dependency('sinatra', '>= 1.3.0')
>    s.add_dependency('sinatra-rabbit', '>= 1.0.11')
> -  s.add_dependency('crack')
>    s.add_dependency('rack', '>= 1.0.0')
>    s.add_dependency('rack-accept')
>    s.add_dependency('json', '>= 1.1.9')
> diff --git a/server/lib/cimi/server.rb b/server/lib/cimi/server.rb
> index 02891c5..41c8bd6 100644
> --- a/server/lib/cimi/server.rb
> +++ b/server/lib/cimi/server.rb
> @@ -14,7 +14,6 @@
>  # under the License.
>  
>  require 'rubygems'
> -require 'crack'
>  require 'yaml'
>  require 'haml'
>  require 'sinatra/base'
> diff --git a/server/lib/deltacloud/collections/addresses.rb 
> b/server/lib/deltacloud/collections/addresses.rb
> index aa51abe..fabbb59 100644
> --- a/server/lib/deltacloud/collections/addresses.rb
> +++ b/server/lib/deltacloud/collections/addresses.rb
> @@ -41,7 +41,7 @@ module Deltacloud::Collections
>            respond_to do |format|
>              format.xml  { haml :"addresses/show", :ugly => true }
>              format.html { haml :"addresses/_address", :layout => false }
> -            format.json { xml_to_json("addresses/show") }
> +            format.json { JSON::dump(:address => @address.to_hash(self)) }
>            end
>          end
>        end
> diff --git a/server/lib/deltacloud/collections/buckets.rb 
> b/server/lib/deltacloud/collections/buckets.rb
> index 90f2306..eb3830d 100644
> --- a/server/lib/deltacloud/collections/buckets.rb
> +++ b/server/lib/deltacloud/collections/buckets.rb
> @@ -91,7 +91,7 @@ module Deltacloud::Collections
>          respond_to do |format|
>            format.xml { haml :"blobs/show" }
>            format.html { haml :"blobs/show" }
> -          format.json { xml_to_json 'blobs/show' }
> +          format.json { JSON::dump(:blob => @blob.to_hash(self) ) }
>          end
>        elsif(env["BLOB_FAIL"])
>          report_error(500) #OK?
> @@ -110,7 +110,7 @@ module Deltacloud::Collections
>          respond_to do |format|
>            format.xml { haml :"blobs/show" }
>            format.html { haml :"blobs/show" }
> -          format.json { xml_to_json 'blobs/show' }
> +          format.json { JSON::dump(:blob => @blob.to_hash(self)) }
>          end
>        end
>      end
> @@ -129,7 +129,7 @@ module Deltacloud::Collections
>            response['Location'] = bucket_url(@bucket.id)
>            respond_to do |format|
>              format.xml  { haml :"buckets/show" }
> -            format.json { xml_to_json('buckets/show') }
> +            format.json { JSON::dump(:bucket => @bucket.to_hash(self)) }
>              format.html do
>                redirect bucket_url(@bucket.id) if @bucket and @bucket.id
>                redirect buckets_url
> @@ -160,7 +160,7 @@ module Deltacloud::Collections
>                respond_to do |format|
>                  format.xml { haml :"blobs/show" }
>                  format.html { haml :"blobs/show" }
> -                format.json { xml_to_json 'blobs/show' }
> +                format.json { JSON::dump(:blob => @blob.to_hash(self)) }
>                end
>              else
>                report_error(404)
> @@ -194,7 +194,7 @@ module Deltacloud::Collections
>              respond_to do |format|
>                format.xml { haml :"blobs/show" }
>                format.html { haml :"blobs/show"}
> -              format.json { xml_to_json 'blobs/show' }
> +              format.json { JSON::dump(:blob => @blob.to_hash(self)) }
>              end
>            end
>          end
> @@ -227,7 +227,7 @@ module Deltacloud::Collections
>                respond_to do |format|
>                  format.xml { haml :"blobs/show" }
>                  format.html { haml :"blobs/show" }
> -                format.json { xml_to_json 'blobs/show' }
> +                format.json { JSON::dump(@blob.to_hash(self)) }
>                end
>              elsif(env["BLOB_FAIL"])
>                report_error(500) #OK?
> @@ -246,7 +246,7 @@ module Deltacloud::Collections
>                respond_to do |format|
>                  format.xml { haml :"blobs/show" }
>                  format.html { haml :"blobs/show" }
> -                format.json { xml_to_json 'blobs/show' }
> +                format.json { JSON::dump(@blob.to_hash(self)) }
>                end
>              end
>            end
> diff --git a/server/lib/deltacloud/collections/firewalls.rb 
> b/server/lib/deltacloud/collections/firewalls.rb
> index 3066c1b..1f35c58 100644
> --- a/server/lib/deltacloud/collections/firewalls.rb
> +++ b/server/lib/deltacloud/collections/firewalls.rb
> @@ -66,7 +66,7 @@ module Deltacloud::Collections
>            respond_to do |format|
>              format.xml  { haml :"firewalls/show" }
>              format.html { haml :"firewalls/show" }
> -            format.json { xml_to_json("firewalls/show") }
> +            format.json { JSON::dump(:firewall => @firewall.to_hash(self)) }
>            end
>          end
>        end
> @@ -109,7 +109,7 @@ module Deltacloud::Collections
>            respond_to do |format|
>              format.xml  { haml :"firewalls/show" }
>              format.html { haml :"firewalls/show" }
> -            format.json { xml_to_json("firewalls/show") }
> +            format.json { JSON::dump(:firewall => @firewall.to_hash(self) ) }
>            end
>          end
>        end
> diff --git a/server/lib/deltacloud/collections/images.rb 
> b/server/lib/deltacloud/collections/images.rb
> index 827e5d4..dbdbdfa 100644
> --- a/server/lib/deltacloud/collections/images.rb
> +++ b/server/lib/deltacloud/collections/images.rb
> @@ -50,7 +50,7 @@ module Deltacloud::Collections
>            response['Location'] = image_url(@image.id)
>            respond_to do |format|
>              format.xml  { haml :"images/show" }
> -            format.json { xml_to_json('images/show') }
> +            format.json { JSON::dump(:image => @image.to_hash(self)) }
>              format.html { haml :"images/show" }
>            end
>          end
> diff --git a/server/lib/deltacloud/collections/instances.rb 
> b/server/lib/deltacloud/collections/instances.rb
> index ce536ce..ffdf09d 100644
> --- a/server/lib/deltacloud/collections/instances.rb
> +++ b/server/lib/deltacloud/collections/instances.rb
> @@ -60,7 +60,13 @@ module Deltacloud::Collections
>            status 201  # Created
>            respond_to do |format|
>              format.xml  { haml :"instances/#{action_handler}" }
> -            format.json { xml_to_json("instances/#{action_handler}") }
> +            format.json do
> +              if @elements
> +                JSON::dump(:instances => @elements.map { |i| i.to_hash(self) 
> })
> +              elsif @instance and @instance.id
> +                JSON::dump(:instance => @instance.to_hash(self))
> +              end
> +            end
>              format.html do
>                if @elements
>                  haml :"instances/index"
> diff --git a/server/lib/deltacloud/collections/keys.rb 
> b/server/lib/deltacloud/collections/keys.rb
> index fa1b3e0..8d5c460 100644
> --- a/server/lib/deltacloud/collections/keys.rb
> +++ b/server/lib/deltacloud/collections/keys.rb
> @@ -42,7 +42,7 @@ module Deltacloud::Collections
>            respond_to do |format|
>              format.xml  { haml :"keys/show", :ugly => true }
>              format.html { haml :"keys/show" }
> -            format.json { xml_to_json("keys/show")}
> +            format.json { JSON::dump(:key => @key.to_hash(self)) }
>            end
>          end
>        end
> diff --git a/server/lib/deltacloud/collections/load_balancers.rb 
> b/server/lib/deltacloud/collections/load_balancers.rb
> index e13ccec..92c7a36 100644
> --- a/server/lib/deltacloud/collections/load_balancers.rb
> +++ b/server/lib/deltacloud/collections/load_balancers.rb
> @@ -41,7 +41,7 @@ module Deltacloud::Collections
>            @unregistered_instances = all_instances - @registered_instances
>            respond_to do |format|
>              format.xml { haml :'load_balancers/show' }
> -            format.json { xml_to_json('load_balancers/show') }
> +            format.json { JSON::dump(:load_balancer => 
> @load_balancer.to_hash(self)) }
>              format.html { haml :'load_balancers/show' }
>            end
>          end
> @@ -59,7 +59,7 @@ module Deltacloud::Collections
>            response['Location'] = load_balancer_url(@load_balancer.id)
>            respond_to do |format|
>              format.xml  { haml :"load_balancers/show" }
> -            format.json { xml_to_json("load_balancers/show")}
> +            format.json { JSON::dump(:load_balancer => 
> @load_balancer.to_hash(self)) }
>              format.html { redirect load_balancer_url(@load_balancer.id)}
>            end
>          end
> @@ -73,7 +73,7 @@ module Deltacloud::Collections
>            @load_balancer = driver.load_balancer(credentials, :id => 
> params[:id])
>            respond_to do |format|
>              format.xml { haml :'load_balancers/show' }
> -            format.json { xml_to_json('load_balancers/show') }
> +            format.json { JSON::dump(:load_balancer => 
> @load_balancer.to_hash(self) ) }
>              format.html { redirect load_balancer_url(@load_balancer.id)}
>            end
>          end
> @@ -87,7 +87,7 @@ module Deltacloud::Collections
>            @load_balancer = driver.load_balancer(credentials, :id => 
> params[:id])
>            respond_to do |format|
>              format.xml { haml :'load_balancers/show' }
> -            format.json { xml_to_json('load_balancers/show')}
> +            format.json { JSON::dump(:load_balancer => 
> @load_balancer.to_hash(self)) }
>              format.html { redirect load_balancer_url(@load_balancer.id) }
>            end
>          end
> diff --git a/server/lib/deltacloud/collections/storage_volumes.rb 
> b/server/lib/deltacloud/collections/storage_volumes.rb
> index bc505cb..3af8015 100644
> --- a/server/lib/deltacloud/collections/storage_volumes.rb
> +++ b/server/lib/deltacloud/collections/storage_volumes.rb
> @@ -48,7 +48,7 @@ module Deltacloud::Collections
>            respond_to do |format|
>              format.xml  { haml :"storage_volumes/show" }
>              format.html { haml :"storage_volumes/show" }
> -            format.json { xml_to_json("storage_volumes/show") }
> +            format.json { JSON::dump(:storage_volume => 
> @storage_volume.to_hash(self)) }
>            end
>          end
>        end
> @@ -63,7 +63,7 @@ module Deltacloud::Collections
>            respond_to do |format|
>              format.html { redirect(storage_volume_url(params[:id]))}
>              format.xml  { haml :"storage_volumes/show" }
> -            format.json { xml_to_json("storage_volumes/show")}
> +            format.json { JSON::dump(:storage_volume => 
> @storage_volume.to_hash(self)) }
>            end
>          end
>        end
> @@ -79,7 +79,7 @@ module Deltacloud::Collections
>            respond_to do |format|
>              format.html { redirect(storage_volume_url(params[:id]))}
>              format.xml  { haml :"storage_volumes/show" }
> -            format.json {  xml_to_json("storage_volumes/show") }
> +            format.json { JSON::dump(:storage_volume => 
> @storage_volume.to_hash(self)) }
>            end
>          end
>        end
> diff --git a/server/lib/deltacloud/helpers/deltacloud_helper.rb 
> b/server/lib/deltacloud/helpers/deltacloud_helper.rb
> index 34519c0..b3db12f 100644
> --- a/server/lib/deltacloud/helpers/deltacloud_helper.rb
> +++ b/server/lib/deltacloud/helpers/deltacloud_helper.rb
> @@ -18,6 +18,27 @@ module Deltacloud::Helpers
>  
>      require 'benchmark'
>  
> +    def collections_to_json(collections)
> +      r = {
> +        :version => settings.version,
> +        :driver => driver_symbol,
> +        :provider => Thread.current[:provider] || ENV['API_PROVIDER'],
> +        :links => collections.map { |c|
> +          {
> +            :rel => c.collection_name,
> +            :href => self.send(:"#{c.collection_name}_url"),
> +            :features => c.features.select { |f| 
> driver.class.has_feature?(c.collection_name, f.name) }.map { |f|
> +              f.operations.map { |o|
> +                { :name => f.name, :rel => o.name, :params => o.params_array 
> }
> +              }
> +            }
> +          }
> +        }
> +      }
> +      r[:provider] ||= 'default'
> +      JSON::dump(:api => r)
> +    end
> +
>      def request_headers
>        env.inject({}){|acc, (k,v)| acc[$1.downcase] = v if k =~ 
> /^http_(.*)/i; acc}
>      end
> @@ -48,22 +69,13 @@ module Deltacloud::Helpers
>          respond_to do |format|
>            format.html { haml :"#{model}/index" }
>            format.xml { haml :"#{model}/index" }
> -          format.json { @media_type=:xml; to_json(haml(:"#{model}/index")) }
> +          format.json { JSON::dump({ model => @elements.map { |el| 
> el.to_hash(self) }}) }
>          end
>        else
>          report_error(@exception.respond_to?(:code) ? @exception.code : 500)
>        end
>      end
>  
> -    def xml_to_json(model)
> -      @media_type = :xml
> -      to_json(haml(:"#{model}"))
> -    end
> -
> -    def to_json(xml)
> -      Crack::XML.parse(xml).to_json
> -    end
> -
>      def show(model)
>        @benchmark = Benchmark.measure do
>          @element = driver.send(model, credentials, { :id => params[:id]} )
> @@ -74,7 +86,7 @@ module Deltacloud::Helpers
>          respond_to do |format|
>            format.html { haml :"#{model.to_s.pluralize}/show" }
>            format.xml { haml :"#{model.to_s.pluralize}/show" }
> -          format.json { @media_type=:xml; 
> to_json(haml(:"#{model.to_s.pluralize}/show")) }
> +          format.json { JSON::dump(model => @element.to_hash(self)) }
>          end
>        else
>          report_error(404)
> @@ -109,7 +121,7 @@ module Deltacloud::Helpers
>  
>        respond_to do |format|
>          format.xml {  haml :"errors/#{@code || @error.code}", :layout => 
> false }
> -        format.json { xml_to_json("errors/#{@code || @error.code}") }
> +        format.json { JSON::dump({ :code => @code || @error.code, :message 
> => message, :error => @error.class.name }) }
>          format.html {
>            begin
>              haml :"errors/#{@code || @error.code}", :layout => :error
> @@ -151,7 +163,7 @@ module Deltacloud::Helpers
>          response = respond_to do |format|
>            format.xml { haml :"instances/show" }
>            format.html { haml :"instances/show" }
> -          format.json { xml_to_json("instances/show") }
> +          format.json { JSON::dump(@instance.to_hash(self)) }
>          end
>          halt 202, response
>        end
> diff --git a/server/lib/deltacloud/models/address.rb 
> b/server/lib/deltacloud/models/address.rb
> index 7054a77..f91b856 100644
> --- a/server/lib/deltacloud/models/address.rb
> +++ b/server/lib/deltacloud/models/address.rb
> @@ -25,4 +25,13 @@ class Address < BaseModel
>      !self.instance_id.nil?
>    end
>  
> +  def to_hash(context)
> +    r = {
> +      :id => self.id,
> +      :associated => associated?
> +    }
> +    r[:instance_id] = instance_id if associated?
> +    r
> +  end
> +
>  end
> diff --git a/server/lib/deltacloud/models/blob.rb 
> b/server/lib/deltacloud/models/blob.rb
> index 7dd08b3..a785d6a 100644
> --- a/server/lib/deltacloud/models/blob.rb
> +++ b/server/lib/deltacloud/models/blob.rb
> @@ -24,4 +24,16 @@ class Blob < BaseModel
>    attr_accessor :content
>    attr_accessor :user_metadata
>  
> +  def to_hash(context)
> +    {
> +      :id => self.id,
> +      :bucket => { :rel => :bucket, :href => context.bucket_url(bucket), :id 
> => bucket },
> +      :content_length => content_length,
> +      :content_type => content_type,
> +      :last_modified => last_modified,
> +      :content => content,
> +      :user_metadata => user_metadata
> +    }
> +  end
> +
>  end
> diff --git a/server/lib/deltacloud/models/bucket.rb 
> b/server/lib/deltacloud/models/bucket.rb
> index 0309628..eab4a69 100644
> --- a/server/lib/deltacloud/models/bucket.rb
> +++ b/server/lib/deltacloud/models/bucket.rb
> @@ -24,4 +24,13 @@ class Bucket < BaseModel
>      @blob_list || []
>    end
>  
> +  def to_hash(context)
> +    {
> +      :id => self.id,
> +      :name => name,
> +      :size => size,
> +      :blob_list => blob_list.map { |b| { :rel => :blob, :href => 
> context.url("/buckets/#{self.id}/#{b}"), :id => b }}
> +    }
> +  end
> +
>  end
> diff --git a/server/lib/deltacloud/models/firewall.rb 
> b/server/lib/deltacloud/models/firewall.rb
> index dc0ae3d..c7003b8 100644
> --- a/server/lib/deltacloud/models/firewall.rb
> +++ b/server/lib/deltacloud/models/firewall.rb
> @@ -19,4 +19,16 @@ class Firewall < BaseModel
>    attr_accessor :description
>    attr_accessor :owner_id
>    attr_accessor :rules
> -end
> \ No newline at end of file
> +
> +  def to_hash(context)
> +    r = {
> +      :id => self.id,
> +      :name => name,
> +      :description => description,
> +      :owner_id => owner_id,
> +      :rules => []
> +    }
> +    r[:rules] = rules.map { |rule| rule.to_hash(context) } if rules and 
> !rules.empty?
> +    r
> +  end
> +end
> diff --git a/server/lib/deltacloud/models/firewall_rule.rb 
> b/server/lib/deltacloud/models/firewall_rule.rb
> index 32e8085..078cf74 100644
> --- a/server/lib/deltacloud/models/firewall_rule.rb
> +++ b/server/lib/deltacloud/models/firewall_rule.rb
> @@ -22,4 +22,18 @@ class FirewallRule < BaseModel
>    attr_accessor :direction #ingress egress
>    attr_accessor :rule_action #Accept/Deny - initially added for FGCP
>    attr_accessor :log_rule #log when rule triggered true/false - added for 
> FGCP
> +
> +  def to_hash(context)
> +    {
> +      :id => self.id,
> +      :allow_protocol => allow_protocol,
> +      :port_from => port_from,
> +      :port_to => port_to,
> +      :sources => sources,
> +      :direction => direction,
> +      :rule_action => rule_action,
> +      :log_rule => log_rule
> +    }.delete_if { |k, v| v.nil? }
> +  end
> +
>  end
> diff --git a/server/lib/deltacloud/models/hardware_profile.rb 
> b/server/lib/deltacloud/models/hardware_profile.rb
> index 6b6370b..f2aa937 100644
> --- a/server/lib/deltacloud/models/hardware_profile.rb
> +++ b/server/lib/deltacloud/models/hardware_profile.rb
> @@ -84,6 +84,15 @@ module Deltacloud
>        }.compact
>      end
>  
> +    def to_hash(context)
> +      r = {
> +        :id => self.id,
> +        :name => name,
> +      }
> +      r.merge!({:properties => @properties}) if [email protected]?
> +      r
> +    end
> +
>      class Property
>        attr_reader :name, :kind, :default
>        # kind == :range
> @@ -112,6 +121,10 @@ module Deltacloud
>          @default = opts[:default] if opts[:default]
>        end
>  
> +      def to_s
> +        "#{@default}"
> +      end
> +
>        def unit
>          HardwareProfile.unit(name)
>        end
> diff --git a/server/lib/deltacloud/models/image.rb 
> b/server/lib/deltacloud/models/image.rb
> index 6d6bc9b..11e5a22 100644
> --- a/server/lib/deltacloud/models/image.rb
> +++ b/server/lib/deltacloud/models/image.rb
> @@ -25,4 +25,18 @@ class Image < BaseModel
>    attr_accessor :hardware_profiles
>    attr_accessor :creation_time
>  
> +  def to_hash(context)
> +    {
> +      :id => self.id,
> +      :name => name,
> +      :description => description,
> +      :owner_id => owner_id,
> +      :architecture => architecture,
> +      :state => state,
> +      :hardware_profiles => hardware_profiles.map { |p|
> +        { :id => p.id, :href => context.hardware_profile_url(p.id), :rel => 
> :hardware_profile }
> +      }
> +    }
> +  end
> +
>  end
> diff --git a/server/lib/deltacloud/models/instance.rb 
> b/server/lib/deltacloud/models/instance.rb
> index f65b9b8..87bbb1b 100644
> --- a/server/lib/deltacloud/models/instance.rb
> +++ b/server/lib/deltacloud/models/instance.rb
> @@ -38,6 +38,43 @@ class Instance < BaseModel
>    attr_accessor :firewalls
>    attr_accessor :storage_volumes
>  
> +  def to_hash(context)
> +    r = {
> +      :id => self.id,
> +      :name => name,
> +      :state => state,
> +      :image => { :href => context.image_url(image_id), :id => image_id, 
> :rel => :image },
> +      :realm => { :href => context.realm_url(realm_id), :id => realm_id, 
> :rel => :realm },
> +      :actions => actions.compact.map { |a|
> +        {
> +          :href => context.send("#{a}_instance_url", self.id),
> +          :rel => "#{a}",
> +          :method => context.instance_action_method(a)
> +        }
> +      },
> +      :hardware_profile => {
> +        :id => instance_profile.id,
> +        :href => context.hardware_profile_url(instance_profile.id),
> +        :rel => :hardware_profile,
> +        :properties => instance_profile.overrides
> +      },
> +      :public_addresses => public_addresses.map { |addr| 
> addr.to_hash(context) },
> +      :private_addresses => private_addresses.map { |addr| 
> addr.to_hash(context) }
> +    }
> +    r.merge!(:launch_time => launch_time) if !launch_time.nil?
> +    r.merge!(:firewalls => firewalls.map { |f| { :id => f.id, :href => 
> context.firewall_url(f.id), :rel => :firewall }}) if firewalls
> +    if storage_volumes
> +      r.merge!(:storage_volumes => storage_volumes.map { |f| { :id => f.id, 
> :href => context.storage_volume_url(f.id), :rel => :storage_volume }})
> +    end
> +    if context.driver.class.has_feature?(:instances, :authentication_key)
> +      r.merge!(:authentication => { :keyname => keyname }) if keyname
> +    end
> +    if context.driver.class.has_feature?(:instances, 
> :authentication_password)
> +      r.merge!(:authentication => { :user => username, :password => password 
> }) if user
> +    end
> +    r
> +  end
> +
>    def storage_volumes
>      @storage_volumes || []
>    end
> @@ -51,7 +88,7 @@ class Instance < BaseModel
>    end
>  
>    def hardware_profile=(profile)
> -    instance_profile = profile
> +    @instance_profile = profile
>    end
>  
>    def initialize(init=nil)
> diff --git a/server/lib/deltacloud/models/instance_address.rb 
> b/server/lib/deltacloud/models/instance_address.rb
> index b5c50e3..b0ea9fc 100644
> --- a/server/lib/deltacloud/models/instance_address.rb
> +++ b/server/lib/deltacloud/models/instance_address.rb
> @@ -37,6 +37,15 @@ class InstanceAddress
>      address
>    end
>  
> +  def to_hash(context)
> +    r = {
> +      :address => address,
> +      :type => address_type
> +    }
> +    r.merge!(:port => port) if !port.nil?
> +    r
> +  end
> +
>    def is_mac?
>      address_type == :mac
>    end
> diff --git a/server/lib/deltacloud/models/key.rb 
> b/server/lib/deltacloud/models/key.rb
> index 388c14a..f69ee68 100644
> --- a/server/lib/deltacloud/models/key.rb
> +++ b/server/lib/deltacloud/models/key.rb
> @@ -51,4 +51,19 @@ class Key < BaseModel
>      "-----BEGIN RSA PRIVATE KEY-----\n"+pem_material+"-----END RSA PRIVATE 
> KEY-----"
>    end
>  
> +  def to_hash(context)
> +    r = {
> +      :id => self.id,
> +      :credential_type => credential_type,
> +      :username => username,
> +      :password => password,
> +      :state => state
> +    }
> +    r[:pem_rsa_key] = pem_rsa_key if pem_rsa_key
> +    r[:fingerprint] = fingerprint if fingerprint
> +    r[:username] = username if username
> +    r[:password] = password if password
> +    r
> +  end
> +
>  end
> diff --git a/server/lib/deltacloud/models/load_balancer.rb 
> b/server/lib/deltacloud/models/load_balancer.rb
> index a57e00a..dfd6ae0 100644
> --- a/server/lib/deltacloud/models/load_balancer.rb
> +++ b/server/lib/deltacloud/models/load_balancer.rb
> @@ -27,10 +27,30 @@ class LoadBalancer < BaseModel
>      @listeners << Listener.new(opts)
>    end
>  
> +  def to_hash(context)
> +    {
> +      :id => self.id,
> +      :realms => realms,
> +      :listeners => listeners.map { |l| l.to_hash(context) },
> +      :instances => instances.map { |i| i.to_hash(context) },
> +      :public_addresses => public_addresses,
> +      :created_at => created_at
> +    }
> +  end
> +
>    class Listener < BaseModel
>      attr_accessor :protocol
>      attr_accessor :load_balancer_port
>      attr_accessor :instance_port
> +
> +    def to_hash(context)
> +      {
> +        :id => self.id,
> +        :protocol => protocol,
> +        :load_balancer_port => load_balancer_port,
> +        :instance_port => instance_port
> +      }
> +    end
>    end
>  
>  end
> diff --git a/server/lib/deltacloud/models/metric.rb 
> b/server/lib/deltacloud/models/metric.rb
> index f6f3822..05609d3 100644
> --- a/server/lib/deltacloud/models/metric.rb
> +++ b/server/lib/deltacloud/models/metric.rb
> @@ -29,12 +29,27 @@ class Metric < BaseModel
>      self
>    end
>  
> +  def to_hash(context)
> +    {
> +      :id => self.id,
> +      :entity => entity,
> +      :properties => properties.map { |p| p.to_hash(context) }
> +    }
> +  end
> +
>    class Property
>      attr_accessor :name, :values
>  
>      def initialize(name, values=nil)
>        @name, @values = name, values
>      end
> +
> +    def to_hash(context)
> +      {
> +        :name => name,
> +        :values => values
> +      }
> +    end
>    end
>  
>  end
> diff --git a/server/lib/deltacloud/models/provider.rb 
> b/server/lib/deltacloud/models/provider.rb
> index 1fc2c7d..fcbc81d 100644
> --- a/server/lib/deltacloud/models/provider.rb
> +++ b/server/lib/deltacloud/models/provider.rb
> @@ -24,4 +24,12 @@ class Provider < BaseModel
>      super(opts)
>    end
>  
> +  def to_hash(context)
> +    {
> +      :id => self.id,
> +      :name => name,
> +      :url => url
> +    }
> +  end
> +
>  end
> diff --git a/server/lib/deltacloud/models/realm.rb 
> b/server/lib/deltacloud/models/realm.rb
> index cfad348..75bebed 100644
> --- a/server/lib/deltacloud/models/realm.rb
> +++ b/server/lib/deltacloud/models/realm.rb
> @@ -21,4 +21,12 @@ class Realm < BaseModel
>    attr_accessor :limit
>    attr_accessor :state
>  
> +  def to_hash(context)
> +    {
> +      :id => self.id,
> +      :name => name,
> +      :state => state
> +    }
> +  end
> +
>  end
> diff --git a/server/lib/deltacloud/models/storage_snapshot.rb 
> b/server/lib/deltacloud/models/storage_snapshot.rb
> index ee7e4b8..6af8d47 100644
> --- a/server/lib/deltacloud/models/storage_snapshot.rb
> +++ b/server/lib/deltacloud/models/storage_snapshot.rb
> @@ -26,4 +26,13 @@ class StorageSnapshot < BaseModel
>      state == 'completed'
>    end
>  
> +  def to_hash(context)
> +    {
> +      :id => self.id,
> +      :state => state,
> +      :storage_volume => { :id => storage_volume_id, :href => 
> context.storage_volume_url(storage_volume_id), :rel => :storage_volume },
> +      :created => created
> +    }
> +  end
> +
>  end
> diff --git a/server/lib/deltacloud/models/storage_volume.rb 
> b/server/lib/deltacloud/models/storage_volume.rb
> index 7aa00a9..345df11 100644
> --- a/server/lib/deltacloud/models/storage_volume.rb
> +++ b/server/lib/deltacloud/models/storage_volume.rb
> @@ -28,4 +28,22 @@ class StorageVolume < BaseModel
>    attr_accessor :kind
>    attr_accessor :description # openstack volumes have a display_description 
> attr
>  
> +  def to_hash(context)
> +    r = {
> +      :id => self.id,
> +      :name => name,
> +      :state => state,
> +      :created => created,
> +      :realm => { :id => realm_id, :href => context.realm_url(realm_id), 
> :rel => :realm },
> +      :device => device,
> +      :kind => kind,
> +    }
> +    r[:actions] = actions.map { |a|
> +      { :href => context.send("#{a}_storage_volume", self.id), :rel => a }
> +    } if actions
> +    r[:instance] = { :id => instance_id, :href => 
> context.instance_url(instance_id), :rel => :instance } if instance_id
> +    r.delete_if { |k, v| v.nil? }
> +    r
> +  end
> +
>  end
> diff --git a/server/lib/deltacloud/server.rb b/server/lib/deltacloud/server.rb
> index 7510dba..7c2b7bf 100644
> --- a/server/lib/deltacloud/server.rb
> +++ b/server/lib/deltacloud/server.rb
> @@ -14,7 +14,6 @@
>  # under the License.
>  
>  require 'rubygems'
> -require 'crack'
>  require 'yaml'
>  require 'haml'
>  require 'sinatra/base'
> @@ -51,7 +50,7 @@ module Deltacloud
>        @collections = driver.supported_collections(credentials)
>        respond_to do |format|
>          format.xml { haml :"api/show" }
> -        format.json { xml_to_json :"api/show" }
> +        format.json { collections_to_json(@collections) }
>          format.html { haml :"api/show" }
>        end
>      end
> diff --git a/server/lib/deltacloud_rack.rb b/server/lib/deltacloud_rack.rb
> index b918566..8ddcb41 100644
> --- a/server/lib/deltacloud_rack.rb
> +++ b/server/lib/deltacloud_rack.rb
> @@ -72,7 +72,6 @@ module Deltacloud
>        respond_to do |format|
>          format.xml { haml :'index', :layout => false }
>          format.html { haml :'index', :layout => false }
> -        format.json { xml_to_json "index" }
>        end
>      end
>    end
> diff --git a/server/tests/deltacloud/deltacloud_helper_test.rb 
> b/server/tests/deltacloud/deltacloud_helper_test.rb
> index c0d1d23..095ffc9 100644
> --- a/server/tests/deltacloud/deltacloud_helper_test.rb
> +++ b/server/tests/deltacloud/deltacloud_helper_test.rb
> @@ -27,10 +27,6 @@ describe Deltacloud::Helpers::Application do
>      @helper.instance_action_method(:destroy).must_equal :delete
>    end
>  
> -  it 'provide helper to parse from XML to JSON' do
> -    @helper.to_json('<xml>1</xml>').must_equal '{"xml":"1"}'
> -  end
> -
>    it 'provide helper for wrapping text nodes with CDATA' do
>      @helper.render_cdata('test').must_equal '<![CDATA[test]]>'
>      @helper.render_cdata('').must_equal '<![CDATA[]]>'
> diff --git a/server/tests/deltacloud/server_test.rb 
> b/server/tests/deltacloud/server_test.rb
> index 0f02272..5a7e660 100644
> --- a/server/tests/deltacloud/server_test.rb
> +++ b/server/tests/deltacloud/server_test.rb
> @@ -120,6 +120,7 @@ describe Deltacloud::API do
>      get root_url
>      status.must_equal 200
>      headers['Content-Type'].must_equal 'application/json'
> +    p response_body.inspect
>      JSON::parse(response_body).must_be_kind_of Hash
>      JSON::parse(response_body)['api'].must_be_kind_of Hash
>    end
> -- 
> 1.8.0.2

-- 
Michal Fojtik <[email protected]>
Deltacloud API, CloudForms

Reply via email to