softlayer_api 1.0.6 → 1.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  h1=. SoftLayer API Client for Ruby
2
2
 
3
- The SoftLayer API Client for Ruby is a library for connecting to and calling the routines of "The SoftLayer API":http://sldn.softlayer.com/wiki/index.php/The_SoftLayer_API from the "Ruby":http://www.ruby-lang.org programming language.
3
+ The SoftLayer API Client for Ruby is a library for connecting to and calling the routines of "The SoftLayer API":http://sldn.softlayer.com/article/The_SoftLayer_API from the "Ruby":http://www.ruby-lang.org programming language.
4
4
 
5
5
  h2. Overview
6
6
 
@@ -30,13 +30,13 @@ installs the gem and makes it available to Ruby scripts. Where the gem is instal
30
30
 
31
31
  h2. Usage
32
32
 
33
- To begin using the Ruby client, you will have to create an instance of the @SoftLayer::Service@ class for each the "API Services":http://sldn.softlayer.com/wiki/index.php/Category:API_Services that your code will call. To create the instance, you will have to provide information that the library will use to authenticate your account with the API servers.
33
+ To begin using the Ruby client, you will have to create an instance of the @SoftLayer::Service@ class for each the "API Services":http://sldn.softlayer.com/reference/services that your code will call. To create the instance, you will have to provide information that the library will use to authenticate your account with the API servers.
34
34
 
35
35
  Once you have created a service object, you will use that service object to call methods in the SoftLayer API.
36
36
 
37
37
  h3. Authentication
38
38
 
39
- That instance will have to know your "API authentication information":http://sldn.softlayer.com/wiki/index.php/Authenticating_to_the_SoftLayer_API consisting of your account username and API key. In addition, you will have to select an endpoint, the web address the client will use to contact the SoftLayer API. You may provide this information either through global variables, or by passing them to the constructor.
39
+ That instance will have to know your "API authentication information":http://sldn.softlayer.com/article/Authenticating_to_the_SoftLayer_API consisting of your account username and API key. In addition, you will have to select an endpoint, the web address the client will use to contact the SoftLayer API. You may provide this information either through global variables, or by passing them to the constructor.
40
40
 
41
41
  h4. Providing authentication information through Globals
42
42
 
@@ -55,7 +55,7 @@ table{position:relative;left:2em;width:80%}.
55
55
  |{vertical-align:baseline;width:35%}.@API_PUBLIC_ENDPOINT@|A constant containing the base address for the public network REST inspired endpoint of the SoftLayer API - @https://api.softlayer.com/rest/v3/@|
56
56
  |{vertical-align:baseline}.@API_PRIVATE_ENDPOINT@|A constant containing the base address for the private network REST inspired endpoint of the SoftLayer API - @https://api.service.softlayer.com/rest/v3/@|
57
57
 
58
- For more information about the two networks see "Choosing_the_Public_or_Private_Network":http://sldn.softlayer.com/wiki/index.php/The_SoftLayer_API#Choosing_the_Public_or_Private_Network. You can change the default endpoint URL by setting the global variable @$SL_API_BASE_URL@ to either of these two values.
58
+ For more information about the two networks see "Choosing_the_Public_or_Private_Network":http://sldn.softlayer.com/article/The_SoftLayer_API#Choosing_the_Public_or_Private_Network. You can change the default endpoint URL by setting the global variable @$SL_API_BASE_URL@ to either of these two values.
59
59
 
60
60
  Here is an example of using these globals to create a service:
61
61
 
@@ -192,7 +192,7 @@ You may add calls to both @object_mask@ and @object_with_id@, but only one call
192
192
 
193
193
  h2. Examples
194
194
 
195
- Here are some examples that demonstrate using the SoftLayer API Ruby Client. The authentication information, of course, is left as an exercise for the reader. The first example uses the @getObject@ method of the "SoftLayer_Account":http://sldn.softlayer.com/wiki/index.php/SoftLayer_Account service:
195
+ Here are some examples that demonstrate using the SoftLayer API Ruby Client. The authentication information, of course, is left as an exercise for the reader. The first example uses the @getObject@ method of the "SoftLayer_Account":http://sldn.softlayer.com/reference/services service:
196
196
 
197
197
  <pre style="border:1pt solid black;background:#eee;padding:0.5em;margin:0.5em"><code style="font-family:Menlo,Monaco,monospace;font-size:9pt">require 'rubygems'
198
198
  require 'softlayer_api'
@@ -31,7 +31,7 @@ require 'pp'
31
31
  $SL_API_USERNAME = "joecustomer" # enter your username here
32
32
  $SL_API_KEY = "feeddeadbeefbadf00d..." # enter your api key here
33
33
 
34
- # use an account service to get a list of the open tickets and print their
34
+ # use an account service to get a list of the open tickets and print their
35
35
  # IDs and titles
36
36
  account_service = SoftLayer::Service.new("SoftLayer_Account")
37
37
 
@@ -35,8 +35,8 @@ ticket_service = SoftLayer::Service.new("SoftLayer_Ticket",
35
35
  begin
36
36
  ticket_ref = ticket_service.object_with_id(1683973)
37
37
 
38
- ticket = ticket_ref.object_mask({"updates" => ["entry", "createDate"]},
39
- "assignedUserId",
38
+ ticket = ticket_ref.object_mask({"updates" => ["entry", "createDate"]},
39
+ "assignedUserId",
40
40
  {"attachedHardware" => "datacenter"}).getObject
41
41
  pp ticket
42
42
  rescue Exception => exception
@@ -36,7 +36,7 @@
36
36
  #
37
37
 
38
38
  module SoftLayer
39
- VERSION = "1.0.6" # version history at the bottom of the file.
39
+ VERSION = "1.0.7" # version history at the bottom of the file.
40
40
 
41
41
  # The base URL of the SoftLayer API's REST-like endpoints available to the public internet.
42
42
  API_PUBLIC_ENDPOINT = 'https://api.softlayer.com/rest/v3/'
@@ -75,8 +75,12 @@ end # module SoftLayer
75
75
  # 1.0.3 - Added a request filter to add result limits to request. Submitted by
76
76
  # JN. Thanks!
77
77
  #
78
- # 1.0.4 - Fixed a bug where the result_limit and result_offset object filters were just not working.
78
+ # 1.0.4 - Fixed a bug where the result_limit and result_offset object filters were just not working.
79
79
  #
80
80
  # 1.0.5 - Fixed a bug where empty hashes and empty arrays would not generate meaningful object masks
81
81
  #
82
- # 1.0.6 - Make all API calls with either a GET or a POST as the HTTP verb.
82
+ # 1.0.6 - Make all API calls with either a GET or a POST as the HTTP verb.
83
+ #
84
+ # 1.0.7 - Calls to the "getObject" method of any service should not take parameters. The gem now
85
+ # warns if you make this type of call and ignores the parameters. This prevents
86
+ # SoftLayer_Virtual_Guest::getObject from accidentally creating (billable) CCI instances.
@@ -27,7 +27,7 @@
27
27
  class Hash
28
28
  def to_sl_object_mask(base = "")
29
29
  return base if(self.empty?)
30
-
30
+
31
31
  # ask the children to convert themselves with the key as the base
32
32
  masked_children = self.map { |key, value| result = value.to_sl_object_mask(key); }.flatten
33
33
 
@@ -81,27 +81,27 @@ module SoftLayer
81
81
  merged_object.parameters = @parameters.merge({ :object_mask => args }) if args && !args.empty?
82
82
  merged_object
83
83
  end
84
-
84
+
85
85
  def result_limit(limit)
86
86
  merged_object = APIParameterFilter.new;
87
87
  merged_object.target = self.target
88
88
  merged_object.parameters = @parameters.merge({ :result_limit => limit })
89
89
  merged_object
90
90
  end
91
-
91
+
92
92
  def server_result_limit
93
93
  self.parameters[:result_limit]
94
94
  end
95
-
95
+
96
96
  def result_offset(offset)
97
97
  self.parameters[:result_offset] = offset
98
98
  self
99
99
  end
100
-
100
+
101
101
  def server_result_offset
102
102
  self.parameters[:result_offset]
103
103
  end
104
-
104
+
105
105
 
106
106
  def method_missing(method_name, *args, &block)
107
107
  return @target.call_softlayer_api_with_params(method_name, self, args, &block)
@@ -205,13 +205,13 @@ module SoftLayer
205
205
 
206
206
  return proxy.object_mask(*args)
207
207
  end
208
-
208
+
209
209
  def result_limit(limit)
210
210
  proxy = APIParameterFilter.new
211
211
  proxy.target = self
212
212
  return proxy.result_limit(limit)
213
213
  end
214
-
214
+
215
215
  def result_offset(offset)
216
216
  proxy = APIParameterFilter.new
217
217
  proxy.target = self
@@ -278,7 +278,7 @@ module SoftLayer
278
278
  # not JSON values. As a result, 'JSON.parse("true")' yields a parsing
279
279
  # exception. To work around this, we force the result JSON text by
280
280
  # including it in Array markers, then take the first element of the
281
- # resulting array as the result of the parsing. This should allow values
281
+ # resulting array as the result of the parsing. This should allow values
282
282
  # like true, false, null, and numbers to parse the same way they would in
283
283
  # a browser.
284
284
  parsed_json = JSON.parse("[ #{json_results} ]")[0]
@@ -316,9 +316,23 @@ module SoftLayer
316
316
  def http_request_for_method(method_name, method_url, request_body = nil)
317
317
  content_type_header = {"Content-Type" => "application/json"}
318
318
 
319
+ # This is a workaround for a potential problem that arises from mis-using the
320
+ # API. If you call SoftLayer_Virtual_Guest and you call the getObject method
321
+ # but pass a virtual guest as a parameter, what happens is the getObject method
322
+ # is called through an HTTP POST verb and the API creates a new CCI that is a copy
323
+ # of the one you passed in.
324
+ #
325
+ # The counter-intuitive creation of a new CCI is unexpected and, even worse,
326
+ # is something you can be billed for. To prevent that, we ignore the request
327
+ # body on a "getObject" call and print out a warning.
328
+ if (method_name == :getObject) && (nil != request_body) then
329
+ $stderr.puts "Warning - The getObject method takes no parameters. The parameters you have provided will be ignored."
330
+ request_body = nil
331
+ end
332
+
319
333
  if request_body && !request_body.empty?
320
334
  url_request = Net::HTTP::Post.new(method_url.request_uri(), content_type_header)
321
- else
335
+ else
322
336
  url_request = Net::HTTP::Get.new(method_url.request_uri())
323
337
  end
324
338
 
@@ -409,7 +423,7 @@ module SoftLayer
409
423
  mask_value = parameters.server_object_mask.to_sl_object_mask.map { |mask_key| URI.encode(mask_key.to_s.strip) }.join(";")
410
424
  query_string = "objectMask=#{mask_value}"
411
425
  end
412
-
426
+
413
427
  if (parameters && parameters.server_result_limit)
414
428
  resultLimit = parameters.server_result_limit
415
429
  resultOffset = parameters.server_result_offset
@@ -224,6 +224,15 @@ describe SoftLayer::Service, "#missing_method" do
224
224
  end
225
225
  end
226
226
 
227
+ describe SoftLayer::Service, "#http_request_for_method" do
228
+ it "should only use HTTP GET for requests to the getObject method even if paramters are provided" do
229
+ service = SoftLayer::Service.new("SoftLayer_Account", :username => "sample_username", :api_key => "blah")
230
+ request = service.http_request_for_method(:getObject, URI.parse("https://api.softlayer.com/rest/v3/SoftLayer_Ticket/12345/getObject.json"), '{"simple" : "object"}')
231
+ request.should be_a_kind_of(Net::HTTP::Get)
232
+ request.should_not be_a_kind_of(Net::HTTP::Post)
233
+ end
234
+ end
235
+
227
236
  describe SoftLayer::Service, "#call_softlayer_api_with_params" do
228
237
  it "should issue an HTTP request for the given method" do
229
238
  service = SoftLayer::Service.new("SoftLayer_Account", :username => "sample_username", :api_key => "blah")
@@ -84,9 +84,9 @@ describe Hash, "#to_sl_object_mask" do
84
84
  it "should resolve the mapped values with the base provided" do
85
85
  {"top" => [ "middle1", {"middle2" => "end"}]}.to_sl_object_mask.should eql(["top.middle1", "top.middle2.end"])
86
86
  end
87
-
87
+
88
88
  it "should handle a complex hash object mask with an inner empty hash" do
89
89
  { "ipAddress" => { "ipAddress" => {}}}.to_sl_object_mask.should eql(["ipAddress.ipAddress"])
90
90
  end
91
-
91
+
92
92
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: softlayer_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.6
4
+ version: 1.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-18 00:00:00.000000000 Z
12
+ date: 2013-04-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json