Class: JSS::APIObject
Overview
This class is the parent to all JSS API objects. It provides standard methods and structures that apply to all API resouces.
See the README.md file for general info about using subclasses of JSS::APIObject
Subclassing
Constructor
In general, subclasses should do any class-specific argument checking before calling super, and then afterwards, use the contents of @init_data to populate any class-specific attributes. @id, @name, @rest_rsrc, and @in_jss are handled here.
If a subclass can be looked up by some key other than :name or :id, the subclass must pass the keys as an Array in the second argument when calling super from #initialize. See Computer#initialize for an example of how to implement this feature.
Object Creation
If a subclass should be able to be created in the JSS be sure to include Creatable
The constructor should verify any extra required data (aside from :name) in the args before or after calling super.
See Creatable for more details.
Object Modification
If a subclass should be modifiable in the JSS, include Updatable, q.v. for details.
Object Deletion
All subclasses can be deleted in the JSS.
Required Constants
Subclasses must provide certain Constants in order to correctly interpret API data and communicate with the API.
RSRC_BASE = [String], The base for REST resources of this class
e.g. ‘computergroups’ in “casper.mycompany.com:8443/JSSResource/computergroups/id/12”
RSRC_LIST_KEY = [Symbol] The Hash key for the JSON list output of all objects of this class in the JSS.
e.g. the JSON output of resource “JSSResource/computergroups” is a hash with one item (an Array of computergroups). That item’s key is the Symbol :computer_groups
RSRC_OBJECT_KEY = [Symbol] The Hash key used for individual JSON object output.
It’s also used in various error messages
e.g. the JSON output of the resource “JSSResource/computergroups/id/436” is a hash with one item (another hash with details of one computergroup). That item’s key is the Symbol :computer_group
VALID_DATA_KEYS = [Array<Symbol>] The Hash keys used to verify validity of :data
When instantiating a subclass using :data => somehash, some minimal checks are performed to ensure the data is valid for the subclass
The Symbols in this Array are compared to the keys of the hash provided. If any of these don’t exist in the hash’s keys, then the :data is not valid and an exception is raised.
The keys :id and :name must always exist in the hash. If only :id and :name are valid, VALID_DATA_KEYS should be an empty array.
e.g. for a department, only :id and :name are valid, so VALID_DATA_KEYS is an empty Array ([]) but for a computer group, the keys :computers and :is_smart must be present as well. so VALID_DATA_KEYS will be [:computers, :is_smart]
NOTE Some API objects have data broken into subsections, in which case the VALID_DATA_KEYS are expected in the section :general.
Optional Constants
OTHER_LOOKUP_KEYS = [HashSymbol=>Hash] Every object can be looked up by
:id and :name, but some have other uniq identifiers that can also be used, e.g. :serial_number, :mac_address, and so on. This Hash, if defined, speficies those other keys for the subclass For more details about this hash, see DEFAULT_LOOKUP_KEYS, APIObject.fetch, and APIObject#lookup_object_data
Direct Known Subclasses
Account, AdvancedSearch, Building, Category, Computer, ComputerInvitation, Department, DistributionPoint, EBook, ExtensionAttribute, Group, LDAPServer, MacApplication, MobileDevice, MobileDeviceApplication, MobileDeviceConfigurationProfile, NetBootServer, NetworkSegment, OSXConfigurationProfile, Package, Peripheral, PeripheralType, Policy, RemovableMacAddress, RestrictedSoftware, Script, Site, SoftwareUpdateServer, User, WebHook
Constant Summary collapse
- REQUIRED_DATA_KEYS =
These Symbols are added to VALID_DATA_KEYS for performing the :data validity test described above.
[:id, :name].freeze
- DEFAULT_LOOKUP_KEYS =
All API objects have an id and a name. As such By these keys are available for object lookups.
Others can be defined by subclasses in their OTHER_LOOKUP_KEYS constant which has the same format, described here:
The merged Hashes DEFAULT_LOOKUP_KEYS and OTHER_LOOKUP_KEYS define what unique identifiers can be passed as parameters to the fetch method for retrieving an object from the API. They also define the class methods that return a list (Array) of all such identifiers for the class (e.g. the :all_ids class method returns an array of all id’s for an APIObject subclass)
Since there’s often a discrepency between the name of the identifier as an attribute (e.g. serial_number) and the REST resource key for retrieving that object (e.g. ../computers/serialnumber/xxxxx) this hash also explicitly provides the REST resource key for a given lookup key, so e.g. both serialnumber and serial_number can be used, and both will have the resource key ‘serialnumber’ and the list method ‘:all_serial_numbers’
Here’s how the Hash is structured, using serialnumber as an example:
LOOKUP_KEYS =
serialnumber: {rsrc_key: :serialnumber, list: :all_serial_numbers, serial_number: :serialnumber, list: :all_serial_numbers
}
{ id: {rsrc_key: :id, list: :all_ids}, name: {rsrc_key: :name, list: :all_names} }.freeze
Instance Attribute Summary collapse
-
#id ⇒ Integer
readonly
The JSS id number.
-
#in_jss ⇒ Boolean
(also: #in_jss?)
readonly
Is it in the JSS?.
-
#name ⇒ String
readonly
The name.
-
#rest_rsrc ⇒ String
readonly
The Rest resource for API access (the part after “JSSResource/” ).
Class Method Summary collapse
-
.all(refresh = false) ⇒ Array<Hash{:name=>String, :id=> Integer}>
Return an Array of Hashes for all objects of this subclass in the JSS.
-
.all_ids(refresh = false) ⇒ Array<Integer>
Returns an Array of the JSS id numbers of all the members of the subclass.
-
.all_names(refresh = false) ⇒ Array<String>
Returns an Array of the JSS names of all the members of the subclass.
-
.all_objects(refresh = false) ⇒ Hash{Integer => Object}
Return an Array of JSS::APIObject subclass instances e.g when called on JSS::Package, return all JSS::Package objects in the JSS.
-
.exist?(identfier, refresh = false) ⇒ Boolean
Return true or false if an object of this subclass with the given name or id exists on the server.
-
.fetch(arg) ⇒ APIObject
Retrieve an object from the API.
-
.get_name(a_thing) ⇒ String
Some API objects contain references to other API objects.
-
.lookup_key_list_methods ⇒ Hash
The available lookup keys mapped to the appropriate list class method (e.g. id: :all_ids ).
-
.lookup_keys ⇒ Array<Symbol>
What are all the lookup keys available for this class?.
-
.make(args = {}) ⇒ APIObject
Make a ruby instance of a not-yet-existing APIObject.
-
.map_all_ids_to(other_key, refresh = false) ⇒ Hash{Integer => Oject}
Return a hash of all objects of this subclass in the JSS where the key is the id, and the value is some other key in the data items returned by the JSS::APIObject.all.
-
.rsrc_keys ⇒ Hash
The available lookup keys mapped to the appropriate resource key for building a REST url to retrieve an object.
-
.valid_id(identfier, refresh = false) ⇒ Integer?
Return an id or nil if an object of this subclass with the given name or id exists on the server.
-
.xml_list(array, content = :name) ⇒ REXML::Element
Convert an Array of Hashes of API object data to a REXML element.
Instance Method Summary collapse
-
#categorizable? ⇒ Boolean
See Categorizable.
-
#creatable? ⇒ Boolean
See Creatable.
-
#criterable? ⇒ Boolean
See criteriable.
-
#delete ⇒ void
Delete this item from the JSS.
-
#extendable? ⇒ Boolean
See extendable.
-
#initialize(args = {}) ⇒ APIObject
constructor
The args hash must include :id, :name, or :data.
-
#locatable? ⇒ Boolean
See Locatable.
-
#matchable? ⇒ Boolean
See Matchable.
-
#purchasable? ⇒ Boolean
See Purchasable.
-
#save ⇒ Integer
Either Create or Update this object in the JSS.
-
#scopable? ⇒ Boolean
See Scopable.
-
#self_servable? ⇒ Boolean
See SelfServable.
-
#to_s ⇒ String
A meaningful string representation of this object.
-
#updatable? ⇒ Boolean
See Updatable.
-
#uploadable? ⇒ Boolean
See Uploadable.
-
#vppable? ⇒ Boolean
See VPPable.
Constructor Details
#initialize(args = {}) ⇒ APIObject
The args hash must include :id, :name, or :data.
-
:id or :name will be looked up via the API
-
if the subclass includes JSS::Creatable, :id can be :new, to create a new object in the JSS. and :name is required
-
-
:data must be the JSON output of a separate JSS::APIConnection query (a Hash of valid object data)
Some subclasses can accept other options, by pasing their keys in a final Array
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 |
# File 'lib/jss/api_object.rb', line 537 def initialize(args = {}) raise JSS::UnsupportedError, 'JSS::APIObject cannot be instantiated' if self.class == JSS::APIObject ####### Previously looked-up JSON data # DEPRECATED: pre-lookedup data is never used # and support for it will be going away. if args[:data] @init_data = args[:data] validate_external_init_data ###### Make a new one in the JSS, but only if we've included the Creatable module elsif args[:id] == :new validate_init_for_creation(args) setup_object_for_creation(args) return ###### Look up the data via the API else @init_data = look_up_object_data(args) end ## end arg parsing parse_init_data @need_to_update = false end |
Instance Attribute Details
#id ⇒ Integer (readonly)
Returns the JSS id number.
505 506 507 |
# File 'lib/jss/api_object.rb', line 505 def id @id end |
#in_jss ⇒ Boolean (readonly) Also known as: in_jss?
Returns is it in the JSS?.
511 512 513 |
# File 'lib/jss/api_object.rb', line 511 def in_jss @in_jss end |
#name ⇒ String (readonly)
Returns the name.
508 509 510 |
# File 'lib/jss/api_object.rb', line 508 def name @name end |
#rest_rsrc ⇒ String (readonly)
Returns the Rest resource for API access (the part after “JSSResource/” ).
514 515 516 |
# File 'lib/jss/api_object.rb', line 514 def rest_rsrc @rest_rsrc end |
Class Method Details
.all(refresh = false) ⇒ Array<Hash{:name=>String, :id=> Integer}>
Return an Array of Hashes for all objects of this subclass in the JSS.
This method is only valid in subclasses of JSS::APIObject, and is the parsed JSON output of an API query for the resource defined in the subclass’s RSRC_BASE, e.g. for JSS::Computer, with the RSRC_BASE of :computers, This method retuens the output of the ‘JSSResource/computers’ resource, which is a list of all computers in the JSS.
Each item in the Array is a Hash with at least two keys, :id and :name. The class methods .all_ids and .all_names provide easier access to those data as mapped Arrays.
Some API classes provide other data in each Hash, e.g. :udid (for computers and mobile devices) or :is_smart (for groups).
Subclasses implementing those API classes should provide .all_xxx class methods for accessing those other values as mapped Arrays, e.g. JSS::Computer.all_udids
The results of the first query for each subclass is stored in JSS.api.object_list_cache and returned at every future call, so as to not requery the server every time.
To force requerying to get updated data, provided a non-false argument. I usually use :refresh, so that it’s obvious what I’m doing, but true, 1, or anything besides false or nil will work.
156 157 158 159 160 161 |
# File 'lib/jss/api_object.rb', line 156 def self.all(refresh = false) raise JSS::UnsupportedError, '.all can only be called on subclasses of JSS::APIObject' if self == JSS::APIObject JSS.api.object_list_cache[self::RSRC_LIST_KEY] = nil if refresh return JSS.api.object_list_cache[self::RSRC_LIST_KEY] if JSS.api.object_list_cache[self::RSRC_LIST_KEY] JSS.api.object_list_cache[self::RSRC_LIST_KEY] = JSS.api.get_rsrc(self::RSRC_BASE)[self::RSRC_LIST_KEY] end |
.all_ids(refresh = false) ⇒ Array<Integer>
Returns an Array of the JSS id numbers of all the members of the subclass.
e.g. When called from subclass JSS::Computer, returns the id’s of all computers in the JSS
173 174 175 |
# File 'lib/jss/api_object.rb', line 173 def self.all_ids(refresh = false) all(refresh).map { |i| i[:id] } end |
.all_names(refresh = false) ⇒ Array<String>
Returns an Array of the JSS names of all the members of the subclass.
e.g. When called from subclass JSS::Computer, returns the names of all computers in the JSS
187 188 189 |
# File 'lib/jss/api_object.rb', line 187 def self.all_names(refresh = false) all(refresh).map { |i| i[:name] } end |
.all_objects(refresh = false) ⇒ Hash{Integer => Object}
Return an Array of JSS::APIObject subclass instances e.g when called on JSS::Package, return all JSS::Package objects in the JSS.
NOTE: This may be slow as it has to look up each object individually! use it wisely.
233 234 235 236 237 238 |
# File 'lib/jss/api_object.rb', line 233 def self.all_objects(refresh = false) objects_key = "#{self::RSRC_LIST_KEY}_objects".to_sym JSS.api.object_list_cache[objects_key] = nil if refresh return JSS.api.object_list_cache[objects_key] if JSS.api.object_list_cache[objects_key] JSS.api.object_list_cache[objects_key] = all(refresh).map { |o| new id: o[:id] } end |
.exist?(identfier, refresh = false) ⇒ Boolean
Return true or false if an object of this subclass with the given name or id exists on the server
249 250 251 252 253 254 255 256 257 258 |
# File 'lib/jss/api_object.rb', line 249 def self.exist?(identfier, refresh = false) case identfier when Integer all_ids(refresh).include? identfier when String all_names(refresh).include? identfier else raise ArgumentError, 'Identifier must be a name (String) or id (Integer)' end end |
.fetch(arg) ⇒ APIObject
Retrieve an object from the API.
This is the preferred way to retrieve existing objects from the JSS. It’s a wrapper for using APIObject.new and avoids the confusion of using ruby’s .new class method when you’re not creating a new object.
For creating new objects in the JSS, use make
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 |
# File 'lib/jss/api_object.rb', line 417 def self.fetch(arg) raise JSS::UnsupportedError, 'JSS::APIObject cannot be instantiated' if self.class == JSS::APIObject # if given a hash (or a colletion of named params) # pass to .new if arg.is_a? Hash raise ArgumentError, 'Use .create to create new JSS objects' if arg[:id] == :new return new arg end # loop thru the lookup_key list methods for this class # and if it's result includes the desired value, # the pass they key and arg to .new lookup_key_list_methods.each do |key, method_name| return new({key => arg}) if self.send(method_name).include? arg end # each key # if we're here, we couldn't find a matching object raise NoSuchItemError, "No #{self::RSRC_OBJECT_KEY} found matching '#{arg}'" end |
.get_name(a_thing) ⇒ String
Some API objects contain references to other API objects. Usually those references are a Hash containing the :id and :name of the target. Sometimes, however the reference is just the name of the target.
A Script has a property :category, which comes from the API as a String, the name of the category for that script. e.g. “GoodStuff”
A Policy also has a property :category, but it comes from the API as a Hash with both the name and id, e.g. {:id => 8, :name => “GoodStuff”}
When that reference is to a single thing (like the category to which something belongs) APIObject subclasses usually store only the name, and use the name when returning data to the API.
When an object references a list of related objects (like the computers assigned to a user) that list will be and Array of Hashes as above, with both the :id and :name
This method is just a handy way to extract the name regardless of how it comes from the API. Most APIObject subclasses use it in their #initialize method
354 355 356 357 358 359 360 361 362 363 |
# File 'lib/jss/api_object.rb', line 354 def self.get_name(a_thing) case a_thing when String a_thing when Hash a_thing[:name] when nil nil end end |
.lookup_key_list_methods ⇒ Hash
Returns the available lookup keys mapped to the appropriate list class method (e.g. id: :all_ids ).
392 393 394 395 396 397 398 399 400 401 |
# File 'lib/jss/api_object.rb', line 392 def self.lookup_key_list_methods hash = {} all_keys = if defined?(self::OTHER_LOOKUP_KEYS) DEFAULT_LOOKUP_KEYS.merge self::OTHER_LOOKUP_KEYS else DEFAULT_LOOKUP_KEYS end all_keys.each { |key, deets| hash[key] = deets[:list]} hash end |
.lookup_keys ⇒ Array<Symbol>
What are all the lookup keys available for this class?
370 371 372 373 |
# File 'lib/jss/api_object.rb', line 370 def self.lookup_keys return DEFAULT_LOOKUP_KEYS.keys unless defined? self::OTHER_LOOKUP_KEYS DEFAULT_LOOKUP_KEYS.keys + self::OTHER_LOOKUP_KEYS.keys end |
.make(args = {}) ⇒ APIObject
Make a ruby instance of a not-yet-existing APIObject.
This is the preferred way to create new objects in the JSS. It’s a wrapper for using APIObject.new with the ‘id: :new’ parameter. and helps avoid the confusion of using ruby’s .new class method for making ruby instances.
For retrieving existing objects in the JSS, use fetch
For actually creating the object in the JSS, see APIObject#create
454 455 456 457 458 459 |
# File 'lib/jss/api_object.rb', line 454 def self.make(args = {}) raise JSS::UnsupportedError, 'JSS::APIObject cannot be instantiated' if self.class == JSS::APIObject raise ArgumentError, "Use '#{self.class}.fetch id: xx' to retrieve existing JSS objects" if args[:id] args[:id] = :new new args end |
.map_all_ids_to(other_key, refresh = false) ⇒ Hash{Integer => Oject}
Return a hash of all objects of this subclass in the JSS where the key is the id, and the value is some other key in the data items returned by the JSS::APIObject.all.
If the other key doesn’t exist in the API data, (eg :udid for JSS::Department) the values will be nil.
Use this method to map ID numbers to other identifiers returned by the API list resources. Invert its result to map the other identfier to ids.
217 218 219 220 221 |
# File 'lib/jss/api_object.rb', line 217 def self.map_all_ids_to(other_key, refresh = false) h = {} all(refresh).each { |i| h[i[:id]] = i[other_key] } h end |
.rsrc_keys ⇒ Hash
Returns the available lookup keys mapped to the appropriate resource key for building a REST url to retrieve an object.
378 379 380 381 382 383 384 385 386 387 |
# File 'lib/jss/api_object.rb', line 378 def self.rsrc_keys hash = {} all_keys = if defined?(self::OTHER_LOOKUP_KEYS) DEFAULT_LOOKUP_KEYS.merge self::OTHER_LOOKUP_KEYS else DEFAULT_LOOKUP_KEYS end all_keys.each { |key, deets| hash[key] = deets[:rsrc_key]} hash end |
.valid_id(identfier, refresh = false) ⇒ Integer?
Return an id or nil if an object of this subclass with the given name or id exists on the server
Subclasses may want to override this method to support more identifiers than name and id.
272 273 274 275 276 277 278 279 280 281 |
# File 'lib/jss/api_object.rb', line 272 def self.valid_id(identfier, refresh = false) case identfier when Integer return identfier if all_ids(refresh).include? identfier when String return map_all_ids_to(:name).invert[identfier] if all_names(refresh).include? identfier else raise ArgumentError, 'Identifier must be a name (String) or id (Integer)' end end |
.xml_list(array, content = :name) ⇒ REXML::Element
Convert an Array of Hashes of API object data to a REXML element.
Given an Array of Hashes of items in the subclass where each Hash has at least an :id or a :name key, (as what comes from the .all class method) return a REXML <classes> element with one <class> element per Hash member.
325 326 327 |
# File 'lib/jss/api_object.rb', line 325 def self.xml_list(array, content = :name) JSS.item_list_to_rexml_list self::RSRC_LIST_KEY, self::RSRC_OBJECT_KEY, array, content end |
Instance Method Details
#categorizable? ⇒ Boolean
Returns See Categorizable.
604 605 606 |
# File 'lib/jss/api_object.rb', line 604 def categorizable? defined? self.class::CATEGORIZABLE end |
#creatable? ⇒ Boolean
Returns See Creatable.
594 595 596 |
# File 'lib/jss/api_object.rb', line 594 def creatable? defined? self.class::CREATABLE end |
#criterable? ⇒ Boolean
Returns See criteriable.
619 620 621 |
# File 'lib/jss/api_object.rb', line 619 def criterable? defined? self.class::CRITERIABLE end |
#delete ⇒ void
This method returns an undefined value.
Delete this item from the JSS.
Subclasses may want to redefine this method, first calling super, then setting other attributes to nil, false, empty, etc..
661 662 663 664 665 666 667 668 669 |
# File 'lib/jss/api_object.rb', line 661 def delete return nil unless @in_jss JSS.api.delete_rsrc @rest_rsrc @rest_rsrc = "#{self.class::RSRC_BASE}/name/#{CGI.escape @name}" @id = nil @in_jss = false @need_to_update = false :deleted end |
#extendable? ⇒ Boolean
Returns See extendable.
624 625 626 |
# File 'lib/jss/api_object.rb', line 624 def extendable? defined? self.class::EXTENDABLE end |
#locatable? ⇒ Boolean
Returns See Locatable.
634 635 636 |
# File 'lib/jss/api_object.rb', line 634 def locatable? defined? self.class::LOCATABLE end |
#matchable? ⇒ Boolean
Returns See Matchable.
629 630 631 |
# File 'lib/jss/api_object.rb', line 629 def matchable? defined? self.class::MATCHABLE end |
#purchasable? ⇒ Boolean
Returns See Purchasable.
639 640 641 |
# File 'lib/jss/api_object.rb', line 639 def purchasable? defined? self.class::PURCHASABLE end |
#save ⇒ Integer
Either Create or Update this object in the JSS
If this item is creatable or updatable, then create it if needed, or update it if it already exists.
577 578 579 580 581 582 583 584 585 |
# File 'lib/jss/api_object.rb', line 577 def save if @in_jss raise JSS::UnsupportedError, 'Updating this object in the JSS is currently not supported' unless updatable? update else raise JSS::UnsupportedError, 'Creating this object in the JSS is currently not supported' unless creatable? create end end |
#scopable? ⇒ Boolean
Returns See Scopable.
644 645 646 |
# File 'lib/jss/api_object.rb', line 644 def scopable? defined? self.class::SCOPABLE end |
#self_servable? ⇒ Boolean
Returns See SelfServable.
614 615 616 |
# File 'lib/jss/api_object.rb', line 614 def self_servable? defined? self.class::SELF_SERVABLE end |
#to_s ⇒ String
A meaningful string representation of this object
675 676 677 |
# File 'lib/jss/api_object.rb', line 675 def to_s "#{self.class}, name: #{@name}, id: #{@id}" end |
#updatable? ⇒ Boolean
Returns See Updatable.
599 600 601 |
# File 'lib/jss/api_object.rb', line 599 def updatable? defined? self.class::UPDATABLE end |
#uploadable? ⇒ Boolean
Returns See Uploadable.
649 650 651 |
# File 'lib/jss/api_object.rb', line 649 def uploadable? defined? self.class::UPLOADABLE end |
#vppable? ⇒ Boolean
Returns See VPPable.
609 610 611 |
# File 'lib/jss/api_object.rb', line 609 def vppable? defined? self.class::VPPABLE end |