Module: Jamf::Sitable

Overview

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:

  • define the constant SITE_SUBSET as either :top, :general, or whatever sub-hash of API data conaints the site info. (most are :general, but some, like advanced searches, are at the top level)

  • call #add_site_to_xml(xmldoc) from their #rest_xml method if they are Updatable or Creatable

Constant Summary collapse

SITABLE =

Module Constants

true
NO_SITE_NAME =

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

'None'.freeze
NO_SITE_ID =
-1
NON_SITES =

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

[
  nil,
  '',
  0,
  NO_SITE_NAME,
  NO_SITE_ID
].freeze

Instance Method Summary collapse

Instance Method Details

#site=(new_site) ⇒ void

This method returns an undefined value.

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

Parameters:

  • new_site (Integer, String)

    The new site

Raises:



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/jamf/api/classic/api_objects/sitable.rb', line 124

def site=(new_site)
  return nil unless updatable? || creatable?

  # unset the site? Use nil or an empty string
  if NON_SITES.include? new_site
    unset_site
    return
  end

  new_id = Jamf::Site.valid_id new_site, cnx: @cnx
  new_name = Jamf::Site.map_all_ids_to(:name, cnx: @cnx)[new_id]
  # no change, go home.
  return nil if new_name == @site_name

  raise Jamf::NoSuchItemError, "Site '#{new_site}' is not known to the JSS" unless new_id

  @site_name = new_name
  @site_id = new_id
  @need_to_update = true
end

#site_assigned?Boolean

Does this object have a site assigned?

Returns:

  • (Boolean)

    Does this object have a site assigned?



108
109
110
111
112
113
114
# File 'lib/jamf/api/classic/api_objects/sitable.rb', line 108

def site_assigned?
  if @site_name == NO_SITE_NAME
    false
  else
    !@site_name.nil?
  end
end

#site_idInteger

The id of the site for this object.

Returns:

  • (Integer)

    The id of the site for this object.



91
92
93
# File 'lib/jamf/api/classic/api_objects/sitable.rb', line 91

def site_id
  @site_id || NO_SITE_ID
end

#site_nameString Also known as: site

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

Returns:

  • (String)

    The name of the site for this object.



82
83
84
# File 'lib/jamf/api/classic/api_objects/sitable.rb', line 82

def site_name
  @site_name || NO_SITE_NAME
end

#site_objectJamf::Site

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

Returns:

  • (Jamf::Site)

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



99
100
101
102
# File 'lib/jamf/api/classic/api_objects/sitable.rb', line 99

def site_object
  return nil unless site_assigned?
  Jamf::Site.fetch id: @site_id
end

#unset_sitevoid

This method returns an undefined value.

Set the site to nothing



149
150
151
152
153
154
155
# File 'lib/jamf/api/classic/api_objects/sitable.rb', line 149

def unset_site
  # no change, go home
  return nil if @site_name.nil?
  @site_name = nil
  @site_id = nil
  @need_to_update = true
end