rackspace-cloud 0.5

Sign up to get free protection for your applications and to get access to all the features.
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/rackspace-cloud.rb'}"
9
+ puts "Loading rackspace-cloud gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
data/test/test_base.rb ADDED
@@ -0,0 +1,149 @@
1
+ require File.join(File.dirname(__FILE__), "test_helper")
2
+
3
+ class Rackspace::CloudServers::BaseTest < ActiveSupport::TestCase
4
+ context "querying a resource with Rackspace" do
5
+ should "return the right resource name" do
6
+ mock_auth_response
7
+ assert_equal "bases", Rackspace::CloudServers::Base.resource
8
+ end
9
+
10
+ should "build the right resource URL for the index" do
11
+ mock_auth_response
12
+ assert_equal "http://test/servers/bases", Rackspace::CloudServers::Base.resource_url
13
+ end
14
+
15
+ should "build the right resource URL for the retrieval" do
16
+ mock_auth_response
17
+ assert_equal "http://test/servers/bases/1", Rackspace::CloudServers::Base.resource_url(1)
18
+ end
19
+
20
+ should "make the right request for find(:all)" do
21
+ mock_auth_response
22
+ expects_get("http://test/servers/bases/detail.json")
23
+ Rackspace::CloudServers::Base.find(:all)
24
+ end
25
+
26
+ should "make the right request for find(:first)" do
27
+ mock_auth_response
28
+ expects_get("http://test/servers/bases/detail.json")
29
+ Rackspace::CloudServers::Base.find(:first)
30
+ end
31
+
32
+ should "make the right request for find(:last)" do
33
+ mock_auth_response
34
+ expects_get("http://test/servers/bases/detail.json")
35
+ Rackspace::CloudServers::Base.find(:last)
36
+ end
37
+
38
+ should "make the right request for all and default to find(:all)" do
39
+ mock_auth_response
40
+ expects_get("http://test/servers/bases/detail.json")
41
+ Rackspace::CloudServers::Base.all
42
+ end
43
+
44
+ should "make the right request for count" do
45
+ mock_auth_response
46
+ expects_get("http://test/servers/bases/detail.json")
47
+ assert_equal 0, Rackspace::CloudServers::Base.count
48
+ end
49
+
50
+ should "make the right request for first and default to find(:first)" do
51
+ mock_auth_response
52
+ expects_get("http://test/servers/bases/detail.json")
53
+ Rackspace::CloudServers::Base.first
54
+ end
55
+
56
+ should "make the right request for last and default to find(:last)" do
57
+ mock_auth_response
58
+ expects_get("http://test/servers/bases/detail.json")
59
+ Rackspace::CloudServers::Base.last
60
+ end
61
+
62
+ should "make the right request for find and default to find(:all)" do
63
+ mock_auth_response
64
+ expects_get("http://test/servers/bases/detail.json")
65
+ Rackspace::CloudServers::Base.find
66
+ end
67
+
68
+ should "make the right request for find(1)" do
69
+ mock_auth_response
70
+ expects_get("http://test/servers/bases/1.json")
71
+ Rackspace::CloudServers::Base.find(1)
72
+ end
73
+
74
+ should "return the right data for find(:all)" do
75
+ mock_auth_response
76
+ expects_get("http://test/servers/bases/detail.json").returns({"bases" => [{"id" => 1}, {"id" => 2}]}.to_json)
77
+ result = Rackspace::CloudServers::Base.find(:all)
78
+ assert_equal 2, result.length
79
+ assert_equal Rackspace::CloudServers::Base, result.first.class
80
+ assert_equal 1, result.first.id
81
+ assert_equal false, result.first.new_record?
82
+ assert_equal Rackspace::CloudServers::Base, result.last.class
83
+ assert_equal 2, result.last.id
84
+ assert_equal false, result.last.new_record?
85
+ end
86
+
87
+ should "return the right data for find(1)" do
88
+ mock_auth_response
89
+ expects_get("http://test/servers/bases/1.json").returns({"base" => {"id" => 1}}.to_json)
90
+ result = Rackspace::CloudServers::Base.find(1)
91
+ assert_equal Rackspace::CloudServers::Base, result.class
92
+ assert_equal 1, result.id
93
+ assert_equal false, result.new_record?
94
+ end
95
+ end
96
+
97
+ context "creating a resource with Rackspace" do
98
+ should "be a new record" do
99
+ base = Rackspace::CloudServers::Base.new
100
+ assert_equal true, base.new_record?
101
+ end
102
+
103
+ should "make the right POST request" do
104
+ mock_auth_response
105
+ expects_post("http://test/servers/bases.json", {"base" => {}}).returns({"base" => {"id" => 1}}.to_json)
106
+ base = Rackspace::CloudServers::Base.new
107
+ assert_equal true, base.save
108
+ assert_equal 1, base.id
109
+ end
110
+
111
+ should "make the right POST request on a direct create call too" do
112
+ mock_auth_response
113
+ expects_post("http://test/servers/bases.json", {"base" => {}}).returns({"base" => {"id" => 1}}.to_json)
114
+ base = Rackspace::CloudServers::Base.create
115
+ assert_equal 1, base.id
116
+ end
117
+ end
118
+
119
+ context "updating a resource with Rackspace" do
120
+ should "make the right PUT request" do
121
+ mock_auth_response
122
+ expects_put("http://test/servers/bases/1.json", {"base" => {}})
123
+ assert_equal true, Rackspace::CloudServers::Base.new(:id => 1).save
124
+ end
125
+ end
126
+
127
+ context "deleting a resource with Rackspace" do
128
+ should "make the right DELETE request" do
129
+ mock_auth_response
130
+ expects_delete("http://test/servers/bases/1.json")
131
+ assert_equal true, Rackspace::CloudServers::Base.new(:id => 1).destroy
132
+ end
133
+ end
134
+
135
+ context "reloading a resource with Rackspace" do
136
+ should "not be allowed when it's a new record" do
137
+ assert_equal false, Rackspace::CloudServers::Base.new.reload
138
+ end
139
+
140
+ should "make the GET request for the server" do
141
+ mock_auth_response
142
+ expects_get("http://test/servers/bases/1.json").returns({"base" => {"id" => 2}}.to_json)
143
+ result = Rackspace::CloudServers::Base.new(:id => 1)
144
+ updated = result.reload
145
+ assert_equal 2, updated.id
146
+ assert_equal 2, result.id
147
+ end
148
+ end
149
+ end
@@ -0,0 +1,219 @@
1
+ require File.join(File.dirname(__FILE__), "test_helper")
2
+
3
+ class Rackspace::ConnectionTest < ActiveSupport::TestCase
4
+ context "working with Rackspace::Connection" do
5
+ should "have JSON as the default accept type" do
6
+ assert_equal "application/json", Rackspace::Connection.default_headers[:accept]
7
+ end
8
+
9
+ should "have JSON as the default content type" do
10
+ assert_equal "application/json", Rackspace::Connection.default_headers[:content_type]
11
+ end
12
+ end
13
+
14
+ context "querying for API versions" do
15
+ setup do
16
+ expects_versions_response
17
+ end
18
+
19
+ should "receive the right arguments for the HTTP request" do
20
+ Rackspace::Connection.versions
21
+ end
22
+
23
+ should "return the right data" do
24
+ versions = Rackspace::Connection.versions
25
+ assert_equal 2, versions.length
26
+ assert_equal true, versions.include?("v1.1")
27
+ assert_equal true, versions.include?("v1.0")
28
+ end
29
+ end
30
+
31
+ context "initializing Rackspace::Connection with default version" do
32
+ setup do
33
+ expects_versions_response
34
+ Rackspace::Connection.init "test_user", "test_key"
35
+ end
36
+
37
+ should "have set API user" do
38
+ assert_equal "test_user", Rackspace::Connection.api_user
39
+ end
40
+
41
+ should "have set API key" do
42
+ assert_equal "test_key", Rackspace::Connection.api_key
43
+ end
44
+
45
+ should "have set API version" do
46
+ assert_equal "v1.0", Rackspace::Connection.api_version
47
+ end
48
+ end
49
+
50
+ context "initializing Rackspace::Connection with specific version" do
51
+ setup do
52
+ expects_versions_response
53
+ Rackspace::Connection.init "test_user", "test_key", "v1.1"
54
+ end
55
+
56
+ should "have set API user" do
57
+ assert_equal "test_user", Rackspace::Connection.api_user
58
+ end
59
+
60
+ should "have set API key" do
61
+ assert_equal "test_key", Rackspace::Connection.api_key
62
+ end
63
+
64
+ should "have set API version" do
65
+ assert_equal "v1.1", Rackspace::Connection.api_version
66
+ end
67
+ end
68
+
69
+ context "initializing Rackspace::Connection with invalid version" do
70
+ setup do
71
+ expects_versions_response
72
+ end
73
+
74
+ should "raise an exception" do
75
+ assert_raise Rackspace::InvalidVersion do
76
+ Rackspace::Connection.init "test_user", "test_key", "v1.2"
77
+ end
78
+ end
79
+ end
80
+
81
+ context "before authenticating with Rackspace a call to authentication info" do
82
+ should "trigger an authentication first time ONLY (auth_response)" do
83
+ Rackspace::Connection.instance_variable_set("@auth_response", nil)
84
+ mock_api_init
85
+ expects_authentication
86
+ Rackspace::Connection.auth_response
87
+ Rackspace::Connection.auth_response
88
+ end
89
+
90
+ should "call auth_response to find the right value (auth_token)" do
91
+ response = mock
92
+ response.expects(:[]).with(:auth_token).returns("123456789")
93
+ Rackspace::Connection.expects(:auth_response).returns(response)
94
+ token = Rackspace::Connection.auth_token
95
+ assert_equal "123456789", token
96
+ end
97
+
98
+ should "call auth_response to find the right value (storage_url)" do
99
+ response = mock
100
+ response.expects(:[]).with(:storage_url).returns("http://test/storage")
101
+ Rackspace::Connection.expects(:auth_response).returns(response)
102
+ url = Rackspace::Connection.storage_url
103
+ assert_equal "http://test/storage", url
104
+ end
105
+
106
+ should "call auth_response to find the right value (server_management_url)" do
107
+ response = mock
108
+ response.expects(:[]).with(:server_management_url).returns("http://test/servers")
109
+ Rackspace::Connection.expects(:auth_response).returns(response)
110
+ url = Rackspace::Connection.server_management_url
111
+ assert_equal "http://test/servers", url
112
+ end
113
+
114
+ should "call auth_response to find the right value (cdn_management_url)" do
115
+ response = mock
116
+ response.expects(:[]).with(:cdn_management_url).returns("http://test/content")
117
+ Rackspace::Connection.expects(:auth_response).returns(response)
118
+ url = Rackspace::Connection.cdn_management_url
119
+ assert_equal "http://test/content", url
120
+ end
121
+ end
122
+
123
+ context "authenticating with Rackspace" do
124
+ setup do
125
+ mock_api_init
126
+ expects_authentication
127
+ end
128
+
129
+ should "receive the right arguments for the HTTP request" do
130
+ Rackspace::Connection.authenticate
131
+ end
132
+
133
+ should "return the important authentication details" do
134
+ response = Rackspace::Connection.authenticate
135
+ assert_equal Hash, response.class
136
+ assert_equal 4, response.keys.length
137
+ assert_equal "123456789", response[:auth_token]
138
+ assert_equal "http://test/storage", response[:storage_url]
139
+ assert_equal "http://test/servers", response[:server_management_url]
140
+ assert_equal "http://test/content", response[:cdn_management_url]
141
+ end
142
+ end
143
+
144
+ context "authenticating with Rackspace without initializing" do
145
+ setup do
146
+ Rackspace::Connection.send(:instance_variable_set, "@initialized", false)
147
+ end
148
+
149
+ should "raise an exception" do
150
+ assert_raise Rackspace::NotInitialized do
151
+ Rackspace::Connection.authenticate
152
+ end
153
+ end
154
+ end
155
+
156
+ context "making GET requests to Rackspace" do
157
+ should "build the right GET request" do
158
+ mock_auth_response "123456789"
159
+ expects_get("http://test/url.json", {"X-TestHeader" => "testing"})
160
+ Rackspace::Connection.get "http://test/url", {"X-TestHeader" => "testing"}
161
+ end
162
+
163
+ should "re-authenticate if it receives a 401 Unauthorized back" do
164
+ mock_auth_response "123456789"
165
+ expects_get("http://test/url.json").raises(RestClient::Unauthorized, "RestClient::Unauthorized")
166
+ expects_authentication "234567891"
167
+ expects_get("http://test/url.json", {"X-Auth-Token" => "234567891"})
168
+ Rackspace::Connection.get "http://test/url"
169
+ end
170
+ end
171
+
172
+ context "making POST requests to Rackspace" do
173
+ should "build the right POST request" do
174
+ mock_auth_response "123456789"
175
+ expects_post("http://test/url.json", {:data1 => "test", :data2 => "test"}, {"X-TestHeader" => "testing"})
176
+ Rackspace::Connection.post "http://test/url", {:data1 => "test", :data2 => "test"}, {"X-TestHeader" => "testing"}
177
+ end
178
+
179
+ should "re-authenticate if it receives a 401 Unauthorized back" do
180
+ mock_auth_response "123456789"
181
+ expects_post("http://test/url.json", {:data1 => "test", :data2 => "test"}).raises(RestClient::Unauthorized, "RestClient::Unauthorized")
182
+ expects_authentication "234567891"
183
+ expects_post("http://test/url.json", {:data1 => "test", :data2 => "test"}, {"X-Auth-Token" => "234567891"})
184
+ Rackspace::Connection.post "http://test/url", {:data1 => "test", :data2 => "test"}
185
+ end
186
+ end
187
+
188
+ context "making PUT requests to Rackspace" do
189
+ should "build the right PUT request" do
190
+ mock_auth_response "123456789"
191
+ expects_put("http://test/url.json", {:data1 => "test", :data2 => "test"}, {"X-TestHeader" => "testing"})
192
+ Rackspace::Connection.put "http://test/url", {:data1 => "test", :data2 => "test"}, {"X-TestHeader" => "testing"}
193
+ end
194
+
195
+ should "re-authenticate if it receives a 401 Unauthorized back" do
196
+ mock_auth_response "123456789"
197
+ expects_put("http://test/url.json", {:data1 => "test", :data2 => "test"}).raises(RestClient::Unauthorized, "RestClient::Unauthorized")
198
+ expects_authentication "234567891"
199
+ expects_put("http://test/url.json", {:data1 => "test", :data2 => "test"}, {"X-Auth-Token" => "234567891"})
200
+ Rackspace::Connection.put "http://test/url", {:data1 => "test", :data2 => "test"}
201
+ end
202
+ end
203
+
204
+ context "making DELETE requests to Rackspace" do
205
+ should "build the right DELETE request" do
206
+ mock_auth_response "123456789"
207
+ expects_delete("http://test/url.json", {"X-TestHeader" => "testing"})
208
+ Rackspace::Connection.delete "http://test/url", {"X-TestHeader" => "testing"}
209
+ end
210
+
211
+ should "re-authenticate if it receives a 401 Unauthorized back" do
212
+ mock_auth_response "123456789"
213
+ expects_delete("http://test/url.json").raises(RestClient::Unauthorized, "RestClient::Unauthorized")
214
+ expects_authentication "234567891"
215
+ expects_delete("http://test/url.json", {"X-Auth-Token" => "234567891"})
216
+ Rackspace::Connection.delete "http://test/url"
217
+ end
218
+ end
219
+ end
@@ -0,0 +1,138 @@
1
+ require File.join(File.dirname(__FILE__), "test_helper")
2
+
3
+ class Rackspace::CloudServers::FlavorTest < ActiveSupport::TestCase
4
+ context "working with Rackspace::CloudServers::Flavor" do
5
+ should "inherit from Rackspace::CloudServers::Base" do
6
+ assert Rackspace::CloudServers::Flavor.ancestors.include?(Rackspace::CloudServers::Base)
7
+ end
8
+ end
9
+
10
+ context "an instance of Rackspace::CloudServers::Flavor" do
11
+ setup do
12
+ @flavor = Rackspace::CloudServers::Flavor.new
13
+ end
14
+
15
+ [:id, :name, :ram, :disk].each do |attrib|
16
+ should "respond to #{attrib}" do
17
+ assert @flavor.respond_to?(attrib)
18
+ end
19
+ end
20
+ end
21
+
22
+ context "querying flavors" do
23
+ should "build the right resource URL for the index" do
24
+ mock_auth_response
25
+ assert_equal "http://test/servers/flavors", Rackspace::CloudServers::Flavor.resource_url
26
+ end
27
+
28
+ should "build the right resource URL for the retrieval" do
29
+ mock_auth_response
30
+ assert_equal "http://test/servers/flavors/1", Rackspace::CloudServers::Flavor.resource_url(1)
31
+ end
32
+
33
+ should "make the right request for find(:all)" do
34
+ mock_auth_response
35
+ expects_get("http://test/servers/flavors/detail.json")
36
+ Rackspace::CloudServers::Flavor.find(:all)
37
+ end
38
+
39
+ should "make the right request for find and default to find(:all)" do
40
+ mock_auth_response
41
+ expects_get("http://test/servers/flavors/detail.json")
42
+ Rackspace::CloudServers::Flavor.find
43
+ end
44
+
45
+ should "make the right request for find(1)" do
46
+ mock_auth_response
47
+ expects_get("http://test/servers/flavors/1.json")
48
+ Rackspace::CloudServers::Flavor.find(1)
49
+ end
50
+
51
+ should "return the right data for find(:all)" do
52
+ mock_auth_response
53
+ expects_get("http://test/servers/flavors/detail.json").returns(find_all_response)
54
+ result = Rackspace::CloudServers::Flavor.find(:all)
55
+ assert_equal 2, result.length
56
+ assert_equal Rackspace::CloudServers::Flavor, result.first.class
57
+ assert_equal 1, result.first.id
58
+ assert_equal Rackspace::CloudServers::Flavor, result.last.class
59
+ assert_equal 2, result.last.id
60
+ end
61
+
62
+ should "return the right data for find(1)" do
63
+ mock_auth_response
64
+ expects_get("http://test/servers/flavors/1.json").returns(find_1_response)
65
+ result = Rackspace::CloudServers::Flavor.find(1)
66
+ assert_equal Rackspace::CloudServers::Flavor, result.class
67
+ assert_equal 1, result.id
68
+ end
69
+ end
70
+
71
+ context "creating a flavor" do
72
+ should "return false as it's read-only" do
73
+ assert_equal false, Rackspace::CloudServers::Flavor.new(:name => "test").save
74
+ end
75
+ end
76
+
77
+ context "deleting a flavor" do
78
+ should "return false as it's read-only" do
79
+ assert_equal false, Rackspace::CloudServers::Flavor.new(:id => 1).destroy
80
+ end
81
+ end
82
+
83
+ context "reloading a flavor instance" do
84
+ should "not be allowed when it's a new record" do
85
+ assert_equal false, Rackspace::CloudServers::Flavor.new.reload
86
+ end
87
+
88
+ should "make the GET request for the flavor" do
89
+ mock_auth_response
90
+ expects_get("http://test/servers/flavors/1.json").returns(find_1_response_reloaded)
91
+ result = Rackspace::CloudServers::Flavor.new(:id => 1)
92
+ updated = result.reload
93
+ assert_equal 100, updated.disk
94
+ assert_equal 100, result.disk
95
+ end
96
+ end
97
+
98
+ def find_all_response
99
+ {
100
+ "flavors" => [
101
+ {
102
+ "id" => 1,
103
+ "name" => "256 MB Server",
104
+ "ram" => 256,
105
+ "disk" => 10
106
+ },
107
+ {
108
+ "id" => 2,
109
+ "name" => "512 MB Server",
110
+ "ram" => 512,
111
+ "disk" => 20
112
+ }
113
+ ]
114
+ }.to_json
115
+ end
116
+
117
+ def find_1_response
118
+ {
119
+ "flavor" => {
120
+ "id" => 1,
121
+ "name" => "256 MB Server",
122
+ "ram" => 256,
123
+ "disk" => 10
124
+ }
125
+ }.to_json
126
+ end
127
+
128
+ def find_1_response_reloaded
129
+ {
130
+ "flavor" => {
131
+ "id" => 1,
132
+ "name" => "256 MB Server",
133
+ "ram" => 256,
134
+ "disk" => 100
135
+ }
136
+ }.to_json
137
+ end
138
+ end