Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

reading

  • Loading branch information...
commit 6b6372c4300e70a1a087cdd1983c757e58e0ef98 1 parent a346f91
@paul authored
View
4 Rakefile
@@ -10,6 +10,10 @@ begin
gem.homepage = "http://github.com/paul/rackcloud"
gem.authors = ["Paul Sadauskas"]
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
+
+ gem.dependency "datapathy"
+ gem.dependency "resourceful"
+
end
rescue LoadError
View
93 lib/datapathy/adapter.rb
@@ -0,0 +1,93 @@
+
+module RackCloud
+
+ class DatapathyRackCloudAdapter < Datapathy::Adapters::AbstractAdapter
+
+ attr_reader :http,
+ :token, :storage_token,
+ :urls, :auth_url,
+ :user, :key
+
+ def initialize(options = {})
+ super
+
+ @user, @key = options[:user], options[:key]
+
+ @auth_url = options[:auth_url] || "https://auth.api.rackspacecloud.com/v1.0"
+ @urls = {:auth => @auth_url}
+
+ @http = Resourceful::HttpAccessor.new
+ @http.logger = Resourceful::StdOutLogger.new
+ @http.cache_manager = Resourceful::InMemoryCacheManager.new
+ get_auth_token!
+ end
+
+ def read(query)
+ response = resource(query.model, true).get(token_header.merge(:content_type => 'application/json'))
+ records = parse_response(response)[root_name(query.model)]
+ query.filter_records(records)
+ end
+
+ protected
+
+ def parse_response(response)
+ JSON.parse(response.body)
+ end
+
+ def base_url(model)
+ case model.to_s
+ when "Server", "Flavor", "Image", "SharedIpGroup"
+ urls[:server_management]
+ else
+ raise ArgumentError, "Unknown model #{model}"
+ end
+ end
+
+ def location(model)
+ model.to_s.underscore.pluralize
+ end
+
+ def root_name(model)
+ name = model.to_s.pluralize
+ name[0] = name[0].chr.downcase![0]
+ name
+ end
+
+ def resource(model, detail = false)
+ url = "#{base_url(model)}/#{location(model)}#{detail ? "/detail" : ""}"
+ http.resource(url)
+ end
+
+ def get_auth_token!
+ response = request_auth
+
+ @token = response.header['X-Auth-Token'].first
+ @storage_token = response.header['X-Storage-Token'].first
+
+ urls[:storage] = response.header['X-Storage-Url'].first
+ urls[:server_management] = response.header['X-Server-Management-Url'].first
+ urls[:cdn_management_uri] = response.header['X-CDN-Management-Url'].first
+ end
+
+ def request_auth
+ http.resource(auth_url).get(auth_headers)
+ end
+
+ def auth_headers
+ {
+ 'X-Auth-User' => user,
+ 'X-Auth-Key' => key
+ }
+ end
+
+ def token_header
+ {
+ 'X-Auth-Token' => token
+ }
+ end
+
+
+ end
+end
+
+
View
22 lib/datapathy/model_extensions.rb
@@ -0,0 +1,22 @@
+
+module RackCloud::Datapathy
+ module ModelExtensions
+
+ def self.included(klass)
+ klass.extend(ClassMethods)
+ end
+
+ module ClassMethods
+
+ def base_url(base_url)
+ if base_url
+ @base_url = base_url
+ else
+ @base_url
+ end
+ end
+
+ end
+
+ end
+end
View
21 lib/rackcloud.rb
@@ -0,0 +1,21 @@
+
+module RackCloud
+
+end
+
+$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + "/../../datapathy/lib"))
+require 'datapathy'
+
+$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + "/../../../../personal/resourceful/lib"))
+require 'resourceful'
+
+require 'json'
+
+require File.join(File.dirname(__FILE__), 'datapathy', 'adapter')
+require File.join(File.dirname(__FILE__), 'rackcloud', 'models', 'server')
+
+require 'yaml'
+config = YAML.load_file(File.expand_path("~/.rackcloud.yml"))
+user, key = config["user"], config["key"]
+
+Datapathy.default_adapter = RackCloud::DatapathyRackCloudAdapter.new(:user => user, :key => key)
View
7 lib/rackcloud/models/server.rb
@@ -0,0 +1,7 @@
+
+class Server
+ include Datapathy::Model
+
+ persists :id, :name, :imageId, :flavorId, :hostId, :status, :metadata, :addresses, :progress
+
+end
View
8 test.rb
@@ -0,0 +1,8 @@
+
+require 'rubygems'
+
+require File.dirname(__FILE__) + "/lib/rackcloud"
+
+require 'pp'
+
+pp Server.all
Please sign in to comment.
Something went wrong with that request. Please try again.