-
Notifications
You must be signed in to change notification settings - Fork 30
JNUC2019 Lab Session F Fetching Objects
-
Let's get a report of all network segments with specified distribution points
-
Unfortunately, the summary-list data from
NetworkSegment.all
only includes their name, id, and start/end address
pp JSS::NetworkSegment.all ;0
# [Array of Hashes]
# => 0
-
For any other info about a network segment, we must fetch the full object from the API and look at it
-
This is slower, but gives us far more info to work with than the summary-lists
JSS::NetworkSegment.all_ids.each do |ns_id|
net_seg = JSS::NetworkSegment.fetch id: ns_id
next unless net_seg.distribution_point
puts "Name: '#{net_seg.name}'\n Dist. Point: '#{net_seg.distribution_point}'"
sleep (rand + 1)
end ;0
# [lines of output]
# => 0
-
Here we're using
each
to loop thru the ids of all network segments -
Each time thru, we use the id to fetch the full network segment
- We store it in the variable
net_seg
- We store it in the variable
-
net_seg
contains an object, an 'instance' of the classJSS::NetworkSegment
- Instances of classes have their own methods, separate from the methods of the class itself
-
NetworkSegment objects have a
distribution_point
method that returns the name of the distribution point, or nil if none is assigned -
So the
next
line skips those that have no distribution point set -
Then we print out two lines with the network segment name and the distribution point
- As with
echo -n
in bash, inside double-quotes,\n
means a return character
- As with
-
And finally, we pause for some random amount of time between zero and two seconds before going to the next
- Just for this lab, because otherwise we might swamp the API server
- Which is a special, public API that has more limits than a private one
- Just for this lab, because otherwise we might swamp the API server