Module: Jamf::Prestage::ClassMethods
- Defined in:
- lib/jamf/api/jamf_pro/mixins/prestage.rb
Overview
Class Methods
Class Method Summary collapse
-
.extended(extender) ⇒ Object
when this module is included, also extend our Class Methods.
Instance Method Summary collapse
-
#assign(*sns_to_assign, to_prestage:, cnx: Jamf.cnx) ⇒ Jamf::OAPISchemas::PrestageScopeResponseV2
Assign one or more serialNumbers to a prestage.
-
#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?.
-
#assigned_prestage_id(sn, cnx: Jamf.cnx) ⇒ String?
The id of the prestage to which the given serialNumber is assigned.
-
#default ⇒ Jamf::Prestage?
Return the Prestage that is marked as default, i.e.
-
#scope_path(prestage_id = nil) ⇒ Object
the endpoint path for the scope of a given prestage id.
-
#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.
-
#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.
-
#unassign(*sns_to_unassign, from_prestage:, cnx: Jamf.cnx) ⇒ Jamf::PrestageScope
Unassign one or more serialNumber from a prestage.
Class Method Details
.extended(extender) ⇒ Object
when this module is included, also extend our Class Methods
52 53 54 |
# File 'lib/jamf/api/jamf_pro/mixins/prestage.rb', line 52 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
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/jamf/api/jamf_pro/mixins/prestage.rb', line 167 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?
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/jamf/api/jamf_pro/mixins/prestage.rb', line 149 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?
123 124 125 |
# File 'lib/jamf/api/jamf_pro/mixins/prestage.rb', line 123 def assigned_prestage_id(sn, cnx: Jamf.cnx) serials_by_prestage_id(cnx: cnx)[sn] end |
#default ⇒ Jamf::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
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/jamf/api/jamf_pro/mixins/prestage.rb', line 61 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
209 210 211 212 213 |
# File 'lib/jamf/api/jamf_pro/mixins/prestage.rb', line 209 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.
83 84 85 86 |
# File 'lib/jamf/api/jamf_pro/mixins/prestage.rb', line 83 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
103 104 105 106 107 108 |
# File 'lib/jamf/api/jamf_pro/mixins/prestage.rb', line 103 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
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/jamf/api/jamf_pro/mixins/prestage.rb', line 189 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 |