Class: JSS::PatchSource

Inherits:
APIObject show all
Includes:
Updatable
Defined in:
lib/jss/api_object/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: JSS::BLANK,
        current_version: JSS::BLANK,
        publisher: JSS::BLANK,
        last_modified: JSS::BLANK,
        app_name: JSS::BLANK
      }
    ]
  }
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**args) ⇒ PatchSource

Init



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/jss/api_object/patch_source.rb', line 303

def initialize(**args)
  raise JSS::UnsupportedError, 'PatchSource is an abstract metaclass. Please use PatchInternalSource or PatchExternalSource' if self.class == JSS::PatchSource

  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?



279
280
281
# File 'lib/jss/api_object/patch_source.rb', line 279

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



283
284
285
# File 'lib/jss/api_object/patch_source.rb', line 283

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



289
290
291
# File 'lib/jss/api_object/patch_source.rb', line 289

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



296
297
298
# File 'lib/jss/api_object/patch_source.rb', line 296

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?



299
300
301
# File 'lib/jss/api_object/patch_source.rb', line 299

def ssl_enabled
  @ssl_enabled
end

Class Method Details

.all(refresh = false, api: JSS.api) ⇒ 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?

  • api (JSS::APIConnection) (defaults to: JSS.api)

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

Returns:



81
82
83
84
85
86
87
88
# File 'lib/jss/api_object/patch_source.rb', line 81

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

.all_external(refresh = false, api: JSS.api) ⇒ Object

Get names, ids for all patch internal sources

the same as JSS::PatchExternalSource.all refresh, api: api

@see  JSS::PatchExternalSource.all


106
107
108
# File 'lib/jss/api_object/patch_source.rb', line 106

def self.all_external(refresh = false, api: JSS.api)
  JSS::PatchExternalSource.all refresh, api: api
end

.all_internal(refresh = false, api: JSS.api) ⇒ Object

Get names, ids for all patch internal sources

the same as JSS::PatchInternalSource.all refresh, api: api

See Also:

  • JSS::PatchInternalSource.all


96
97
98
# File 'lib/jss/api_object/patch_source.rb', line 96

def self.all_internal(refresh = false, api: JSS.api)
  JSS::PatchInternalSource.all refresh, api: api
end

.all_objects(refresh = false, api: JSS.api) ⇒ Object



112
113
114
115
116
117
118
119
# File 'lib/jss/api_object/patch_source.rb', line 112

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

.available_name_ids(source, api: JSS.api) ⇒ 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:



231
232
233
# File 'lib/jss/api_object/patch_source.rb', line 231

def self.available_name_ids(source, api: JSS.api)
  available_titles(source, api: api).map { |t| t[:name_id] }
end

.available_titles(source, api: JSS.api) ⇒ 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

  • api (JSS::APIConnection) (defaults to: JSS.api)

    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:



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/jss/api_object/patch_source.rb', line 199

def self.available_titles(source, api: JSS.api)
  src_id = valid_patch_source_id source, api: api
  raise JSS::NoSuchItemError, "No Patch Source found matching: #{source}" unless src_id

  rsrc_base =
    if valid_patch_source_type(src_id, api: api) == :internal
      JSS::PatchInternalSource::AVAILABLE_TITLES_RSRC
    else
      JSS::PatchExternalSource::AVAILABLE_TITLES_RSRC
    end

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

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

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

.delete(victims, api: JSS.api) ⇒ Object

Only JSS::PatchExternalSources can be deleted

See Also:



171
172
173
174
175
176
177
178
179
180
# File 'lib/jss/api_object/patch_source.rb', line 171

def self.delete(victims, api: JSS.api)
  case self.name
  when 'JSS::PatchSource'
    JSS::PatchExternalSource victims, api: api
  when 'JSS::PatchExternalSource'
    super
  when 'JSS::PatchInternalSource'
    raise JSS::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: fetching a non-existent which is why we rescue internal server errors.

See Also:



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/jss/api_object/patch_source.rb', line 128

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

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

.make(**args) ⇒ Object

Only JSS::PatchExternalSources can be created

See Also:



156
157
158
159
160
161
162
163
164
165
# File 'lib/jss/api_object/patch_source.rb', line 156

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

.valid_patch_source_id(ident, refresh = false, api: JSS.api) ⇒ 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

  • api (JSS::APIConnection) (defaults to: JSS.api)

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

Returns:

  • (Integer, nil)

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

See Also:



251
252
253
254
255
# File 'lib/jss/api_object/patch_source.rb', line 251

def self.valid_patch_source_id(ident, refresh = false, api: JSS.api)
  id = JSS::PatchInternalSource.valid_id ident, refresh, api: api
  id ||= JSS::PatchExternalSource.valid_id ident, refresh, api: api
  id
end

.valid_patch_source_type(ident, refresh = false, api: JSS.api) ⇒ 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

  • api (JSS::APIConnection) (defaults to: JSS.api)

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

Returns:

  • (Symbol, nil)

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



269
270
271
272
273
# File 'lib/jss/api_object/patch_source.rb', line 269

def self.valid_patch_source_type(ident, refresh = false, api: JSS.api)
  return :internel if JSS::PatchInternalSource.valid_id ident, refresh, api: api
  return :external if JSS::PatchExternalSource.valid_id ident, refresh, api: api
  nil
end

Instance Method Details

#available_name_idsObject

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

See Also:



338
339
340
# File 'lib/jss/api_object/patch_source.rb', line 338

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

#available_titlesObject

Get a list of patch titles available from this Patch Source

See Also:



331
332
333
# File 'lib/jss/api_object/patch_source.rb', line 331

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

#deleteObject

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



344
345
346
347
348
349
350
351
# File 'lib/jss/api_object/patch_source.rb', line 344

def delete
  case self.class.name
  when 'JSS::PatchExternalSource'
    super
  when 'JSS::PatchInternalSource'
    raise JSS::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:

#updateBoolean Originally defined in module Updatable

Save changes to the JSS

Returns:

  • (Boolean)

    success

Raises: