Skip to content

JNUC2019 Lab Session G Creating Objects

Chris Lasell edited this page Oct 31, 2019 · 7 revisions

Hands On, Real-time Classic API Using ruby-jss

(Home)

Lab Session Contents - 6 - Creating Objects

Previous           TOC           Next


Creating a Static Group

  • Let's create a static group and do some work with it

    • ruby-jss can be used to create smart groups
      • Dealing with criteria programmatically is more complex than we have time for today
  • First, invent a unique name for your group, and store it in a variable:

    • Use something that no one else in this room will use
    • Store it in the variable grp_name
# REPLACE THE NAME IN THE QUOTES WITH SOMETHING UNIQUE
grp_name = 'my-awesome-computer-group'
# => "my-awesome-computer-group"
  • Now create our group object in ruby, and store it in another variable
my_grp = JSS::ComputerGroup.make name: grp_name, type: :static ;0
# => 0

pp my_grp ;0
# [group info]
# => 0
  • For objects in ruby-jss that are 'creatable', you use the make class method to make a new one

    • You must always provide a name: parameter, some classes require other info
    • Group objects require the type: parameter which must be either :static or :smart
  • This creates a local instance of the JSS::ComputerGroup class

    • It isn't yet stored in the JSS, it only exists in ruby's memory
  • For now, we don't need to set any other values for this group, so lets save it to the JSS:

my_grp_id = my_grp.save
# => 61
  • save sends changes to the API, either creating or updating the object in the JSS

    • You can also use create or update as appropriate
  • Whenever you use save, or create or update on a ruby-jss object, the return value is the id number of the thing you created or updated.

Creating Five Computers

  • You rarely need to create computer records using the API

  • But we need to do that here, so we each can have our own computer records to work with

  • We're going to create very minimal records, just enough to do what we need

    • JSS::Computer objects have LOTS of data in them, way more than we can look at today
  • First, pick another unique name to use as the base-name for your computers:

    • Again, choose something no one else will
    • Store it in the variable comp_base_name
# REPLACE THE NAME IN THE QUOTES WITH SOMETHING UNIQUE
comp_base_name = 'my-awesome-computer'
# => "my-awesome-computer"
  • Next, make an empty array to hold the ids of the computers we create, we'll use them later:
new_comp_ids = []
# => []
  • Now we're going to do a loop 5 times and make new computer records:
5.times do |pass_num|
  comp_name = "#{comp_base_name}-#{pass_num}"

  comp_sn = comp_name + "-sn"

  comp_udid = `uuidgen`.chomp

  comp = JSS::Computer.make name: comp_name, sn: comp_sn, udid: comp_udid; 0

  new_comp_ids << comp.save

  puts "Created Computer '#{comp_name}'"
  puts "  id: #{new_comp_ids.last}"
  puts "  sn: #{comp_sn}"
  puts "  udid: #{comp_udid}"
end
# [ lines of output]
# => 5
  • Oh look! An iterator method!

    • They aren't just for arrays, times works with integers.
  • We use the base-name and pass-number thru the loop to create a unique name and serialnumber

  • We create the udid from the output of a shell command uuidgen

    • The chomp method removes the trailing newline we got from the shell
  • Then we make our local ruby Computer instance

    • Again, until we save it, it only exists in local memory, not in the JSS
  • In one step, we save it to the JSS, and append the new id number to the array we created above

    • << will append something to the end of an array
  • Finally, we output some lines saying what we did

  • To see that your computers were created, try this:

pp JSS::Computer.all_names(:refresh).select { |name| name.start_with? comp_base_name } ;0
# [array of computer names]
# => 0

Previous           TOC           Next

Clone this wiki locally