Class: SoftLayer::BaseClass

Inherits:
Object
  • Object
show all
Defined in:
lib/softlayer.rb

Overview

The Base class for our generated class.

Constant Summary collapse

WSDLBASE =
'http://api.service.softlayer.com/soap/v3'
WSDLPARAM =
'?wsdl'
@@wsdl =
{ }
@@apiUser =
nil
@@apiKey =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ BaseClass

The initializer. Arguments:

user

The API User

key

The API Key

initParams

This object’s initParam (just the key)

debug

Enable debug after driver creation. (IO handler)

user and key are optional. The first time they’re presented they’re saved to class variables and reused later as necessary. Supplying user and key later does not overwrite the class variables. initParams is required where the api requires it.



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/softlayer.rb', line 198

def initialize(args)
  @apiUser = args[:user] unless args[:user].nil?
  @apiKey = args[:key] unless args[:key].nil?
  @initParam = args[:initParam]

  @@apiUser = args[:user] unless (args[:user].nil? || !@@apiUser.nil?)
  @@apiKey = args[:key] unless (args[:key].nil? || !@@apiKey.nil?)
  @apiUser = @@apiUser unless (@@apiUser.nil? || !@apiUser.nil?)
  @apiKey = @@apiKey unless (@@apiKey.nil? || !@apiKey.nil?)
  @authHeader = Param.new('authenticate', {'username' => @apiUser, 'apiKey' => @apiKey})

  self.class.cacheWSDL
  @slapi = @@wsdl[self.soapClass].create_rpc_driver unless @@wsdl[self.soapClass].nil?
  raise SoftLayer::Exception.new(:message => 'WSDL endpoint not available.') if @slapi.nil?
  
  self.debug=args[:debug] unless args[:debug].nil?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missingObject

Make a direct api call. Paramaters are a hash where the key is passed to ParamHeader as the tag, and the value is passed as the tag content, unless it’s a magic paramater. Magic Paramaters:

initParam

Initialization paramater for this call (just the key), therwise @initParam is used.

limit

A Result Limit array of two elements range and offset. If @resultLimit is set it’s used

if limit is not and if neither is set, no limit is applied.

If a block is provided, the limit’s range (or fewer) elements will yield to the block until the dataset is exhausted. If no limit is provided with the block a limit of [1,0] is assumed initially. Aliased to #method_missing. Alias the above slapiCall to #method_missing.



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/softlayer.rb', line 338

def slapiCall(method, args = { }, &block)

  initParam = args[:initParam] unless args[:initParam].nil?
  args.delete(:initParam) unless args[:initParam].nil?
  initParam = Param.new("#{self.soapClass}InitParameters", { 'id' => initParam }) unless initParam.nil?
  initParam = @initParam if initParam.nil?
  resultLimit = ResultLimit.new('resultLimit', args[:limit]) unless args[:limit].nil?
  args.delete(:limit) unless args[:limit].nil?
  resultLimit = @resultLimit if resultLimit.nil?

  @slapi.headerhandler << @authHeader unless @slapi.headerhandler.include?(@authHeader)
  paramHeaders = []
  unless args.nil?
    args.each do |k,v|
      p = Param.new(k.to_s,v)
      paramHeaders.push(p)
      @slapi.headerhandler << p
    end
  end
  @slapi.headerhandler << initParam unless @slapi.headerhandler.include?(@authHeader)
  @slapi.headerhandler << @objectMask unless @objectMask.nil?
  @slapi.headerhandler << resultLimit unless resultLimit.nil?

  if block_given?
    go=true
    resultLimit = ResultLimit.new('resultLimit', [1,0]) if resultLimit.nil? # this is broken.
    @slapi.headerhandler << resultLimit unless @slapi.headerhandler.include?(resultLimit)
    while(go) do
      res = realCall(method.to_s)
      yield(res) unless (res.nil? || (res.respond_to?(:empty) && res.empty?))
      go = false if res.nil?
      go = false if (res.respond_to?(:size) && (res.size < resultLimit.limit))
      resultLimit.offset=resultLimit.offset + resultLimit.limit
    end
    headerClean(resultLimit,paramHeaders)
    return true
  else
    res = realCall(method.to_s)
    headerClean(resultLimit,paramHeaders)
    return res
  end
end

Class Method Details

.cacheWSDLObject

Get the WSDL, parse it, and save it to a Class level hash. Returns false of we couldn’t parse the WSDL.



349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/softlayer.rb', line 349

def self.cacheWSDL
  return unless @@wsdl[self.soapClass].nil?

  begin
    # XXX: Silence soap4r's bogus use of Kernel#warn
    v = $VERBOSE
    $VERBOSE=nil
    @@wsdl[self.soapClass] = SOAP::WSDLDriverFactory.new(self.wsdlUrl)
    $VERBOSE = v
    return true
  rescue => e
    return SoftLayer::Exception.new(:exception => e)
  end 
end

.soapClassObject

Returns this Class’s SOAP Class.



375
376
377
# File 'lib/softlayer.rb', line 375

def self.soapClass
  self.name.to_s.gsub(/::/, '_')
end

.wsdlObject

Return this Class’s WSDL.



365
366
367
# File 'lib/softlayer.rb', line 365

def self.wsdl
  return @@wsdl[self.soapClass]
end

.wsdlUrlObject

Return this Class’s WSDL URL.



370
371
372
# File 'lib/softlayer.rb', line 370

def self.wsdlUrl
  return URI.parse("#{WSDLBASE}/#{self.soapClass}#{WSDLPARAM}")
end

Instance Method Details

#[](key) ⇒ Object

This returns key values from this Service’s associated Type (retrieved using #getObject).



222
223
224
225
# File 'lib/softlayer.rb', line 222

def [](key)
  @slapiObject = self.getObject if @slapiobject.nil?
  return @slapiObject[key.to_s]
end

#debug=(dev) ⇒ Object

Enable (or disable) debug. (paramater is the IO handler to write to)



343
344
345
# File 'lib/softlayer.rb', line 343

def debug=(dev)
  @slapi.wiredump_dev=(dev)
end

#objectMaskObject



259
260
261
# File 'lib/softlayer.rb', line 259

def objectMask
  return @objectMask
end

#objectMask=(mask) ⇒ Object

Set the object mask which ia passed as a hash of optional hashes (otherwise the hash elements should have a nil value). Using the example from the wiki:

<SoftLayer_AccountObjectMask xsi:type="v3:SoftLayer_AccountObjectMask">
  <mask xsi:type="slt:SoftLayer_Account" xmlns:slt="http://api.service.softlayer.com/soap/v3/SLTypes/">
      <domains>
          <resourceRecords />
      </domains>
      <openTickets>
          <assignedUser />
          <attachedHardware />
          <updates />
      </openTickets>
      <userCount />
  </mask>
</SoftLayer_AccountObjectMask>

{ 'domains' => { 'resourceRecords' => nil }, 'openTicket' => { 'assignedUser' => nil, 'attachedHardware' => nil, 'updates' => nil },
 userCount => nil }

Changing this resets the cached object used by #[]



250
251
252
253
254
255
256
257
# File 'lib/softlayer.rb', line 250

def objectMask=(mask)
  if mask.class == ObjectMask
    @objectMask = mask
  else
    @objectMask = ObjectMask.new("#{self.soapClass}ObjectMask", mask)
  end
  @slapiObject = nil
end

#resultLimitObject



279
280
281
# File 'lib/softlayer.rb', line 279

def resultLimit
  return @resultLimit
end

#resultLimit=(arg) ⇒ Object

Set an object wide result set (or clear it) arg can be one of three things:

  • nil clears the resultLimit

  • A Result Limit array of two elements range and offset.

  • An existing ResultLimit object



268
269
270
271
272
273
274
275
276
277
# File 'lib/softlayer.rb', line 268

def resultLimit=(arg)
  case arg.class
  when NilClass
    @resultLimit = nil
  when Array
    @resultLimit = ResultLimit.new('resultLimit',arg)
  when ResultLimit
    @resultLimit = arg
  end
end

#setObject(obj) ⇒ Object



227
228
229
# File 'lib/softlayer.rb', line 227

def setObject(obj)
  @slapiObject = obj
end

#slapiCall(method, args = { }, &block) ⇒ Object Also known as: method_missing, call

Make a direct api call. Paramaters are a hash where the key is passed to ParamHeader as the tag, and the value is passed as the tag content, unless it’s a magic paramater. Magic Paramaters:

initParam

Initialization paramater for this call (just the key), therwise @initParam is used.

limit

A Result Limit array of two elements range and offset. If @resultLimit is set it’s used

if limit is not and if neither is set, no limit is applied.

If a block is provided, the limit’s range (or fewer) elements will yield to the block until the dataset is exhausted. If no limit is provided with the block a limit of [1,0] is assumed initially. Aliased to #method_missing.



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/softlayer.rb', line 294

def slapiCall(method, args = { }, &block)

  initParam = args[:initParam] unless args[:initParam].nil?
  args.delete(:initParam) unless args[:initParam].nil?
  initParam = Param.new("#{self.soapClass}InitParameters", { 'id' => initParam }) unless initParam.nil?
  initParam = @initParam if initParam.nil?
  resultLimit = ResultLimit.new('resultLimit', args[:limit]) unless args[:limit].nil?
  args.delete(:limit) unless args[:limit].nil?
  resultLimit = @resultLimit if resultLimit.nil?

  @slapi.headerhandler << @authHeader unless @slapi.headerhandler.include?(@authHeader)
  paramHeaders = []
  unless args.nil?
    args.each do |k,v|
      p = Param.new(k.to_s,v)
      paramHeaders.push(p)
      @slapi.headerhandler << p
    end
  end
  @slapi.headerhandler << initParam unless @slapi.headerhandler.include?(@authHeader)
  @slapi.headerhandler << @objectMask unless @objectMask.nil?
  @slapi.headerhandler << resultLimit unless resultLimit.nil?

  if block_given?
    go=true
    resultLimit = ResultLimit.new('resultLimit', [1,0]) if resultLimit.nil? # this is broken.
    @slapi.headerhandler << resultLimit unless @slapi.headerhandler.include?(resultLimit)
    while(go) do
      res = realCall(method.to_s)
      yield(res) unless (res.nil? || (res.respond_to?(:empty) && res.empty?))
      go = false if res.nil?
      go = false if (res.respond_to?(:size) && (res.size < resultLimit.limit))
      resultLimit.offset=resultLimit.offset + resultLimit.limit
    end
    headerClean(resultLimit,paramHeaders)
    return true
  else
    res = realCall(method.to_s)
    headerClean(resultLimit,paramHeaders)
    return res
  end
end

#soapClassObject

Return this object’s matching SLAPI SOAP Class.



217
218
219
# File 'lib/softlayer.rb', line 217

def soapClass
  return self.class.to_s.gsub(/::/, '_')
end