lib/jss/api_object/creatable.rb in ruby-jss-0.9.2 vs lib/jss/api_object/creatable.rb in ruby-jss-0.10.0a1

- old
+ new

@@ -67,18 +67,22 @@ ##################################### ### Mixed-in Instance Methods ##################################### - ### Create a new object in the JSS. - ### - ### @return [Integer] the jss ID of the newly created object - ### - def create + # Create a new object in the JSS. + # + # @param api[JSS::APIConnection] the API in which to create the object + # Defaults to the API used to instantiate this object + # + # @return [Integer] the jss ID of the newly created object + # + def create(api: nil) + api ||= @api raise JSS::UnsupportedError, "Creating or editing #{self.class::RSRC_LIST_KEY} isn't yet supported. Please use other Casper workflows." unless respond_to? :create raise AlreadyExistsError, "This #{self.class::RSRC_OBJECT_KEY} already exists. Use #update to make changes." if @in_jss - JSS.api_connection.post_rsrc(@rest_rsrc, rest_xml) =~ %r{><id>(\d+)</id><} + api.post_rsrc(@rest_rsrc, rest_xml) =~ %r{><id>(\d+)</id><} @id = Regexp.last_match(1).to_i @in_jss = true @need_to_update = false @rest_rsrc = "#{self.class::RSRC_BASE}/id/#{@id}" @id @@ -86,28 +90,35 @@ ### 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 api[JSS::APIConnection] the API in which to create the object + ### Defaults to the API used to instantiate this object + ### ### @return [APIObject] An uncreated clone of this APIObject with the given name ### - def clone(new_name) + def clone(new_name, api: nil) + api ||= @api raise JSS::UnsupportedError, 'This class is not creatable in via ruby-jss' unless respond_to? :create raise JSS::AlreadyExistsError, "A #{self.class::RSRC_OBJECT_KEY} already exists with that name" if \ - self.class.all_names.include? new_name + 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}" + 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