module Jamf::Sitable

A mix-in module that centralizes the code for handling objects which can be assigned a ‘site’ in the JSS.

Objects in the JSS present site data in the top-level :general Hash in the :site key which is a Hash with a :name and :id key.

Classes mixing in this module MUST:

Constants

NON_SITES

Setting the site to any of these values will unset the site

NO_SITE_ID
NO_SITE_NAME

When no site has been assigned, this is the ‘name’ and id used

SITABLE

Module Constants

Public Instance Methods

site()
Alias for: site_name
site=(new_site) click to toggle source

Change the site of this object. Any of the NON_SITES values will unset the site

@param new_site[Integer, String] The new site

@return [void]

    # File lib/jamf/api/classic/api_objects/sitable.rb
124 def site=(new_site)
125   return nil unless updatable? || creatable?
126 
127   # unset the site? Use nil or an empty string
128   if NON_SITES.include? new_site
129     unset_site
130     return
131   end
132 
133   new_id = Jamf::Site.valid_id new_site, cnx: @cnx
134   new_name = Jamf::Site.map_all_ids_to(:name, cnx: @cnx)[new_id]
135   # no change, go home.
136   return nil if new_name == @site_name
137 
138   raise Jamf::NoSuchItemError, "Site '#{new_site}' is not known to the JSS" unless new_id
139 
140   @site_name = new_name
141   @site_id = new_id
142   @need_to_update = true
143 end
site_assigned?() click to toggle source

Does this object have a site assigned?

@return [Boolean] Does this object have a site assigned?

    # File lib/jamf/api/classic/api_objects/sitable.rb
108 def site_assigned?
109   if @site_name == NO_SITE_NAME
110     false
111   else
112     !@site_name.nil?
113   end
114 end
site_id() click to toggle source

The id of the site for this object.

@return [Integer] The id of the site for this object.

   # File lib/jamf/api/classic/api_objects/sitable.rb
91 def site_id
92   @site_id || NO_SITE_ID
93 end
site_name() click to toggle source

The name of the site for this object. For backward compatibility, this is aliased to just ‘site’

@return [String] The name of the site for this object.

   # File lib/jamf/api/classic/api_objects/sitable.rb
82 def site_name
83   @site_name || NO_SITE_NAME
84 end
Also aliased as: site
site_object() click to toggle source

The Jamf::Site instance for this object’s site

@return [Jamf::Site] The Jamf::Site instance for this object’s site

    # File lib/jamf/api/classic/api_objects/sitable.rb
 99 def site_object
100   return nil unless site_assigned?
101   Jamf::Site.fetch id: @site_id
102 end
unset_site() click to toggle source

Set the site to nothing

@return [void]

    # File lib/jamf/api/classic/api_objects/sitable.rb
149 def unset_site
150   # no change, go home
151   return nil if @site_name.nil?
152   @site_name = nil
153   @site_id = nil
154   @need_to_update = true
155 end

Private Instance Methods

add_site_to_xml(xmldoc) click to toggle source

Add the site to the XML for POSTing or PUTting to the API.

@param xmldoc The in-construction XML document

@return [void]

    # File lib/jamf/api/classic/api_objects/sitable.rb
184 def add_site_to_xml(xmldoc)
185   root = xmldoc.root
186   site_elem =
187     if self.class::SITE_SUBSET == :top
188       root.add_element 'site'
189     else
190       parent_elem = root.elements[self.class::SITE_SUBSET.to_s]
191       parent_elem ||= root.add_element(self.class::SITE_SUBSET.to_s)
192       parent_elem.add_element 'site'
193     end
194   site_elem.add_element('name').text = site_name.to_s
195 end
parse_site() click to toggle source

Parse the site data from any incoming API data

@return [void]

    # File lib/jamf/api/classic/api_objects/sitable.rb
165 def parse_site
166   site_data =
167     if self.class::SITE_SUBSET == :top
168       @init_data[:site]
169     elsif @init_data[self.class::SITE_SUBSET]
170       @init_data[self.class::SITE_SUBSET][:site]
171     end
172   site_data ||= { name: NO_SITE_NAME, id: NO_SITE_ID }
173 
174   @site_name = site_data[:name]
175   @site_id = site_data[:id]
176 end