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.



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/softlayer.rb', line 181

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
  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 call method to #method_missing.



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/softlayer.rb', line 319

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 = @slapi.call(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 = @slapi.call(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.



332
333
334
335
336
337
338
339
340
341
# File 'lib/softlayer.rb', line 332

def self.cacheWSDL
  return unless @@wsdl[self.soapClass].nil?
  
  begin
    @@wsdl[self.soapClass] = SOAP::WSDLDriverFactory.new(self.wsdlUrl)
    return true
  rescue => e
    return false
  end 
end

.soapClassObject

Returns this Class’s SOAP Class.



354
355
356
# File 'lib/softlayer.rb', line 354

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

.wsdlObject

Return this Class’s WSDL.



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

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

.wsdlUrlObject

Return this Class’s WSDL URL.



349
350
351
# File 'lib/softlayer.rb', line 349

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).



203
204
205
206
# File 'lib/softlayer.rb', line 203

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

#call(method, args = { }, &block) ⇒ Object



321
322
323
# File 'lib/softlayer.rb', line 321

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

#debug=(dev) ⇒ Object

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



326
327
328
# File 'lib/softlayer.rb', line 326

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

#objectMaskObject



240
241
242
# File 'lib/softlayer.rb', line 240

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 #[]



231
232
233
234
235
236
237
238
# File 'lib/softlayer.rb', line 231

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

#resultLimitObject



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

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



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

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



208
209
210
# File 'lib/softlayer.rb', line 208

def setObject(obj)
  @slapiObject = obj
end

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

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.



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/softlayer.rb', line 275

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 = @slapi.call(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 = @slapi.call(method.to_s)
    headerClean(resultLimit,paramHeaders)
    return res
  end
end

#soapClassObject

Return this object’s matching SLAPI SOAP Class.



198
199
200
# File 'lib/softlayer.rb', line 198

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