module Jamf::Prestage::ClassMethods

Class Methods

Public Class Methods

extended(extender) click to toggle source

when this module is included, also extend our Class Methods

   # File lib/jamf/api/jamf_pro/mixins/prestage.rb
67 def self.extended(extender)
68   Jamf.load_msg "--> #{extender} is extending Jamf::Prestage::ClassMethods"
69 end

Public Instance Methods

assign(*sns_to_assign, to_prestage:, cnx: Jamf.cnx) click to toggle source

Assign one or more serialNumbers to a prestage @return [Jamf::OAPISchemas::PrestageScopeResponseV2] the new scope for the prestage

    # File lib/jamf/api/jamf_pro/mixins/prestage.rb
182 def assign(*sns_to_assign, to_prestage:, cnx: Jamf.cnx)
183   prestage_id = valid_id to_prestage
184   raise Jamf::NoSuchItemError, "No #{self} matching '#{to_prestage}'" unless prestage_id
185 
186   # upcase all sns
187   sns_to_assign.map!(&:to_s)
188   sns_to_assign.map!(&:upcase)
189 
190   # get the current scope of the prestage
191   spath = scope_path(prestage_id)
192   scope = INSTANCE_SCOPE_OBJECT.new cnx.get(spath)
193 
194   # add the new sns to the existing ones
195   new_scope_sns = scope.assignments.map(&:serialNumber)
196   new_scope_sns += sns_to_assign
197   new_scope_sns.uniq!
198 
199   update_scope(spath, new_scope_sns, scope.versionLock, cnx)
200 end
assigned?(sn, prestage: nil, cnx: Jamf.cnx) click to toggle source

Is the given serialNumber assigned to any prestage, or to the given prestage if a prestage is specified?

This uses .serials_by_prestage_id, the class-level scope path which gets a hash of all assigned SNS => the id of the prestage they are assigned to. The instance#assigned? method uses a different path which returnds more data in an OAPI object.

NOTE: If a serial number isn’t assigned to any prestage, it may really be unassigned or it may not exist in your ADE. To see if a SN exists in one of your Device Enrollment instances, use Jamf::DeviceEnrollment.include?

@param sn [String] the serial number to look for

@param prestage [Integer, String] If provided, the id or name of

an existing prestage in which to look for the sn. if omitted, all
prestages are searched.

@param cnx the API connection to use

@return [Boolean] Is the sn assigned, at all or to the given prestage?

    # File lib/jamf/api/jamf_pro/mixins/prestage.rb
164 def assigned?(sn, prestage: nil, cnx: Jamf.cnx)
165   assigned_id = assigned_prestage_id(sn, cnx: cnx)
166 
167   # it isn't assigned at all
168   return false unless assigned_id
169 
170   # we are looking to see if its assigned at all, which it is
171   return true unless prestage
172 
173   # we are looking to see if its in a specific prestage
174   psid = valid_id prestage, cnx: cnx
175   raise Jamf::NoSuchItemError, "No #{self} matching '#{prestage}'" unless psid
176 
177   psid == assigned_id
178 end
assigned_prestage_id(sn, cnx: Jamf.cnx) click to toggle source

The id of the prestage to which the given serialNumber is assigned. nil if not assigned or not in ADE.

NOTE: If a serial number isn’t assigned to any prestage, it may really be unassigned or it may not exist in your ADE. To see if a SN exists in one of your Device Enrollment instances, use Jamf::DeviceEnrollment.include?

@param sn [String] the serial number to look for

@param cnx the API connection to use

@return [String, nil] The id of prestage to which the SN is assigned

    # File lib/jamf/api/jamf_pro/mixins/prestage.rb
138 def assigned_prestage_id(sn, cnx: Jamf.cnx)
139   serials_by_prestage_id(cnx: cnx)[sn]
140 end
default() click to toggle source

Return the Prestage that is marked as default, i.e. the one that new SNs are assigned to when first added. Nil if no default is defined @return [Jamf::Prestage, nil]

   # File lib/jamf/api/jamf_pro/mixins/prestage.rb
76 def default
77   # only one can be true at a time, so sort desc by that field,
78   # and the true one will be at the top
79   default_prestage_data = all.select { |d| d[:defaultPrestage] }.first
80 
81   # Just in case there was no true one, make sure defaultPrestage is true
82   return unless default_prestage_data&.dig(:defaultPrestage)
83 
84   fetch id: default_prestage_data[:id]
85 end
scope_path(prestage_id = nil) click to toggle source

the endpoint path for the scope of a given prestage id

    # File lib/jamf/api/jamf_pro/mixins/prestage.rb
224 def scope_path(prestage_id = nil)
225   pfx = defined?(self::SCOPE_PATH_PREFIX) ? self::SCOPE_PATH_PREFIX : get_path
226 
227   prestage_id ? "#{pfx}/#{prestage_id}/#{SCOPE_PATH}" : "#{pfx}/#{SCOPE_PATH}"
228 end
serials_by_prestage_id(refresh = false, cnx: Jamf.cnx) click to toggle source

Return all scoped serial numbers and the id of the prestage they are assigned to.

@param refresh re-read the list from the API? DEPRECATED:

the data is always read from the API. If making many calls at once,
consider capturing serials_by_prestage_id in your own variable

@param cnx the API connection to use

@return [Hash {String => Integer}] The Serials and prestage IDs

    # File lib/jamf/api/jamf_pro/mixins/prestage.rb
 98 def serials_by_prestage_id(refresh = false, cnx: Jamf.cnx) # rubocop:disable Lint/UnusedMethodArgument
 99   api_reponse = ALL_SCOPES_OBJECT.new cnx.jp_get(scope_path)
100   api_reponse.serialsByPrestageId.transform_keys(&:to_s)
101 end
serials_for_prestage(prestage_ident, refresh = false, cnx: Jamf.cnx) click to toggle source

Get the assigned serialnumbers for a given prestage, without having to instantiate it

@paream prestage_ident [Integer, String] the id or name of

an existing prestage.

@param refresh re-read the list from the API? DEPRECATED:

the data is always read from the API. If making many calls at once,
consisider capturing serials_by_prestage_id in your own variable and using
it as this method does

@param cnx the API connection to use

@return [Array<String>] the SN’s assigned to the prestage

    # File lib/jamf/api/jamf_pro/mixins/prestage.rb
118 def serials_for_prestage(prestage_ident, refresh = false, cnx: Jamf.cnx) # rubocop:disable Lint/UnusedMethodArgument
119   id = valid_id prestage_ident, cnx: cnx
120   raise Jamf::NoSuchItemError, "No #{self} matching '#{prestage_ident}'" unless id
121 
122   serials_by_prestage_id(cnx: cnx).select { |_sn, psid| id == psid }.keys
123 end
unassign(*sns_to_unassign, from_prestage:, cnx: Jamf.cnx) click to toggle source

Unassign one or more serialNumber from a prestage @return [Jamf::PrestageScope] the new scope for the prestage

    # File lib/jamf/api/jamf_pro/mixins/prestage.rb
204 def unassign(*sns_to_unassign, from_prestage:, cnx: Jamf.cnx)
205   prestage_id = valid_id from_prestage
206   raise Jamf::NoSuchItemError, "No #{self} matching '#{from_prestage}'" unless prestage_id
207 
208   # upcase all sns
209   sns_to_unassign.map!(&:to_s)
210   sns_to_unassign.map!(&:upcase)
211 
212   # get the current scope of the prestage
213   spath = scope_path(prestage_id)
214   scope = INSTANCE_SCOPE_OBJECT.new cnx.get(spath)
215 
216   new_scope_sns = scope.assignments.map(&:serialNumber)
217   new_scope_sns -= sns_to_unassign
218 
219   update_scope(spath, new_scope_sns, scope.versionLock, cnx)
220 end

Private Instance Methods

update_scope(spath, new_scope_sns, vlock, cnx) click to toggle source

used by assign and unassign

    # File lib/jamf/api/jamf_pro/mixins/prestage.rb
234 def update_scope(spath, new_scope_sns, vlock, cnx)
235   assignment_data = {
236     serialNumbers: new_scope_sns,
237     versionLock: vlock
238   }
239   INSTANCE_SCOPE_OBJECT.new cnx.jp_put(spath, assignment_data)
240 end