incubator-deltacloud-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lut...@apache.org
Subject svn commit: r962003 - in /incubator/deltacloud/trunk/client-ruby: Rakefile lib/ lib/deltacloud.rb specs/ specs/discovery_spec.rb specs/initialization_spec.rb specs/spec_helper.rb
Date Thu, 08 Jul 2010 23:16:12 GMT
Author: lutter
Date: Thu Jul  8 23:16:12 2010
New Revision: 962003

URL: http://svn.apache.org/viewvc?rev=962003&view=rev
Log:
Implement entry-point discover in client.

Added:
    incubator/deltacloud/trunk/client-ruby/Rakefile
    incubator/deltacloud/trunk/client-ruby/lib/
    incubator/deltacloud/trunk/client-ruby/lib/deltacloud.rb
    incubator/deltacloud/trunk/client-ruby/specs/
    incubator/deltacloud/trunk/client-ruby/specs/discovery_spec.rb
    incubator/deltacloud/trunk/client-ruby/specs/initialization_spec.rb
    incubator/deltacloud/trunk/client-ruby/specs/spec_helper.rb

Added: incubator/deltacloud/trunk/client-ruby/Rakefile
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/client-ruby/Rakefile?rev=962003&view=auto
==============================================================================
--- incubator/deltacloud/trunk/client-ruby/Rakefile (added)
+++ incubator/deltacloud/trunk/client-ruby/Rakefile Thu Jul  8 23:16:12 2010
@@ -0,0 +1,8 @@
+
+require 'spec/rake/spectask'
+
+
+desc "Run all examples"
+Spec::Rake::SpecTask.new('spec') do |t|
+  t.spec_files = FileList['specs/**/*_spec.rb']
+end

Added: incubator/deltacloud/trunk/client-ruby/lib/deltacloud.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/client-ruby/lib/deltacloud.rb?rev=962003&view=auto
==============================================================================
--- incubator/deltacloud/trunk/client-ruby/lib/deltacloud.rb (added)
+++ incubator/deltacloud/trunk/client-ruby/lib/deltacloud.rb Thu Jul  8 23:16:12 2010
@@ -0,0 +1,66 @@
+require 'uri'
+require 'net/http'
+require 'logger'
+require 'rexml/document'
+
+class DeltaCloud
+
+  attr_accessor :logger
+  attr_reader :api_uri
+  attr_reader :entry_points
+
+  def initialize(name, password, api_uri, &block)
+    @logger       = Logger.new( STDERR ) 
+    @name         = name
+    @password     = password
+    @api_uri      = URI.parse( api_uri )
+    @entry_points = {}
+    connect( &block ) if ( block )
+  end
+
+  def connect(&block)
+    @http = Net::HTTP.new( api_host, api_port )
+    discover_entry_points
+    block.call( self ) if block
+  end
+
+  def api_host
+    @api_uri.host
+  end
+
+  def api_port
+    @api_uri.port
+  end
+
+  def api_path
+    @api_uri.path
+  end
+
+  private
+
+  attr_reader :http
+
+  def discover_entry_points()
+    @entry_points = {}
+    logger << "Discoverying entry points at #{@api_uri}"
+    request do |response|
+      if ( response.is_a?( Net::HTTPSuccess ) )
+        doc = REXML::Document.new( response.body )
+        doc.get_elements( 'api/link' ).each do |link|
+          rel = link.attributes['rel']
+          uri = link.text
+          @entry_points[rel.to_sym] = uri
+        end
+
+      end
+    end
+  end
+
+  def request(method=:get, path='', &block)
+    request = eval( "Net::HTTP::#{method.to_s.capitalize}" ).new( api_path + path )
+    request.basic_auth( @name, @password )
+    request['Accept'] = 'text/xml'
+    http.request( request, &block )
+  end
+
+end

Added: incubator/deltacloud/trunk/client-ruby/specs/discovery_spec.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/client-ruby/specs/discovery_spec.rb?rev=962003&view=auto
==============================================================================
--- incubator/deltacloud/trunk/client-ruby/specs/discovery_spec.rb (added)
+++ incubator/deltacloud/trunk/client-ruby/specs/discovery_spec.rb Thu Jul  8 23:16:12 2010
@@ -0,0 +1,10 @@
+
+describe "discovering the API entry points" do
+
+  it "should be able to locate the flavors collection"
+  it "should be able to locate the images collection"
+  it "should be able to locate the instances collection"
+  it "should be able to locate the storage volumes collection"
+  it "should be able to locate the storage snapshots collection"
+
+end

Added: incubator/deltacloud/trunk/client-ruby/specs/initialization_spec.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/client-ruby/specs/initialization_spec.rb?rev=962003&view=auto
==============================================================================
--- incubator/deltacloud/trunk/client-ruby/specs/initialization_spec.rb (added)
+++ incubator/deltacloud/trunk/client-ruby/specs/initialization_spec.rb Thu Jul  8 23:16:12
2010
@@ -0,0 +1,23 @@
+require 'specs/spec_helper'
+
+describe "initializing the client" do
+
+  it "should parse valid API URIs" do
+    client = DeltaCloud.new( "name", "password", API_URL )
+    client.api_host.should eql( "localhost" )
+    client.api_port.should eql( 3000 )
+    client.api_path.should eql( "/api" )
+  end
+
+  it "should discover entry points upon connection" do
+    client = DeltaCloud.new( "name", "password", API_URL )
+    client.connect do |client|
+      client.entry_points[:flavors].should           eql( "#{API_URL}/flavors" )
+      client.entry_points[:images].should            eql( "#{API_URL}/images" )
+      client.entry_points[:instances].should         eql( "#{API_URL}/instances" )
+      client.entry_points[:storage_volumes].should   eql( "#{API_URL}/storage/volumes" )
+      client.entry_points[:storage_snapshots].should eql( "#{API_URL}/storage/snapshots"
)
+    end
+  end
+
+end

Added: incubator/deltacloud/trunk/client-ruby/specs/spec_helper.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/client-ruby/specs/spec_helper.rb?rev=962003&view=auto
==============================================================================
--- incubator/deltacloud/trunk/client-ruby/specs/spec_helper.rb (added)
+++ incubator/deltacloud/trunk/client-ruby/specs/spec_helper.rb Thu Jul  8 23:16:12 2010
@@ -0,0 +1,9 @@
+
+API_HOST = 'localhost'
+API_PORT = 3000
+API_PATH = '/api'
+
+API_URL = "http://#{API_HOST}:#{API_PORT}#{API_PATH}"
+
+$: << File.dirname( __FILE__ ) + '/../lib'
+require 'deltacloud'



Mime
View raw message