module Jamf::Creatable

A mix-in module that allows objects to be created in the JSS via the API.

When a Jamf::APIObject subclass includes this module, that subclass can be instantiated with :id => :new, and :name => “some_new_name”.

Classes mixing this module must provide a rest_xml instance method that returns the XML String to be submitted to the API for object creation.

The instance can be used to set desired values for the new object, and once everything’s good, use create to create it in the JSS.

If a Creatable object requires more data than just a :name for creation, the subclass may want to redefine initialize to require those data before calling super, or may want to redefine create or rest_xml to check the data for consistency, and then call super

It is also wise to have the individual setter methods do data validation

@see APIObject#save

Constants

CREATABLE

Constants

Public Instance Methods

clone(new_name, api: nil, cnx: nil) click to toggle source

make a clone of this API object, with a new name. The class must be creatable

@param name [String] the name for the new object

@param cnx [Jamf::Connection] the API in which to create the object

Defaults to the API used to instantiate this object

@return [APIObject] An unsaved clone of this APIObject with the given name

    # File lib/jamf/api/classic/api_objects/creatable.rb
 97 def clone(new_name, api: nil, cnx: nil)
 98   cnx = api if api
 99   cnx ||= @cnx
100 
101   raise Jamf::UnsupportedError, 'This class is not creatable in via ruby-jss' unless creatable?
102   raise Jamf::AlreadyExistsError, "A #{self.class::RSRC_OBJECT_KEY} already exists with that name" if \
103     self.class.all_names(:refresh, cnx: cnx).include? new_name
104 
105   orig_in_jss = @in_jss
106   @in_jss = false
107   orig_id = @id
108   @id = nil
109   orig_rsrc = @rest_rsrc
110   @rest_rsrc = "#{self.class::RSRC_BASE}/name/#{CGI.escape new_name.to_s}"
111   orig_cnx = @cnx
112   @cnx = cnx
113 
114   new_obj = dup
115 
116   @in_jss = orig_in_jss
117   @id = orig_id
118   @rest_rsrc = orig_rsrc
119   @cnx = orig_cnx
120   new_obj.name = new_name
121 
122   new_obj
123 end

Private Instance Methods

create_in_jamf() click to toggle source

Create a new object in the JSS

@return [Integer] the id of the new object

   # File lib/jamf/api/classic/api_objects/creatable.rb
71 def create_in_jamf
72   raise Jamf::UnsupportedError, "Creating or editing #{self.class::RSRC_LIST_KEY} isn't yet supported. Please use other Casper workflows." unless creatable?
73 
74   @cnx.c_post(rest_rsrc, rest_xml) =~ %r{><id>(\d+)</id><}
75   @id = Regexp.last_match(1).to_i
76   @in_jss = true
77   @need_to_update = false
78   @rest_rsrc = "#{self.class::RSRC_BASE}/id/#{@id}"
79 
80   # clear any caches for this class
81   # so they'll re-cache as needed
82   @cnx.flushcache self.class::RSRC_LIST_KEY
83 
84   @id
85 end