softlayer_api 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE.textile +9 -0
- data/README.textile +251 -0
- data/examples/accountInformation.rb +41 -0
- data/examples/createTicket.rb +57 -0
- data/examples/openTickets.rb +52 -0
- data/examples/ticket_info.rb +52 -0
- data/lib/softlayer/base.rb +61 -0
- data/lib/softlayer/object_mask_helpers.rb +48 -0
- data/lib/softlayer/service.rb +407 -0
- data/lib/softlayer_api.rb +29 -0
- data/test/SoftLayer_APIParameterFilter.rb +81 -0
- data/test/SoftLayer_Service.rb +376 -0
- data/test/SoftLayer_ToObjectMask.rb +83 -0
- metadata +76 -0
@@ -0,0 +1,29 @@
|
|
1
|
+
# Copyright (c) 2010, SoftLayer Technologies, Inc. All rights reserved.
|
2
|
+
#
|
3
|
+
# Redistribution and use in source and binary forms, with or without
|
4
|
+
# modification, are permitted provided that the following conditions are met:
|
5
|
+
#
|
6
|
+
# * Redistributions of source code must retain the above copyright notice,
|
7
|
+
# this list of conditions and the following disclaimer.
|
8
|
+
# * Redistributions in binary form must reproduce the above copyright notice,
|
9
|
+
# this list of conditions and the following disclaimer in the documentation
|
10
|
+
# and/or other materials provided with the distribution.
|
11
|
+
# * Neither SoftLayer Technologies, Inc. nor the names of its contributors may
|
12
|
+
# be used to endorse or promote products derived from this software without
|
13
|
+
# specific prior written permission.
|
14
|
+
#
|
15
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
16
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
17
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
18
|
+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
19
|
+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
20
|
+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
21
|
+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
22
|
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
23
|
+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
24
|
+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
25
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
26
|
+
|
27
|
+
require 'softlayer/base'
|
28
|
+
require 'softlayer/object_mask_helpers'
|
29
|
+
require 'softlayer/service'
|
@@ -0,0 +1,81 @@
|
|
1
|
+
# Copyright (c) 2010, SoftLayer Technologies, Inc. All rights reserved.
|
2
|
+
#
|
3
|
+
# Redistribution and use in source and binary forms, with or without
|
4
|
+
# modification, are permitted provided that the following conditions are met:
|
5
|
+
#
|
6
|
+
# * Redistributions of source code must retain the above copyright notice,
|
7
|
+
# this list of conditions and the following disclaimer.
|
8
|
+
# * Redistributions in binary form must reproduce the above copyright notice,
|
9
|
+
# this list of conditions and the following disclaimer in the documentation
|
10
|
+
# and/or other materials provided with the distribution.
|
11
|
+
# * Neither SoftLayer Technologies, Inc. nor the names of its contributors may
|
12
|
+
# be used to endorse or promote products derived from this software without
|
13
|
+
# specific prior written permission.
|
14
|
+
#
|
15
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
16
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
17
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
18
|
+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
19
|
+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
20
|
+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
21
|
+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
22
|
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
23
|
+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
24
|
+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
25
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
26
|
+
|
27
|
+
$: << File.expand_path(File.join(File.dirname(__FILE__), "../lib"))
|
28
|
+
|
29
|
+
require 'rubygems'
|
30
|
+
require 'softlayer_api'
|
31
|
+
require 'spec'
|
32
|
+
|
33
|
+
describe SoftLayer::APIParameterFilter, "#object_with_id" do
|
34
|
+
it "should intitialize with empty parameter values" do
|
35
|
+
filter = SoftLayer::APIParameterFilter.new
|
36
|
+
filter.server_object_id.should be_nil
|
37
|
+
filter.server_object_mask.should be_nil
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should store its value in server_object_id when called " do
|
41
|
+
filter = SoftLayer::APIParameterFilter.new
|
42
|
+
result = filter.object_with_id(12345)
|
43
|
+
result.server_object_id.should eql(12345)
|
44
|
+
result.parameters.should eql({:server_object_id => 12345})
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should allow call chaining with object_mask " do
|
48
|
+
filter = SoftLayer::APIParameterFilter.new
|
49
|
+
result = filter.object_with_id(12345).object_mask("fish", "cow", "duck")
|
50
|
+
result.server_object_id.should == 12345
|
51
|
+
result.server_object_mask.should == ["fish", "cow", "duck"]
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe SoftLayer::APIParameterFilter, "#object_mask" do
|
56
|
+
it "should store its value in server_object_mask when called" do
|
57
|
+
filter = SoftLayer::APIParameterFilter.new
|
58
|
+
result = filter.object_mask("fish", "cow", "duck")
|
59
|
+
result.server_object_mask.should == ["fish", "cow", "duck"]
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should allow call chaining with object_with_id" do
|
63
|
+
filter = SoftLayer::APIParameterFilter.new
|
64
|
+
result = filter.object_mask("fish", "cow", "duck").object_with_id(12345)
|
65
|
+
result.server_object_id.should == 12345
|
66
|
+
result.server_object_mask.should == ["fish", "cow", "duck"]
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
describe SoftLayer::APIParameterFilter, "#method_missing" do
|
71
|
+
it "should invoke call_softlayer_api_with_params(method_name, self, args, &block) on it's target with itself and the method_missing parameters" do
|
72
|
+
filter = SoftLayer::APIParameterFilter.new.object_mask("fish", "cow", "duck").object_with_id(12345)
|
73
|
+
|
74
|
+
target = mock("method_missing_target")
|
75
|
+
target.should_receive(:call_softlayer_api_with_params).with(:getObject, filter, ["marshmallow"])
|
76
|
+
|
77
|
+
filter.target = target
|
78
|
+
|
79
|
+
filter.getObject("marshmallow")
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,376 @@
|
|
1
|
+
# Copyright (c) 2010, SoftLayer Technologies, Inc. All rights reserved.
|
2
|
+
#
|
3
|
+
# Redistribution and use in source and binary forms, with or without
|
4
|
+
# modification, are permitted provided that the following conditions are met:
|
5
|
+
#
|
6
|
+
# * Redistributions of source code must retain the above copyright notice,
|
7
|
+
# this list of conditions and the following disclaimer.
|
8
|
+
# * Redistributions in binary form must reproduce the above copyright notice,
|
9
|
+
# this list of conditions and the following disclaimer in the documentation
|
10
|
+
# and/or other materials provided with the distribution.
|
11
|
+
# * Neither SoftLayer Technologies, Inc. nor the names of its contributors may
|
12
|
+
# be used to endorse or promote products derived from this software without
|
13
|
+
# specific prior written permission.
|
14
|
+
#
|
15
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
16
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
17
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
18
|
+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
19
|
+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
20
|
+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
21
|
+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
22
|
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
23
|
+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
24
|
+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
25
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
26
|
+
|
27
|
+
$: << File.expand_path(File.join(File.dirname(__FILE__), "../lib"))
|
28
|
+
|
29
|
+
require 'rubygems'
|
30
|
+
require 'softlayer_api'
|
31
|
+
require 'spec'
|
32
|
+
require 'spec/autorun'
|
33
|
+
|
34
|
+
describe SoftLayer::Service, "#new" do
|
35
|
+
before(:each) do
|
36
|
+
$SL_API_USERNAME = "some_default_username"
|
37
|
+
$SL_API_KEY = "some_default_api_key"
|
38
|
+
$SL_API_BASE_URL = SoftLayer::API_PUBLIC_ENDPOINT
|
39
|
+
end
|
40
|
+
|
41
|
+
after(:each) do
|
42
|
+
$SL_API_USERNAME = nil
|
43
|
+
$SL_API_KEY = nil
|
44
|
+
$SL_API_BASE_URL = SoftLayer::API_PUBLIC_ENDPOINT
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should reject a nil or empty service name" do
|
48
|
+
lambda() {service = SoftLayer::Service.new(nil)}.should raise_error
|
49
|
+
lambda() {service = SoftLayer::Service.new("")}.should raise_error
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should remember service name passed in" do
|
53
|
+
service = SoftLayer::Service.new("SoftLayer_Account")
|
54
|
+
service.service_name.should == "SoftLayer_Account"
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should pickup default username" do
|
58
|
+
$SL_API_USERNAME = "sample"
|
59
|
+
service = SoftLayer::Service.new("SoftLayer_Account")
|
60
|
+
service.username.should == "sample"
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should accept a username in options" do
|
64
|
+
$SL_API_USERNAME = "sample"
|
65
|
+
service = SoftLayer::Service.new("SoftLayer_Account", :username => 'fred')
|
66
|
+
service.username.should == "fred"
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should pickup default api key" do
|
70
|
+
$SL_API_KEY = "sample"
|
71
|
+
service = SoftLayer::Service.new("SoftLayer_Account")
|
72
|
+
service.api_key.should == "sample"
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should accept an api key in options" do
|
76
|
+
$SL_API_KEY = "sample"
|
77
|
+
service = SoftLayer::Service.new("SoftLayer_Account", :api_key => 'fred')
|
78
|
+
service.api_key.should == "fred"
|
79
|
+
end
|
80
|
+
|
81
|
+
it "should fail if username is empty" do
|
82
|
+
lambda() do
|
83
|
+
$SL_API_USERNAME = ""
|
84
|
+
$SL_API_KEY = "sample"
|
85
|
+
|
86
|
+
service = SoftLayer::Service.new("SoftLayer_Account")
|
87
|
+
end.should raise_error
|
88
|
+
|
89
|
+
lambda() do
|
90
|
+
$SL_API_USERNAME = "good_username"
|
91
|
+
$SL_API_KEY = "sample"
|
92
|
+
|
93
|
+
service = SoftLayer::Service.new("SoftLayer_Account", :username => "")
|
94
|
+
end.should raise_error
|
95
|
+
end
|
96
|
+
|
97
|
+
it "should fail if username is nil" do
|
98
|
+
lambda() do
|
99
|
+
$SL_API_USERNAME = nil
|
100
|
+
service = SoftLayer::Service.new("SoftLayer_Account", :api_key => "sample")
|
101
|
+
end.should raise_error
|
102
|
+
|
103
|
+
lambda() do
|
104
|
+
$SL_API_KEY = "sample"
|
105
|
+
service = SoftLayer::Service.new("SoftLayer_Account", :username => nil, :api_key => "sample")
|
106
|
+
end.should raise_error
|
107
|
+
end
|
108
|
+
|
109
|
+
it "should fail if api_key is empty" do
|
110
|
+
lambda() do
|
111
|
+
$SL_API_USERNAME = "good_username"
|
112
|
+
$SL_API_KEY = ""
|
113
|
+
|
114
|
+
service = SoftLayer::Service.new("SoftLayer_Account")
|
115
|
+
end.should raise_error
|
116
|
+
|
117
|
+
lambda() do
|
118
|
+
$SL_API_USERNAME = "good_username"
|
119
|
+
$SL_API_KEY = "good_api_key"
|
120
|
+
|
121
|
+
service = SoftLayer::Service.new("SoftLayer_Account", :username => "sample", :api_key => "")
|
122
|
+
end.should raise_error
|
123
|
+
end
|
124
|
+
|
125
|
+
it "should fail if api_key is nil" do
|
126
|
+
lambda() do
|
127
|
+
$SL_API_KEY = nil
|
128
|
+
service = SoftLayer::Service.new("SoftLayer_Account", :username => "sample")
|
129
|
+
end.should raise_error
|
130
|
+
|
131
|
+
lambda() do
|
132
|
+
$SL_API_KEY = nil
|
133
|
+
service = SoftLayer::Service.new("SoftLayer_Account", :username => "sample", :api_key => nil)
|
134
|
+
end.should raise_error
|
135
|
+
end
|
136
|
+
|
137
|
+
it "should pickup default base url" do
|
138
|
+
$SL_API_BASE_URL = nil
|
139
|
+
service = SoftLayer::Service.new("SoftLayer_Account")
|
140
|
+
service.endpoint_url.should == SoftLayer::API_PUBLIC_ENDPOINT
|
141
|
+
end
|
142
|
+
|
143
|
+
it "should get base URL from globals" do
|
144
|
+
$SL_API_BASE_URL = "http://someendpoint.softlayer.com/from/globals"
|
145
|
+
service = SoftLayer::Service.new("SoftLayer_Account")
|
146
|
+
service.endpoint_url.should == "http://someendpoint.softlayer.com/from/globals"
|
147
|
+
end
|
148
|
+
|
149
|
+
it "should accept a base url through options" do
|
150
|
+
service = SoftLayer::Service.new("SoftLayer_Account", :endpoint_url => "http://someendpoint.softlayer.com")
|
151
|
+
service.endpoint_url.should == "http://someendpoint.softlayer.com"
|
152
|
+
end
|
153
|
+
end #describe SoftLayer#new
|
154
|
+
|
155
|
+
describe SoftLayer::Service, "#username=" do
|
156
|
+
it "should not allow you to set a nil or empty username" do
|
157
|
+
service = SoftLayer::Service.new("SoftLayer_Account", :username => "sample", :api_key => "sample")
|
158
|
+
lambda() {service.username = ""}.should raise_error
|
159
|
+
lambda() {service.username = nil}.should raise_error
|
160
|
+
end
|
161
|
+
|
162
|
+
it "should strip whitespace" do
|
163
|
+
service = SoftLayer::Service.new("SoftLayer_Account", :username => "sample", :api_key => "sample")
|
164
|
+
service.username = " whitespace "
|
165
|
+
service.username.should == "whitespace"
|
166
|
+
end
|
167
|
+
end #describe SoftLayer#username=
|
168
|
+
|
169
|
+
describe SoftLayer::Service, "#api_key=" do
|
170
|
+
it "should not allow you to set a nil or empty api_key" do
|
171
|
+
service = SoftLayer::Service.new("SoftLayer_Account", :username => "sample", :api_key => "sample")
|
172
|
+
lambda() {service.api_key = ""}.should raise_error
|
173
|
+
lambda() {service.api_key = nil}.should raise_error
|
174
|
+
end
|
175
|
+
|
176
|
+
it "should strip whitespace" do
|
177
|
+
service = SoftLayer::Service.new("SoftLayer_Account", :username => "sample", :api_key => "sample")
|
178
|
+
service.api_key = " fred "
|
179
|
+
service.api_key.should == "fred"
|
180
|
+
end
|
181
|
+
end #describe SoftLayer#api_key=
|
182
|
+
|
183
|
+
describe SoftLayer::Service, "#endpoint_url=" do
|
184
|
+
it "should not allow you to set a nil or empty endpoint_url" do
|
185
|
+
service = SoftLayer::Service.new("SoftLayer_Account", :username => "sample", :api_key => "sample", :endpoint_url => "http://someendpoint.softlayer.com")
|
186
|
+
lambda() {service.endpoint_url = ""}.should raise_error
|
187
|
+
lambda() {service.endpoint_url = nil}.should raise_error
|
188
|
+
end
|
189
|
+
|
190
|
+
it "should strip whitespace" do
|
191
|
+
service = SoftLayer::Service.new("SoftLayer_Account", :username => "sample", :api_key => "sample", :endpoint_url => "http://someendpoint.softlayer.com")
|
192
|
+
service.endpoint_url = " http://someendpoint.softlayer.com "
|
193
|
+
service.endpoint_url.should == "http://someendpoint.softlayer.com"
|
194
|
+
end
|
195
|
+
end #describe SoftLayer#endpoint_url=
|
196
|
+
|
197
|
+
describe SoftLayer::Service, "#url_to_call_method" do
|
198
|
+
it "should concatenate the method to the base url" do
|
199
|
+
service = SoftLayer::Service.new("SoftLayer_Account", :username => "sample_username", :api_key => "blah")
|
200
|
+
|
201
|
+
# make sure we've picked up the default API key (can be wrong if one of the other tests is misbehaving)
|
202
|
+
service.endpoint_url.should == SoftLayer::API_PUBLIC_ENDPOINT
|
203
|
+
|
204
|
+
call_url = service.url_to_call_method("getOpenTickets", nil);
|
205
|
+
call_url.scheme.should == "https"
|
206
|
+
call_url.host.should == "api.softlayer.com"
|
207
|
+
call_url.path.should == "/rest/v3/SoftLayer_Account/getOpenTickets.json"
|
208
|
+
end #describe SoftLayer#url_to_call_method=
|
209
|
+
end
|
210
|
+
|
211
|
+
describe SoftLayer::Service, "#object_with_id" do
|
212
|
+
it "should add an object to the URL for a request " do
|
213
|
+
service = SoftLayer::Service.new("SoftLayer_Ticket", :username => "sample_username", :api_key => "blah")
|
214
|
+
service.should_receive(:issue_http_request).with(URI.parse("https://api.softlayer.com/rest/v3/SoftLayer_Ticket/12345/getObject.json"), an_instance_of(Net::HTTP::Get))
|
215
|
+
service.object_with_id(12345).getObject
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
describe SoftLayer::Service, "#missing_method" do
|
220
|
+
it "should translate unknown method into an api call" do
|
221
|
+
service = SoftLayer::Service.new("SoftLayer_Account", :username => "sample_username", :api_key => "blah")
|
222
|
+
service.should_receive(:call_softlayer_api_with_params).with(:getOpenTickets, nil, ["marshmallow"])
|
223
|
+
response = service.getOpenTickets("marshmallow")
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
227
|
+
describe SoftLayer::Service, "#call_softlayer_api_with_params" do
|
228
|
+
it "should issue an HTTP request for the given method" do
|
229
|
+
service = SoftLayer::Service.new("SoftLayer_Account", :username => "sample_username", :api_key => "blah")
|
230
|
+
service.should_receive(:issue_http_request).with(any_args())
|
231
|
+
service.call_softlayer_api_with_params(:getObject, nil, []);
|
232
|
+
end
|
233
|
+
|
234
|
+
it "should include the object id in the url created" do
|
235
|
+
service = SoftLayer::Service.new("SoftLayer_Ticket", :username => "sample_username", :api_key => "blah")
|
236
|
+
service.should_receive(:issue_http_request).with(URI.parse("https://api.softlayer.com/rest/v3/SoftLayer_Ticket/12345/getObject.json"), an_instance_of(Net::HTTP::Get))
|
237
|
+
service.call_softlayer_api_with_params(:getObject, SoftLayer::APIParameterFilter.new.object_with_id(12345), []);
|
238
|
+
end
|
239
|
+
|
240
|
+
it "should include the object mask in the url created" do
|
241
|
+
expected_uri = "https://api.softlayer.com/rest/v3/SoftLayer_Account/getObject.json?objectMask=cow;duck;chicken;bull%20dog"
|
242
|
+
|
243
|
+
service = SoftLayer::Service.new("SoftLayer_Account", :username => "sample_username", :api_key => "blah")
|
244
|
+
service.should_receive(:issue_http_request).with(URI.parse(expected_uri), an_instance_of(Net::HTTP::Get))
|
245
|
+
service.call_softlayer_api_with_params(:getObject, SoftLayer::APIParameterFilter.new.object_mask("cow ", " duck", "chicken", "bull dog"), []);
|
246
|
+
end
|
247
|
+
|
248
|
+
it "should warn about calling a get method with arguments" do
|
249
|
+
service = SoftLayer::Service.new("SoftLayer_Account", :username => "sample_username", :api_key => "blah")
|
250
|
+
service.should_receive(:issue_http_request).with(any_args())
|
251
|
+
$stderr.should_receive(:puts).with("Warning - The HTTP request for getObject does not allow arguments to be passed to the server")
|
252
|
+
service.getObject("Hello", "Bindigo")
|
253
|
+
end
|
254
|
+
|
255
|
+
it "should return the parsed JSON when completing successfully" do
|
256
|
+
service = SoftLayer::Service.new("SoftLayer_Account", :username => "sample_username", :api_key => "blah")
|
257
|
+
service.should_receive(:issue_http_request).with(any_args()).and_return('{"successful":"Yipeee!", "array":[1,2,3], "bool":true}')
|
258
|
+
result = service.getObject
|
259
|
+
result.should == {"array"=>[1, 2, 3], "successful"=>"Yipeee!", "bool"=>true}
|
260
|
+
end
|
261
|
+
|
262
|
+
it "should raise an exception when completing unsuccessfully" do
|
263
|
+
service = SoftLayer::Service.new("SoftLayer_Account", :username => "sample_username", :api_key => "blah")
|
264
|
+
service.should_receive(:issue_http_request).with(any_args()).and_return('{"error":"Function (\"getSnargled\") is not a valid method for this service"}')
|
265
|
+
lambda{ result = service.getSnargled }.should raise_error
|
266
|
+
end
|
267
|
+
end
|
268
|
+
|
269
|
+
describe SoftLayer::Service, "#object_with_id" do
|
270
|
+
it "should return an APIParameterFilter with itself as the target" do
|
271
|
+
service = SoftLayer::Service.new("SoftLayer_Account", :username => "sample_username", :api_key => "blah")
|
272
|
+
filter = service.object_with_id(12345)
|
273
|
+
|
274
|
+
filter.should_not be_nil
|
275
|
+
filter.target.should === service
|
276
|
+
filter.server_object_id.should == 12345
|
277
|
+
end
|
278
|
+
end
|
279
|
+
|
280
|
+
describe SoftLayer::Service, "#object_mask" do
|
281
|
+
it "should return an APIParameterFilter with itself as the target" do
|
282
|
+
service = SoftLayer::Service.new("SoftLayer_Account", :username => "sample_username", :api_key => "blah")
|
283
|
+
filter = service.object_mask("fish", "cow", "duck")
|
284
|
+
|
285
|
+
filter.should_not be_nil
|
286
|
+
filter.target.should === service
|
287
|
+
filter.server_object_mask.should == ["fish", "cow", "duck"]
|
288
|
+
end
|
289
|
+
end
|
290
|
+
|
291
|
+
describe SoftLayer::Service, "#http_request_for_method" do
|
292
|
+
it "should generate a GET request for methods staring with get" do
|
293
|
+
service = SoftLayer::Service.new("SoftLayer_Account", :username => "sample_username", :api_key => "blah")
|
294
|
+
url_request = service.http_request_for_method(:getObject, URI.parse("http://bogus.com"))
|
295
|
+
url_request.should_not be_nil
|
296
|
+
url_request.should be_kind_of(Net::HTTP::Get)
|
297
|
+
end
|
298
|
+
|
299
|
+
it "should generate a POST request for methods starting with crate" do
|
300
|
+
service = SoftLayer::Service.new("SoftLayer_Account", :username => "sample_username", :api_key => "blah")
|
301
|
+
url_request = service.http_request_for_method(:createSomeTicket, URI.parse("http://bogus.com"))
|
302
|
+
url_request.should_not be_nil
|
303
|
+
url_request.should be_kind_of(Net::HTTP::Post)
|
304
|
+
end
|
305
|
+
|
306
|
+
it "should generate a PUT request for methods starting with edit" do
|
307
|
+
service = SoftLayer::Service.new("SoftLayer_Account", :username => "sample_username", :api_key => "blah")
|
308
|
+
url_request = service.http_request_for_method(:editFoo, URI.parse("http://bogus.com"))
|
309
|
+
url_request.should_not be_nil
|
310
|
+
url_request.should be_kind_of(Net::HTTP::Put)
|
311
|
+
|
312
|
+
# I know of at least one service (I think it's the ticket service) that uses "edit" as
|
313
|
+
# a method name. Make sure that the lack of additional characters doesn't throw off
|
314
|
+
# the service
|
315
|
+
service = SoftLayer::Service.new("SoftLayer_Account", :username => "sample_username", :api_key => "blah")
|
316
|
+
url_request = service.http_request_for_method(:edit, URI.parse("http://bogus.com"))
|
317
|
+
url_request.should_not be_nil
|
318
|
+
url_request.should be_kind_of(Net::HTTP::Put)
|
319
|
+
end
|
320
|
+
|
321
|
+
it "should generate a DELETE request for methods starting with delete" do
|
322
|
+
service = SoftLayer::Service.new("SoftLayer_Account", :username => "sample_username", :api_key => "blah")
|
323
|
+
url_request = service.http_request_for_method(:deleteObject, URI.parse("http://bogus.com"))
|
324
|
+
|
325
|
+
url_request.should_not be_nil
|
326
|
+
url_request.should be_kind_of(Net::HTTP::Delete)
|
327
|
+
end
|
328
|
+
end
|
329
|
+
|
330
|
+
describe SoftLayer::Service, "#marshall_arguments_for_call" do
|
331
|
+
service = SoftLayer::Service.new("SoftLayer_Account", :username => "sample_username", :api_key => "blah")
|
332
|
+
request_body = service.marshall_arguments_for_call(["first", 3, {"cow" => "chicken"}])
|
333
|
+
request_body.should == '{"parameters":["first",3,{"cow":"chicken"}]}'
|
334
|
+
end
|
335
|
+
|
336
|
+
describe SoftLayer::Service, " creating option proxies" do
|
337
|
+
it "should allow me to create a proxy object with just the object_with_id option" do
|
338
|
+
service = SoftLayer::Service.new("SoftLayer_Ticket", :username => "sample_username", :api_key => "blah")
|
339
|
+
ticket_proxy = service.object_with_id(123456)
|
340
|
+
|
341
|
+
ticket_proxy.server_object_id.should eql(123456)
|
342
|
+
service.should_receive(:call_softlayer_api_with_params).with(any_args())
|
343
|
+
ticket_proxy.getObject
|
344
|
+
end
|
345
|
+
|
346
|
+
it "should allow me to create a proxy object with just the object_mask option" do
|
347
|
+
service = SoftLayer::Service.new("SoftLayer_Ticket", :username => "sample_username", :api_key => "blah")
|
348
|
+
ticket_proxy = service.object_mask("fish", "cow", "duck")
|
349
|
+
|
350
|
+
ticket_proxy.server_object_mask.should eql(["fish", "cow", "duck"])
|
351
|
+
service.should_receive(:call_softlayer_api_with_params).with(any_args())
|
352
|
+
ticket_proxy.getObject
|
353
|
+
end
|
354
|
+
|
355
|
+
it "should not modify an object_with_id proxy even if that proxy is used with a mask" do
|
356
|
+
service = SoftLayer::Service.new("SoftLayer_Ticket", :username => "sample_username", :api_key => "blah")
|
357
|
+
ticket_proxy = service.object_with_id(123456)
|
358
|
+
|
359
|
+
service.should_receive(:issue_http_request).with(URI.parse("https://api.softlayer.com/rest/v3/SoftLayer_Ticket/123456/getObject.json?objectMask=fish;cow;duck"), an_instance_of(Net::HTTP::Get))
|
360
|
+
ticket_proxy.object_mask("fish", "cow", "duck").getObject
|
361
|
+
|
362
|
+
ticket_proxy.server_object_id.should eql(123456)
|
363
|
+
ticket_proxy.server_object_mask.should be_nil
|
364
|
+
end
|
365
|
+
|
366
|
+
it "should not modify an object_mask proxy even if it is used with an object ID" do
|
367
|
+
service = SoftLayer::Service.new("SoftLayer_Ticket", :username => "sample_username", :api_key => "blah")
|
368
|
+
masked_proxy = service.object_mask("fish", "cow", "duck")
|
369
|
+
|
370
|
+
service.should_receive(:issue_http_request).with(URI.parse("https://api.softlayer.com/rest/v3/SoftLayer_Ticket/123456/getObject.json?objectMask=fish;cow;duck"), an_instance_of(Net::HTTP::Get))
|
371
|
+
masked_proxy.object_with_id(123456).getObject
|
372
|
+
|
373
|
+
masked_proxy.server_object_id.should be_nil
|
374
|
+
masked_proxy.server_object_mask.should eql(["fish", "cow", "duck"])
|
375
|
+
end
|
376
|
+
end
|