Method: JSS::APIObject.valid_id

Defined in:
lib/jss/api_object.rb

.valid_id(identifier, refresh = false, api: JSS.api) ⇒ Integer?

Return the id of the object of this subclass with the given identifier.

Return nil if no object has an identifier that matches.

For all objects the ‘name’ is an identifier. Some objects have more, e.g. udid, mac_address & serial_number. Matches are case-insensitive.

NOTE: while name is an identifier, for Computers and MobileDevices, it need not be unique in Jamf. If name is matched, which one gets returned is undefined. In short - dont’ use names here unless you know they are unique.

one of the available lookup_keys

Parameters:

  • identfier (String, Integer)

    An identifier for an object, a value for

  • refresh (Boolean) (defaults to: false)

    Should the data be re-read from the server

  • api (JSS::APIConnection) (defaults to: JSS.api)

    an API connection to use for the query. Defaults to the corrently active API. See JSS::APIConnection

Returns:

  • (Integer, nil)

    the id of the matching object, or nil if it doesn’t exist



584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
# File 'lib/jss/api_object.rb', line 584

def self.valid_id(identifier, refresh = false, api: JSS.api)

  # refresh if needed
  all(refresh, api: api) if refresh

  # it its a valid id, return it
  return identifier if all_ids(api: api).include? identifier

  keys_to_check = lookup_keys(no_aliases: true)
  keys_to_check.delete :id # we've already checked :id

  keys_to_check.each do |key|
    mapped_ids = map_all_ids_to key, api: api
    matches = mapped_ids.select { |_id, ident| ident.casecmp? identifier }
    # If exactly one match, return the id
    return matches.keys.first if matches.size == 1
  end

  nil
end