# class Chef::Provider::Group

### Attributes

change_desc[RW]
group_exists[RW]

### Public Class Methods

new(new_resource, run_context) click to toggle source
Calls superclass method Chef::Provider.new

    # File lib/chef/provider/group.rb, line 34
    def initialize(new_resource, run_context)
      super
      @group_exists = true
    end

### Public Instance Methods

action_create() click to toggle source

    # File lib/chef/provider/group.rb, line 123
    def action_create
      case @group_exists
      when false
        converge_by("create #{@new_resource}") do
          create_group
          Chef::Log.info("#{@new_resource} created")
        end
      else
        if compare_group
          converge_by(["alter group #{@new_resource}"] + change_desc) do
            manage_group
            Chef::Log.info("#{@new_resource} altered")
          end
        end
      end
    end

action_manage() click to toggle source

    # File lib/chef/provider/group.rb, line 149
    def action_manage
      if @group_exists && compare_group
        converge_by(["manage group #{@new_resource}"] + change_desc) do
          manage_group
          Chef::Log.info("#{@new_resource} managed")
        end
      end
    end

action_modify() click to toggle source

    # File lib/chef/provider/group.rb, line 158
    def action_modify
      if compare_group
        converge_by(["modify group #{@new_resource}"] + change_desc) do
          manage_group
          Chef::Log.info("#{@new_resource} modified")
        end
      end
    end

action_remove() click to toggle source

    # File lib/chef/provider/group.rb, line 140
    def action_remove
      if @group_exists
        converge_by("remove group #{@new_resource}") do
          remove_group
          Chef::Log.info("#{@new_resource} removed")
        end
      end
    end

compare_group() click to toggle source

Check to see if a group needs any changes. Populate @change_desc with a description of why a change must occur

#### Returns¶ ↑

<true>

  If a change is required

<false>

  If a change is not required

    # File lib/chef/provider/group.rb, line 85
    def compare_group
      @change_desc = [ ]
      if @new_resource.gid.to_s  != @current_resource.gid.to_s
        @change_desc << "change gid #{@current_resource.gid} to #{@new_resource.gid}"
      end

      if(@new_resource.append)
        missing_members = []
        @new_resource.members.each do |member|
          next if has_current_group_member?(member)
          missing_members << member
        end
        if missing_members.length > 0
          @change_desc << "add missing member(s): #{missing_members.join(", ")}"
        end

        members_to_be_removed = []
        @new_resource.excluded_members.each do |member|
          if has_current_group_member?(member)
            members_to_be_removed << member
          end
        end
        if members_to_be_removed.length > 0
          @change_desc << "remove existing member(s): #{members_to_be_removed.join(", ")}"
        end
      else
        if @new_resource.members != @current_resource.members
          @change_desc << "replace group members with new list of members"
        end
      end

      !@change_desc.empty?
    end

create_group() click to toggle source

    # File lib/chef/provider/group.rb, line 167
    def create_group
      raise NotImplementedError, "subclasses of Chef::Provider::Group should define #create_group"
    end

define_resource_requirements() click to toggle source

    # File lib/chef/provider/group.rb, line 60
    def define_resource_requirements
      requirements.assert(:modify) do |a|
        a.assertion { @group_exists }
        a.failure_message(Chef::Exceptions::Group, "Cannot modify #{@new_resource} - group does not exist!")
        a.whyrun("Group #{@new_resource} does not exist. Unless it would have been created earlier in this run, this attempt to modify it would fail.")
      end

      requirements.assert(:all_actions) do |a|
        # Make sure that the resource doesn't contain any common
        # user names in the members and exclude_members properties.
        if !@new_resource.members.nil? && !@new_resource.excluded_members.nil?
          common_members = @new_resource.members & @new_resource.excluded_members
          a.assertion { common_members.empty? }
          a.failure_message(Chef::Exceptions::ConflictingMembersInGroup, "Attempting to both add and remove users from a group: '#{common_members.join(', ')}'")
          # No why-run alternative
        end
      end
    end

has_current_group_member?(member) click to toggle source

    # File lib/chef/provider/group.rb, line 119
    def has_current_group_member?(member)
      @current_resource.members.include?(member)
    end

load_current_resource() click to toggle source

    # File lib/chef/provider/group.rb, line 39
    def load_current_resource
      @current_resource = Chef::Resource::Group.new(@new_resource.name)
      @current_resource.group_name(@new_resource.group_name)

      group_info = nil
      begin
        group_info = Etc.getgrnam(@new_resource.group_name)
      rescue ArgumentError => e
        @group_exists = false
        Chef::Log.debug("#{@new_resource} group does not exist")
      end

      if group_info
        @new_resource.gid(group_info.gid) unless @new_resource.gid
        @current_resource.gid(group_info.gid)
        @current_resource.members(group_info.mem)
      end

      @current_resource
    end

manage_group() click to toggle source

    # File lib/chef/provider/group.rb, line 171
    def manage_group
      raise NotImplementedError, "subclasses of Chef::Provider::Group should define #manage_group"
    end

remove_group() click to toggle source

    # File lib/chef/provider/group.rb, line 175
    def remove_group
      raise NotImplementedError, "subclasses of Chef::Provider::Group should define #remove_group"
    end

whyrun_supported?() click to toggle source

    # File lib/chef/provider/group.rb, line 30
    def whyrun_supported?
      true
    end