ruby-jss 0.6.5 → 0.6.6

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.

Files changed (79) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES.md +57 -5
  3. data/lib/jss.rb +78 -94
  4. data/lib/jss/api_connection.rb +8 -0
  5. data/lib/jss/api_object.rb +126 -102
  6. data/lib/jss/api_object/creatable.rb +33 -15
  7. data/lib/jss/api_object/distribution_point.rb +4 -1
  8. data/lib/jss/api_object/extension_attribute.rb +1 -1
  9. data/lib/jss/api_object/package.rb +121 -187
  10. data/lib/jss/api_object/policy.rb +590 -251
  11. data/lib/jss/api_object/script.rb +92 -128
  12. data/lib/jss/api_object/self_servable.rb +93 -117
  13. data/lib/jss/api_object/updatable.rb +12 -27
  14. data/lib/jss/api_object/uploadable.rb +12 -15
  15. data/lib/jss/client.rb +178 -156
  16. data/lib/jss/db_connection.rb +34 -49
  17. data/lib/jss/ruby_extensions/string.rb +25 -21
  18. data/lib/jss/version.rb +1 -1
  19. data/lib/jss/webhooks.rb +52 -0
  20. data/lib/jss/webhooks/README.md +269 -0
  21. data/lib/jss/webhooks/configuration.rb +212 -0
  22. data/lib/jss/webhooks/data/sample_handlers/RestAPIOperation-executable +90 -0
  23. data/lib/jss/webhooks/data/sample_handlers/RestAPIOperation.rb +44 -0
  24. data/lib/jss/webhooks/data/sample_jsons/ComputerAdded.json +27 -0
  25. data/lib/jss/webhooks/data/sample_jsons/ComputerCheckIn.json +27 -0
  26. data/lib/jss/webhooks/data/sample_jsons/ComputerInventoryCompleted.json +27 -0
  27. data/lib/jss/webhooks/data/sample_jsons/ComputerPolicyFinished.json +27 -0
  28. data/lib/jss/webhooks/data/sample_jsons/ComputerPushCapabilityChanged.json +27 -0
  29. data/lib/jss/webhooks/data/sample_jsons/JSSShutdown.json +14 -0
  30. data/lib/jss/webhooks/data/sample_jsons/JSSStartup.json +14 -0
  31. data/lib/jss/webhooks/data/sample_jsons/MobileDeviceCheckIn.json +26 -0
  32. data/lib/jss/webhooks/data/sample_jsons/MobileDeviceCommandCompleted.json +26 -0
  33. data/lib/jss/webhooks/data/sample_jsons/MobileDeviceEnrolled.json +26 -0
  34. data/lib/jss/webhooks/data/sample_jsons/MobileDevicePushSent.json +26 -0
  35. data/lib/jss/webhooks/data/sample_jsons/MobileDeviceUnEnrolled.json +26 -0
  36. data/lib/jss/webhooks/data/sample_jsons/PatchSoftwareTitleUpdated.json +14 -0
  37. data/lib/jss/webhooks/data/sample_jsons/PushSent.json +11 -0
  38. data/lib/jss/webhooks/data/sample_jsons/RestAPIOperation.json +15 -0
  39. data/lib/jss/webhooks/data/sample_jsons/SCEPChallenge.json +10 -0
  40. data/lib/jss/webhooks/data/sample_jsons/SmartGroupComputerMembershipChange.json +13 -0
  41. data/lib/jss/webhooks/data/sample_jsons/SmartGroupMobileDeviceMembershipChange.json +13 -0
  42. data/lib/jss/webhooks/event.rb +138 -0
  43. data/lib/jss/webhooks/event/computer_added.rb +37 -0
  44. data/lib/jss/webhooks/event/computer_check_in.rb +37 -0
  45. data/lib/jss/webhooks/event/computer_inventory_completed.rb +37 -0
  46. data/lib/jss/webhooks/event/computer_policy_finished.rb +37 -0
  47. data/lib/jss/webhooks/event/computer_push_capability_changed.rb +37 -0
  48. data/lib/jss/webhooks/event/handlers.rb +191 -0
  49. data/lib/jss/webhooks/event/jss_shutdown.rb +37 -0
  50. data/lib/jss/webhooks/event/jss_startup.rb +37 -0
  51. data/lib/jss/webhooks/event/mobile_device_check_in.rb +37 -0
  52. data/lib/jss/webhooks/event/mobile_device_command_completed.rb +37 -0
  53. data/lib/jss/webhooks/event/mobile_device_enrolled.rb +37 -0
  54. data/lib/jss/webhooks/event/mobile_device_push_sent.rb +37 -0
  55. data/lib/jss/webhooks/event/mobile_device_unenrolled.rb +37 -0
  56. data/lib/jss/webhooks/event/patch_software_title_updated.rb +37 -0
  57. data/lib/jss/webhooks/event/push_sent.rb +37 -0
  58. data/lib/jss/webhooks/event/rest_api_operation.rb +37 -0
  59. data/lib/jss/webhooks/event/scep_challenge.rb +37 -0
  60. data/lib/jss/webhooks/event/smart_group_computer_membership_change.rb +37 -0
  61. data/lib/jss/webhooks/event/smart_group_mobile_device_membership_change.rb +37 -0
  62. data/lib/jss/webhooks/event/webhook.rb +39 -0
  63. data/lib/jss/webhooks/event_objects.rb +111 -0
  64. data/lib/jss/webhooks/event_objects/computer.rb +48 -0
  65. data/lib/jss/webhooks/event_objects/jss.rb +35 -0
  66. data/lib/jss/webhooks/event_objects/mobile_device.rb +47 -0
  67. data/lib/jss/webhooks/event_objects/patch_software_title_update.rb +37 -0
  68. data/lib/jss/webhooks/event_objects/push.rb +32 -0
  69. data/lib/jss/webhooks/event_objects/rest_api_operation.rb +36 -0
  70. data/lib/jss/webhooks/event_objects/scep_challenge.rb +31 -0
  71. data/lib/jss/webhooks/event_objects/smart_group.rb +34 -0
  72. data/lib/jss/webhooks/server_app.rb +36 -0
  73. data/lib/jss/webhooks/server_app/routes.rb +26 -0
  74. data/lib/jss/webhooks/server_app/routes/handle_webhook_event.rb +38 -0
  75. data/lib/jss/webhooks/server_app/routes/home.rb +36 -0
  76. data/lib/jss/webhooks/server_app/self_signed_cert.rb +64 -0
  77. data/lib/jss/webhooks/server_app/server.rb +59 -0
  78. data/lib/jss/webhooks/version.rb +31 -0
  79. metadata +63 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: edccb1a8e4436aae8603aecc748d5cd817115144
4
- data.tar.gz: 7f169bdfa8623ee12699e241872ddee9259d19b9
3
+ metadata.gz: cc64e9aded3743cf2fd02ec8fb482457e9f39bd0
4
+ data.tar.gz: 56ffca6c41aee0c3716911327f7498b9076ce31d
5
5
  SHA512:
6
- metadata.gz: a25d238371cdfd1157b3e11320002ac10f6da9b90acf4b10cadfba5f54cc932d0b3d4525d22f3e471f318b778ed0c45afc987a560bb08afd3a86b0eec66bb19a
7
- data.tar.gz: 48f2c4bc65aad055eb5e84eb3ef2a195706d4253cdb7b4a0ba3675d76fea364921ef7fb79002e78aea358138a4dd3b19056a4b1f425b4589cb3b5a3e2b929f3b
6
+ metadata.gz: 5331d7bfedde26f888004442a3093201abaa1d8ad0baafacfc06997192d91526604996d00a0a688e648516c513372532a80eb239e7ff4f7db79b391dbe5ac838
7
+ data.tar.gz: c353adf65aa0031cddcc381a1e0eec3fbd10c7cb83b92a7ce329a2aff97244b62ac1affa9447f3760906269409db9aa4214c4f0ad0dec92ca0959d762d701c6c
data/CHANGES.md CHANGED
@@ -1,5 +1,57 @@
1
1
  # Change History
2
2
 
3
+ ## v0.6.6 2016-11-30
4
+
5
+ - Added String#jss_to_pathname to convert Strings to Pathname instances in JSS::Configuration.
6
+
7
+ - JSS::DBConnection#connect now returns the server hostname, to match the behavior of JSS::APIConnection#connect
8
+
9
+ - JSS::Client: added .console_user method
10
+
11
+ - JSS::Policy, now creatable, and self-servable, and more attributes are updatable
12
+
13
+ - JSS::SelfServable, finally getting this module functional, at least for policies
14
+
15
+ - JSS::Creatable, added #clone method - all creatable objects can be cloned easily.
16
+
17
+ - JSS::APIObject, added .exist? and .valid_id class methods and #to_s instance method
18
+
19
+ - Change the mountpoint directory for JSS::DistributionPoint#mount to /tmp, because Sierra doesn't allow mounting in /Volumes except by root. (Thanks @LM2807! )
20
+
21
+ - Starting cleaning up code to better adhere to [RuboCop](http://rubocop.readthedocs.io/en/latest/) standards
22
+
23
+ - Added alpha version of a JSS WebHooks framwork
24
+
25
+ ## v0.6.5 2016-08-10
26
+
27
+ - Cleanup of redundant constants (Thanks @aurica!)
28
+
29
+ - Added JSS::ComputerInvitation class (Thanks @tostart-pickagreatname!)
30
+
31
+ - Added JSS::Account class (Thanks @JonPulsifer!)
32
+
33
+ - Added JSS::OSXConfigurationProfile class (Thanks @tostart-pickagreatname!)
34
+
35
+ - JSS::Computer: added methods #boot_drive, #filevault2_enabled?, and #filevault1_accounts
36
+
37
+ - Various small bugfixes & improvements
38
+
39
+ ## v0.6.4 2016-03-24
40
+
41
+ - JSS::Package#dlete can optionally delete the master file at the same time
42
+
43
+ - Added an example ruby-jss.conf file with internal documentation
44
+
45
+ - Various small bugfixes & improvements
46
+
47
+ - Updated the config file name to match the new gem name, maintaining backwards compatibility
48
+
49
+ - Improved error messages
50
+
51
+ ## v0.6.3 2016-03-09
52
+
53
+ Maintenence version bump to fix an issue uploading to rubygems.org
54
+
3
55
  ## v0.6.2 2016-03-08
4
56
 
5
57
  As of v0.6.2, the github project, and rubygem have been renamed to 'ruby-jss'. The 'require' name is now 'jss'.
@@ -19,7 +71,7 @@ The 'jss-api' gem has been updated one last time, also to v0.6.2. That gem has a
19
71
  #### bugfixes
20
72
 
21
73
  - JSS::stdin_lines no longer uses a constant to store incoming stdin data at load time, which causes hangs when there's no terminal on stdin. Now stdin is only read when the method is called, and data stored in a module variable.
22
-
74
+
23
75
  - JSS::Composer::mk_dmg fix for building/indexing dmg's, no longer creates an unreadable .Trashes folder.
24
76
 
25
77
  - Several small typos and other tiny bugs.
@@ -33,10 +85,10 @@ The 'jss-api' gem has been updated one last time, also to v0.6.2. That gem has a
33
85
 
34
86
  ## v0.6.0 2016-01-06
35
87
 
36
- This version of the jss-api gem incorporates changes needed for the upcoming release of d3 (a.k.a. depot3),
88
+ This version of the jss-api gem incorporates changes needed for the upcoming release of d3 (a.k.a. depot3),
37
89
  a package/patch management system for Casper, which was the reason for the jss-api gem to begin with.
38
90
 
39
- As such, while the JSS module will continue to be a separate git repo, specific commits of it will be
91
+ As such, while the JSS module will continue to be a separate git repo, specific commits of it will be
40
92
  submodules of the depot3 git repo, starting with this one, or one shortly hereafter.
41
93
 
42
94
 
@@ -74,7 +126,7 @@ Also: As of casper 9.72, the argument requirements havechanged for 'jamf install
74
126
  #### bugfixes & cleanup
75
127
 
76
128
  - location.rb: location value setters are now properly converted to strings
77
- - api_connection.rb: #connect now takes :use_ssl option (defaults to true)
129
+ - api_connection.rb: #connect now takes :use_ssl option (defaults to true)
78
130
  - api_connection.rb: #connect will accept arbitrary ports when :use_ssl is true
79
131
 
80
132
  #### additions & features
@@ -107,6 +159,6 @@ Also: As of casper 9.72, the argument requirements havechanged for 'jamf install
107
159
  - many documentation updates as we adjust to being live
108
160
  - minor bugfixes
109
161
 
110
- ## v0.5.0 2014-10-23
162
+ ## v0.5.0 2014-10-23
111
163
 
112
164
  - first opensource release
data/lib/jss.rb CHANGED
@@ -23,18 +23,17 @@
23
23
  ###
24
24
 
25
25
  ###
26
- ### JSS, A Ruby module for interacting with the JAMF Software Server via it's REST API.
26
+ ### JSS, A Ruby module for interacting with the JAMF Pro Server via it's REST API.
27
27
  ###
28
28
  module JSS
29
29
 
30
30
  ### The minimum Ruby version that works with this gem
31
- MINIMUM_RUBY_VERSION = "2.0.0"
31
+ MINIMUM_RUBY_VERSION = '2.0.0'.freeze
32
32
 
33
33
  if Gem::Version.new(RUBY_VERSION) < Gem::Version.new(MINIMUM_RUBY_VERSION)
34
34
  raise "Can't use the JSS module, ruby itself must be version #{MINIMUM_RUBY_VERSION} or greater."
35
35
  end
36
36
 
37
- #####################################
38
37
  ### Required Libraries, etc
39
38
  #####################################
40
39
 
@@ -45,12 +44,13 @@ module JSS
45
44
  require 'pathname'
46
45
  require 'fileutils'
47
46
  require 'uri'
48
- require "ipaddr"
49
- require "rexml/document"
50
- require "base64"
51
- require "shellwords"
52
- require "digest"
47
+ require 'ipaddr'
48
+ require 'rexml/document'
49
+ require 'base64'
50
+ require 'shellwords'
51
+ require 'digest'
53
52
  require 'yaml'
53
+ require 'open3'
54
54
 
55
55
  ###################
56
56
  ### Gems
@@ -58,145 +58,129 @@ module JSS
58
58
  require 'json'
59
59
  require 'plist'
60
60
 
61
-
62
-
63
- #####################################
64
61
  ### Constants
65
62
  #####################################
66
63
 
67
64
  ### The minimum JSS version that works with this gem, as returned by the API
68
65
  ### in the deprecated 'jssuser' resource
69
- MINIMUM_SERVER_VERSION = "9.4"
70
-
71
-
66
+ MINIMUM_SERVER_VERSION = '9.4'.freeze
72
67
 
73
68
  ### The current local UTC offset as a fraction of a day (Time.now.utc_offset is the offset in seconds,
74
69
  ### 60*60*24 is the seconds in a day)
75
- TIME_ZONE_OFFSET = Rational(Time.now.utc_offset, 60*60*24)
70
+ TIME_ZONE_OFFSET = Rational(Time.now.utc_offset, 60 * 60 * 24)
76
71
 
77
72
  ### These are handy for testing values without making new arrays, strings, etc every time.
78
- TRUE_FALSE = [true, false]
73
+ TRUE_FALSE = [true, false].freeze
79
74
 
80
75
  ### When parsing a date/time data into a Time object, these will return nil
81
- NIL_DATES = [0, nil, '', '0']
82
-
76
+ NIL_DATES = [0, nil, '', '0'].freeze
83
77
 
84
- #####################################
85
78
  ### Module Variables
86
79
  #####################################
87
80
 
88
81
  ### The contents of anything piped to stdin, split into lines. See {JSS.stdin}
89
82
  @@stdin_lines = nil
90
83
 
91
- #####################################
92
84
  ### Module Methods
93
85
  #####################################
94
86
 
95
-
96
- ###
97
87
  ### Define classes and submodules here so that they don't
98
88
  ### generate errors when referenced during the loading of
99
89
  ### the library.
100
90
  ###
101
91
 
102
- #####################################
103
92
  ### Sub Modules
104
93
  #####################################
105
94
 
106
- module Composer ; end
95
+ module Composer; end
107
96
 
108
97
  ### Mix-in Sub Modules
109
98
 
110
- module Creatable ; end
111
- module FileUpload ; end
112
- module Locatable ; end
113
- module Matchable ; end
114
- module Purchasable ; end
115
- module Updatable ; end
116
- module Extendable ; end
99
+ module Creatable; end
100
+ module FileUpload; end
101
+ module Locatable; end
102
+ module Matchable; end
103
+ module Purchasable; end
104
+ module Updatable; end
105
+ module Extendable; end
117
106
 
118
107
  ### Mix-in Sub Modules with Classes
119
108
 
120
- module Criteriable ; end
121
- class Criteriable::Criteria ; end
122
- class Criteriable::Criterion ; end
123
-
124
- module Scopable ; end
125
- class Scopable::Scope ; end
109
+ module Criteriable
110
+ class Criteria; end
111
+ class Criterion; end
112
+ end
113
+ module Scopable
114
+ class Scope ; end
115
+ end
126
116
 
127
- #####################################
128
117
  ### Classes
129
118
  #####################################
130
119
 
131
- class APIObject ; end
132
- class APIConnection ; end
133
- class Client ; end
134
- class DBConnection ; end
135
- class Server ; end
136
- class Preferences ; end
120
+ class APIObject; end
121
+ class APIConnection; end
122
+ class Client; end
123
+ class DBConnection; end
124
+ class Server; end
125
+ class Preferences; end
137
126
 
138
- #####################################
139
127
  ### SubClasses
140
128
  #####################################
141
129
 
142
130
  ### APIObject Classes with SubClasses
143
131
 
144
- class AdvancedSearch < JSS::APIObject ; end
145
- class AdvancedComputerSearch < JSS::AdvancedSearch ; end
146
- class AdvancedMobileDeviceSearch < JSS::AdvancedSearch ; end
147
- class AdvancedUserSearch < JSS::AdvancedSearch ; end
148
-
132
+ class AdvancedSearch < JSS::APIObject; end
133
+ class AdvancedComputerSearch < JSS::AdvancedSearch; end
134
+ class AdvancedMobileDeviceSearch < JSS::AdvancedSearch; end
135
+ class AdvancedUserSearch < JSS::AdvancedSearch; end
149
136
 
150
- class ExtensionAttribute < JSS::APIObject ; end
151
- class ComputerExtensionAttribute < JSS::ExtensionAttribute ; end
152
- class MobileDeviceExtensionAttribute < JSS::ExtensionAttribute ; end
153
- class UserExtensionAttribute < JSS::ExtensionAttribute ; end
137
+ class ExtensionAttribute < JSS::APIObject; end
138
+ class ComputerExtensionAttribute < JSS::ExtensionAttribute; end
139
+ class MobileDeviceExtensionAttribute < JSS::ExtensionAttribute; end
140
+ class UserExtensionAttribute < JSS::ExtensionAttribute; end
154
141
 
155
- class Group < JSS::APIObject ; end
156
- class ComputerGroup < JSS::Group ; end
157
- class MobileDeviceGroup < JSS::Group ; end
158
- class UserGroup < JSS::Group ; end
142
+ class Group < JSS::APIObject; end
143
+ class ComputerGroup < JSS::Group; end
144
+ class MobileDeviceGroup < JSS::Group; end
145
+ class UserGroup < JSS::Group; end
159
146
 
160
147
  ### APIObject Classes without SubClasses
161
148
 
162
- class Account < JSS::APIObject ; end
163
- class Building < JSS::APIObject ; end
164
- class Category < JSS::APIObject ; end
165
- class Computer < JSS::APIObject ; end
166
- class Department < JSS::APIObject ; end
167
- class DistributionPoint < JSS::APIObject ; end
168
- class LDAPServer < JSS::APIObject ; end
169
- class MobileDevice < JSS::APIObject ; end
170
- class NetBootServer < JSS::APIObject ; end
171
- class NetworkSegment < JSS::APIObject ; end
172
- class OSXConfigurationProfile < JSS::APIObject ; end
173
- class Package < JSS::APIObject ; end
174
- class PeripheralType < JSS::APIObject ; end
175
- class Peripheral < JSS::APIObject ; end
176
- class Policy < JSS::APIObject ; end
177
- class RemovableMacAddress < JSS::APIObject ; end
178
- class RestrictedSoftware < JSS::APIObject ; end
179
- class Script < JSS::APIObject ; end
180
- class Site < JSS::APIObject ; end
181
- class SoftwareUpdateServer < JSS::APIObject ; end
182
- class User < JSS::APIObject ; end
183
-
149
+ class Account < JSS::APIObject; end
150
+ class Building < JSS::APIObject; end
151
+ class Category < JSS::APIObject; end
152
+ class Computer < JSS::APIObject; end
153
+ class Department < JSS::APIObject; end
154
+ class DistributionPoint < JSS::APIObject; end
155
+ class LDAPServer < JSS::APIObject; end
156
+ class MobileDevice < JSS::APIObject; end
157
+ class NetBootServer < JSS::APIObject; end
158
+ class NetworkSegment < JSS::APIObject; end
159
+ class OSXConfigurationProfile < JSS::APIObject; end
160
+ class Package < JSS::APIObject; end
161
+ class PeripheralType < JSS::APIObject; end
162
+ class Peripheral < JSS::APIObject; end
163
+ class Policy < JSS::APIObject; end
164
+ class RemovableMacAddress < JSS::APIObject; end
165
+ class RestrictedSoftware < JSS::APIObject; end
166
+ class Script < JSS::APIObject; end
167
+ class Site < JSS::APIObject; end
168
+ class SoftwareUpdateServer < JSS::APIObject; end
169
+ class User < JSS::APIObject; end
184
170
 
185
171
  end # module JSS
186
172
 
187
-
188
- ##################
189
173
  ### Load the rest of the module
190
174
 
191
- require "jss/utility"
192
- require "jss/composer"
193
- require "jss/compatibility"
194
- require "jss/ruby_extensions"
195
- require "jss/exceptions"
196
- require "jss/api_connection"
197
- require "jss/api_object"
198
- require "jss/server"
199
- require "jss/client"
200
- require "jss/configuration"
201
- require "jss/db_connection"
202
- require "jss/version"
175
+ require 'jss/utility'
176
+ require 'jss/composer'
177
+ require 'jss/compatibility'
178
+ require 'jss/ruby_extensions'
179
+ require 'jss/exceptions'
180
+ require 'jss/api_connection'
181
+ require 'jss/api_object'
182
+ require 'jss/server'
183
+ require 'jss/client'
184
+ require 'jss/configuration'
185
+ require 'jss/db_connection'
186
+ require 'jss/version'
@@ -233,6 +233,14 @@ module JSS
233
233
  return @connected ? @server_host : nil
234
234
  end # connect
235
235
 
236
+ ### A useful string about this connection
237
+ ###
238
+ ### @return [String]
239
+ ###
240
+ def to_s
241
+ @connected ? "Using #{@rest_url} as user #{@jss_user}" : "not connected"
242
+ end
243
+
236
244
  ###
237
245
  ### Reset the response timeout for the rest connection
238
246
  ###
@@ -25,19 +25,15 @@
25
25
  ###
26
26
  module JSS
27
27
 
28
- #####################################
29
28
  ### Module Variables
30
29
  #####################################
31
30
 
32
- #####################################
33
31
  ### Module Methods
34
32
  #####################################
35
33
 
36
- #####################################
37
34
  ### Classes
38
35
  #####################################
39
36
 
40
- ###
41
37
  ### This class is the parent to all JSS API objects. It provides standard methods and structures
42
38
  ### that apply to all API resouces.
43
39
  ###
@@ -113,11 +109,9 @@ module JSS
113
109
  ###
114
110
  class APIObject
115
111
 
116
- #####################################
117
112
  ### Mix-Ins
118
113
  #####################################
119
114
 
120
- #####################################
121
115
  ### Class Variables
122
116
  #####################################
123
117
 
@@ -131,11 +125,9 @@ module JSS
131
125
  ###
132
126
  @@all_items = {}
133
127
 
134
- #####################################
135
128
  ### Class Methods
136
129
  #####################################
137
130
 
138
- ###
139
131
  ### Return an Array of Hashes for all objects of this subclass in the JSS.
140
132
  ###
141
133
  ### This method is only valid in subclasses of JSS::APIObject, and is
@@ -167,13 +159,12 @@ module JSS
167
159
  ### @return [Array<Hash{:name=>String, :id=> Integer}>]
168
160
  ###
169
161
  def self.all(refresh = false)
170
- raise JSS::UnsupportedError, ".all can only be called on subclasses of JSS::APIObject" if self == JSS::APIObject
162
+ raise JSS::UnsupportedError, '.all can only be called on subclasses of JSS::APIObject' if self == JSS::APIObject
171
163
  @@all_items[self::RSRC_LIST_KEY] = nil if refresh
172
164
  return @@all_items[self::RSRC_LIST_KEY] if @@all_items[self::RSRC_LIST_KEY]
173
165
  @@all_items[self::RSRC_LIST_KEY] = JSS::API.get_rsrc(self::RSRC_BASE)[self::RSRC_LIST_KEY]
174
166
  end
175
167
 
176
- ###
177
168
  ### Returns an Array of the JSS id numbers of all the members
178
169
  ### of the subclass.
179
170
  ###
@@ -185,10 +176,9 @@ module JSS
185
176
  ### @return [Array<Integer>] the ids of all items of this subclass in the JSS
186
177
  ###
187
178
  def self.all_ids(refresh = false)
188
- self.all(refresh).map{|i| i[:id]}
179
+ all(refresh).map { |i| i[:id] }
189
180
  end
190
181
 
191
- ###
192
182
  ### Returns an Array of the JSS names of all the members
193
183
  ### of the subclass.
194
184
  ###
@@ -200,10 +190,9 @@ module JSS
200
190
  ### @return [Array<String>] the names of all item of this subclass in the JSS
201
191
  ###
202
192
  def self.all_names(refresh = false)
203
- self.all(refresh).map{|i| i[:name]}
193
+ all(refresh).map { |i| i[:name] }
204
194
  end
205
195
 
206
- ###
207
196
  ### Return a hash of all objects of this subclass
208
197
  ### in the JSS where the key is the id, and the value
209
198
  ### is some other key in the data items returned by the JSS::APIObject.all.
@@ -232,11 +221,10 @@ module JSS
232
221
  ###
233
222
  def self.map_all_ids_to(other_key, refresh = false)
234
223
  h = {}
235
- self.all(refresh).each{|i| h[i[:id]] = i[other_key]}
224
+ all(refresh).each { |i| h[i[:id]] = i[other_key] }
236
225
  h
237
226
  end
238
227
 
239
-
240
228
  ### Return an Array of JSS::APIObject subclass instances
241
229
  ### e.g when called on JSS::Package, return all JSS::Package
242
230
  ### objects in the JSS.
@@ -251,12 +239,52 @@ module JSS
251
239
  objects_key = "#{self::RSRC_LIST_KEY}_objects".to_sym
252
240
  @@all_items[objects_key] = nil if refresh
253
241
  return @@all_items[objects_key] if @@all_items[objects_key]
254
- @@all_items[objects_key] = self.all(refresh = false).map{|o| self.new :id => o[:id]}
242
+ @@all_items[objects_key] = all(refresh = false).map { |o| new id: o[:id] }
255
243
  end
256
244
 
245
+ ### Return true or false if an object of this subclass
246
+ ### with the given name or id exists on the server
247
+ ###
248
+ ### @param identfier [String,Integer] The name or id of object to check for
249
+ ###
250
+ ### @param refresh [Boolean] Should the data be re-read from the server
251
+ ###
252
+ ### @return [Boolean] does an object with the given name or id exist?
253
+ ###
254
+ def self.exist?(identfier, refresh = false)
255
+ case identfier
256
+ when Integer
257
+ all_ids(refresh).include? identfier
258
+ when String
259
+ all_names(refresh).include? identfier
260
+ else
261
+ raise ArgumentError, 'Identifier must be a name (String) or id (Integer)'
262
+ end
263
+ end
257
264
 
258
-
265
+ ### Return an id or nil if an object of this subclass
266
+ ### with the given name or id exists on the server
267
+ ###
268
+ ### Subclasses may want to override this method to support more
269
+ ### identifiers than name and id.
259
270
  ###
271
+ ### @param identfier [String,Integer] The name or id of object to check for
272
+ ###
273
+ ### @param refresh [Boolean] Should the data be re-read from the server
274
+ ###
275
+ ### @return [Integer, nil] the id of the matching object, or nil if it doesn't exist
276
+ ###
277
+ def self.valid_id(identfier, refresh = false)
278
+ case identfier
279
+ when Integer
280
+ return identfier if all_ids(refresh).include? identfier
281
+ when String
282
+ return map_all_ids_to(:name).invert[identfier] if all_names(refresh).include? identfier
283
+ else
284
+ raise ArgumentError, 'Identifier must be a name (String) or id (Integer)'
285
+ end
286
+ end
287
+
260
288
  ### Convert an Array of Hashes of API object data to a
261
289
  ### REXML element.
262
290
  ###
@@ -303,7 +331,6 @@ module JSS
303
331
  JSS.item_list_to_rexml_list self::RSRC_LIST_KEY, self::RSRC_OBJECT_KEY, array, content
304
332
  end
305
333
 
306
- ###
307
334
  ### Some API objects contain references to other API objects. Usually those
308
335
  ### references are a Hash containing the :id and :name of the target. Sometimes,
309
336
  ### however the reference is just the name of the target.
@@ -332,35 +359,31 @@ module JSS
332
359
  ###
333
360
  def self.get_name(a_thing)
334
361
  case a_thing
335
- when String
336
- a_thing
337
- when Hash
338
- a_thing[:name]
339
- when nil
340
- nil
362
+ when String
363
+ a_thing
364
+ when Hash
365
+ a_thing[:name]
366
+ when nil
367
+ nil
341
368
  end
342
369
  end
343
370
 
344
- #####################################
345
371
  ### Class Constants
346
372
  #####################################
347
373
 
348
- ###
349
374
  ### These Symbols are added to VALID_DATA_KEYS for performing the
350
375
  ### :data validity test described above.
351
376
  ###
352
- REQUIRED_DATA_KEYS = [:id, :name]
377
+ REQUIRED_DATA_KEYS = [:id, :name].freeze
353
378
 
354
- ###
355
379
  ### By default, these keys are available for object lookups
356
380
  ### Others can be added by subclasses using an array of them
357
381
  ### as the second argument to super(initialize)
358
382
  ### The keys must be Symbols that match the keyname in the resource url.
359
383
  ### e.g. :serialnumber for JSSResource/computers/serialnumber/xxxxx
360
384
  ###
361
- DEFAULT_LOOKUP_KEYS = [:id, :name]
385
+ DEFAULT_LOOKUP_KEYS = [:id, :name].freeze
362
386
 
363
- #####################################
364
387
  ### Attributes
365
388
  #####################################
366
389
 
@@ -376,11 +399,9 @@ module JSS
376
399
  ### @return [String] the Rest resource for API access (the part after "JSSResource/" )
377
400
  attr_reader :rest_rsrc
378
401
 
379
- #####################################
380
402
  ### Constructor
381
403
  #####################################
382
404
 
383
- ###
384
405
  ### The args hash must include :id, :name, or :data.
385
406
  ### * :id or :name will be looked up via the API
386
407
  ### * * if the subclass includes JSS::Creatable, :id can be :new, to create a new object in the JSS.
@@ -402,56 +423,57 @@ module JSS
402
423
  ###
403
424
  def initialize(args = {}, other_lookup_keys = [])
404
425
 
405
- ################################
406
- ################################
407
426
  ####### Previously looked-up JSON data
408
427
  if args[:data]
409
428
 
410
429
  @init_data = args[:data]
411
430
  ### Does this data come in subsets?
412
- @got_subsets = @init_data[:general].kind_of?(Hash)
431
+ @got_subsets = @init_data[:general].is_a?(Hash)
413
432
 
414
433
  ### data must include all they keys in REQUIRED_DATA_KEYS + VALID_DATA_KEYS
415
434
  ### in either the main hash keys or the :general sub-hash, if it exists
416
435
  hash_to_check = @got_subsets ? @init_data[:general] : @init_data
417
436
  combined_valid_keys = self.class::REQUIRED_DATA_KEYS + self.class::VALID_DATA_KEYS
418
437
  keys_ok = (hash_to_check.keys & combined_valid_keys).count == combined_valid_keys.count
419
- raise JSS::InvalidDataError, ":data is not valid JSON for a #{self.class::RSRC_OBJECT_KEY} from the API. It needs at least the keys :#{combined_valid_keys.join ', :'}" unless keys_ok
438
+ unless keys_ok
439
+ raise(
440
+ JSS::InvalidDataError,
441
+ ":data is not valid JSON for a #{self.class::RSRC_OBJECT_KEY} from the API. It needs at least the keys :#{combined_valid_keys.join ', :'}"
442
+ )
443
+ end
420
444
 
421
445
  ### and the id must be in the jss
422
- raise NoSuchItemError, "No #{self.class::RSRC_OBJECT_KEY} with JSS id: #{@init_data[:id]}" unless self.class.all_ids.include? hash_to_check[:id]
446
+ raise NoSuchItemError, "No #{self.class::RSRC_OBJECT_KEY} with JSS id: #{@init_data[:id]}" unless \
447
+ self.class.all_ids.include? hash_to_check[:id]
423
448
 
424
-
425
- ################################
426
- ################################
427
449
  ###### Make a new one in the JSS, but only if we've included the Creatable module
428
450
  elsif args[:id] == :new
429
451
 
430
- raise JSS::UnsupportedError, "Creating #{self.class::RSRC_LIST_KEY} isn't yet supported. Please use other Casper workflows." unless defined? self.class::CREATABLE
431
-
432
- raise JSS::MissingDataError, "You must provide a :name for a new #{self.class::RSRC_OBJECT_KEY}." unless args[:name]
452
+ raise JSS::UnsupportedError, "Creating #{self.class::RSRC_LIST_KEY} isn't yet supported. Please use other Casper workflows." \
453
+ unless defined? self.class::CREATABLE
433
454
 
434
- raise JSS::AlreadyExistsError, "A #{self.class::RSRC_OBJECT_KEY} already exists with the name '#{args[:name]}'" if self.class.all_names.include? args[:name]
455
+ raise JSS::MissingDataError, "You must provide a :name for a new #{self.class::RSRC_OBJECT_KEY}." \
456
+ unless args[:name]
435
457
 
458
+ raise JSS::AlreadyExistsError, "A #{self.class::RSRC_OBJECT_KEY} already exists with the name '#{args[:name]}'" \
459
+ if self.class.all_names.include? args[:name]
436
460
 
437
461
  ### NOTE: subclasses may want to pre-populate more keys in @init_data when :id == :new
438
462
  ### then parse them into attributes later.
439
463
  @name = args[:name]
440
- @init_data = {:name => args[:name]}
464
+ @init_data = { name: args[:name] }
441
465
  @in_jss = false
442
466
  @rest_rsrc = "#{self.class::RSRC_BASE}/name/#{CGI.escape @name}"
443
467
  @need_to_update = true
444
468
  return
445
469
 
446
- ################################
447
- ################################
448
470
  ###### Look up the data via the API
449
471
  else
450
472
  ### what lookup key are we using?
451
473
  combined_lookup_keys = self.class::DEFAULT_LOOKUP_KEYS + other_lookup_keys
452
474
  lookup_key = (combined_lookup_keys & args.keys)[0]
453
475
 
454
- raise JSS::MissingDataError, "Args must include :#{combined_lookup_keys.join(', :')}, or :data" unless lookup_key
476
+ raise JSS::MissingDataError, "Args must include :#{combined_lookup_keys.join(', :')}, or :data" unless lookup_key
455
477
 
456
478
  rsrc = "#{self.class::RSRC_BASE}/#{lookup_key}/#{args[lookup_key]}"
457
479
 
@@ -477,7 +499,7 @@ module JSS
477
499
  # Whereever both :id and :name are, that's the main subset
478
500
 
479
501
  @init_data.keys.each do |subset|
480
- @main_subset = @init_data[subset] if @init_data[subset].kind_of? Hash and @init_data[subset][:id] and @init_data[subset][:name]
502
+ @main_subset = @init_data[subset] if @init_data[subset].is_a?(Hash) && @init_data[subset][:id] && @init_data[subset][:name]
481
503
  break if @main_subset
482
504
  end
483
505
  @main_subset ||= @init_data
@@ -487,27 +509,25 @@ module JSS
487
509
 
488
510
  # many things have a :site
489
511
  if @main_subset[:site]
490
- @site = JSS::APIObject.get_name( @main_subset[:site])
512
+ @site = JSS::APIObject.get_name(@main_subset[:site])
491
513
  end
492
514
 
493
515
  # many things have a :category
494
516
  if @main_subset[:category]
495
- @category = JSS::APIObject.get_name( @main_subset[:category])
517
+ @category = JSS::APIObject.get_name(@main_subset[:category])
496
518
  end
497
519
 
498
520
  # set empty strings to nil
499
521
  @init_data.jss_nillify! '', :recurse
500
522
 
501
523
  @in_jss = true
502
- @rest_rsrc = "#{self.class::RSRC_BASE}/id/#{@id}"
524
+ @rest_rsrc = "#{self.class::RSRC_BASE}/id/#{@id}"
503
525
  @need_to_update = false
504
526
  end # init
505
527
 
506
- #####################################
507
528
  ### Public Instance Methods
508
529
  #####################################
509
530
 
510
- ###
511
531
  ### Either Create or Update this object in the JSS
512
532
  ###
513
533
  ### If this item is creatable or updatable, then
@@ -517,18 +537,16 @@ module JSS
517
537
  ###
518
538
  def save
519
539
  if @in_jss
520
- raise JSS::UnsupportedError, "Updating this object in the JSS is currently not supported" \
540
+ raise JSS::UnsupportedError, 'Updating this object in the JSS is currently not supported' \
521
541
  unless defined? self.class::UPDATABLE
522
- return self.update
542
+ update
523
543
  else
524
- raise JSS::UnsupportedError, "Creating this object in the JSS is currently not supported" \
544
+ raise JSS::UnsupportedError, 'Creating this object in the JSS is currently not supported' \
525
545
  unless defined? self.class::CREATABLE
526
- return self.create
546
+ create
527
547
  end
528
548
  end
529
549
 
530
-
531
- ###
532
550
  ### Delete this item from the JSS.
533
551
  ###
534
552
  ### Subclasses may want to redefine this method,
@@ -544,14 +562,23 @@ module JSS
544
562
  @id = nil
545
563
  @in_jss = false
546
564
  @need_to_update = false
565
+ :deleted
547
566
  end # delete
548
567
 
549
- #####################################
568
+
569
+ ### A meaningful string representation of this object
570
+ ###
571
+ ### @return [String]
572
+ ###
573
+ def to_s
574
+ "#{self.class}, name: #{@name}, id: #{@id}"
575
+ end
576
+
577
+
550
578
  ### Private Instance Methods
551
579
  #####################################
552
580
  private
553
581
 
554
- ###
555
582
  ### Return a String with the XML Resource
556
583
  ### for submitting creation or changes to the JSS via
557
584
  ### the API via the Creatable or Updatable modules
@@ -562,59 +589,56 @@ module JSS
562
589
  doc = REXML::Document.new APIConnection::XML_HEADER
563
590
  tmpl = doc.add_element self.class::RSRC_OBJECT_KEY.to_s
564
591
  tmpl.add_element('name').text = @name
565
- return doc.to_s
592
+ doc.to_s
566
593
  end
567
594
 
568
-
569
595
  ### Aliases
570
596
 
571
597
  alias in_jss? in_jss
572
598
 
573
599
  end # class APIObject
574
600
 
575
-
576
-
577
601
  end # module JSS
578
602
 
579
603
  ### Mix-in Sub Modules
580
- require "jss/api_object/creatable"
581
- require "jss/api_object/uploadable"
582
- require "jss/api_object/locatable"
583
- require "jss/api_object/matchable"
584
- require "jss/api_object/purchasable"
585
- require "jss/api_object/updatable"
586
- require "jss/api_object/extendable"
587
- require "jss/api_object/self_servable"
604
+ require 'jss/api_object/creatable'
605
+ require 'jss/api_object/uploadable'
606
+ require 'jss/api_object/locatable'
607
+ require 'jss/api_object/matchable'
608
+ require 'jss/api_object/purchasable'
609
+ require 'jss/api_object/updatable'
610
+ require 'jss/api_object/extendable'
611
+ require 'jss/api_object/self_servable'
588
612
 
589
613
  ### Mix-in Sub Modules with Classes
590
- require "jss/api_object/criteriable"
591
- require "jss/api_object/scopable"
614
+ require 'jss/api_object/criteriable'
615
+ require 'jss/api_object/scopable'
592
616
 
593
617
  ### APIObject SubClasses with SubClasses
594
- require "jss/api_object/advanced_search"
595
- require "jss/api_object/extension_attribute"
596
- require "jss/api_object/group"
618
+ require 'jss/api_object/advanced_search'
619
+ require 'jss/api_object/extension_attribute'
620
+ require 'jss/api_object/group'
597
621
 
598
622
  ### APIObject SubClasses without SubClasses
599
- require "jss/api_object/account"
600
- require "jss/api_object/building"
601
- require "jss/api_object/category"
602
- require "jss/api_object/computer"
603
- require "jss/api_object/computer_invitation"
604
- require "jss/api_object/department"
605
- require "jss/api_object/distribution_point"
606
- require "jss/api_object/ldap_server"
607
- require "jss/api_object/mobile_device"
608
- require "jss/api_object/netboot_server"
609
- require "jss/api_object/network_segment"
610
- require "jss/api_object/osx_configuration_profile"
611
- require "jss/api_object/package"
612
- require "jss/api_object/peripheral_type"
613
- require "jss/api_object/peripheral"
614
- require "jss/api_object/policy"
615
- require "jss/api_object/removable_macaddr"
616
- require "jss/api_object/restricted_software"
617
- require "jss/api_object/script"
618
- require "jss/api_object/site"
619
- require "jss/api_object/software_update_server"
620
- require "jss/api_object/user"
623
+ require 'jss/api_object/account'
624
+ require 'jss/api_object/building'
625
+ require 'jss/api_object/category'
626
+ require 'jss/api_object/computer'
627
+ require 'jss/api_object/computer_invitation'
628
+ require 'jss/api_object/department'
629
+ require 'jss/api_object/distribution_point'
630
+ require 'jss/api_object/ldap_server'
631
+ require 'jss/api_object/mobile_device'
632
+ require 'jss/api_object/netboot_server'
633
+ require 'jss/api_object/network_segment'
634
+ require 'jss/api_object/osx_configuration_profile'
635
+ require 'jss/api_object/package'
636
+ require 'jss/api_object/peripheral_type'
637
+ require 'jss/api_object/peripheral'
638
+ require 'jss/api_object/policy'
639
+ require 'jss/api_object/removable_macaddr'
640
+ require 'jss/api_object/restricted_software'
641
+ require 'jss/api_object/script'
642
+ require 'jss/api_object/site'
643
+ require 'jss/api_object/software_update_server'
644
+ require 'jss/api_object/user'