class Jamf::Peripheral

A peripheral in the JSS

@see Jamf::APIObject

Constants

OBJECT_HISTORY_OBJECT_TYPE

the object type for this object in the object history table. See {APIObject#add_object_history_entry}

RSRC_BASE

The base for REST resources of this class

RSRC_LIST_KEY

the hash key used for the JSON list output of all objects in the JSS

RSRC_OBJECT_KEY

The hash key used for the JSON object output. It’s also used in various error messages

SITE_SUBSET

Where is the Site data in the API JSON?

Attributes

bar_code_1[R]

@return [String] the “bar code 1” value

bar_code_2[R]

@return [String] the “bar code 2” value

barcode1[R]

@return [String] the “bar code 1” value

barcode2[R]

@return [String] the “bar code 2” value

barcode_1[R]

@return [String] the “bar code 1” value

barcode_2[R]

@return [String] the “bar code 2” value

computer_id[R]

@return [Integer] the id number of the computer to which this periph is connected

type[R]

@return [String] the type of peripheral

Public Class Methods

new(**args) click to toggle source

@see APIObject

Calls superclass method Jamf::APIObject::new
    # File lib/jamf/api/classic/api_objects/peripheral.rb
118 def initialize(**args)
119 
120   ### periphs don't really have names, and the JSS module list method for
121   ### periphs gives the computer_id as the name, so give it a temp
122   ### name of "-1", which shouldn't ever exist in the JSS
123   args[:name] ||= "-1"
124 
125   super
126 
127   if args[:id] == :new
128     raise Jamf::InvalidDataError, "New Peripherals must have a :type, which must be one of those defined in the JSS." unless args[:type]
129     @type = args[:type]
130     raise Jamf::InvalidDataError, "No peripheral type '#{@type}' in the JSS" unless Jamf::PeripheralType.all_names(:refresh, cnx: @cnx).include? @type
131     @fields = {}
132     @rest_rsrc = 'peripherals/id/-1'
133     @site = "None"
134     return
135   end
136 
137   @type =  @init_data[:general][:type]
138   @site = Jamf::APIObject.get_name(@init_data[:general][:site])
139   @bar_code_1 = @init_data[:general][:bar_code_1]
140   @bar_code_2 = @init_data[:general][:bar_code_2]
141   @computer_id = @init_data[:general][:computer_id]
142 
143   ### fill in the fields
144   @fields = {}
145   @init_data[:general][:fields].each{|f| @fields[f[:name]] = f[:value] }
146 
147   ### get the field defs for this PeriphType, omitting the leading nil
148   @field_defs ||= Jamf::PeripheralType.fetch(:name => @type).fields.compact
149 
150 
151 end

Public Instance Methods

associate(computer) click to toggle source

Associate this peripheral with a computer.

@param computer the name or id of a computer in the JSS

@return [void]

    # File lib/jamf/api/classic/api_objects/peripheral.rb
230 def associate(computer)
231   if computer =~ /^d+$/
232     raise Jamf::NoSuchItemError, "No computer in the JSS with id #{computer}" unless Jamf::Computer.all_ids(cnx: @cnx).include? computer
233     @computer_id = computer
234   else
235     raise Jamf::NoSuchItemError, "No computer in the JSS with name #{computer}" unless Jamf::Computer.all_names(cnx: @cnx).include? computer
236     @computer_id = Jamf::Computer.map_all_ids_to(:name, cnx: @cnx).invert[computer]
237   end
238   @need_to_update = true
239 end
Also aliased as: assign_to
bar_code_1=(new_value) click to toggle source

Set the value of barcode 1

@param new_value the new barcode value

@return [void]

    # File lib/jamf/api/classic/api_objects/peripheral.rb
204 def bar_code_1= (new_value)
205     @bar_code_1 = new_value
206     @need_to_update = true
207 end
Also aliased as: barcode_1=, barcode1=
bar_code_2=(new_value) click to toggle source

Set the value of barcode 2

@param new_value the new barcode value

@return [void]

    # File lib/jamf/api/classic/api_objects/peripheral.rb
217 def bar_code_2= (new_value)
218     @bar_code_2 = new_value
219     @need_to_update = true
220 end
Also aliased as: barcode_2=, barcode2=
create() click to toggle source

reset the restrsrc after creation

@see Jamf::Creatable#create

Calls superclass method Jamf::APIObject::create
    # File lib/jamf/api/classic/api_objects/peripheral.rb
159 def create
160   super
161   @rest_rsrc = "peripherals/id/#{@id}"
162   @id
163 end
disassociate() click to toggle source

Disassociate this peripheral from any computer.

This seems to have no effect in the JSS, the computer/user/location data always shows the most recent.

@return [void]

    # File lib/jamf/api/classic/api_objects/peripheral.rb
250 def disassociate
251   @computer_id = nil
252   @need_to_update = true
253 end
Also aliased as: unassign
fields() click to toggle source

@return [Hash] the field values of the peripheral

Each key is the fields name, as a String
and the value is the fields value, also as a String
    # File lib/jamf/api/classic/api_objects/peripheral.rb
178 def fields
179   @fields
180 end
name=(newname) click to toggle source

periphs don’t have names

    # File lib/jamf/api/classic/api_objects/peripheral.rb
168 def name= (newname)
169   raise Jamf::UnsupportedError, "Peripherals don't have names."
170 end
set_field(field, value) click to toggle source

Set the value of a field. It will be checked to ensure validity.

@param field the field to set

@param value the new value for the field

@return [void]

    # File lib/jamf/api/classic/api_objects/peripheral.rb
191 def set_field(field, value)
192   check_field(field, value)
193   @fields[field] = value
194   @need_to_update = true
195 end

Private Instance Methods

assign_to(computer)
Alias for: associate
barcode1=(new_value)
Alias for: bar_code_1=
barcode2=(new_value)
Alias for: bar_code_2=
barcode_1=(new_value)
Alias for: bar_code_1=
barcode_2=(new_value)
Alias for: bar_code_2=
check_field(field, value) click to toggle source

check a field, the field name must match those defined in the appropriate peripheral type. If a field is a menu field, the value must also be one of those defined in the periph type. Raise an exception if wrong.

    # File lib/jamf/api/classic/api_objects/peripheral.rb
267 def check_field(field, value)
268   ### get the field defs for this PeriphType, omitting the leading nil
269   @field_defs ||= Jamf::PeripheralType.fetch(:name => @type, cnx: @cnx).fields.compact
270 
271   ### we must have the right number of fields, and they must have the same names
272   ### as the definition
273   required_fields = @field_defs.map{|f| f[:name]}
274   raise Jamf::InvalidDataError, "Peripherals of type '#{@type}' doesn't have a field '#{field}', they only have: #{required_fields.join(', ')}" unless required_fields.include? field
275 
276   ### any menu fields can only have values as defined by the type.
277   menu_flds = @field_defs.select{|f| f[:type] == "menu" }
278   menu_flds.each do |mf|
279     next unless mf[:name] == field
280     raise Jamf::InvalidDataError, "The value for field '#{field}' must be one of: #{mf[:choices].join(', ')}" unless mf[:choices].include? value
281   end #if menu_flds.include? field
282 
283 end
rest_xml() click to toggle source

Return the REST XML for this pkg, with the current values, for saving or updating

    # File lib/jamf/api/classic/api_objects/peripheral.rb
289 def rest_xml
290   doc = REXML::Document.new Jamf::Connection::XML_HEADER
291   periph = doc.add_element RSRC_OBJECT_KEY.to_s
292 
293   general = periph.add_element('general')
294   general.add_element('type').text = @type
295   general.add_element('site').add_element('name').text = @site
296   general.add_element('bar_code_1').text = @bar_code_1
297   general.add_element('bar_code_2').text = @bar_code_2
298   general.add_element('computer_id').text = @computer_id
299 
300   fields = general.add_element('fields')
301   @fields.each do |n,v|
302     fld =  fields.add_element('field')
303     fld.add_element('name').text = n
304     fld.add_element('value').text = v
305   end
306 
307   if has_location?
308     periph << location_xml
309   end
310   if has_purchasing?
311     periph << purchasing_xml
312   end
313   add_site_to_xml doc
314 
315   return doc.to_s
316 end
unassign()
Alias for: disassociate