Module: Jamf::Extendable

Included in:
Computer, MobileDevice, User
Defined in:
lib/jamf/api/classic/api_objects/extendable.rb

Overview

A mix-in module for handling extension attribute data for objects in the JSS.

This module provides standardized ways to deal with Extension Attribute data in objects that gather that data (Computers, MobileDevices, and Users). For working with the Extension Attributes themselves, see ExtensionAttribute and its subclasses.

API objects that have Extension Attribute data return it in an Array of Hashes, one for each defined ExtensionAttribute for the class; i.e. a Computer‘s Array has one Hash for each ComputerExtensionAttribute defined in the JSS.

The Hash keys are:

  • :id => the ExtAttr id

  • :name => the ExtAttr name

  • :type => the data type of the ExtAttr value

  • :value => the value for the ExtAttr for this object as of the last report.

Classes including this module must define the constant EXT_ATTRIB_CLASS specifying which ExtensionAttribute subclass defines the relevant extension attributes. For Example, Computer sets this:

EXT_ATTRIB_CLASS = Jamf::ComputerExtensionAttribute

Parsing also populates @ext_attrs which is a Hash of name => value for each EA.

When updating or creating, those classes must add the REXML output of #ext_attr_xml to their rest_xml output.

Constant Summary collapse

EXTENDABLE =

Constants

true
INVALID_DATE =
'-- INVALIDLY FORMATTED DATE --'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#extension_attributesArray<Hash> (readonly)

Returns The extension attribute values for the object.

Returns:

  • (Array<Hash>)

    The extension attribute values for the object



55
56
57
# File 'lib/jamf/api/classic/api_objects/extendable.rb', line 55

def extension_attributes
  @extension_attributes
end

Instance Method Details

#ea_namesArray<String>

Returns the names of all known EAs.

Returns:



78
79
80
# File 'lib/jamf/api/classic/api_objects/extendable.rb', line 78

def ea_names
  ea_types.keys
end

#ea_typesHash{String => String}

Returns EA names => data type (one of ‘String’, ‘Number’, or ‘Date’).

Returns:

  • (Hash{String => String})

    EA names => data type (one of ‘String’, ‘Number’, or ‘Date’)



84
85
86
87
88
89
90
# File 'lib/jamf/api/classic/api_objects/extendable.rb', line 84

def ea_types
  return @ea_types if @ea_types

  @ea_types = {}
  extension_attributes.each { |ea| @ea_types[ea[:name]] = ea[:type] }
  @ea_types
end

#ext_attr_xmlREXML::Element

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

TODO: make this (and all XML amending) method take the in-progress XML doc and add (or not) the EA xml to it. See how Sitable#add_site_to_xml works, as called from Computer.rest_xml

Returns:

  • (REXML::Element)

    An <extension_attribute> element to be included in the rest_xml of objects that mix-in this module.



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/jamf/api/classic/api_objects/extendable.rb', line 200

def ext_attr_xml
  @changed_eas ||= []
  eaxml = REXML::Element.new('extension_attributes')
  @extension_attributes.each do |ea|
    next unless @changed_eas.include? ea[:name]

    ea_el = eaxml.add_element('extension_attribute')
    ea_el.add_element('name').text = ea[:name]

    if ea[:type] == 'Date'
      begin
        ea_el.add_element('value').text = ea[:value].to_jss_date
      rescue
        ea_el.add_element('value').text = ea[:value].to_s
      end
    else
      ea_el.add_element('value').text = ea[:value].to_s
    end # if
  end # each do ea

  eaxml
end

#ext_attrsObject

An easier-to-use hash of EA name to EA value. This isn’t created until its needed, to speed up instantiation.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/jamf/api/classic/api_objects/extendable.rb', line 95

def ext_attrs
  return @ext_attrs if @ext_attrs

  @ext_attrs = {}
  @extension_attributes.each do |ea|
    @ext_attrs[ea[:name]] =
      case ea[:type]

      when 'Date'
        begin # if there's random non-date data, the parse will fail
          Jamf.parse_time ea[:value]
        rescue
          INVALID_DATE
        end

      when *Jamf::ExtensionAttribute::NUMERIC_TYPES
        ea[:value].to_i unless ea[:value].to_s.empty?

      else # String
        ea[:value]
      end # case
  end # each do ea

  @ext_attrs
end

#parse_ext_attrsvoid

This method returns an undefined value.

Populate @extension_attributes (the Array of Hashes that comes from the API) and @ext_attr_names, which is a Hash mapping the EA names to their values. This is called during initialization for all objects that mix in this module



67
68
69
70
71
72
73
74
# File 'lib/jamf/api/classic/api_objects/extendable.rb', line 67

def parse_ext_attrs
  @extension_attributes = @init_data[:extension_attributes]
  @extension_attributes ||= []

  # remember changes as they happen so
  # we only send changes back to the server.
  @changed_eas = []
end

#set_ext_attr(ea_name, value, validate_popup_choice: true, refresh: false) ⇒ void

This method returns an undefined value.

Set the value of an extension attribute

The new value is validated based on the data type of the Ext. Attrib:

  • If the ext. attrib. is defined with a data type of Integer/Number, the value must be an Integer.

  • If defined with a data type of Date, the value will be parsed as a timestamp, and parsing may raise an exception. Dates can’t be blank.

  • If defined wth data type of String, ‘to_s` will be called on the value.

By default, the full EA definition object is fetched to see if the EA’s input type is ‘popup menu’, and if so, the new value must be one of the defined popup choices, or blank.

The EA definitions used for popup validation are cached, so we don’t have to reach out to the server every time. If you expect the definition to have changed since it was cached, provide a truthy value to the refresh: parameter

To bypass popup validation complepletely, provide a falsey value to the validate_popup_choice: parameter. WARNING: beware that your value is the correct type and format, or you might get errors when saving back to the API.

Note that while the Jamf Pro Web interface does not allow editing the values of Extension Attributes populated by Scripts or LDAP, the API does allow it. Bear in mind however that those values will be reset again at the next recon.

Parameters:

  • name (String)

    the name of the extension attribute to set

  • value (String, Time, Integer)

    the new value for the extension attribute for this user

  • validate_popup_choice (Boolean) (defaults to: true)

    validate the new value against the E.A. definition. Defaults to true.

  • refresh (Boolean) (defaults to: false)

    Re-read the ext. attrib definition from the API, for popup validation.

Raises:

  • (ArgumentError)


164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/jamf/api/classic/api_objects/extendable.rb', line 164

def set_ext_attr(ea_name, value, validate_popup_choice: true, refresh: false)
  raise ArgumentError, "Unknown Extension Attribute Name: '#{ea_name}'" unless ea_types.key? ea_name

  value = validate_ea_value(ea_name, value, validate_popup_choice, refresh)

  # update this ea hash in the @extension_attributes array
  ea_hash = @extension_attributes.find { |ea| ea[:name] == ea_name }

  raise Jamf::NoSuchItemError, "#{self.class} '#{name}'(id #{id}) does not know about ExtAttr '#{ea_name}'. Please re-fetch and try again." unless ea_hash

  ea_hash[:value] = value

  # update the shortcut hash too
  @ext_attrs[ea_name] = value if @ext_attrs
  @changed_eas << ea_name
  @need_to_update = true
end

#unsaved_eas?Boolean

are there any changes in the EAs needing to be saved?

Returns:

  • (Boolean)


186
187
188
# File 'lib/jamf/api/classic/api_objects/extendable.rb', line 186

def unsaved_eas?
  @need_to_update && @changed_eas && !@changed_eas.empty?
end

#validate_ea_value(ea_name, value, validate_popup_choice, refresh) ⇒ Object

is the value being passed to set_ext_attr valid? Converts values as needed (e.g. strings to integers or Times)

If the EA is defined to hold a string, any value is accepted and converted with #to_s

Note: All EAs can be blank

Parameters:

  • name (String)

    the name of the extension attribute to set

  • value (String, Time, Integer)

    the new value for the extension attribute for this user

  • validate_popup_choice (Boolean)

    validate the new value against the E.A. definition. Defaults to true.

  • refresh (Boolean)

    Re-read the ext. attrib definition from the API, for popup validation.

Returns:

  • (Object)

    the possibly modified valid value



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/jamf/api/classic/api_objects/extendable.rb', line 244

def validate_ea_value(ea_name, value, validate_popup_choice, refresh)
  return Jamf::BLANK if value.to_s == Jamf::BLANK

  value =
    case ea_types[ea_name]
    when Jamf::ExtensionAttribute::DATA_TYPE_DATE
      Jamf.parse_time(value).to_s
    when *Jamf::ExtensionAttribute::NUMERIC_TYPES
      validate_integer_ea_value ea_name, value
    else
      value.to_s
    end # case

  validate_popup_value(ea_name, value, refresh) if validate_popup_choice

  value
end

#validate_integer_ea_value(ea_name, value) ⇒ Object

raise error if the value isn’t an integer



263
264
265
266
267
268
269
270
271
# File 'lib/jamf/api/classic/api_objects/extendable.rb', line 263

def validate_integer_ea_value(ea_name, value)
  if value.is_a? Integer
    value
  elsif value.to_s.jss_integer?
    value.to_s.to_i
  else
    raise Jamf::InvalidDataError, "The value for #{ea_name} must be an integer"
  end # if
end

#validate_popup_value(ea_name, value, refresh) ⇒ Object

Raise an error if the named EA has a popup menu, but the provided value isn’t one of the menu items



276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/jamf/api/classic/api_objects/extendable.rb', line 276

def validate_popup_value(ea_name, value, refresh)
  # get the ea def. instance from the api cache, or the api
  cnx.c_ext_attr_definition_cache[self.class] ||= {}
  cnx.c_ext_attr_definition_cache[self.class][ea_name] = nil if refresh
  cnx.c_ext_attr_definition_cache[self.class][ea_name] ||= self.class::EXT_ATTRIB_CLASS.fetch name: ea_name, cnx: cnx

  ea_def = cnx.c_ext_attr_definition_cache[self.class][ea_name]
  return unless ea_def.from_popup_menu?

  return if ea_def.popup_choices.include? value.to_s

  raise Jamf::UnsupportedError, "The value for #{ea_name} must be one of: '#{ea_def.popup_choices.join("' '")}'"
end