lib/jss/api_object/group.rb in ruby-jss-1.1.3 vs lib/jss/api_object/group.rb in ruby-jss-1.2.0b1

- old
+ new

@@ -82,11 +82,11 @@ # Returns an Array of all the static # groups. # def self.all_static(refresh = false, api: JSS.api) - all(refresh, api: api).select { |g| (g[:is_smart]) } + all(refresh, api: api).reject { |g| g[:is_smart] } end # Immediatly add and/or remove members in a static group without # instantiating it first. Uses the <x_additions> and <x_deletions> # XML elements available when sending a PUT request to the API. @@ -103,11 +103,11 @@ # connection if not specified # # @return [void] # def self.change_membership(group, add_members: [], remove_members: [], api: JSS.api) - raise JSS::NoSuchItemError, "No #{self} matching '#{ident}'" unless (group_id = valid_id group, api: api) + raise JSS::NoSuchItemError, "No #{self} matching '#{group}'" unless (group_id = valid_id group, api: api) raise JSS::UnsupportedError, "Not a static group, can't change membership directly" if map_all_ids_to(:is_smart, api: api)[group_id] add_members = [add_members].flatten remove_members = [remove_members].flatten return if add_members.empty? && remove_members.empty? @@ -208,11 +208,11 @@ # Constructor ##################################### # When creating a new group in the JSS, you must call .make with a :type key - # and a value of :smart or :static, as well as a :name and the :id => :new + # and a value of :smart or :static, as well as a :name # # @see JSS::APIObject # def initialize(args = {}) if args[:id] == :new @@ -234,27 +234,65 @@ # Public Instance Methods ##################################### # @see Creatable#create # - def create(calculate_members: true) + # @param calculate_members [Boolan] should the local membership list be + # re-read from the API after the group is created? + # + # @param retries [Integer] If calculate_members is true, refetching the + # group to re-read the membership can happen too fast, the JSS won't know + # it exists yet and will throw a RestClient::NotFound error. If that + # happens, try again this many times with a 1 second pause between attempts. + # + def create(calculate_members: true, retries: 10) if @is_smart raise JSS::MissingDataError, 'No criteria specified for smart group' unless @criteria end super() - refresh_members if calculate_members + + if calculate_members + tries = 0 + while tries < retries + begin + refresh_members + break + rescue RestClient::NotFound + sleep 1 + tries += 1 + end # begin + end # while + end # if calc members + @id end # @see Updatable#update # - def update - super - refresh_members + def update(refresh: true) + super() + refresh_members if refresh @id end + # Wrapper/alias for both create and update + def save(**params) + params[:calculate_members] = true if params[:calculate_members].nil? + params[:retries] = 10 if params[:retries].nil? + params[:refresh] = true if params[:refresh].nil? + + if @in_jss + raise JSS::UnsupportedError, 'Updating this object in the JSS is currently not supported by ruby-jss' unless updatable? + + + update refresh: params[:refresh] + else + raise JSS::UnsupportedError, 'Creating this object in the JSS is currently not supported by ruby-jss' unless creatable? + create calculate_members: params[:calculate_members], retries: params[:retries] + end + end + # @see APIObject#delete # def delete super @is_smart = nil @@ -267,9 +305,10 @@ # # @param new_criteria[JSS::Criteria] the new criteria for the smart group # def criteria=(new_criteria) raise InvalidDataError, 'Only smart groups have criteria.' unless @is_smart + super end # How many members of the group? #