File: ruby.rb

package info (click to toggle)
puppet-module-openstacklib 27.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 956 kB
  • sloc: ruby: 4,549; python: 33; sh: 22; makefile: 10
file content (102 lines) | stat: -rw-r--r-- 2,412 bytes parent folder | download | duplicates (4)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
require 'facter'

require File.expand_path('../../../util/openstackconfig', __FILE__)


Puppet::Type.type(:openstack_config).provide(:ruby) do

  def self.instances
    if self.respond_to?(:file_path)
      config = Puppet::Util::OpenStackConfig.new(file_path)
      resources = []
      config.section_names.each do |section_name|
        config.get_settings(section_name).each do |setting, value|
          resources.push(
            new(
              :name   => namevar(section_name, setting),
              :value  => value,
              :ensure => :present
            )
          )
        end
      end
      resources
    else
      raise(Puppet::Error,
        'OpenStackConfig only support collecting instances when a file path ' +
        'is hard coded'
      )
    end
  end

  def self.namevar(section_name, setting)
    "#{section_name}/#{setting}"
  end

  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
    !config.get_value(section, setting).nil?
  end

  def create
    config.set_value(section, setting, resource[:value])
    config.save
    @config = nil
  end

  def destroy
    config.remove_setting(section, setting)
    config.save
    @config = nil
  end

  def value=(value)
    config.set_value(section, setting, resource[:value])
    config.save
  end

  def value
    val = config.get_value(section, setting)
    if !val.kind_of?(Array)
      [val]
    else
      val
    end
  end

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

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

  def ensure_absent_val
    # :array_matching => :all values comes in form of array even when they
    # are passed as single string
    if resource[:value].kind_of?(Array) and not resource[:ensure_absent_val].kind_of?(Array)
      [resource[:ensure_absent_val]]
    else
      resource[:ensure_absent_val]
    end
  end

  def file_path
    self.class.file_path
  end

  private
  def config
    @config ||= Puppet::Util::OpenStackConfig.new(file_path)
  end
end