jamesn-softlayer-ruby 0.5.0.0 → 0.6.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.
Files changed (5) hide show
  1. data/Changes +7 -0
  2. data/README +5 -8
  3. data/lib/softlayer.rb +89 -24
  4. data/sample/resultlimit.rb +71 -0
  5. metadata +6 -3
data/Changes ADDED
@@ -0,0 +1,7 @@
1
+
2
+
3
+ Changes since 0.5.0.
4
+
5
+ Implemented resultLimit headers. See readme and the rdoc for SoftLayer::ResultLimit.
6
+ Renamed SoftLayer::ObjectMaskHeader to SoftLayer::ObjectMask and SoftLayer::ParamHeader to SoftLayer::Param
7
+
data/README CHANGED
@@ -1,4 +1,4 @@
1
- This is a module and class factory for SoftLayer's customer portal API (http://sldn.softlayer.com/). See the rdoc documentation and the sample directory for usage details.
1
+ This is a module and class factory for SoftLayer's customer portal API (http://sldn.softlayer.com ). See the rdoc documentation and the sample directory for usage details.
2
2
 
3
3
  The factory creates classes as they are declared. As a group:
4
4
 
@@ -22,7 +22,7 @@ puts account['email']
22
22
  Object Masks:
23
23
 
24
24
  account.objectMask = { 'allBillingItems' => nil }
25
- pp account['allBillingItems]
25
+ pp account['allBillingItems']
26
26
 
27
27
 
28
28
  API user and key is cached after first use (but can be overridden on a per object basis):
@@ -42,16 +42,13 @@ account['nasNetworkStorage'].each do |n|
42
42
  pp nas.capacityGb
43
43
  end
44
44
 
45
- ToDo:
45
+ Result Limits:
46
46
 
47
- Result Limits. Can be done now as a parameter to any method, but should be integrated such that this does the right thing:
48
-
49
- account.getAllBillingItems(:limit => [X,Y]) do |o,bia|
47
+ account.getAllBillingItems(:limit => [X,Y]) do |bia|
50
48
  ( blah )
51
49
  end
52
50
 
53
- Such that is +o+ is the offset and +bia+ is an array containing X or fewer elements (or a single element if :limit[0] == 1);
54
- blah executes on each batch until it's done.
51
+ Such that +bia+ is an array containing X or fewer elements; blah executes on each batch until there's no more.
55
52
 
56
53
  Without a block, should return the 5 elements offset from Y:
57
54
 
data/lib/softlayer.rb CHANGED
@@ -61,16 +61,6 @@ module SoftLayer
61
61
  end
62
62
  end
63
63
 
64
- # Derive a Class from a SOAP::Mapping::Object
65
- # +obj+:: The object to derive a class from.
66
- # Returns a new object with +obj+ as the cached object. (obj['id'] == :initParam)
67
- #
68
- # XXX: We should use the SOAP mapping registry to avoid doing this, but what the hell.
69
- # Also, there's no guartinee there's a class here to be created, so the assumption is
70
- # that you know what you're doing when you use this (much like everything else).
71
- def SoftLayer::deriveClass(args)
72
- end
73
-
74
64
  # Create a Ruby class to match an SLAPI WSDL endpoint.
75
65
  # Args:
76
66
  # +class+:: The name of the class to create in Ruby format.
@@ -111,8 +101,8 @@ module SoftLayer
111
101
  return klass
112
102
  end
113
103
 
114
- # A class to old Paramaters.
115
- class ParamHeader < SOAP::Header::SimpleHandler
104
+ # A class to old Paramaters.
105
+ class Param < SOAP::Header::SimpleHandler
116
106
  def initialize(tag, out)
117
107
  @out = out
118
108
  super(XSD::QName.new(nil, tag))
@@ -132,7 +122,8 @@ module SoftLayer
132
122
  end
133
123
 
134
124
  # A class to hold the object mask.
135
- class ObjectMaskHeader < SOAP::Header::SimpleHandler
125
+ class ObjectMask < SOAP::Header::SimpleHandler
126
+
136
127
  def initialize(tag, out)
137
128
  @out = out
138
129
  super(XSD::QName.new(nil, tag))
@@ -149,7 +140,21 @@ module SoftLayer
149
140
  def []=(k,v)
150
141
  @out[k]=v
151
142
  end
143
+ end
144
+
145
+ class ResultLimit < SOAP::Header::SimpleHandler
146
+ attr_accessor :limit, :offset
152
147
 
148
+ # limit should be an array of two elements; limit and offset.
149
+ def initialize(tag, limit)
150
+ @limit = limit[0]
151
+ @offset = limit[1]
152
+ super(XSD::QName.new(nil, tag))
153
+ end
154
+
155
+ def on_simple_outbound
156
+ { 'limit' => @limit, 'offset' => @offset }
157
+ end
153
158
  end
154
159
 
155
160
  # The Base class for our generated class.
@@ -182,13 +187,14 @@ module SoftLayer
182
187
  @@apiKey = args[:key] unless (args[:key].nil? || !@@apiKey.nil?)
183
188
  @apiUser = @@apiUser unless (@@apiUser.nil? || !@apiUser.nil?)
184
189
  @apiKey = @@apiKey unless (@@apiKey.nil? || !@apiKey.nil?)
190
+ @authHeader = Param.new('authenticate', {'username' => @apiUser, 'apiKey' => @apiKey})
185
191
 
186
192
  self.class.cacheWSDL
187
193
  @slapi = @@wsdl[self.soapClass].create_rpc_driver
188
194
  self.debug=args[:debug] unless args[:debug].nil?
189
195
  end
190
196
 
191
- # Return this's object's matching SLAPI SOAP Class.
197
+ # Return this object's matching SLAPI SOAP Class.
192
198
  def soapClass
193
199
  return self.class.to_s.gsub(/::/, '_')
194
200
  end
@@ -223,10 +229,10 @@ module SoftLayer
223
229
  # userCount => nil }
224
230
  # Changing this resets the cached object used by #[]
225
231
  def objectMask=(mask)
226
- if mask.class == ObjectMaskHeader
232
+ if mask.class == ObjectMask
227
233
  @objectMask = mask
228
234
  else
229
- @objectMask = ObjectMaskHeader.new("#{self.soapClass}ObjectMask", mask)
235
+ @objectMask = ObjectMask.new("#{self.soapClass}ObjectMask", mask)
230
236
  end
231
237
  @slapiObject = nil
232
238
  end
@@ -234,33 +240,83 @@ module SoftLayer
234
240
  def objectMask
235
241
  return @objectMask
236
242
  end
243
+
244
+ # Set an object wide result set (or clear it)
245
+ # arg can be one of three things:
246
+ # * nil clears the resultLimit
247
+ # * A Result Limit array of two elements range and offset.
248
+ # * An existing ResultLimit object
249
+ def resultLimit=(arg)
250
+ case arg.class
251
+ when NilClass
252
+ @resultLimit = nil
253
+ when Array
254
+ @resultLimit = ResultLimit.new('resultLimit',arg)
255
+ when ResultLimit
256
+ @resultLimit = arg
257
+ end
258
+ end
259
+
260
+ def resultLimit
261
+ return @resultLimit
262
+ end
237
263
 
238
264
 
239
265
  # Make a direct api call. Paramaters are a hash where the key is passed to ParamHeader as the tag, and the value
240
266
  # is passed as the tag content, unless it's a magic paramater.
241
267
  # Magic Paramaters:
242
- # +initParam+:: Initialization paramater for this call (just the key). Otherwise @initParam is used.
243
- #
268
+ # +initParam+:: Initialization paramater for this call (just the key), therwise @initParam is used.
269
+ # +limit+:: A Result Limit array of two elements range and offset. If @resultLimit is set it's used
270
+ # if +limit+ is not and if neither is set, no limit is applied.
271
+ #
272
+ # If a block is provided, the limit's range (or fewer) elements will yield to the block until the dataset
273
+ # is exhausted. If no limit is provided with the block a limit of [1,0] is assumed initially.
244
274
  # Aliased to #method_missing.
245
- def slapiCall(method, args = { })
275
+ def slapiCall(method, args = { }, &block)
246
276
  initParam = args[:initParam] unless args[:initParam].nil?
247
277
  args.delete(:initParam) unless args[:initParam].nil?
278
+ initParam = Param.new("#{self.soapClass}InitParameters", { 'id' => initParam }) unless initParam.nil?
248
279
  initParam = @initParam if initParam.nil?
280
+ resultLimit = ResultLimit.new('resultLimit', args[:limit]) unless args[:limit].nil?
281
+ args.delete(:limit) unless args[:limit].nil?
282
+ resultLimit = @resultLimit if resultLimit.nil?
249
283
 
250
- @slapi.headerhandler << ParamHeader.new('authenticate', {'username' => @apiUser, 'apiKey' => @apiKey})
284
+ @slapi.headerhandler << @authHeader unless @slapi.headerhandler.include?(@authHeader)
285
+ paramHeaders = []
251
286
  unless args.nil?
252
287
  args.each do |k,v|
253
- @slapi.headerhandler << ParamHeader.new(k.to_s,v)
288
+ p = ParamHeader.new(k.to_s,v)
289
+ paramHeaders.push(p)
290
+ @slapi.headerhandler << p
254
291
  end
255
292
  end
256
- @slapi.headerhandler << ParamHeader.new("#{self.soapClass}InitParameters", { 'id' => initParam})
293
+ @slapi.headerhandler << initParam unless @slapi.headerhandler.include?(@authHeader)
257
294
  @slapi.headerhandler << @objectMask unless @objectMask.nil?
258
- return @slapi.call(method.to_s)
295
+ @slapi.headerhandler << resultLimit unless resultLimit.nil?
296
+
297
+ if block_given?
298
+ go=true
299
+ resultLimit = ResultLimit.new('resultLimit', [1,0]) if resultLimit.nil? # this is broken.
300
+ @slapi.headerhandler << resultLimit unless @slapi.headerhandler.include?(resultLimit)
301
+ while(go) do
302
+ res = @slapi.call(method.to_s)
303
+ yield(res) unless (res.nil? || (res.respond_to?(:empty) && res.empty?))
304
+ go = false if res.nil?
305
+ go = false if (res.respond_to?(:size) && (res.size < resultLimit.limit))
306
+ resultLimit.offset=resultLimit.offset + resultLimit.limit
307
+ end
308
+ headerClean(resultLimit,paramHeaders)
309
+ return true
310
+ else
311
+ res = @slapi.call(method.to_s)
312
+ headerClean(resultLimit,paramHeaders)
313
+ return res
314
+ end
259
315
  end
260
316
 
261
317
  # Alias the above call method to #method_missing.
262
318
  alias_method :method_missing, :slapiCall
263
-
319
+
264
320
  # Enable (or disable) debug. (paramater is the IO handler to write to)
265
321
  def debug=(dev)
266
322
  @slapi.wiredump_dev=(dev)
@@ -293,6 +349,15 @@ module SoftLayer
293
349
  def self.soapClass
294
350
  self.name.to_s.gsub(/::/, '_')
295
351
  end
352
+
353
+ private
354
+
355
+ # Clean the headers out of the driver.
356
+ def headerClean(rl,ha)
357
+ @slapi.headerhandler.delete(rl)
358
+ ha.each { |h| @slapi.headerhandler.delete(h) }
359
+
360
+ end
296
361
 
297
362
  end
298
363
  end
@@ -0,0 +1,71 @@
1
+ #!/usr/bin/env ruby
2
+ # resultlimit
3
+ #
4
+ # Copyright (c) 2009, James Nuckolls. All rights reserved.
5
+ #
6
+ # Redistribution and use in source and binary forms, with or without
7
+ # modification, are permitted provided that the following conditions are met:
8
+ #
9
+ # * Redistributions of source code must retain the above copyright notice,
10
+ # this list of conditions and the following disclaimer.
11
+ # * Redistributions in binary form must reproduce the above copyright notice,
12
+ # this list of conditions and the following disclaimer in the documentation
13
+ # and/or other materials provided with the distribution.
14
+ # * Neither "James Nuckolls" nor the names of any contributors may
15
+ # be used to endorse or promote products derived from this software without
16
+ # specific prior written permission.
17
+ #
18
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
+ # POSSIBILITY OF SUCH DAMAGE.
29
+ #
30
+ #= Description
31
+ # Fun with Range Limits.
32
+ #= ToDo
33
+ #
34
+
35
+ require 'rubygems' rescue LoadError
36
+ require 'pp'
37
+ require 'softlayer'
38
+
39
+ AUTH_USER = ARGV[0]
40
+ AUTH_KEY = ARGV[2]
41
+ ACCT_ID = ARGV[1]
42
+
43
+ SLAPICLASSES = [ 'SoftLayer::Account' ]
44
+ SoftLayer::declareClasses(:ruby => SLAPICLASSES)
45
+
46
+ range = 5
47
+ offset = 0
48
+ account = SoftLayer::Account.new(:user => AUTH_USER, :key => AUTH_KEY, :initParam => ACCT_ID)
49
+ # account.debug=STDOUT
50
+ bia = account.getAllBillingItems(:limit => [range,offset])
51
+ pp bia
52
+ puts "=================================="
53
+ offset = range + offset
54
+ bia = account.getAllBillingItems(:limit => [range,offset])
55
+ pp bia
56
+ puts "=================================="
57
+ offset = range + offset
58
+ account.getAllBillingItems(:limit => [range,offset]) do |bi|
59
+ pp bi
60
+ puts "================== #{offset}"
61
+ offset = range + offset
62
+ end
63
+
64
+ range = 5
65
+ offset = 0
66
+ # account.debug=STDOUT
67
+ account.getInvoices(:limit => [range,offset]) do |bi|
68
+ pp bi
69
+ puts "================== #{offset}"
70
+ offset = offset + range
71
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jamesn-softlayer-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0.0
4
+ version: 0.6.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Nuckolls
@@ -9,7 +9,7 @@ autorequire: softlayer
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-11 22:00:00 -07:00
12
+ date: 2009-04-13 22:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -23,7 +23,7 @@ dependencies:
23
23
  version: 1.5.0
24
24
  version:
25
25
  description:
26
- email: jamesn@what.net
26
+ email: afeIfz.drtgcE@what.net
27
27
  executables: []
28
28
 
29
29
  extensions: []
@@ -31,13 +31,16 @@ extensions: []
31
31
  extra_rdoc_files:
32
32
  - README
33
33
  - LICENSE
34
+ - Changes
34
35
  files:
35
36
  - lib/softlayer.rb
36
37
  - sample/nascapacity.rb
37
38
  - sample/billingreport.rb
38
39
  - sample/objectmask.rb
40
+ - sample/resultlimit.rb
39
41
  - README
40
42
  - LICENSE
43
+ - Changes
41
44
  has_rdoc: true
42
45
  homepage: http://github.com/jamesn/softlayer-ruby
43
46
  post_install_message: