Module: Puppet::Util::NetworkDevice::Dsl

Included in:
Cisco_ios::Facts, Cisco_ios::Model::Base
Defined in:
lib/puppet/util/network_device/dsl.rb

Instance Method Summary collapse

Instance Method Details

#evaluate_new_paramsObject



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
# File 'lib/puppet/util/network_device/dsl.rb', line 66

def evaluate_new_params
  Puppet::Util::NetworkDevice::Sorter.new(@params).tsort.each do |param|
    #Skip if the param has already been evaluated
    next if param.evaluated
    if param.cmd != false
      # Let the Transport Cache the Command for us since we are only dealing here with 'show' type commands
      out = @transport.command(param.cmd, :cache => true, :noop => false)
      # This is here for the Specs
      # FIXME
      if out.nil?
        param.evaluated = true
        next
      end
      param.parse(out)
    elsif param.match_param.is_a? Array
      param.parse([param.match_param].flatten.collect{|p|@params[p].value})
    else
      param.parse(@params[param.match_param].value)
    end
    @after_hooks ||= {}
    if @after_hooks[param.name]
      @after_hooks[param.name].each do |mod|
        register_new_module(mod[:mod], mod[:path_addition]) if mod[:block].call
      end
    end
  end
  evaluate_new_params unless @params.each_value.select {|param| param.evaluated == false}.empty?
end

#params_to_hashObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/puppet/util/network_device/dsl.rb', line 28

def params_to_hash
  @params.inject({}) {|res, data|
    unless respond_to?(:skip_params_to_hash) && skip_params_to_hash.include?(data[0])
      unless data[1].value.nil? || data[1].value.to_s.empty?
        if data[1].value.is_a?(Hash)
          res.merge!(data[1].value)
        else
          res[data[0]] = data[1].value
        end
      end
    end
    res
  }
end

#register_array(param, match_re, fetch_cmd, cmd, &block) ⇒ Object

register a simple array-valued param transform the array using a block if necessary



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/puppet/util/network_device/dsl.rb', line 146

def register_array(param, match_re, fetch_cmd, cmd, &block)
  register_param param do
    match do |txt|
      result = txt.scan(match_re).flatten
      if block_given?
        yield result
      else
        result
      end
    end
    cmd fetch_cmd
    add do |transport, value|
      transport.command("#{cmd} #{value}")
    end
    remove do |transport, old_value|
      transport.command("no #{cmd} #{old_value}")
    end
  end
end

#register_bool(param, match_re, fetch_cmd, cmd) ⇒ Object

register a simple yes/no param. the regexp must match if the param is present



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/puppet/util/network_device/dsl.rb', line 125

def register_bool(param, match_re, fetch_cmd, cmd)
  register_param param do
    match do |txt|
      if !!txt.match(match_re)
        :present
      else
        :absent
      end
    end
    cmd fetch_cmd
    add do |transport, _|
      transport.command(cmd)
    end
    remove do |transport, _|
      transport.command("no #{cmd}")
    end
  end
end

#register_model(param, klass, match_re, fetch_cmd) ⇒ Object

register a model based param



116
117
118
119
120
121
122
# File 'lib/puppet/util/network_device/dsl.rb', line 116

def register_model(param, klass, match_re, fetch_cmd)
  register_param param, Puppet::Util::NetworkDevice::Cisco_ios::Model::ModelValue do
    model klass
    match match_re
    cmd fetch_cmd
  end
end

#register_module_after(param, mod, path_addition = "", &block) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/puppet/util/network_device/dsl.rb', line 43

def register_module_after(param, mod, path_addition = "", &block)
  # Register a new Module after the required Fact has been evaluated
  # Pass a Block that must evaluate to true or false to make sure we dont
  # include Modules by accident
  @after_hooks ||= {}
  @after_hooks[param] ||= []
  @after_hooks[param] << {:mod => mod, :path_addition => path_addition, :block => block}
end

#register_new_module(mod, path_addition = "") ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/puppet/util/network_device/dsl.rb', line 52

def register_new_module(mod, path_addition = "")
  @included_modules ||= []
  unless @included_modules.include?(mod)
    Puppet::Util::Autoload.new(self, File.join(mod_path_base, path_addition), :wrap => false).load(mod)
    if path_addition.empty?
      mod_const_base.const_get(mod.to_s.capitalize).register(self)
      @included_modules << mod
    else
      mod_const_base.const_get(path_addition.to_s.capitalize).const_get(mod.to_s.capitalize).register(self)
      @included_modules << mod
    end
  end
end

#register_param(params, klass = nil, &block) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/puppet/util/network_device/dsl.rb', line 6

def register_param(params, klass = nil, &block)
  # Make it so that we can register multiple Params at the same time
  # and assign every Param an index number that must match the Regex
  klass ||= param_class
  @params ||= {}
  [params].flatten.each_with_index do |param, idx|
    @params[param] = klass.new(param, transport, facts, idx, &block)
  end
end

#register_scoped(params, scope_match, klass = nil, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/puppet/util/network_device/dsl.rb', line 16

def register_scoped(params, scope_match, klass = nil, &block)
  int_name = name
  register_param(params, klass) do
    raise "no name set" if name.nil?
    scope scope_match
    scope_name int_name
    # Pass the Block to a Helper Method so we are in the right Scope
    # when evaluating the block
    evaluate &block
  end
end

#register_simple(param, match_re, fetch_cmd, cmd) ⇒ Object

register a simple param using the specified regexp and commands



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/puppet/util/network_device/dsl.rb', line 102

def register_simple(param, match_re, fetch_cmd, cmd)
  register_param param do
    match match_re
    cmd fetch_cmd
    add  do |transport, value|
      transport.command("#{cmd} #{value}")
    end
    remove do |transport, old_value|
      transport.command("no #{cmd} #{old_value}")
    end
  end
end

#retrieveObject



95
96
97
98
99
# File 'lib/puppet/util/network_device/dsl.rb', line 95

def retrieve
  register_new_module(:base)
  evaluate_new_params
  params_to_hash
end