File: ini_setting.rb

package info (click to toggle)
puppet-module-openstacklib 17.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 768 kB
  • sloc: ruby: 3,858; python: 37; sh: 21; makefile: 10
file content (82 lines) | stat: -rw-r--r-- 1,884 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
require 'facter'

Puppet::Type.type(:openstack_config).provide(
  :ini_setting,
  :parent => Puppet::Type.type(:ini_setting).provider(:ruby)
) do

  def exists?
    immutable_string = Facter.value(:os_immutable) || '<_IMMUTABLE_>'
    if resource[:value] == ensure_absent_val
      resource[:ensure] = :absent
    elsif resource[:value] == immutable_string or resource[:value] == [immutable_string]
      resource[:value] = value
      # when the value is undefined, we keep it that way.
      if value.nil? or (value.kind_of?(Array) and value[0].nil?)
        resource[:ensure] = :absent
      end
    end
    super
  end

  def create
    resource[:value] = transform(:to, resource[:value])
    super
  end

  def section
    resource[:name].split('/', 2).first
  end

  def setting
    resource[:name].split('/', 2).last
  end

  def value=(value)
    new_value = transform(:to, value)

    ini_file.set_value(section, setting, new_value)
    ini_file.save
  end

  def value
    value = ini_file.get_value(section, setting)
    new_value = transform(:from, value)
    @property_hash[:value] = new_value
    new_value
  end

  def ensure_absent_val
    resource[:ensure_absent_val]
  end

  def transform_to
    return nil unless resource.to_hash.has_key? :transform_to
    resource[:transform_to]
  end

  def transform_to=(value)
    @property_hash[:transform_to] = value
  end

  def separator
    '='
  end

  def file_path
    self.class.file_path
  end

  def transform(direction, value)
    new_value = value
    if !transform_to.nil? && !transform_to.empty?
      transformation_function = "#{direction}_#{transform_to}".to_sym
      if self.respond_to?(transformation_function)
        new_value = send(transformation_function, value)
      else
        error("Cannot find transformation #{transformation_function} for #{value}")
      end
    end
    new_value
  end
end