Class: Jamf::PatchSource

Inherits:
APIObject show all
Includes:
Updatable
Defined in:
lib/jamf/api/classic/base_classes/patch_source.rb

Overview

A patch source. The abstract parent class of PatchInternalSource and PatchExternalSource

See Also:

Direct Known Subclasses

PatchExternalSource, PatchInternalSource

Constant Summary collapse

HTTP =
'http'.freeze
HTTPS =
'https'.freeze
DFT_ENABLED =
false
DFT_SSL =
true
DFT_SSL_PORT =
443
DFT_NO_SSL_PORT =
80
AVAILABLE_TITLES_RSRC =
'patchavailabletitles/sourceid/'.freeze
AVAILABLE_TITLES_DATA_MAP =

TODO: remove this and adjust parsing when jamf fixes the JSON Data map for PatchReport XML data parsing cuz Borked JSON

See Also:

  • for details
{
  patch_available_titles: {
    available_titles: [
      {
        name_id: Jamf::BLANK,
        current_version: Jamf::BLANK,
        publisher: Jamf::BLANK,
        last_modified: Jamf::BLANK,
        app_name: Jamf::BLANK
      }
    ]
  }
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**args) ⇒ PatchSource

Init



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/jamf/api/classic/base_classes/patch_source.rb', line 328

def initialize(**args)
  if instance_of?(Jamf::PatchSource)
    raise Jamf::UnsupportedError,
          'PatchSource is an abstract metaclass. Please use PatchInternalSource or PatchExternalSource'
  end

  super

  @enabled = @init_data[:enabled].to_s.jss_to_bool
  @enabled ||= false

  # derive the data not provided for this source type
  if @init_data[:endpoint]
    @endpoint = @init_data[:endpoint]
    url = URI.parse endpoint
    @host_name = url.host
    @port = url.port
    @ssl_enabled = url.scheme == HTTPS
  else
    @host_name = @init_data[:host_name]
    @port = @init_data[:port].to_i
    @port ||= ssl_enabled? ? DFT_SSL_PORT : DFT_NO_SSL_PORT
    @ssl_enabled = @init_data[:ssl_enabled].to_s.jss_to_bool
    @ssl_enabled ||= false
    @endpoint = "#{ssl_enabled ? HTTPS : HTTP}://#{host_name}:#{port}/"
  end
end

Instance Attribute Details

#enabledBoolean (readonly) Also known as: enabled?

Returns Is this source enabled?.

Returns:

  • (Boolean)

    Is this source enabled?



304
305
306
# File 'lib/jamf/api/classic/base_classes/patch_source.rb', line 304

def enabled
  @enabled
end

#endpointString (readonly) Also known as: url

Returns The URL from which patch info is retrieved.

Returns:

  • (String)

    The URL from which patch info is retrieved



308
309
310
# File 'lib/jamf/api/classic/base_classes/patch_source.rb', line 308

def endpoint
  @endpoint
end

#host_nameString (readonly) Also known as: hostname, host

Returns The host name of the patch source.

Parameters:

  • newname (String)

    The new host name (external sources only)

Returns:

  • (String)

    The host name of the patch source



314
315
316
# File 'lib/jamf/api/classic/base_classes/patch_source.rb', line 314

def host_name
  @host_name
end

#need_to_updateBoolean (readonly) Originally defined in module Updatable

Returns do we have unsaved changes?.

Returns:

  • (Boolean)

    do we have unsaved changes?

#portInteger (readonly)

Returns the TCP port of the patch source.

Parameters:

  • new_port (Integer)

    The new port (external sources only)

Returns:

  • (Integer)

    the TCP port of the patch source



321
322
323
# File 'lib/jamf/api/classic/base_classes/patch_source.rb', line 321

def port
  @port
end

#ssl_enabledBoolean (readonly) Also known as: ssl_enabled?

Returns Is SSL enabled for the patch source?.

Returns:

  • (Boolean)

    Is SSL enabled for the patch source?



324
325
326
# File 'lib/jamf/api/classic/base_classes/patch_source.rb', line 324

def ssl_enabled
  @ssl_enabled
end

Class Method Details

.all(refresh = false, api: nil, cnx: Jamf.cnx) ⇒ Array<Hash{:name=>String, :id=> Integer, :type => Symbol}>

Get names, ids and types for all patch sources

Parameters:

  • refresh (Boolean) (defaults to: false)

    should the data be re-queried from the API?

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

    an API connection to use for the query. Defaults to the corrently active API. See Connection

Returns:



81
82
83
84
85
86
87
88
89
90
# File 'lib/jamf/api/classic/base_classes/patch_source.rb', line 81

def self.all(refresh = false, api: nil, cnx: Jamf.cnx)
  cnx = api if api

  if self == Jamf::PatchSource
    int = Jamf::PatchInternalSource.all(refresh, cnx: cnx).each { |s| s[:type] = :internal }
    ext = Jamf::PatchExternalSource.all(refresh, cnx: cnx).each { |s| s[:type] = :external }
    return (int + ext).sort! { |s1, s2| s1[:id] <=> s2[:id] }
  end
  super
end

.all_external(refresh = false, api: nil, cnx: Jamf.cnx) ⇒ Object

Get names, ids for all patch internal sources

the same as Jamf::PatchExternalSource.all refresh, cnx: cnx

@see  Jamf::PatchExternalSource.all


110
111
112
113
114
# File 'lib/jamf/api/classic/base_classes/patch_source.rb', line 110

def self.all_external(refresh = false, api: nil, cnx: Jamf.cnx)
  cnx = api if api

  Jamf::PatchExternalSource.all refresh, cnx: cnx
end

.all_internal(refresh = false, api: nil, cnx: Jamf.cnx) ⇒ Object

Get names, ids for all patch internal sources

the same as Jamf::PatchInternalSource.all refresh, cnx: cnx

See Also:

  • Jamf::PatchInternalSource.all


98
99
100
101
102
# File 'lib/jamf/api/classic/base_classes/patch_source.rb', line 98

def self.all_internal(refresh = false, api: nil, cnx: Jamf.cnx)
  cnx = api if api

  Jamf::PatchInternalSource.all refresh, cnx: cnx
end

.all_objects(refresh = false, api: nil, cnx: Jamf.cnx) ⇒ Object



118
119
120
121
122
123
124
125
126
127
# File 'lib/jamf/api/classic/base_classes/patch_source.rb', line 118

def self.all_objects(refresh = false, api: nil, cnx: Jamf.cnx)
  cnx = api if api

  if self == Jamf::PatchSource
    int = Jamf::PatchInternalSource.all_objects refresh, cnx: cnx
    ext = Jamf::PatchExternalSource.all_objects refresh, cnx: cnx
    return (int + ext).sort! { |s1, s2| s1.id <=> s2.id }
  end
  super
end

.available_name_ids(source, api: nil, cnx: Jamf.cnx) ⇒ Array<String>

FOr a given patch source, an array of available ‘name_id’s which are uniq identifiers for titles available on that source.

Returns:

  • (Array<String>)

    the name_ids available on the source

See Also:



249
250
251
252
253
# File 'lib/jamf/api/classic/base_classes/patch_source.rb', line 249

def self.available_name_ids(source, api: nil, cnx: Jamf.cnx)
  cnx = api if api

  available_titles(source, cnx: cnx).map { |t| t[:name_id] }
end

.available_titles(source, api: nil, cnx: Jamf.cnx) ⇒ Array<Hash{Symbol:String}>

Get a list of patch titles available from a Patch Source (either internal or external, since they have unique ids )

get the available titles

Parameters:

  • source (String, Integer)

    name or id of the Patch Source for which to

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

    The api connection to use for the query Defaults to the currently active connection

Returns:

  • (Array<Hash{Symbol:String}>)

    One hash for each available title, with these keys:

    :name_id String
    :current_version String
    :publisher String
    :last_modified Time
    :app_name  String
    

Raises:



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/jamf/api/classic/base_classes/patch_source.rb', line 215

def self.available_titles(source, api: nil, cnx: Jamf.cnx)
  cnx = api if api

  src_id = valid_patch_source_id source, cnx: cnx
  raise Jamf::NoSuchItemError, "No Patch Source found matching: #{source}" unless src_id

  rsrc_base =
    if valid_patch_source_type(src_id, cnx: cnx) == :internal
      Jamf::PatchInternalSource::AVAILABLE_TITLES_RSRC
    else
      Jamf::PatchExternalSource::AVAILABLE_TITLES_RSRC
    end

  rsrc = "#{rsrc_base}#{src_id}"

  begin
    # TODO: remove this and adjust parsing when jamf fixes the JSON
    raw = Jamf::XMLWorkaround.data_via_xml(rsrc, AVAILABLE_TITLES_DATA_MAP, cnx)
  rescue Jamf::NoSuchItemError
    return []
  end

  titles = raw[:patch_available_titles][:available_titles]
  titles.each { |t| t[:last_modified] = Jamf.parse_time t[:last_modified] }
  titles
end

.create(**args) ⇒ Object

Only Jamf::PatchExternalSources can be created

See Also:



165
166
167
168
169
170
171
172
173
174
# File 'lib/jamf/api/classic/base_classes/patch_source.rb', line 165

def self.create(**args)
  case name
  when 'Jamf::PatchSource'
    Jamf::PatchExternalSource.make args
  when 'Jamf::PatchExternalSource'
    super
  when 'Jamf::PatchInternalSource'
    raise Jamf::UnsupportedError, 'PatchInteralSources cannot be created.'
  end
end

.delete(victims, api: nil, cnx: Jamf.cnx) ⇒ Object

Only Jamf::PatchExternalSources can be deleted

See Also:



185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/jamf/api/classic/base_classes/patch_source.rb', line 185

def self.delete(victims, api: nil, cnx: Jamf.cnx)
  cnx = api if api

  case name
  when 'Jamf::PatchSource'
    Jamf::PatchExternalSource victims, cnx: cnx
  when 'Jamf::PatchExternalSource'
    super
  when 'Jamf::PatchInternalSource'
    raise Jamf::UnsupportedError, 'PatchInteralSources cannot be deleted.'
  end
end

.fetch(searchterm = nil, **args) ⇒ Object

Fetch either an internal or external patch source

BUG: there’s an API bug when fetching a non-existent patch source which is why we rescue 500 internal server errors and report them as ‘no matching patch source’

See Also:



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/jamf/api/classic/base_classes/patch_source.rb', line 137

def self.fetch(searchterm = nil, **args)
  if self == Jamf::PatchSource
    begin
      fetched = Jamf::PatchInternalSource.fetch searchterm, **args
    rescue
      fetched = nil
    end
    unless fetched
      begin
        fetched = Jamf::PatchExternalSource.fetch searchterm, **args
      rescue
        raise Jamf::NoSuchItemError, 'No matching PatchSource found'
      end
    end
    return fetched
  end # if self == Jamf::PatchSource

  begin
    super searchterm, **args
  rescue Jamf::NoSuchItemError
    raise Jamf::NoSuchItemError, "No matching #{self::RSRC_OBJECT_KEY} found"
  end
end

.make(**args) ⇒ Object

bacward compatibility



177
178
179
# File 'lib/jamf/api/classic/base_classes/patch_source.rb', line 177

def self.make(**args)
  create(**args)
end

.valid_patch_source_id(ident, refresh = false, api: nil, cnx: Jamf.cnx) ⇒ Integer?

Given a name or id for a Patch Source (internal or external) return the id if it exists, or nil if it doesn’t.

NOTE: does not indicate which kind of source it is, just that it exists and can be used as a source_id for a patch title.

Parameters:

  • ident (String, Integer)

    the name or id to validate

  • refresh (Boolean) (defaults to: false)

    Should the data be re-read from the server

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

    an API connection to use for the query. Defaults to the corrently active API. See Connection

Returns:

  • (Integer, nil)

    the valid id or nil if it doesn’t exist.

See Also:



271
272
273
274
275
276
277
# File 'lib/jamf/api/classic/base_classes/patch_source.rb', line 271

def self.valid_patch_source_id(ident, refresh = false, api: nil, cnx: Jamf.cnx)
  cnx = api if api

  id = Jamf::PatchInternalSource.valid_id ident, refresh, cnx: cnx
  id ||= Jamf::PatchExternalSource.valid_id ident, refresh, cnx: cnx
  id
end

.valid_patch_source_type(ident, refresh = false, api: nil, cnx: Jamf.cnx) ⇒ Symbol?

Given a name or id for a Patch Source return :internal or :external if it exists, or nil if it doesnt.

Parameters:

  • ident (String, Integer)

    the name or id to validate

  • refresh (Boolean) (defaults to: false)

    Should the data be re-read from the server

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

    an API connection to use for the query. Defaults to the corrently active API. See Connection

Returns:

  • (Symbol, nil)

    :internal, :external, or nil if it doesn’t exist.



291
292
293
294
295
296
297
298
# File 'lib/jamf/api/classic/base_classes/patch_source.rb', line 291

def self.valid_patch_source_type(ident, refresh = false, api: nil, cnx: Jamf.cnx)
  cnx = api if api

  return :internel if Jamf::PatchInternalSource.valid_id ident, refresh, cnx: cnx
  return :external if Jamf::PatchExternalSource.valid_id ident, refresh, cnx: cnx

  nil
end

Instance Method Details

#available_name_idsObject

Get a list of available name_id’s for this patch source

See Also:



366
367
368
# File 'lib/jamf/api/classic/base_classes/patch_source.rb', line 366

def available_name_ids
  self.class.available_name_ids id, cnx: cnx
end

#available_titlesObject

Get a list of patch titles available from this Patch Source

See Also:



359
360
361
# File 'lib/jamf/api/classic/base_classes/patch_source.rb', line 359

def available_titles
  self.class.available_titles id, cnx: cnx
end

#deleteObject

Delete this instance This method is needed to override APIObject#delete



372
373
374
375
376
377
378
379
# File 'lib/jamf/api/classic/base_classes/patch_source.rb', line 372

def delete
  case self.class.name
  when 'Jamf::PatchExternalSource'
    super
  when 'Jamf::PatchInternalSource'
    raise Jamf::UnsupportedError, 'PatchInteralSources cannot be deleted.'
  end
end

#name=(newname) ⇒ void Originally defined in module Updatable

This method returns an undefined value.

Change the name of this item Remember to #update to push changes to the server.

Parameters:

  • newname (String)

    the new name

Raises: