Module: Jamf::Prestage::ClassMethods

Defined in:
lib/jamf/api/jamf_pro/mixins/prestage.rb

Overview

Class Methods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(extender) ⇒ Object

when this module is included, also extend our Class Methods



67
68
69
# File 'lib/jamf/api/jamf_pro/mixins/prestage.rb', line 67

def self.extended(extender)
  Jamf.load_msg "--> #{extender} is extending Jamf::Prestage::ClassMethods"
end

Instance Method Details

#assign(*sns_to_assign, to_prestage:, cnx: Jamf.cnx) ⇒ Jamf::OAPISchemas::PrestageScopeResponseV2

Assign one or more serialNumbers to a prestage

Returns:

Raises:



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/jamf/api/jamf_pro/mixins/prestage.rb', line 182

def assign(*sns_to_assign, to_prestage:, cnx: Jamf.cnx)
  prestage_id = valid_id to_prestage
  raise Jamf::NoSuchItemError, "No #{self} matching '#{to_prestage}'" unless prestage_id

  # upcase all sns
  sns_to_assign.map!(&:to_s)
  sns_to_assign.map!(&:upcase)

  # get the current scope of the prestage
  spath = scope_path(prestage_id)
  scope = INSTANCE_SCOPE_OBJECT.new cnx.get(spath)

  # add the new sns to the existing ones
  new_scope_sns = scope.assignments.map(&:serialNumber)
  new_scope_sns += sns_to_assign
  new_scope_sns.uniq!

  update_scope(spath, new_scope_sns, scope.versionLock, cnx)
end

#assigned?(sn, prestage: nil, cnx: Jamf.cnx) ⇒ Boolean

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?

Parameters:

  • sn (String)

    the serial number to look for

  • prestage (Integer, String) (defaults to: nil)

    If provided, the id or name of an existing prestage in which to look for the sn. if omitted, all prestages are searched.

  • cnx (Jamf::Connection) (defaults to: Jamf.cnx)

    the API connection to use

Returns:

  • (Boolean)

    Is the sn assigned, at all or to the given prestage?

Raises:



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/jamf/api/jamf_pro/mixins/prestage.rb', line 164

def assigned?(sn, prestage: nil, cnx: Jamf.cnx)
  assigned_id = assigned_prestage_id(sn, cnx: cnx)

  # it isn't assigned at all
  return false unless assigned_id

  # we are looking to see if its assigned at all, which it is
  return true unless prestage

  # we are looking to see if its in a specific prestage
  psid = valid_id prestage, cnx: cnx
  raise Jamf::NoSuchItemError, "No #{self} matching '#{prestage}'" unless psid

  psid == assigned_id
end

#assigned_prestage_id(sn, cnx: Jamf.cnx) ⇒ String?

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?

Parameters:

  • sn (String)

    the serial number to look for

  • cnx (Jamf::Connection) (defaults to: Jamf.cnx)

    the API connection to use

Returns:

  • (String, nil)

    The id of prestage to which the SN is assigned



138
139
140
# File 'lib/jamf/api/jamf_pro/mixins/prestage.rb', line 138

def assigned_prestage_id(sn, cnx: Jamf.cnx)
  serials_by_prestage_id(cnx: cnx)[sn]
end

#defaultJamf::Prestage?

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

Returns:



76
77
78
79
80
81
82
83
84
85
# File 'lib/jamf/api/jamf_pro/mixins/prestage.rb', line 76

def default
  # only one can be true at a time, so sort desc by that field,
  # and the true one will be at the top
  default_prestage_data = all.select { |d| d[:defaultPrestage] }.first

  # Just in case there was no true one, make sure defaultPrestage is true
  return unless default_prestage_data&.dig(:defaultPrestage)

  fetch id: default_prestage_data[:id]
end

#scope_path(prestage_id = nil) ⇒ Object

the endpoint path for the scope of a given prestage id



224
225
226
227
228
# File 'lib/jamf/api/jamf_pro/mixins/prestage.rb', line 224

def scope_path(prestage_id = nil)
  pfx = defined?(self::SCOPE_PATH_PREFIX) ? self::SCOPE_PATH_PREFIX : get_path

  prestage_id ? "#{pfx}/#{prestage_id}/#{SCOPE_PATH}" : "#{pfx}/#{SCOPE_PATH}"
end

#serials_by_prestage_id(refresh = false, cnx: Jamf.cnx) ⇒ Hash {String => Integer}

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

Parameters:

  • refresh (Boolean) (defaults to: false)

    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

  • cnx (Jamf::Connection) (defaults to: Jamf.cnx)

    the API connection to use

Returns:

  • (Hash {String => Integer})

    The Serials and prestage IDs



98
99
100
101
# File 'lib/jamf/api/jamf_pro/mixins/prestage.rb', line 98

def serials_by_prestage_id(refresh = false, cnx: Jamf.cnx) # rubocop:disable Lint/UnusedMethodArgument
  api_reponse = ALL_SCOPES_OBJECT.new cnx.jp_get(scope_path)
  api_reponse.serialsByPrestageId.transform_keys(&:to_s)
end

#serials_for_prestage(prestage_ident, refresh = false, cnx: Jamf.cnx) ⇒ Array<String>

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

Parameters:

  • refresh (Boolean) (defaults to: false)

    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

  • cnx (Jamf::Connection) (defaults to: Jamf.cnx)

    the API connection to use

Returns:

  • (Array<String>)

    the SN’s assigned to the prestage

Raises:



118
119
120
121
122
123
# File 'lib/jamf/api/jamf_pro/mixins/prestage.rb', line 118

def serials_for_prestage(prestage_ident, refresh = false, cnx: Jamf.cnx) # rubocop:disable Lint/UnusedMethodArgument
  id = valid_id prestage_ident, cnx: cnx
  raise Jamf::NoSuchItemError, "No #{self} matching '#{prestage_ident}'" unless id

  serials_by_prestage_id(cnx: cnx).select { |_sn, psid| id == psid }.keys
end

#unassign(*sns_to_unassign, from_prestage:, cnx: Jamf.cnx) ⇒ Jamf::PrestageScope

Unassign one or more serialNumber from a prestage

Returns:

  • (Jamf::PrestageScope)

    the new scope for the prestage

Raises:



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/jamf/api/jamf_pro/mixins/prestage.rb', line 204

def unassign(*sns_to_unassign, from_prestage:, cnx: Jamf.cnx)
  prestage_id = valid_id from_prestage
  raise Jamf::NoSuchItemError, "No #{self} matching '#{from_prestage}'" unless prestage_id

  # upcase all sns
  sns_to_unassign.map!(&:to_s)
  sns_to_unassign.map!(&:upcase)

  # get the current scope of the prestage
  spath = scope_path(prestage_id)
  scope = INSTANCE_SCOPE_OBJECT.new cnx.get(spath)

  new_scope_sns = scope.assignments.map(&:serialNumber)
  new_scope_sns -= sns_to_unassign

  update_scope(spath, new_scope_sns, scope.versionLock, cnx)
end