ruby-jss 1.3.2 → 1.5.3
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of ruby-jss might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/CHANGES.md +122 -0
- data/lib/jamf.rb +18 -16
- data/lib/jamf/api/base_classes/collection_resource.rb +613 -0
- data/lib/jamf/api/{abstract_classes → base_classes}/json_object.rb +109 -101
- data/lib/jamf/api/{abstract_classes → base_classes}/prestage.rb +55 -30
- data/lib/jamf/api/{abstract_classes → base_classes}/resource.rb +10 -6
- data/lib/jamf/api/{abstract_classes → base_classes}/singleton_resource.rb +4 -3
- data/lib/jamf/api/connection.rb +13 -9
- data/lib/jamf/api/connection/api_error.rb +8 -8
- data/lib/jamf/api/connection/token.rb +36 -15
- data/lib/jamf/api/json_objects/device_enrollment_device.rb +14 -7
- data/lib/jamf/api/json_objects/{location.rb → device_enrollment_device_sync_state.rb} +27 -41
- data/lib/jamf/api/json_objects/device_enrollment_sync_status.rb +1 -1
- data/lib/jamf/api/json_objects/{attachment.rb → locale.rb} +14 -23
- data/lib/jamf/api/json_objects/md_prestage_name.rb +1 -1
- data/lib/jamf/api/json_objects/md_prestage_names.rb +2 -2
- data/lib/jamf/api/json_objects/md_prestage_skip_setup_items.rb +50 -1
- data/lib/jamf/api/json_objects/prestage_assignment.rb +2 -2
- data/lib/jamf/api/json_objects/prestage_location.rb +3 -3
- data/lib/jamf/api/json_objects/prestage_purchasing_data.rb +7 -7
- data/lib/jamf/api/json_objects/prestage_scope.rb +1 -1
- data/lib/jamf/api/{resources/collection_resources → json_objects}/time_zone.rb +9 -23
- data/lib/jamf/api/mixins/{abstract.rb → base_class.rb} +34 -16
- data/lib/jamf/api/mixins/bulk_deletable.rb +27 -6
- data/lib/jamf/api/mixins/change_log.rb +201 -51
- data/lib/jamf/api/{resources/collection_resources/computer.rb → mixins/filterable.rb} +19 -17
- data/lib/jamf/api/mixins/pageable.rb +208 -0
- data/lib/jamf/api/{json_objects/installed_application.rb → mixins/sortable.rb} +33 -33
- data/lib/jamf/api/resources/collection_resources/building.rb +16 -9
- data/lib/jamf/api/resources/collection_resources/category.rb +5 -4
- data/lib/jamf/api/resources/collection_resources/computer_prestage.rb +12 -5
- data/lib/jamf/api/resources/collection_resources/department.rb +1 -3
- data/lib/jamf/api/resources/collection_resources/device_enrollment.rb +13 -13
- data/lib/jamf/api/resources/collection_resources/inventory_preload_record.rb +11 -3
- data/lib/jamf/api/resources/collection_resources/mobile_device_prestage.rb +25 -23
- data/lib/jamf/api/resources/collection_resources/script.rb +61 -25
- data/lib/jamf/api/resources/singleton_resources/app_store_country_codes.rb +15 -5
- data/lib/jamf/api/resources/singleton_resources/locales.rb +155 -0
- data/lib/jamf/api/resources/singleton_resources/time_zones.rb +213 -0
- data/lib/jamf/validate.rb +63 -24
- data/lib/jamf/version.rb +1 -1
- data/lib/jss.rb +2 -1
- data/lib/jss/api_connection.rb +113 -406
- data/lib/jss/api_object.rb +10 -20
- data/lib/jss/api_object/advanced_search.rb +27 -26
- data/lib/jss/api_object/app_store_country_codes.rb +298 -0
- data/lib/jss/api_object/categorizable.rb +1 -1
- data/lib/jss/api_object/computer.rb +13 -0
- data/lib/jss/api_object/configuration_profile.rb +60 -4
- data/lib/jss/api_object/directory_binding_type.rb +66 -60
- data/lib/jss/api_object/directory_binding_type/active_directory.rb +71 -34
- data/lib/jss/api_object/directory_binding_type/admitmac.rb +536 -467
- data/lib/jss/api_object/directory_binding_type/centrify.rb +21 -7
- data/lib/jss/api_object/directory_binding_type/open_directory.rb +4 -4
- data/lib/jss/api_object/distribution_point.rb +2 -2
- data/lib/jss/api_object/dock_item.rb +102 -96
- data/lib/jss/api_object/ebook.rb +1 -2
- data/lib/jss/api_object/extendable.rb +1 -1
- data/lib/jss/api_object/extension_attribute.rb +4 -3
- data/lib/jss/api_object/group.rb +33 -2
- data/lib/jss/api_object/mac_application.rb +107 -8
- data/lib/jss/api_object/network_segment.rb +43 -12
- data/lib/jss/api_object/package.rb +1 -1
- data/lib/jss/api_object/patch_source.rb +10 -9
- data/lib/jss/api_object/policy.rb +217 -28
- data/lib/jss/api_object/printer.rb +10 -4
- data/lib/jss/api_object/scopable.rb +10 -15
- data/lib/jss/api_object/scopable/scope.rb +389 -73
- data/lib/jss/api_object/self_servable.rb +17 -9
- data/lib/jss/api_object/uploadable.rb +1 -1
- data/lib/jss/api_object/user.rb +42 -1
- data/lib/jss/api_object/vpp_account.rb +209 -0
- data/lib/jss/api_object/vppable.rb +169 -13
- data/lib/jss/exceptions.rb +3 -0
- data/lib/jss/server.rb +15 -0
- data/lib/jss/utility.rb +142 -37
- data/lib/jss/validate.rb +53 -10
- data/lib/jss/version.rb +1 -1
- metadata +45 -61
- data/lib/jamf/api/abstract_classes/advanced_search.rb +0 -86
- data/lib/jamf/api/abstract_classes/collection_resource.rb +0 -433
- data/lib/jamf/api/abstract_classes/generic_reference.rb +0 -145
- data/lib/jamf/api/abstract_classes/prestage_skip_setup_items.rb +0 -126
- data/lib/jamf/api/json_objects/account_prefs.rb +0 -79
- data/lib/jamf/api/json_objects/android_details.rb +0 -139
- data/lib/jamf/api/json_objects/appletv_details.rb +0 -110
- data/lib/jamf/api/json_objects/cellular_network.rb +0 -151
- data/lib/jamf/api/json_objects/computer_prestage_skip_setup_items.rb +0 -67
- data/lib/jamf/api/json_objects/criterion.rb +0 -152
- data/lib/jamf/api/json_objects/extension_attribute_value.rb +0 -128
- data/lib/jamf/api/json_objects/installed_certificate.rb +0 -53
- data/lib/jamf/api/json_objects/installed_configuration_profile.rb +0 -67
- data/lib/jamf/api/json_objects/installed_ebook.rb +0 -58
- data/lib/jamf/api/json_objects/installed_provisioning_profile.rb +0 -59
- data/lib/jamf/api/json_objects/ios_details.rb +0 -244
- data/lib/jamf/api/json_objects/mobile_device_details.rb +0 -219
- data/lib/jamf/api/json_objects/mobile_device_security.rb +0 -101
- data/lib/jamf/api/json_objects/purchasing_data.rb +0 -125
- data/lib/jamf/api/mixins/locatable.rb +0 -124
- data/lib/jamf/api/mixins/referable.rb +0 -92
- data/lib/jamf/api/resources/collection_resources/account.rb +0 -163
- data/lib/jamf/api/resources/collection_resources/advanced_mobile_device_search.rb +0 -52
- data/lib/jamf/api/resources/collection_resources/advanced_user_search.rb +0 -52
- data/lib/jamf/api/resources/collection_resources/extension_attribute.rb +0 -45
- data/lib/jamf/api/resources/collection_resources/mobile_device.rb +0 -315
- data/lib/jamf/api/resources/collection_resources/site.rb +0 -77
- data/lib/jamf/api/resources/singleton_resources/authorization.rb +0 -88
- data/lib/jamf/api/resources/singleton_resources/client_checkin_settings.rb +0 -139
- data/lib/jamf/api/resources/singleton_resources/reenrollment_settings.rb +0 -95
@@ -1,86 +0,0 @@
|
|
1
|
-
# Copyright 2020 Pixar
|
2
|
-
|
3
|
-
#
|
4
|
-
# Licensed under the Apache License, Version 2.0 (the "Apache License")
|
5
|
-
# with the following modification; you may not use this file except in
|
6
|
-
# compliance with the Apache License and the following modification to it:
|
7
|
-
# Section 6. Trademarks. is deleted and replaced with:
|
8
|
-
#
|
9
|
-
# 6. Trademarks. This License does not grant permission to use the trade
|
10
|
-
# names, trademarks, service marks, or product names of the Licensor
|
11
|
-
# and its affiliates, except as required to comply with Section 4(c) of
|
12
|
-
# the License and to reproduce the content of the NOTICE file.
|
13
|
-
#
|
14
|
-
# You may obtain a copy of the Apache License at
|
15
|
-
#
|
16
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
17
|
-
#
|
18
|
-
# Unless required by applicable law or agreed to in writing, software
|
19
|
-
# distributed under the Apache License with the above modification is
|
20
|
-
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
21
|
-
# KIND, either express or implied. See the Apache License for the specific
|
22
|
-
# language governing permissions and limitations under the Apache License.
|
23
|
-
#
|
24
|
-
#
|
25
|
-
|
26
|
-
# The Module
|
27
|
-
module Jamf
|
28
|
-
|
29
|
-
# Classes
|
30
|
-
#####################################
|
31
|
-
|
32
|
-
# A building defined in the JSS
|
33
|
-
class AdvancedSearch < Jamf::CollectionResource
|
34
|
-
|
35
|
-
extend Jamf::Abstract
|
36
|
-
|
37
|
-
# Constants
|
38
|
-
#####################################
|
39
|
-
|
40
|
-
# Object Model / Attributes
|
41
|
-
# See APIObject class documentation for details
|
42
|
-
# of how the OBJECT_MODEL hash works.
|
43
|
-
#####################################
|
44
|
-
OBJECT_MODEL = {
|
45
|
-
|
46
|
-
# @!attribute [r] id
|
47
|
-
# @return [Integer]
|
48
|
-
id: {
|
49
|
-
class: :integer,
|
50
|
-
identifier: :primary,
|
51
|
-
readonly: true
|
52
|
-
},
|
53
|
-
|
54
|
-
# @!attribute name
|
55
|
-
# @return [String]
|
56
|
-
name: {
|
57
|
-
class: :string,
|
58
|
-
identifier: true
|
59
|
-
},
|
60
|
-
|
61
|
-
# @!attribute criteria
|
62
|
-
# @return [Array<Jamf::Criterion>]
|
63
|
-
criteria: {
|
64
|
-
class: Jamf::Criterion,
|
65
|
-
multi: true,
|
66
|
-
required: true
|
67
|
-
},
|
68
|
-
|
69
|
-
# @!attribute displayFields
|
70
|
-
# @return [Array<String>]
|
71
|
-
displayFields: {
|
72
|
-
class: :string,
|
73
|
-
multi: true
|
74
|
-
},
|
75
|
-
|
76
|
-
# @!attribute site
|
77
|
-
# @return [Jamf::Site::Reference]
|
78
|
-
site: {
|
79
|
-
class: Jamf::Site::Reference
|
80
|
-
}
|
81
|
-
|
82
|
-
}.freeze
|
83
|
-
|
84
|
-
end # class
|
85
|
-
|
86
|
-
end # module
|
@@ -1,433 +0,0 @@
|
|
1
|
-
# Copyright 2020 Pixar
|
2
|
-
|
3
|
-
#
|
4
|
-
# Licensed under the Apache License, Version 2.0 (the "Apache License")
|
5
|
-
# with the following modification; you may not use this file except in
|
6
|
-
# compliance with the Apache License and the following modification to it:
|
7
|
-
# Section 6. Trademarks. is deleted and replaced with:
|
8
|
-
#
|
9
|
-
# 6. Trademarks. This License does not grant permission to use the trade
|
10
|
-
# names, trademarks, service marks, or product names of the Licensor
|
11
|
-
# and its affiliates, except as required to comply with Section 4(c) of
|
12
|
-
# the License and to reproduce the content of the NOTICE file.
|
13
|
-
#
|
14
|
-
# You may obtain a copy of the Apache License at
|
15
|
-
#
|
16
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
17
|
-
#
|
18
|
-
# Unless required by applicable law or agreed to in writing, software
|
19
|
-
# distributed under the Apache License with the above modification is
|
20
|
-
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
21
|
-
# KIND, either express or implied. See the Apache License for the specific
|
22
|
-
# language governing permissions and limitations under the Apache License.
|
23
|
-
#
|
24
|
-
#
|
25
|
-
|
26
|
-
# The module
|
27
|
-
module Jamf
|
28
|
-
|
29
|
-
# A Collection Resource in Jamf Pro
|
30
|
-
#
|
31
|
-
# See {Jamf::Resource} for general info about API resources.
|
32
|
-
#
|
33
|
-
# Collection resources have more than one resource within them, and those
|
34
|
-
# can (usually) be created and deleted as well as fetched and updated.
|
35
|
-
# The entire collection (or a part of it) can also be fetched as an Array.
|
36
|
-
# When the whole collection is fetched, the result is cached for future use.
|
37
|
-
#
|
38
|
-
# # Subclassing
|
39
|
-
#
|
40
|
-
# ## Creatability, & Deletability
|
41
|
-
#
|
42
|
-
# Sometimes the API doesn't support creation of new members of the collection.
|
43
|
-
# If that's the case, just extend the subclass with Jamf::UnCreatable
|
44
|
-
# and the '.create' class method will raise an error.
|
45
|
-
#
|
46
|
-
# Similarly for deletion of members: if the API doesn't have a way to delete
|
47
|
-
# them, extend the subclass with Jamf::UnDeletable
|
48
|
-
#
|
49
|
-
# See also Jamf::JSONObject, which talks about extending subclasses
|
50
|
-
# with Jamf::Immutable
|
51
|
-
#
|
52
|
-
# ## Bulk Deletion
|
53
|
-
#
|
54
|
-
# Some collection resources have a resource for bulk deletion, passing in
|
55
|
-
# a JSON array of ids to delete.
|
56
|
-
#
|
57
|
-
# If so, just define a BULK_DELETE_RSRC, and the .delete class method
|
58
|
-
# will use it, rather than making multiple calls to delete individual
|
59
|
-
# items. See Jamf::Category::BULK_DELETE_RSRC for an example
|
60
|
-
#
|
61
|
-
# @abstract
|
62
|
-
#
|
63
|
-
class CollectionResource < Jamf::Resource
|
64
|
-
|
65
|
-
extend Jamf::Abstract
|
66
|
-
include Comparable
|
67
|
-
|
68
|
-
# Public Class Methods
|
69
|
-
#####################################
|
70
|
-
|
71
|
-
# @return [Array<Symbol>] the attribute names that are marked as identifiers
|
72
|
-
#
|
73
|
-
def self.identifiers
|
74
|
-
self::OBJECT_MODEL.select { |_attr, deets| deets[:identifier] }.keys
|
75
|
-
end
|
76
|
-
|
77
|
-
# The Collection members Array for this class, retrieved from
|
78
|
-
# the RSRC_PATH as Parsed JSON, but not instantiated into instances
|
79
|
-
# unless instantiate: is truthy.
|
80
|
-
#
|
81
|
-
# E.g. for {Jamf::Settings::Building}, this would be the Array of Hashes
|
82
|
-
# returned by GETing the resource .../settings/obj/building
|
83
|
-
#
|
84
|
-
# This Array is cached in the {Jamf::Connection} instance used to
|
85
|
-
# retrieve it, and future calls to .all will return the cached Array
|
86
|
-
# unless refresh is truthy.
|
87
|
-
#
|
88
|
-
# TODO: Investigate https://www.rubydoc.info/github/mloughran/api_cache
|
89
|
-
#
|
90
|
-
# @param refresh[Boolean] re-read the data from the API?
|
91
|
-
#
|
92
|
-
# @param cnx[Jamf::Connection] an API connection to use for the query.
|
93
|
-
# Defaults to the corrently active connection. See {Jamf::Connection}
|
94
|
-
#
|
95
|
-
# @param instantiate[Boolean] The Array contains instances of this class
|
96
|
-
# rather than the JSON Hashes from the API.
|
97
|
-
#
|
98
|
-
# @return [Array<Object>] An Array of all objects of this class in the JSS.
|
99
|
-
#
|
100
|
-
def self.all(refresh = false, cnx: Jamf.cnx, instantiate: false)
|
101
|
-
validate_not_abstract
|
102
|
-
cnx.collection_cache[self] = nil if refresh
|
103
|
-
if cnx.collection_cache[self]
|
104
|
-
return cnx.collection_cache[self] unless instantiate
|
105
|
-
|
106
|
-
return cnx.collection_cache[self].map { |m| new m }
|
107
|
-
end
|
108
|
-
|
109
|
-
# TODO: make sure all collection resources use this format
|
110
|
-
# for paging. Also -ask Jamf about a url that returns
|
111
|
-
# ALL objects in one query, regardless of number.
|
112
|
-
page = 0
|
113
|
-
raw = cnx.get "#{rsrc_path}?page=#{page}&size=1000000&sort=id%3Aasc"
|
114
|
-
results = raw[:results]
|
115
|
-
|
116
|
-
until results.size >= raw[:totalCount]
|
117
|
-
page += 1
|
118
|
-
raw = cnx.get "#{rsrc_path}?page=#{page}&size=1000000&sort=id%3Aasc"
|
119
|
-
results += raw[:results]
|
120
|
-
end
|
121
|
-
|
122
|
-
|
123
|
-
cnx.collection_cache[self] = results
|
124
|
-
|
125
|
-
return cnx.collection_cache[self] unless instantiate
|
126
|
-
|
127
|
-
cnx.collection_cache[self].map { |m| new m }
|
128
|
-
end
|
129
|
-
|
130
|
-
# An array of the ids for all collection members. According to the
|
131
|
-
# specs ALL collection resources must have an ID, which is used in the
|
132
|
-
# resource path.
|
133
|
-
#
|
134
|
-
# @param refresh (see .all)
|
135
|
-
#
|
136
|
-
# @param cnx (see .all)
|
137
|
-
#
|
138
|
-
# @return [Array<Integer>]
|
139
|
-
#
|
140
|
-
def self.all_ids(refresh = false, cnx: Jamf.cnx)
|
141
|
-
all(refresh, cnx: cnx).map { |m| m[:id] }
|
142
|
-
end
|
143
|
-
|
144
|
-
# rubocop:disable Naming/UncommunicativeMethodParamName
|
145
|
-
|
146
|
-
# A Hash of all members of this collection where the keys are some
|
147
|
-
# identifier and values are any other attribute.
|
148
|
-
#
|
149
|
-
# @param ident [Symbol] An identifier of this Class, used as the key
|
150
|
-
# for the mapping Hash. Aliases are acceptable, e.g. :sn for :serialNumber
|
151
|
-
#
|
152
|
-
# @param to [Symbol] The attribute to which the ident will be mapped.
|
153
|
-
# Aliases are acceptable, e.g. :name for :displayName
|
154
|
-
#
|
155
|
-
# @param refresh (see .all)
|
156
|
-
#
|
157
|
-
# @param cnx (see .all)
|
158
|
-
#
|
159
|
-
# @return [Hash {Symbol: Object}] A Hash of identifier mapped to attribute
|
160
|
-
#
|
161
|
-
def self.map_all(ident, to:, cnx: Jamf.cnx, refresh: false)
|
162
|
-
real_ident = attr_key_for_alias ident
|
163
|
-
raise Jamf::InvalidDataError, "No identifier #{ident} for class #{self}" unless
|
164
|
-
identifiers.include? real_ident
|
165
|
-
|
166
|
-
real_to = attr_key_for_alias to
|
167
|
-
raise Jamf::NoSuchItemError, "No attribute #{to} for class #{self}" unless self::OBJECT_MODEL.key? real_to
|
168
|
-
|
169
|
-
ident = real_ident
|
170
|
-
to = real_to
|
171
|
-
list = all refresh, cnx: cnx
|
172
|
-
to_class = self::OBJECT_MODEL[to][:class]
|
173
|
-
mapped = list.map do |i|
|
174
|
-
[
|
175
|
-
i[ident],
|
176
|
-
to_class.is_a?(Symbol) ? i[to] : to_class.new(i[to])
|
177
|
-
]
|
178
|
-
end # do i
|
179
|
-
mapped.to_h
|
180
|
-
end
|
181
|
-
# rubocop:enable Naming/UncommunicativeMethodParamName
|
182
|
-
|
183
|
-
# Given any identfier value for this collection, return the id of the
|
184
|
-
# object that has such an identifier.
|
185
|
-
#
|
186
|
-
# Return nil if there's no match for the given value.
|
187
|
-
#
|
188
|
-
# If you know the value is a certain identifier, e.g. a serialNumber,
|
189
|
-
# then you can specify the identifier for a faster search:
|
190
|
-
#
|
191
|
-
# valid_id serialNumber: 'AB12DE34' # => Int or nil
|
192
|
-
#
|
193
|
-
# If you don't know wich identifier you have, just pass the value and
|
194
|
-
# all identifiers are searched
|
195
|
-
#
|
196
|
-
# valid_id 'AB12DE34' # => Int or nil
|
197
|
-
# valid_id 'SomeComputerName' # => Int or nil
|
198
|
-
#
|
199
|
-
# When the value is a string, the seach is case-insensitive
|
200
|
-
#
|
201
|
-
# TODO: When 'Searchability' is more dialed in via the searchable
|
202
|
-
# mixin, which implements enpoints like 'POST /v1/search-mobile-devices'
|
203
|
-
# then use that before using the 'all' list.
|
204
|
-
#
|
205
|
-
# @param value [Object] A value to search for as an identifier.
|
206
|
-
#
|
207
|
-
# @param refresh[Boolean] Reload the list data from the API
|
208
|
-
#
|
209
|
-
# @param ident: [Symbol] Restrict the search to this identifier.
|
210
|
-
# E.g. if :serialNumber, then the value must be
|
211
|
-
# a known serial number, it is not checked against other identifiers
|
212
|
-
#
|
213
|
-
# @param cnx: (see .all)
|
214
|
-
#
|
215
|
-
# @return [Object, nil] the primary identifier of the matching object,
|
216
|
-
# or nil if it doesn't exist
|
217
|
-
#
|
218
|
-
def self.valid_id(value = nil, refresh: true, cnx: Jamf.cnx, **ident_hash)
|
219
|
-
unless ident_hash.empty?
|
220
|
-
ident, value = ident_hash.first
|
221
|
-
return id_from_other_ident ident, value, refresh, cnx: cnx
|
222
|
-
end
|
223
|
-
|
224
|
-
# check the id itself first
|
225
|
-
return value if all_ids(refresh, cnx: cnx).include? value
|
226
|
-
|
227
|
-
idents = identifiers - [:id]
|
228
|
-
val_is_str = value.is_a? String
|
229
|
-
|
230
|
-
idents.each do |ident|
|
231
|
-
match = all(refresh, cnx: cnx).select do |m|
|
232
|
-
val_is_str ? m[ident].to_s.casecmp?(value) : m[ident] == value
|
233
|
-
end.first
|
234
|
-
return match[:id] if match
|
235
|
-
end # identifiers.each do |ident|
|
236
|
-
|
237
|
-
nil
|
238
|
-
end
|
239
|
-
|
240
|
-
# Bu default, subclasses are creatable, i.e. new instances can be created
|
241
|
-
# with .create, and added to the JSS with .save
|
242
|
-
# If a subclass is NOT creatble for any reason, just add
|
243
|
-
# extend Jamf::UnCreatable
|
244
|
-
# and this method will return false
|
245
|
-
#
|
246
|
-
# @return [Boolean]
|
247
|
-
def self.creatable?
|
248
|
-
true
|
249
|
-
end
|
250
|
-
|
251
|
-
# Make a new thing to be added to the API
|
252
|
-
def self.create(**params)
|
253
|
-
validate_not_abstract
|
254
|
-
raise Jamf::UnsupportedError, "#{self}'s are not currently creatable via the API" unless creatable?
|
255
|
-
|
256
|
-
cnx = params.delete :cnx
|
257
|
-
cnx ||= Jamf.cnx
|
258
|
-
|
259
|
-
params.delete :id # no such animal when .creating
|
260
|
-
|
261
|
-
params.keys.each do |param|
|
262
|
-
raise ArgumentError, "Unknown parameter: #{param}" unless self::OBJECT_MODEL.key? param
|
263
|
-
|
264
|
-
params[param] = validate_attr param, params[param], cnx: cnx
|
265
|
-
end
|
266
|
-
|
267
|
-
params[:creating_from_create] = true
|
268
|
-
new params, cnx: cnx
|
269
|
-
end
|
270
|
-
|
271
|
-
# Retrieve a member of a CollectionResource from the API
|
272
|
-
#
|
273
|
-
# To create new members to be added to the JSS, use
|
274
|
-
# {Jamf::CollectionResource.create}
|
275
|
-
#
|
276
|
-
# If you know the specific identifier attribute you're looking up, e.g.
|
277
|
-
# :id or :name or :udid, (or an aliase thereof) then you can specify it like
|
278
|
-
# `.fetch name: 'somename'`, or `.fetch udid: 'someudid'`
|
279
|
-
#
|
280
|
-
# If you don't know if (or don't want to type it) you can just use
|
281
|
-
# `.fetch 'somename'`, or `.fetch 'someudid'` and all identifiers will be
|
282
|
-
# searched for a match.
|
283
|
-
#
|
284
|
-
# @param ident_value[Object] A value for any identifier for this subclass.
|
285
|
-
# All identifier attributes will be searched for a match.
|
286
|
-
#
|
287
|
-
# @param cnx[Jamf::Connection] the connection to use to fetch the object
|
288
|
-
#
|
289
|
-
# @param ident_hash[Hash] an identifier attribute key and a search value
|
290
|
-
#
|
291
|
-
# @return [CollectionResource] The ruby-instance of a Jamf object
|
292
|
-
#
|
293
|
-
def self.fetch(ident_value = nil, cnx: Jamf.cnx, **ident_hash)
|
294
|
-
validate_not_abstract
|
295
|
-
|
296
|
-
id =
|
297
|
-
if ident_value == :random
|
298
|
-
all_ids.sample
|
299
|
-
elsif ident_value
|
300
|
-
valid_id ident_value, cnx: cnx
|
301
|
-
elsif ident_hash.empty?
|
302
|
-
nil
|
303
|
-
else
|
304
|
-
ident, lookup_value = ident_hash.first
|
305
|
-
valid_id ident => lookup_value, cnx: cnx
|
306
|
-
end
|
307
|
-
|
308
|
-
raise Jamf::NoSuchItemError, "No matching #{self}" unless id
|
309
|
-
|
310
|
-
data = cnx.get "#{rsrc_path}/#{id}"
|
311
|
-
new data, cnx: cnx
|
312
|
-
end # fetch
|
313
|
-
|
314
|
-
# By default, CollectionResource subclass instances are deletable.
|
315
|
-
# If not, just extend the subclass with Jamf::UnDeletable, and this
|
316
|
-
# will return false, and .delete & #delete will raise errors
|
317
|
-
def self.deletable?
|
318
|
-
true
|
319
|
-
end
|
320
|
-
|
321
|
-
# Delete one or more objects by identifier
|
322
|
-
# Any valid identifier for the class can be used (id, name, udid, etc)
|
323
|
-
# Identifiers can be provided as an array or as separate parameters
|
324
|
-
#
|
325
|
-
# e.g. .delete [1,3, 34, 4]
|
326
|
-
# or .delete 'myComputer', 'that-computer', 'OtherComputer'
|
327
|
-
#
|
328
|
-
# @param idents[Array<integer>, Integer]
|
329
|
-
#
|
330
|
-
# @param cnx[Jamf::Connection]
|
331
|
-
#
|
332
|
-
# @return [Array] the identifiers that were not found, so couldn't be deleted
|
333
|
-
#
|
334
|
-
def self.delete(*idents, cnx: Jamf.cnx)
|
335
|
-
raise Jamf::UnsupportedError, "Deleting #{self} objects is not currently supported" unless deletable?
|
336
|
-
|
337
|
-
idents.flatten!
|
338
|
-
no_valid_ids = []
|
339
|
-
|
340
|
-
idents.map do |ident|
|
341
|
-
id = valid_id ident
|
342
|
-
no_valid_ids << ident unless id
|
343
|
-
id
|
344
|
-
end
|
345
|
-
idents.compact!
|
346
|
-
|
347
|
-
# TODO: some rsrcs have a 'bulk delete' version...
|
348
|
-
idents.each { |id| cnx.delete "#{rsrc_path}/#{id}" }
|
349
|
-
|
350
|
-
no_valid_ids
|
351
|
-
end
|
352
|
-
|
353
|
-
# Private Class Methods
|
354
|
-
#####################################
|
355
|
-
|
356
|
-
# TODO: better pluralizing?
|
357
|
-
#
|
358
|
-
def self.create_list_methods(attr_name, attr_def)
|
359
|
-
list_method_name = "all_#{attr_name}s"
|
360
|
-
|
361
|
-
define_singleton_method(list_method_name) do |refresh = false, cnx: Jamf.cnx|
|
362
|
-
all_list = all(refresh, cnx: cnx)
|
363
|
-
if attr_def[:class].is_a? Symbol
|
364
|
-
all_list.map { |i| i[attr_name] }.uniq
|
365
|
-
else
|
366
|
-
all_list.map { |i| attr_def[:class].new i[attr_name] }
|
367
|
-
end
|
368
|
-
end # define_singleton_method
|
369
|
-
|
370
|
-
return unless attr_def[:aliases]
|
371
|
-
|
372
|
-
# aliases - TODO: is there a more elegant way?
|
373
|
-
attr_def[:aliases].each do |a|
|
374
|
-
define_singleton_method("all_#{a}s") do |refresh = false, cnx: Jamf.cnx|
|
375
|
-
send list_method_name, refresh, cnx: cnx
|
376
|
-
end # define_singleton_method
|
377
|
-
end # each alias
|
378
|
-
end # create_list_methods
|
379
|
-
private_class_method :create_list_methods
|
380
|
-
|
381
|
-
# Given an indentier attr. key, and a value,
|
382
|
-
# return the id where that ident has that value, or nil
|
383
|
-
#
|
384
|
-
def self.id_from_other_ident(ident, value, refresh = true, cnx: Jamf.cnx)
|
385
|
-
raise ArgumentError, "Unknown identifier '#{ident}' for #{self}" unless identifiers.include? ident
|
386
|
-
|
387
|
-
# check the id itself first
|
388
|
-
return value if ident == :id && all_ids(refresh, cnx: cnx).include?(value)
|
389
|
-
|
390
|
-
# all ident values => ids
|
391
|
-
ident_map = map_all(ident, to: :id, cnx: cnx, refresh: refresh)
|
392
|
-
|
393
|
-
# case-insensitivity for string values
|
394
|
-
value = ident_map.keys.j_ci_fetch(value) if value.is_a? String
|
395
|
-
|
396
|
-
ident_map[value]
|
397
|
-
end
|
398
|
-
private_class_method :id_from_other_ident
|
399
|
-
|
400
|
-
# Instance Methods
|
401
|
-
#####################################
|
402
|
-
|
403
|
-
def exist?
|
404
|
-
!@id.nil?
|
405
|
-
end
|
406
|
-
|
407
|
-
def rsrc_path
|
408
|
-
return unless exist?
|
409
|
-
"#{self.class.rsrc_path}/#{@id}"
|
410
|
-
end
|
411
|
-
|
412
|
-
def delete
|
413
|
-
raise Jamf::UnsupportedError, "Deleting #{self} objects is not currently supported" unless self.class.deletable?
|
414
|
-
@cnx.delete rsrc_path
|
415
|
-
end
|
416
|
-
|
417
|
-
# Two collection resource objects are the same if their id's are the same
|
418
|
-
def <=>(other)
|
419
|
-
id <=> other.id
|
420
|
-
end
|
421
|
-
|
422
|
-
# Private Instance Methods
|
423
|
-
############################################
|
424
|
-
private
|
425
|
-
|
426
|
-
def create_in_jamf
|
427
|
-
result = @cnx.post self.class.rsrc_path, to_jamf
|
428
|
-
@id = result[:id]
|
429
|
-
end
|
430
|
-
|
431
|
-
end # class CollectionResource
|
432
|
-
|
433
|
-
end # module JAMF
|