]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/inifile/lib/puppet/provider/ini_setting/ruby.rb
add puppetlabs/inifile to 3rdparty
[dsa-puppet.git] / 3rdparty / modules / inifile / lib / puppet / provider / ini_setting / ruby.rb
1 require File.expand_path('../../../util/ini_file', __FILE__)
2
3 Puppet::Type.type(:ini_setting).provide(:ruby) do
4
5   def self.instances
6     # this code is here to support purging and the query-all functionality of the
7     # 'puppet resource' command, on a per-file basis.  Users
8     # can create a type for a specific config file with a provider that uses
9     # this as its parent and implements the method
10     # 'self.file_path', and that will provide the value for the path to the
11     # ini file (rather than needing to specify it on each ini setting
12     # declaration).  This allows 'purging' to be used to clear out
13     # all settings from a particular ini file except those included in
14     # the catalog.
15     if self.respond_to?(:file_path)
16       # figure out what to do about the seperator
17       ini_file  = Puppet::Util::IniFile.new(file_path, '=')
18       resources = []
19       ini_file.section_names.each do |section_name|
20         ini_file.get_settings(section_name).each do |setting, value|
21           resources.push(
22             new(
23               :name   => namevar(section_name, setting),
24               :value  => value,
25               :ensure => :present
26             )
27           )
28         end
29       end
30       resources
31     else
32       raise(Puppet::Error, 'Ini_settings only support collecting instances when a file path is hard coded')
33     end
34   end
35
36   def self.namevar(section_name, setting)
37     "#{section_name}/#{setting}"
38   end
39
40   def exists?
41     !ini_file.get_value(section, setting).nil?
42   end
43
44   def create
45     ini_file.set_value(section, setting, resource[:value])
46     ini_file.save
47     @ini_file = nil
48   end
49
50   def destroy
51     ini_file.remove_setting(section, setting)
52     ini_file.save
53     @ini_file = nil
54   end
55
56   def value
57     ini_file.get_value(section, setting)
58   end
59
60   def value=(value)
61     ini_file.set_value(section, setting, resource[:value])
62     ini_file.save
63   end
64
65   def section
66     # this method is here so that it can be overridden by a child provider
67     resource[:section]
68   end
69
70   def setting
71     # this method is here so that it can be overridden by a child provider
72     resource[:setting]
73   end
74
75   def file_path
76     # this method is here to support purging and sub-classing.
77     # if a user creates a type and subclasses our provider and provides a
78     # 'file_path' method, then they don't have to specify the
79     # path as a parameter for every ini_setting declaration.
80     # This implementation allows us to support that while still
81     # falling back to the parameter value when necessary.
82     if self.class.respond_to?(:file_path)
83       self.class.file_path
84     else
85       resource[:path]
86     end
87   end
88
89   def separator
90     if resource.class.validattr?(:key_val_separator)
91       resource[:key_val_separator] || '='
92     else
93       '='
94     end
95   end
96
97   def section_prefix
98     if resource.class.validattr?(:section_prefix)
99       resource[:section_prefix] || '['
100     else
101       '['
102     end
103   end
104
105   def section_suffix
106     if resource.class.validattr?(:section_suffix)
107       resource[:section_suffix] || ']'
108     else
109       ']'
110     end
111   end
112
113   private
114   def ini_file
115     @ini_file ||= Puppet::Util::IniFile.new(file_path, separator, section_prefix, section_suffix)
116   end
117
118 end