haunm haunm - 1 year ago 75
Ruby Question

Ruby : SoftLayer API : getPendingEvents fails to show many relational properties

I'm trying to make a softlayer API call using Ruby to see upcoming maintenance and the machines that may be effected by the maintenance. I have a few questions but I'm running into an issue seeing many of the relational properties documented here:

Here is my simple program:

require 'rubygems'
require 'softlayer_api'
require 'pp'

client = SoftLayer::Client.new(:username => user, :api_key => api_key, :timeout => 99999999)

account = client['Account'].object_mask("mask[pendingEventCount]").getObject()
pending_event_count = account["pendingEventCount"]

current_time = Time.now.to_i

for i in 0..(pending_event_count/30.0).ceil - 1
list_of_pending_events = client['Account'].result_limit(i*30,30).object_mask("mask[id, startDate, endDate, recoveryTime, subject, summary]").getPendingEvents
for x in 0..list_of_pending_events.length - 1
start_time = DateTime.parse(list_of_pending_events[x]['startDate']).to_time.to_i
if start_time > current_time
pp list_of_pending_events[x]

The above works, but if I try to add a relational property to the mask, such as "impactedResources" it will fail saying that property does not belong to SoftLayer_Notification_Occurrence_Event. Can someone help explain why this, and many other, relational properties are not valid in the above call?

Also, two quick other questions on this topic:

1) Why do some of the results in getPendingEvents have start AND end times in the past? And why do some have a missing end time altogether? Notice I'm checking if the start time is greater than the current time as there seems to be old maintenance data in the results.

2) Am I taking the right approach for getting upcoming maintenance and figuring out machines that will be impacted? (Using getPendingEvents and then the 'impactedResources' property)

Answer Source

I used your code and I added the "impactedResources" to the object mask and it worked fine. You may be getting the issue, because your API user does not have enough permissions, I recomend you try with the master user.

regarding to your other questions:

1.- When I ran your code I got only 3 events whose startDate is greater than the current time, so it worked fine for me. If you still are getting that issue you can try using objectFilters.

require 'rubygems'
require 'softlayer_api'
require 'pp'

client = SoftLayer::Client.new(:username => user, :api_key => apikey, :timeout => 99999999)

object_filter = SoftLayer::ObjectFilter.new
object_filter.set_criteria_for_key_path('pendingEvents.startDate',           'operation' => 'greaterThanDate',
                                                                                  'options' => [{
                                                                                    'name' => 'date',
                                                                                    'value' => ['9/8/2016']

list_of_pending_events = client['Account'].object_filter(object_filter).object_mask("mask[id, startDate, endDate, recoveryTime, subject, summary]").getPendingEvents
pp list_of_pending_events

2.- Yes, that approach wil work.


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download