Class: Chef::Provider::Group

Inherits:
Chef::Provider show all
Defined in:
lib/chef/provider/group.rb,
lib/chef/provider/group/pw.rb,
lib/chef/provider/group/aix.rb,
lib/chef/provider/group/dscl.rb,
lib/chef/provider/group/suse.rb,
lib/chef/provider/group/gpasswd.rb,
lib/chef/provider/group/solaris.rb,
lib/chef/provider/group/usermod.rb,
lib/chef/provider/group/windows.rb,
lib/chef/provider/group/groupadd.rb,
lib/chef/provider/group/groupmod.rb

Direct Known Subclasses

Dscl, Groupadd, Groupmod, Pw, Windows

Defined Under Namespace

Classes: Aix, Dscl, Gpasswd, Groupadd, Groupmod, Pw, Solaris, Suse, Usermod, Windows

Instance Attribute Summary collapse

Attributes inherited from Chef::Provider

#action, #after_resource, #current_resource, #logger, #new_resource, #recipe_name, #run_context

Instance Method Summary collapse

Methods inherited from Chef::Provider

action, #action_nothing, #check_resource_semantics!, #cleanup_after_converge, #compile_and_converge_action, #converge_by, #converge_if_changed, #cookbook_name, #description, #events, include_resource_dsl?, include_resource_dsl_module, #introduced, #load_after_resource, #node, #process_resource_requirements, provides, provides?, #requirements, #resource_collection, #resource_updated?, #run_action, #set_updated_status, supports?, use, use_inline_resources, #validate_required_properties!, #whyrun_mode?, #whyrun_supported?

Methods included from Mixin::LazyModuleInclude

#descendants, #include, #included

Methods included from Mixin::PowershellOut

#powershell_out, #powershell_out!

Methods included from Mixin::WindowsArchitectureHelper

#assert_valid_windows_architecture!, #disable_wow64_file_redirection, #forced_32bit_override_required?, #is_i386_process_on_x86_64_windows?, #node_supports_windows_architecture?, #node_windows_architecture, #restore_wow64_file_redirection, #valid_windows_architecture?, #with_os_architecture, #wow64_architecture_override_required?, #wow64_directory

Methods included from Mixin::PowershellExec

#powershell_exec, #powershell_exec!

Methods included from DSL::Powershell

#ps_credential

Methods included from DSL::RegistryHelper

#registry_data_exists?, #registry_get_subkeys, #registry_get_values, #registry_has_subkeys?, #registry_key_exists?, #registry_value_exists?

Methods included from DSL::ChefVault

#chef_vault, #chef_vault_item, #chef_vault_item_for_environment

Methods included from DSL::DataQuery

#data_bag, #data_bag_item, #search, #tagged?

Methods included from EncryptedDataBagItem::CheckEncrypted

#encrypted?

Methods included from DSL::PlatformIntrospection

#older_than_win_2012_or_8?, #platform?, #platform_family?, #value_for_platform, #value_for_platform_family

Methods included from Mixin::Provides

#provided_as, #provides, #provides?

Methods included from Mixin::DescendantsTracker

#descendants, descendants, #direct_descendants, direct_descendants, #find_descendants_by_name, find_descendants_by_name, #inherited, store_inherited

Methods included from DSL::Recipe

#exec, #have_resource_class_for?, #resource_class_for

Methods included from DSL::Definitions

add_definition, #evaluate_resource_definition, #has_resource_definition?

Methods included from DSL::Resources

add_resource_dsl, remove_resource_dsl

Methods included from DSL::Cheffish

load_cheffish

Methods included from DSL::RebootPending

#reboot_pending?

Methods included from DSL::IncludeRecipe

#include_recipe, #load_recipe

Methods included from Mixin::NotifyingBlock

#notifying_block, #subcontext_block

Methods included from DSL::DeclareResource

#build_resource, #declare_resource, #delete_resource, #delete_resource!, #edit_resource, #edit_resource!, #find_resource, #find_resource!, #resources, #with_run_context

Constructor Details

#initialize(new_resource, run_context) ⇒ Group

Returns a new instance of Group.


28
29
30
31
# File 'lib/chef/provider/group.rb', line 28

def initialize(new_resource, run_context)
  super
  @group_exists = true
end

Instance Attribute Details

#change_descObject

Returns the value of attribute change_desc


26
27
28
# File 'lib/chef/provider/group.rb', line 26

def change_desc
  @change_desc
end

#group_existsObject

Returns the value of attribute group_exists


25
26
27
# File 'lib/chef/provider/group.rb', line 25

def group_exists
  @group_exists
end

Instance Method Details

#compare_groupObject

Check to see if a group needs any changes. Populate ==== Returns :: If a change is required :: If a change is not required


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/chef/provider/group.rb', line 79

def compare_group
  @change_desc = [ ]
  unless group_gid_match?
    @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)

      validate_member!(member)
      missing_members << member
    end
    unless missing_members.empty?
      @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
    unless members_to_be_removed.empty?
      @change_desc << "remove existing member(s): #{members_to_be_removed.join(", ")}"
    end
  elsif !group_members_match?
    @change_desc << "replace group members with new list of members: #{new_resource.members.join(", ")}"
  end

  !@change_desc.empty?
end

#create_groupObject

Raises:

  • (NotImplementedError)

175
176
177
# File 'lib/chef/provider/group.rb', line 175

def create_group
  raise NotImplementedError, "subclasses of Chef::Provider::Group should define #create_group"
end

#define_resource_requirementsObject


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/chef/provider/group.rb', line 54

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

#group_gid_match?Boolean

Returns:

  • (Boolean)

113
114
115
# File 'lib/chef/provider/group.rb', line 113

def group_gid_match?
  new_resource.gid.to_s == current_resource.gid.to_s
end

#group_members_match?Boolean

Returns:

  • (Boolean)

117
118
119
# File 'lib/chef/provider/group.rb', line 117

def group_members_match?
  [new_resource.members].flatten.sort == [current_resource.members].flatten.sort
end

#has_current_group_member?(member) ⇒ Boolean

Returns:

  • (Boolean)

121
122
123
# File 'lib/chef/provider/group.rb', line 121

def has_current_group_member?(member)
  current_resource.members.include?(member)
end

#load_current_resourceObject


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/chef/provider/group.rb', line 33

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
    @group_exists = false
    logger.trace("#{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_groupObject

Raises:

  • (NotImplementedError)

179
180
181
# File 'lib/chef/provider/group.rb', line 179

def manage_group
  raise NotImplementedError, "subclasses of Chef::Provider::Group should define #manage_group"
end

#remove_groupObject

Raises:

  • (NotImplementedError)

183
184
185
# File 'lib/chef/provider/group.rb', line 183

def remove_group
  raise NotImplementedError, "subclasses of Chef::Provider::Group should define #remove_group"
end

#validate_member!(member) ⇒ Object


125
126
127
128
129
# File 'lib/chef/provider/group.rb', line 125

def validate_member!(member)
  # Sub-classes can do any validation if needed
  # and raise an error if validation fails
  true
end