Skip to content
Browse files

Merge branch 'master' of github.com:fog/fog into auth20

  • Loading branch information...
2 parents ea9a39b + fd126aa commit 1b708e05a5a2c30e58ddc336f6d757cfd77b5fe3 Kyle Rames committed Feb 22, 2013
View
2 lib/fog/joyent/models/compute/server.rb
@@ -53,7 +53,7 @@ def stop
def resize(flavor)
requires :id
- service.resize_machine(id, flavor)
+ service.resize_machine(id, flavor.name)
true
end
View
2 lib/fog/joyent/requests/compute/resize_machine.rb
@@ -1,13 +1,15 @@
module Fog
module Compute
class Joyent
+ class Real
def resize_machine(id, package)
request(
:method => "POST",
:path => "/my/machines/#{id}",
:query => {"action" => "resize", "package" => package}
)
end
+ end
end
end
end
View
19 lib/fog/rackspace/mock_data.rb
@@ -1,6 +1,9 @@
module Fog
module Rackspace
module MockData
+
+ NOT_FOUND_ID = "NOT-FOUND"
+
def data
@@data ||= Hash.new do |hash, key|
hash[key] = begin
@@ -101,19 +104,27 @@ def data
}
#Mock Data Hash
- {
+ h = {
#Compute V2
- :flavors => {flavor_id => flavor},
- :images => {image_id => image},
+ :flavors => Hash.new { |h,k| h[k] = flavor unless k == NOT_FOUND_ID},
+ :images => Hash.new { |h,k| h[k] = image unless k == NOT_FOUND_ID },
+ :networks => Hash.new { |h,k| h[k] = network unless k == NOT_FOUND_ID },
+
:servers => {},
- :networks => { network_id => network },
#Block Storage
:volumes => {},
:snapshots => {},
:volume_attachments => [],
:volume_types => {volume_type1_id => volume_type1, volume_type2_id => volume_type2},
}
+
+ # seed with initial data
+ h[:flavors][flavor_id] = flavor
+ h[:images][image_id] = image
+ h[:networks][network_id] = network
+
+ h
end
end[@rackspace_api_key]
end
View
2 lib/fog/rackspace/models/compute_v2/servers.rb
@@ -19,6 +19,8 @@ def all
end
# Creates a new server and populates ssh keys
+ # @note This method is incompatible with Cloud Servers utlizing RackConnect. RackConnect users
+ # should use server personalization to install keys. Please see Server#personality for more information.
# @example
# service = Fog::Compute.new(:provider => 'rackspace',
# :version => :v2,
View
3 lib/fog/rackspace/models/storage/directories.rb
@@ -26,8 +26,11 @@ def get(key, options = {})
directory.merge_attributes(key => value)
end
end
+
+ directory.metadata = Metadata.from_headers(data.headers)
directory.files.merge_attributes(options)
directory.files.instance_variable_set(:@loaded, true)
+
data.body.each do |file|
directory.files << directory.files.new(file)
end
View
25 lib/fog/rackspace/models/storage/directory.rb
@@ -1,5 +1,6 @@
require 'fog/core/model'
require 'fog/rackspace/models/storage/files'
+require 'fog/rackspace/models/storage/metadata'
module Fog
module Storage
@@ -9,10 +10,27 @@ class Directory < Fog::Model
identity :key, :aliases => 'name'
- attribute :bytes, :aliases => 'X-Container-Bytes-Used'
- attribute :count, :aliases => 'X-Container-Object-Count'
+ attribute :bytes, :aliases => 'X-Container-Bytes-Used', :type => :integer
+ attribute :count, :aliases => 'X-Container-Object-Count', :type => :integer
attribute :cdn_cname
+ def metadata=(hash)
+ if hash.is_a? Fog::Storage::Rackspace::Metadata
+ @metadata = hash
+ else
+ @metadata = Fog::Storage::Rackspace::Metadata.new(hash)
+ end
+ @metadata
+ end
+
+ def metadata
+ unless @metadata
+ response = service.head_container(key)
+ @metadata = Fog::Storage::Rackspace::Metadata.from_headers(response.headers)
+ end
+ @metadata
+ end
+
def destroy
requires :key
service.delete_container(key)
@@ -58,7 +76,8 @@ def public_url
def save
requires :key
- service.put_container(key)
+ headers = @metadata.nil? ? {} : metadata.to_headers
+ service.put_container(key, headers)
if service.cdn && public?
# if public and CDN connection then update cdn to public
View
79 lib/fog/rackspace/models/storage/metadata.rb
@@ -0,0 +1,79 @@
+module Fog
+ module Storage
+ class Rackspace
+
+ class Metadata
+
+ # META_PREFIX = "X-Object-Meta-"
+ # REMOVE_META_PREFIX = "X-Remove-Object-Meta-"
+ META_PREFIX = "X-Container-Meta-"
+ REMOVE_META_PREFIX = "X-Remove-Container-Meta-"
+
+ # Cloud Files will ignore headers without a value
+ DUMMY_VALUE = 1
+
+ KEY_REGEX = /^#{META_PREFIX}(.*)/
+
+ attr_reader :data
+
+ def initialize(hash={})
+ @data = hash || {}
+ @deleted_hash = {}
+ end
+
+ def delete(key)
+ data.delete(key)
+ @deleted_hash[key] = nil
+ end
+
+ def to_headers
+ headers = {}
+ h = data.merge(@deleted_hash)
+ h.each_pair do |k,v|
+ key = to_header_key(k,v)
+ headers[key] = v || DUMMY_VALUE
+ end
+
+ headers
+ end
+
+ def self.from_headers(headers)
+ metadata = Metadata.new
+ headers.each_pair do |k, v|
+ key = Metadata.to_key(k)
+ next unless key
+ metadata.data[key] = Fog::JSON.decode(v)
+ end
+ metadata
+ end
+
+ def respond_to?(method_sym, include_private = false)
+ super(method_sym, include_private) || data.respond_to?(method_sym, include_private)
+ end
+
+ def method_missing(method, *args, &block)
+ data.send(method, *args, &block)
+ end
+
+ private
+
+ def self.to_key(key)
+ m = key.match KEY_REGEX
+ return nil unless m && m[1]
+
+ a = m[1].split('-')
+ a.collect!(&:downcase)
+ str = a.join('_')
+ str.to_sym
+ end
+
+ def to_header_key(key, value)
+ prefix = value.nil? ? REMOVE_META_PREFIX : META_PREFIX
+ prefix + key.to_s.split(/[-_]/).collect(&:capitalize).join('-')
+ end
+
+ end
+
+ end
+ end
+end
View
3 lib/fog/rackspace/requests/storage/put_container.rb
@@ -8,10 +8,11 @@ class Real
# ==== Parameters
# * name<~String> - Name for container, should be < 256 bytes and must not contain '/'
#
- def put_container(name)
+ def put_container(name, options={})
request(
:expects => [201, 202],
:method => 'PUT',
+ :headers => options,
:path => Fog::Rackspace.escape(name)
)
end
View
2 tests/core/user_agent_tests.rb
@@ -1,6 +1,6 @@
Shindo.tests('Fog::Connection', 'core') do
tests('user_agent').returns("fog/#{Fog::VERSION}") do
conn = Fog::Connection.new("http://www.testserviceurl.com", false, {})
- conn.instance_variable_get(:@excon).connection[:headers]['User-Agent']
+ conn.instance_variable_get(:@excon).data[:headers]['User-Agent']
end
end
View
2 tests/rackspace/models/compute_v2/flavors_tests.rb
@@ -12,6 +12,6 @@
end
tests("failure").returns(nil) do
- service.flavors.get('some_random_identity')
+ service.flavors.get(Fog::Rackspace::MockData::NOT_FOUND_ID)
end
end
View
2 tests/rackspace/models/compute_v2/images_tests.rb
@@ -15,6 +15,6 @@
end
tests("failure").returns(nil) do
- service.images.get('some_random_identity')
+ service.images.get(Fog::Rackspace::MockData::NOT_FOUND_ID)
end
end
View
29 tests/rackspace/models/storage/directories_tests.rb
@@ -0,0 +1,29 @@
+Shindo.tests('Fog::Rackspace::Storage | directories', ['rackspace']) do
+
+ pending if Fog.mocking?
+
+ @service = Fog::Storage[:rackspace]
+
+
+ begin
+ @name = "fog-directories-test-#{Time.now.to_i.to_s}"
+ @filename = 'lorem.txt'
+ @dir = @service.directories.create :key => @name, :metadata => {:fog_test => true}
+ @file = @dir.files.create :key => @filename, :body => lorem_file
+
+ tests('#get').succeeds do
+ instance = @service.directories.get @name
+ returns(false) { instance.nil? }
+ returns(true) { instance.metadata[:fog_test] }
+ returns(@name) { instance.key }
+ returns(1) { instance.count }
+ returns( Fog::Storage.get_body_size(lorem_file)) {instance.bytes }
+ returns(@filename) { instance.files.first.key }
+ end
+
+ ensure
+ @file.destroy if @file
+ @dir.destroy if @dir
+ end
+
+end
View
56 tests/rackspace/models/storage/directory_tests.rb
@@ -0,0 +1,56 @@
+Shindo.tests('Fog::Rackspace::Storage | directory', ['rackspace']) do
+
+ pending if Fog.mocking?
+
+ @service = Fog::Storage[:rackspace]
+
+ def container_meta_attributes
+ @service.head_container(@instance.key).headers.reject {|k, v| !(k =~ /X-Container-Meta-/)}
+ end
+
+ directory_attributes = {
+ # Add a random suffix to prevent collision
+ :key => "fog-directory-tests-#{rand(65536)}"
+ }
+
+ model_tests(@service.directories, directory_attributes, Fog.mocking?) do
+ tests('#public_url').returns(nil) do
+ @instance.public_url
+ end
+ end
+
+ directory_attributes[:metadata] = {:draft => 'true'}
+
+ tests('metadata') do
+ pending if Fog.mocking?
+ model_tests(@service.directories, directory_attributes, Fog.mocking?) do
+ tests('sets metadata on create').returns({:draft => 'true'}) do
+ @instance.metadata.data
+ end
+ tests('update metadata').returns({"X-Container-Meta-Draft"=>"true", "X-Container-Meta-Color"=>"green"}) do
+ @instance.metadata[:color] = 'green'
+ @instance.save
+ container_meta_attributes
+ end
+ tests('set metadata to nil').returns({"X-Container-Meta-Draft"=>"true"}) do
+ @instance.metadata[:color] = nil
+ @instance.save
+ container_meta_attributes
+ end
+ tests('delete metadata').returns({}) do
+ @instance.metadata.delete(:draft)
+ @instance.save
+ container_meta_attributes
+ end
+
+ tests('should retrieve metadata when necessary') do
+ @service.put_container(@instance.key, {"X-Container-Meta-List-Test"=>"true"} )
+ dir = @service.directories.find {|d| d.key == @instance.key }
+ returns(nil) { dir.instance_variable_get("@metadata") }
+ returns(true) { dir.metadata[:list_test] }
+ end
+
+ end
+ end
+
+end
View
68 tests/rackspace/models/storage/metadata_tests.rb
@@ -0,0 +1,68 @@
+require 'fog/rackspace/models/storage/metadata'
+
+Shindo.tests('Fog::Rackspace::Storage | metadata', ['rackspace']) do
+
+ tests('#to_key') do
+ tests('valid key').returns(:image_size) do
+ Fog::Storage::Rackspace::Metadata.send(:to_key, "X-Container-Meta-Image-Size")
+ end
+ tests('invalid key').returns(nil) do
+ Fog::Storage::Rackspace::Metadata.send(:to_key, "bad-key")
+ end
+ end
+
+ tests('#to_header_key') do
+ metadata = Fog::Storage::Rackspace::Metadata.new
+
+ tests('key to add').returns("X-Container-Meta-Thumbnail-Image") do
+ metadata.send(:to_header_key, :thumbnail_image, true)
+ end
+
+ tests('key to remove').returns("X-Remove-Container-Meta-Thumbnail-Image") do
+ metadata.send(:to_header_key, :thumbnail_image, nil)
+ end
+ end
+
+ tests('#to_headers').returns({"X-Container-Meta-Preview"=>true, "X-Remove-Container-Meta-Delete-Me"=>1}) do
+ metadata = Fog::Storage::Rackspace::Metadata.new
+ metadata[:preview] = true
+ metadata[:delete_me] = nil
+
+ metadata.to_headers
+ end
+
+ tests("#from_headers").returns({:my_boolean=>true, :my_integer=>42, :my_string=>"I am a string"}) do
+ headers = {
+ "X-Container-Meta-My-Integer"=> "42",
+ "X-Container-Meta-My-Boolean"=> "true",
+ "X-Container-Meta-My-String"=> "\"I am a string\""
+ }
+
+ metadata = Fog::Storage::Rackspace::Metadata.from_headers headers
+ metadata.data
+ end
+
+ tests("#delete").returns({"X-Remove-Container-Meta-Delete-Me"=>1}) do
+ metadata = Fog::Storage::Rackspace::Metadata.new
+ metadata.delete(:delete_me)
+
+ metadata.to_headers
+ end
+
+ tests("#respond_to?") do
+ tests('Should respond to all of the methods in Hash class').returns(true) do
+ metadata = Fog::Storage::Rackspace::Metadata.new
+ Hash.instance_methods.all? {|method| metadata.respond_to?(method)}
+ end
+ tests('Should respond to all of the methods in the Metadata class').returns(true) do
+ metadata = Fog::Storage::Rackspace::Metadata.new
+ metadata.methods.all? {|method| metadata.respond_to?(method)}
+ end
+ end
+
+ tests("#method_missing").returns(true) do
+ metadata = Fog::Storage::Rackspace::Metadata.new
+ metadata[:test] = true
+ metadata[:test]
+ end
+end
View
4 tests/rackspace/requests/compute/image_tests.rb
@@ -57,12 +57,12 @@
tests('failure') do
tests('#delete_image(0)').raises(Excon::Errors::BadRequest) do
- @service.delete_image(0)
+ @service.delete_image(Fog::Rackspace::MockData::NOT_FOUND_ID)
end
tests('#get_image_details(0)').raises(Fog::Compute::Rackspace::NotFound) do
pending if Fog.mocking?
- @service.get_image_details(0)
+ @service.get_image_details(Fog::Rackspace::MockData::NOT_FOUND_ID)
end
end
View
4 tests/rackspace/requests/compute_v2/image_tests.rb
@@ -59,11 +59,11 @@
tests('failure') do
tests('#delete_image').raises(Excon::Errors::BadRequest) do
- Fog::Compute[:rackspace].delete_image(0)
+ Fog::Compute[:rackspace].delete_image(Fog::Rackspace::MockData::NOT_FOUND_ID)
end
tests('#get_image').raises(Fog::Compute::RackspaceV2::NotFound) do
- service.get_image(0)
+ service.get_image(Fog::Rackspace::MockData::NOT_FOUND_ID)
end
end
ensure
View
9 tests/rackspace/requests/storage/container_tests.rb
@@ -10,11 +10,18 @@
tests('success') do
- tests("#put_container('fogcontainertests')").succeeds do
+ tests("#put_container('fogcontainertests', {})").succeeds do
pending if Fog.mocking?
Fog::Storage[:rackspace].put_container('fogcontainertests')
end
+ tests("#put_container('fogcontainertests', 'X-Container-Meta-Color'=>'green')").succeeds do
+ pending if Fog.mocking?
+ Fog::Storage[:rackspace].put_container('fogcontainertests', 'X-Container-Meta-Color'=>'green')
+ response = Fog::Storage[:rackspace].head_container('fogcontainertests')
+ returns('green') { response.headers['X-Container-Meta-Color'] }
+ end
+
tests("#get_container('fogcontainertests')").formats(@container_format) do
pending if Fog.mocking?
Fog::Storage[:rackspace].get_container('fogcontainertests').body

0 comments on commit 1b708e0

Please sign in to comment.
Something went wrong with that request. Please try again.