Method: JSS::Creatable#clone

Defined in:
lib/jss/api_object/creatable.rb

#clone(new_name, api: nil) ⇒ APIObject

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

Parameters:

  • name (String)

    the name for the new object

  • api (JSS::APIConnection) (defaults to: nil)

    the API in which to create the object Defaults to the API used to instantiate this object

Returns:

  • (APIObject)

    An uncreated clone of this APIObject with the given name

Raises:


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/jss/api_object/creatable.rb', line 101

def clone(new_name, api: nil)
  api ||= @api
  raise JSS::UnsupportedError, 'This class is not creatable in via ruby-jss' unless creatable?
  raise JSS::AlreadyExistsError, "A #{self.class::RSRC_OBJECT_KEY} already exists with that name" if \
    self.class.all_names(:refresh, api: api).include? new_name

  orig_in_jss = @in_jss
  @in_jss = false
  orig_id = @id
  @id = nil
  orig_rsrc = @rest_rsrc
  @rest_rsrc = "#{self.class::RSRC_BASE}/name/#{CGI.escape new_name.to_s}"
  orig_api = @api
  @api = api

  new_obj = dup

  @in_jss = orig_in_jss
  @id = orig_id
  @rest_rsrc = orig_rsrc
  @api = orig_api
  new_obj.name = new_name

  new_obj
end