ruby-jss 1.2.3 → 1.2.4a1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/jamf.rb +169 -0
- data/lib/jamf/api/abstract_classes/collection_resource.rb +422 -0
- data/lib/jamf/api/abstract_classes/generic_reference.rb +145 -0
- data/lib/jamf/api/abstract_classes/json_object.rb +1074 -0
- data/lib/jamf/api/abstract_classes/prestage.rb +219 -0
- data/lib/jamf/api/abstract_classes/prestage_skip_setup_items.rb +126 -0
- data/lib/jamf/api/abstract_classes/resource.rb +250 -0
- data/lib/jamf/api/abstract_classes/singleton_resource.rb +87 -0
- data/lib/jamf/api/attribute_classes/ip_address.rb +66 -0
- data/lib/jamf/api/attribute_classes/timestamp.rb +144 -0
- data/lib/jamf/api/connection.rb +734 -0
- data/lib/jamf/api/connection/api_error.rb +111 -0
- data/lib/jamf/api/connection/api_error_styleguide.rb +96 -0
- data/lib/jamf/api/connection/token.rb +220 -0
- data/lib/jamf/api/json_objects/account_prefs.rb +79 -0
- data/lib/jamf/api/json_objects/android_details.rb +139 -0
- data/lib/jamf/api/json_objects/appletv_details.rb +110 -0
- data/lib/jamf/api/json_objects/attachment.rb +68 -0
- data/lib/jamf/api/json_objects/cellular_network.rb +151 -0
- data/lib/jamf/api/json_objects/change_log_entry.rb +77 -0
- data/lib/jamf/api/json_objects/computer_prestage_skip_setup_items.rb +67 -0
- data/lib/jamf/api/json_objects/country.rb +51 -0
- data/lib/jamf/api/json_objects/extension_attribute_value.rb +128 -0
- data/lib/jamf/api/json_objects/installed_application.rb +59 -0
- data/lib/jamf/api/json_objects/installed_certificate.rb +53 -0
- data/lib/jamf/api/json_objects/installed_configuration_profile.rb +67 -0
- data/lib/jamf/api/json_objects/installed_ebook.rb +58 -0
- data/lib/jamf/api/json_objects/installed_provisioning_profile.rb +59 -0
- data/lib/jamf/api/json_objects/inventory_preload_extension_attribute.rb +52 -0
- data/lib/jamf/api/json_objects/ios_details.rb +244 -0
- data/lib/jamf/api/json_objects/location.rb +95 -0
- data/lib/jamf/api/json_objects/md_prestage_name.rb +57 -0
- data/lib/jamf/api/json_objects/md_prestage_names.rb +82 -0
- data/lib/jamf/api/json_objects/md_prestage_skip_setup_items.rb +165 -0
- data/lib/jamf/api/json_objects/mobile_device_details.rb +219 -0
- data/lib/jamf/api/json_objects/mobile_device_security.rb +101 -0
- data/lib/jamf/api/json_objects/prestage_assignment.rb +61 -0
- data/lib/jamf/api/json_objects/prestage_location.rb +104 -0
- data/lib/jamf/api/json_objects/prestage_purchasing_data.rb +132 -0
- data/lib/jamf/api/json_objects/prestage_scope.rb +54 -0
- data/lib/jamf/api/json_objects/prestage_sync_status.rb +63 -0
- data/lib/jamf/api/json_objects/purchasing_data.rb +125 -0
- data/lib/jamf/api/mixins/abstract.rb +58 -0
- data/lib/jamf/api/mixins/bulk_deletable.rb +39 -0
- data/lib/jamf/api/mixins/change_log.rb +136 -0
- data/lib/jamf/api/mixins/extendable.rb +75 -0
- data/lib/jamf/api/mixins/immutable.rb +39 -0
- data/lib/jamf/api/mixins/locatable.rb +124 -0
- data/lib/jamf/api/mixins/lockable.rb +48 -0
- data/lib/jamf/api/mixins/referable.rb +92 -0
- data/lib/jamf/api/mixins/searchable.rb +202 -0
- data/lib/jamf/api/mixins/uncreatable.rb +40 -0
- data/lib/jamf/api/mixins/undeletable.rb +40 -0
- data/lib/jamf/api/resources/collection_resources/account.rb +163 -0
- data/lib/jamf/api/resources/collection_resources/building.rb +114 -0
- data/lib/jamf/api/resources/collection_resources/category.rb +82 -0
- data/lib/jamf/api/resources/collection_resources/computer.rb +49 -0
- data/lib/jamf/api/resources/collection_resources/computer_prestage.rb +80 -0
- data/lib/jamf/api/resources/collection_resources/department.rb +79 -0
- data/lib/jamf/api/resources/collection_resources/extension_attribute.rb +45 -0
- data/lib/jamf/api/resources/collection_resources/inventory_preload_record.rb +274 -0
- data/lib/jamf/api/resources/collection_resources/md_prestage.rb +139 -0
- data/lib/jamf/api/resources/collection_resources/mobile_device.rb +315 -0
- data/lib/jamf/api/resources/collection_resources/script.rb +190 -0
- data/lib/jamf/api/resources/collection_resources/site.rb +77 -0
- data/lib/jamf/api/resources/singleton_resources/app_store_country_codes.rb +131 -0
- data/lib/jamf/api/resources/singleton_resources/authorization.rb +88 -0
- data/lib/jamf/api/resources/singleton_resources/client_checkin_settings.rb +139 -0
- data/lib/jamf/api/resources/singleton_resources/reenrollment_settings.rb +95 -0
- data/lib/jamf/client.rb +301 -0
- data/lib/jamf/client/jamf_binary.rb +132 -0
- data/lib/jamf/client/jamf_helper.rb +298 -0
- data/lib/jamf/client/management_action.rb +114 -0
- data/lib/jamf/compatibility.rb +88 -0
- data/lib/jamf/composer.rb +190 -0
- data/lib/jamf/configuration.rb +281 -0
- data/lib/jamf/exceptions.rb +107 -0
- data/lib/jamf/ruby_extensions.rb +36 -0
- data/lib/jamf/ruby_extensions/array.rb +35 -0
- data/lib/jamf/ruby_extensions/array/predicates.rb +46 -0
- data/lib/jamf/ruby_extensions/array/utils.rb +47 -0
- data/lib/jamf/ruby_extensions/filetest.rb +32 -0
- data/lib/jamf/ruby_extensions/filetest/predicates.rb +46 -0
- data/lib/jamf/ruby_extensions/hash.rb +33 -0
- data/lib/jamf/ruby_extensions/hash/backports.rb +92 -0
- data/lib/jamf/ruby_extensions/ipaddr.rb +37 -0
- data/lib/jamf/ruby_extensions/ipaddr/utils.rb +95 -0
- data/lib/jamf/ruby_extensions/object.rb +30 -0
- data/lib/jamf/ruby_extensions/object/predicates.rb +51 -0
- data/lib/jamf/ruby_extensions/pathname.rb +39 -0
- data/lib/jamf/ruby_extensions/pathname/predicates.rb +50 -0
- data/lib/jamf/ruby_extensions/pathname/utils.rb +75 -0
- data/lib/jamf/ruby_extensions/string.rb +35 -0
- data/lib/jamf/ruby_extensions/string/backports.rb +66 -0
- data/lib/jamf/ruby_extensions/string/conversions.rb +65 -0
- data/lib/jamf/ruby_extensions/string/predicates.rb +47 -0
- data/lib/jamf/utility.rb +423 -0
- data/lib/jamf/validate.rb +224 -0
- data/lib/jamf/version.rb +32 -0
- data/lib/jpapi.rb +26 -0
- data/lib/jss/version.rb +1 -1
- metadata +104 -4
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# Copyright 2019 Pixar
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "Apache License")
|
|
4
|
+
# with the following modification; you may not use this file except in
|
|
5
|
+
# compliance with the Apache License and the following modification to it:
|
|
6
|
+
# Section 6. Trademarks. is deleted and replaced with:
|
|
7
|
+
#
|
|
8
|
+
# 6. Trademarks. This License does not grant permission to use the trade
|
|
9
|
+
# names, trademarks, service marks, or product names of the Licensor
|
|
10
|
+
# and its affiliates, except as required to comply with Section 4(c) of
|
|
11
|
+
# the License and to reproduce the content of the NOTICE file.
|
|
12
|
+
#
|
|
13
|
+
# You may obtain a copy of the Apache License at
|
|
14
|
+
#
|
|
15
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
16
|
+
#
|
|
17
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
18
|
+
# distributed under the Apache License with the above modification is
|
|
19
|
+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
20
|
+
# KIND, either express or implied. See the Apache License for the specific
|
|
21
|
+
# language governing permissions and limitations under the Apache License.
|
|
22
|
+
#
|
|
23
|
+
#
|
|
24
|
+
|
|
25
|
+
module Jamf
|
|
26
|
+
|
|
27
|
+
# This mixin overrides JSONObject.mutable? to return false,
|
|
28
|
+
# meaning that no setters are ever defined, and if the
|
|
29
|
+
# object is a Jamf::Resource, #save will raise an error
|
|
30
|
+
#
|
|
31
|
+
module Immutable
|
|
32
|
+
|
|
33
|
+
def mutable?
|
|
34
|
+
false
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
end # Lockable
|
|
38
|
+
|
|
39
|
+
end # Jamf
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
# Copyright 2019 Pixar
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "Apache License")
|
|
4
|
+
# with the following modification; you may not use this file except in
|
|
5
|
+
# compliance with the Apache License and the following modification to it:
|
|
6
|
+
# Section 6. Trademarks. is deleted and replaced with:
|
|
7
|
+
#
|
|
8
|
+
# 6. Trademarks. This License does not grant permission to use the trade
|
|
9
|
+
# names, trademarks, service marks, or product names of the Licensor
|
|
10
|
+
# and its affiliates, except as required to comply with Section 4(c) of
|
|
11
|
+
# the License and to reproduce the content of the NOTICE file.
|
|
12
|
+
#
|
|
13
|
+
# You may obtain a copy of the Apache License at
|
|
14
|
+
#
|
|
15
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
16
|
+
#
|
|
17
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
18
|
+
# distributed under the Apache License with the above modification is
|
|
19
|
+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
20
|
+
# KIND, either express or implied. See the Apache License for the specific
|
|
21
|
+
# language governing permissions and limitations under the Apache License.
|
|
22
|
+
#
|
|
23
|
+
#
|
|
24
|
+
|
|
25
|
+
module Jamf
|
|
26
|
+
|
|
27
|
+
# Classes mixing this in have a Jamf::Location instance in their :location
|
|
28
|
+
# attribute.
|
|
29
|
+
#
|
|
30
|
+
# See the Jamf::JSONObject docs for OBJECT_MODEL for defining the
|
|
31
|
+
# :location attribute. It should always be marked `readonly: true`,
|
|
32
|
+
# since attempting to set the location as a whole will raise an error.
|
|
33
|
+
#
|
|
34
|
+
# Instead of setting it as a whole, you must use the Jamf::Location object
|
|
35
|
+
# stored in the :location attribute to access it's values, like so:
|
|
36
|
+
#
|
|
37
|
+
# mycomputer.location.department # => 'Muggle Studies'
|
|
38
|
+
# mycomputer.location.room = 'Great Hall'
|
|
39
|
+
#
|
|
40
|
+
# All location attributes can be set that way, but building, room, department,
|
|
41
|
+
# & phoneNumber are more related to the physical location, so must be used
|
|
42
|
+
# that way.
|
|
43
|
+
#
|
|
44
|
+
# The other attributes, username, realName, position, and emailAddress, are
|
|
45
|
+
# more related to the assinged user than the physical location, and are also
|
|
46
|
+
# acessible via the #user method, which returns a simple wrapper for those
|
|
47
|
+
# attributes, like this:
|
|
48
|
+
#
|
|
49
|
+
# mycomputer.user.name # => 'adumble' user.username works too
|
|
50
|
+
# mycomputer.user.realName = 'Albus Dumbledore'
|
|
51
|
+
# mycomputer.user.position = 'Headmaster'
|
|
52
|
+
# mycomputer.user.emailAddress = 'adumble@hogwarts.edu.uk'
|
|
53
|
+
#
|
|
54
|
+
# are more related to the machine and its physical location
|
|
55
|
+
# than to the user responsible for the machine.
|
|
56
|
+
#
|
|
57
|
+
module Locatable
|
|
58
|
+
|
|
59
|
+
def location=(_loc)
|
|
60
|
+
raise Jamf::UnsupportedError, 'Location cannot be set as a whole, set its attributes individually'
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def user
|
|
64
|
+
@location_user ||= Jamf::Locatable::User.new(self)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# a class for user-specific data in a Location object
|
|
68
|
+
class User
|
|
69
|
+
|
|
70
|
+
def initialize(assigned_object)
|
|
71
|
+
@assigned_object = assigned_object
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def username
|
|
75
|
+
@assigned_object.location.username
|
|
76
|
+
end
|
|
77
|
+
alias name username
|
|
78
|
+
|
|
79
|
+
def username=(val)
|
|
80
|
+
@assigned_object.location.username = val
|
|
81
|
+
end
|
|
82
|
+
alias name= username=
|
|
83
|
+
|
|
84
|
+
def realName
|
|
85
|
+
@assigned_object.location.realName
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def realName=(val)
|
|
89
|
+
@assigned_object.location.realName = val
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def emailAddress
|
|
93
|
+
@assigned_object.location.emailAddress
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def emailAddress=(val)
|
|
97
|
+
@assigned_object.location.emailAddress = val
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def position
|
|
101
|
+
@assigned_object.location.position
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def position=(val)
|
|
105
|
+
@assigned_object.location.position = val
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# Remove large cached items from
|
|
109
|
+
# the instance_variables used to create
|
|
110
|
+
# pretty-print (pp) output.
|
|
111
|
+
#
|
|
112
|
+
# @return [Array] the desired instance_variables
|
|
113
|
+
#
|
|
114
|
+
def pretty_print_instance_variables
|
|
115
|
+
vars = instance_variables.sort
|
|
116
|
+
vars.delete :@assigned_object
|
|
117
|
+
vars
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
end # class user
|
|
121
|
+
|
|
122
|
+
end # locatable
|
|
123
|
+
|
|
124
|
+
end # Jamf
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# Copyright 2019 Pixar
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "Apache License")
|
|
4
|
+
# with the following modification; you may not use this file except in
|
|
5
|
+
# compliance with the Apache License and the following modification to it:
|
|
6
|
+
# Section 6. Trademarks. is deleted and replaced with:
|
|
7
|
+
#
|
|
8
|
+
# 6. Trademarks. This License does not grant permission to use the trade
|
|
9
|
+
# names, trademarks, service marks, or product names of the Licensor
|
|
10
|
+
# and its affiliates, except as required to comply with Section 4(c) of
|
|
11
|
+
# the License and to reproduce the content of the NOTICE file.
|
|
12
|
+
#
|
|
13
|
+
# You may obtain a copy of the Apache License at
|
|
14
|
+
#
|
|
15
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
16
|
+
#
|
|
17
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
18
|
+
# distributed under the Apache License with the above modification is
|
|
19
|
+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
20
|
+
# KIND, either express or implied. See the Apache License for the specific
|
|
21
|
+
# language governing permissions and limitations under the Apache License.
|
|
22
|
+
#
|
|
23
|
+
#
|
|
24
|
+
|
|
25
|
+
module Jamf
|
|
26
|
+
|
|
27
|
+
# Classes mixing this in have a 'versionLock' attribute and implement
|
|
28
|
+
# 'Optimistic Locking'
|
|
29
|
+
# https://stackoverflow.com/questions/129329/optimistic-vs-pessimistic-locking/129397#129397
|
|
30
|
+
#
|
|
31
|
+
# When the object is saved, the versionLock is sent back with the data
|
|
32
|
+
# and if it doesn't match whats on the server, then the object has been updated
|
|
33
|
+
# from elsewhere since we fetched it, and a 409 Conflict error is raised with
|
|
34
|
+
# the reason OPTIMISTIC_LOCK_FAILED.
|
|
35
|
+
#
|
|
36
|
+
# If that happens, the save doesnt happen, the object is re-fetched,
|
|
37
|
+
# and the user can try again.
|
|
38
|
+
#
|
|
39
|
+
module Lockable
|
|
40
|
+
|
|
41
|
+
def initialize(data, cnx: Jamf.cnx)
|
|
42
|
+
@versionLock = data[:versionLock]
|
|
43
|
+
super
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
end # Lockable
|
|
47
|
+
|
|
48
|
+
end # Jamf
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# Copyright 2019 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
|
+
module Jamf
|
|
27
|
+
|
|
28
|
+
# CollectionResource Class instances with this module mixed-in can be
|
|
29
|
+
# referred to in other classes.
|
|
30
|
+
#
|
|
31
|
+
# Doing so dynamically defines a new 'Reference' class, which is a
|
|
32
|
+
# subclass of {Jamf::GenericReference}
|
|
33
|
+
#
|
|
34
|
+
# Examples:
|
|
35
|
+
#
|
|
36
|
+
# 1) A Policy can have a category assigned to it, and the policy's API data
|
|
37
|
+
# will contain a reference to a category.
|
|
38
|
+
#
|
|
39
|
+
# Jamf::Policy instances have a :category attribute, which contains an
|
|
40
|
+
# instance of Jamf::Category::Reference
|
|
41
|
+
#
|
|
42
|
+
# Mixing Referable into Jamf::Category automatically creates the class
|
|
43
|
+
# Jamf::Category::Reference
|
|
44
|
+
#
|
|
45
|
+
# 2) The API data for a Jamf::ComputerGroup contains a an Array of references
|
|
46
|
+
# to member Jamf::Computers. That Array arrives in JSON like this:
|
|
47
|
+
#
|
|
48
|
+
# computers: [
|
|
49
|
+
# { id: 234, name: 'foobar' },
|
|
50
|
+
# { id: 698, name: 'barfoo' }
|
|
51
|
+
# ]
|
|
52
|
+
#
|
|
53
|
+
# Mixing Referable into Jamf::Computer defines the class
|
|
54
|
+
# Jamf::Computer::Reference and allows Jamf::ComputerGroups to maintain an
|
|
55
|
+
# array of Jamf::Computer::Reference objects representing that list.
|
|
56
|
+
# When needed to send to the API, it will send this:
|
|
57
|
+
#
|
|
58
|
+
# computers: [
|
|
59
|
+
# { id: 234 },
|
|
60
|
+
# { id: 698 }
|
|
61
|
+
# ]
|
|
62
|
+
#
|
|
63
|
+
# Parsing the API data into Reference instances, and converting them back for
|
|
64
|
+
# the API is handled by the Reference class.
|
|
65
|
+
#
|
|
66
|
+
# TODO: Handle if any references contain keys other than :id and :name.
|
|
67
|
+
#
|
|
68
|
+
module Referable
|
|
69
|
+
|
|
70
|
+
# this will hopefully autoload generic_reference
|
|
71
|
+
GENREF = Jamf::GenericReference
|
|
72
|
+
|
|
73
|
+
# This is run when Referable is included in some class 'referent'
|
|
74
|
+
# It creates class referent::Reference as a subclass of
|
|
75
|
+
# GenericReference, and sets the REFERENT_CLASS constant of
|
|
76
|
+
# referent::Reference to referent.
|
|
77
|
+
#
|
|
78
|
+
def self.included(referent)
|
|
79
|
+
raise JSS::InvalidDataError, "'#{referent}' is not a subclass of Jamf::CollectionResource, can't include 'Referable'" unless referent.ancestors.include? Jamf::CollectionResource
|
|
80
|
+
referent.const_set :Reference, Class.new(GENREF)
|
|
81
|
+
referent::Reference.const_set :REFERENT_CLASS, referent
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# @return [self.class::GenericReference] A reference to this object.
|
|
85
|
+
#
|
|
86
|
+
def reference
|
|
87
|
+
@reference ||= self.class::Reference.new self
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
end # module Referable
|
|
91
|
+
|
|
92
|
+
end # module
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
# Copyright 2019 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
|
+
module Jamf
|
|
27
|
+
|
|
28
|
+
# Code for CollectionResources that have a .../searchObjects resource.
|
|
29
|
+
# These resources provide for basic searching as will as sorting and paging of
|
|
30
|
+
# the found set.
|
|
31
|
+
#
|
|
32
|
+
# NOTE: This module provides class methods, not instance methods, and
|
|
33
|
+
# therefore must be mixed in with `extend` and not `include`
|
|
34
|
+
#
|
|
35
|
+
# Classes extended with this module *must* define:
|
|
36
|
+
#
|
|
37
|
+
# SEARCH_RSRC [String] the resource to which the search is POSTed.
|
|
38
|
+
#
|
|
39
|
+
#
|
|
40
|
+
# Classes including this module *may* define:
|
|
41
|
+
#
|
|
42
|
+
# SEARCH_FIELDS [Hash{Symbol: Symbol}]
|
|
43
|
+
#
|
|
44
|
+
# The hash keys are the names of searchable fields, e.g. 'name', or 'udid'
|
|
45
|
+
# and the values are the class expected for the field, one of:
|
|
46
|
+
# :string, :integer, or :boolean
|
|
47
|
+
#
|
|
48
|
+
# Without defining search fields, every search returns all objects, but
|
|
49
|
+
# the sorting and paging options are honored.
|
|
50
|
+
#
|
|
51
|
+
# If search fields are provided, then they can be specified when performing
|
|
52
|
+
# a search. If more than one is specified, they are AND'ed together.
|
|
53
|
+
#
|
|
54
|
+
# To Search:
|
|
55
|
+
#
|
|
56
|
+
# 1) Set result sort order
|
|
57
|
+
#
|
|
58
|
+
# If you want sorted results, first set the sort order with a combination of
|
|
59
|
+
# `MyClass.search_result_order = field, dir` where field is the name of the
|
|
60
|
+
# sort fiend and dir is :asc or :desc
|
|
61
|
+
#
|
|
62
|
+
# If you want a multi-level sort, set the first sort field as above,
|
|
63
|
+
# then use `MyClass.search_result_order_append field, dir` to add
|
|
64
|
+
# subsequent sort fields
|
|
65
|
+
#
|
|
66
|
+
# To clear them out and start over, call `MyClass.clear_search_result_order`
|
|
67
|
+
#
|
|
68
|
+
# 2) Set page size
|
|
69
|
+
#
|
|
70
|
+
# If you want paged results then call `MyClass.search_result_page_size = X`
|
|
71
|
+
# before starting your seacch
|
|
72
|
+
#
|
|
73
|
+
# 3) Do the search:
|
|
74
|
+
# MyClass.search
|
|
75
|
+
#
|
|
76
|
+
module Searchable
|
|
77
|
+
|
|
78
|
+
SORT_DIRECTIONS = {
|
|
79
|
+
asc: 'ASC',
|
|
80
|
+
desc: 'DESC'
|
|
81
|
+
}.freeze
|
|
82
|
+
|
|
83
|
+
def search_result_page_size
|
|
84
|
+
@search_result_page_size ||= 0
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def search_result_page_size=(size)
|
|
88
|
+
raise 'Size must be a non-negative integer' unless size.is_a?(Integer) && size >= 0
|
|
89
|
+
@search_result_page_size = size
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def search_result_order
|
|
93
|
+
@search_result_order ||= []
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def search_result_order=(fieldname, dir = :asc)
|
|
97
|
+
raise 'direction must be either :asc, or :desc' unless SORT_DIRECTIONS.key? dir
|
|
98
|
+
@search_result_order = [{ field: fieldname, direction: SORT_DIRECTIONS[dir] }]
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def search_result_order_append(fieldname, dir = :asc)
|
|
102
|
+
raise 'direction must be either :asc, or :desc' unless SORT_DIRECTIONS.key? dir
|
|
103
|
+
search_result_order << { field: fieldname, direction: SORT_DIRECTIONS[dir] }
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def clear_search_result_order
|
|
107
|
+
@search_result_order = []
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# TODO: occasional excludedIds array
|
|
111
|
+
def search(pageNumber: 0, cnx: Jamf.cnx, load_all: true, **terms)
|
|
112
|
+
raise "#{self} is not searchable in the API" unless defined? self::SEARCH_RSRC
|
|
113
|
+
|
|
114
|
+
search_body = {
|
|
115
|
+
pageNumber: pageNumber,
|
|
116
|
+
pageSize: @search_result_page_size,
|
|
117
|
+
isLoadToEnd: load_all,
|
|
118
|
+
orderBy: @search_result_order
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if defined? self::SEARCH_FIELDS
|
|
122
|
+
terms.each do |fld, val|
|
|
123
|
+
next unless self::SEARCH_FIELDS.key? fld
|
|
124
|
+
|
|
125
|
+
case self::SEARCH_FIELDS[fld]
|
|
126
|
+
when :integer
|
|
127
|
+
val = Jamf::Validate.integer val, "Search value for #{fld} must be an Integer"
|
|
128
|
+
when :string
|
|
129
|
+
val = Jamf::Validate.string val, "Search value for #{fld} must be a String"
|
|
130
|
+
when :boolean
|
|
131
|
+
val = Jamf::Validate.boolean val, "Search value for #{fld} must be a boolean"
|
|
132
|
+
end # case
|
|
133
|
+
|
|
134
|
+
search_body[fld] = val
|
|
135
|
+
end # terms.each
|
|
136
|
+
end # if defined? self::SEARCH_FIELDS
|
|
137
|
+
|
|
138
|
+
cnx.post self::SEARCH_RSRC, search_body
|
|
139
|
+
|
|
140
|
+
end # def search
|
|
141
|
+
|
|
142
|
+
class OrderBy < Jamf::JSONObject
|
|
143
|
+
|
|
144
|
+
SORT_DIRECTIONS = {
|
|
145
|
+
asc: 'ASC',
|
|
146
|
+
desc: 'DESC'
|
|
147
|
+
}.freeze
|
|
148
|
+
|
|
149
|
+
OBJECT_MODEL = {
|
|
150
|
+
|
|
151
|
+
# @!attribute field
|
|
152
|
+
# @return [String]
|
|
153
|
+
field: {
|
|
154
|
+
class: :string
|
|
155
|
+
},
|
|
156
|
+
|
|
157
|
+
# @!attribute direction
|
|
158
|
+
# @return [String]
|
|
159
|
+
direction: {
|
|
160
|
+
class: :string,
|
|
161
|
+
enum: SORT_DIRECTIONS
|
|
162
|
+
}
|
|
163
|
+
}.freeze
|
|
164
|
+
|
|
165
|
+
end # class Orderby
|
|
166
|
+
|
|
167
|
+
class SeachParams < Jamf::JSONObject
|
|
168
|
+
|
|
169
|
+
OBJECT_MODEL = {
|
|
170
|
+
|
|
171
|
+
# @!attribute pageSize
|
|
172
|
+
# @return [Integer]
|
|
173
|
+
pageSize: {
|
|
174
|
+
class: :integer
|
|
175
|
+
},
|
|
176
|
+
|
|
177
|
+
# @!attribute pageNumber
|
|
178
|
+
# @return [Integer]
|
|
179
|
+
pageNumber: {
|
|
180
|
+
class: :integer
|
|
181
|
+
},
|
|
182
|
+
|
|
183
|
+
# @!attribute isLoadToEnd
|
|
184
|
+
# @return [Boolean]
|
|
185
|
+
isLoadToEnd: {
|
|
186
|
+
class: :boolean
|
|
187
|
+
},
|
|
188
|
+
|
|
189
|
+
# @!attribute orderBy
|
|
190
|
+
# @return [Boolean]
|
|
191
|
+
orderBy: {
|
|
192
|
+
class: Jamf::Searchable::OrderBy,
|
|
193
|
+
multi: true
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
}.freeze
|
|
197
|
+
|
|
198
|
+
end # class SearchParams
|
|
199
|
+
|
|
200
|
+
end # module searchable
|
|
201
|
+
|
|
202
|
+
end # module
|