Class: Puppet::Provider::Vrf::CiscoIos

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/provider/vrf/cisco_ios.rb

Overview

NTP Server Puppet Provider for Cisco IOS devices

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.commands_hashObject



8
9
10
# File 'lib/puppet/provider/vrf/cisco_ios.rb', line 8

def self.commands_hash
  @commands_hash ||= PuppetX::CiscoIOS::Utility.load_yaml(File.expand_path(__dir__) + '/command.yaml')
end

.create_commands_from_instance(instance) ⇒ Object



35
36
37
# File 'lib/puppet/provider/vrf/cisco_ios.rb', line 35

def self.create_commands_from_instance(instance)
  [PuppetX::CiscoIOS::Utility.set_values(instance, commands_hash)]
end

.instances_from_cli(output) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/puppet/provider/vrf/cisco_ios.rb', line 12

def self.instances_from_cli(output)
  new_instance_fields = []
  output.scan(%r{#{PuppetX::CiscoIOS::Utility.get_instances(commands_hash)}}).each do |raw_instance_fields|
    new_instance = PuppetX::CiscoIOS::Utility.parse_resource(raw_instance_fields, commands_hash)
    new_instance[:ensure] = 'present'
    new_instance.delete_if { |_k, v| v.nil? }
    if new_instance[:route_targets]
      target = []
      if new_instance[:route_targets].is_a? Array
        new_instance[:route_targets].each do |rt|
          target << rt.split("\s")
        end
      else
        target << new_instance[:route_targets].split("\s")
      end
      new_instance[:route_targets] = target
    end

    new_instance_fields << new_instance
  end
  new_instance_fields
end

.update_commands_from_is_should(is, should) ⇒ Object



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
# File 'lib/puppet/provider/vrf/cisco_ios.rb', line 39

def self.update_commands_from_is_should(is, should)
  array_of_commands = []

  # The old route_distinguisher must be explicitly removed before a new one can be set.
  #   It will also be removed if should[:route_distinguisher] is set to 'unset'
  if is[:route_distinguisher] && (should[:route_distinguisher] && is[:route_distinguisher] != should[:route_distinguisher] || should[:route_distinguisher] == 'unset')
    array_of_commands << "no #{PuppetX::CiscoIOS::Utility.build_commmands_from_attribute_set_values({ route_distinguisher: is[:route_distinguisher] }, commands_hash)[0]}"
  end

  attributes_to_remove = (is.to_a - should.to_a).to_h
  if attributes_to_remove[:route_targets]
    attributes_to_remove[:route_targets].each do |rtarget|
      array_of_commands << "no #{PuppetX::CiscoIOS::Utility.build_commmands_from_attribute_set_values({ route_targets: "#{rtarget[0]} #{rtarget[1]}" }, commands_hash)[0]}"
    end
  end
  attributes_that_differ = (should.to_a - is.to_a).to_h
  if attributes_that_differ[:route_targets]
    attributes_that_differ[:route_targets].each do |rtarget|
      array_of_commands += PuppetX::CiscoIOS::Utility.build_commmands_from_attribute_set_values({ route_targets: "#{rtarget[0]} #{rtarget[1]}" }, commands_hash)
    end
  end
  attributes_that_differ.delete(:route_targets)

  array_of_commands += PuppetX::CiscoIOS::Utility.build_commmands_from_attribute_set_values(attributes_that_differ, commands_hash)
  array_of_commands
end

Instance Method Details

#canonicalize(_context, resources) ⇒ Object



124
125
126
# File 'lib/puppet/provider/vrf/cisco_ios.rb', line 124

def canonicalize(_context, resources)
  resources
end

#commands_hashObject



66
67
68
# File 'lib/puppet/provider/vrf/cisco_ios.rb', line 66

def commands_hash
  Puppet::Provider::Vrf::CiscoIos.commands_hash
end

#create(context, name, should) ⇒ Object



115
116
117
118
119
120
121
122
# File 'lib/puppet/provider/vrf/cisco_ios.rb', line 115

def create(context, name, should)
  array_of_commands_to_run = Puppet::Provider::Vrf::CiscoIos.create_commands_from_instance(should)
  array_of_commands_to_run.each do |command|
    context.transport.run_command_conf_t_mode(command)
  end
  is = { name: name, ensure: 'present' }
  update(context, name, is, should)
end

#delete(context, name, _should) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/puppet/provider/vrf/cisco_ios.rb', line 100

def delete(context, name, _should)
  clear_hash = { name: name, ensure: 'absent' }
  array_of_commands_to_run = Puppet::Provider::Vrf::CiscoIos.create_commands_from_instance(clear_hash)
  array_of_commands_to_run.each do |command|
    context.transport.run_command_conf_t_mode(command)
  end
end

#get(context, _names = nil) ⇒ Object



93
94
95
96
97
98
# File 'lib/puppet/provider/vrf/cisco_ios.rb', line 93

def get(context, _names = nil)
  output = context.transport.run_command_enable_mode(PuppetX::CiscoIOS::Utility.get_values(commands_hash))
  return [] if output.nil?
  return_value = Puppet::Provider::Vrf::CiscoIos.instances_from_cli(output)
  PuppetX::CiscoIOS::Utility.enforce_simple_types(context, return_value)
end

#set(context, changes) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/puppet/provider/vrf/cisco_ios.rb', line 70

def set(context, changes)
  changes.each do |name, change|
    should = change[:should]
    is = change[:is]
    is = { name: name, ensure: 'absent' } if is.nil?
    should = { name: name, ensure: 'absent' } if should.nil?

    if is[:ensure].to_s == 'absent' && should[:ensure].to_s == 'present'
      context.creating(name) do
        create(context, name, should)
      end
    elsif is[:ensure].to_s == 'present' && should[:ensure].to_s == 'present'
      context.updating(name) do
        update(context, name, is, should)
      end
    elsif is[:ensure].to_s == 'present' && should[:ensure].to_s == 'absent'
      context.deleting(name) do
        delete(context, name, should)
      end
    end
  end
end

#update(context, name, is, should) ⇒ Object



108
109
110
111
112
113
# File 'lib/puppet/provider/vrf/cisco_ios.rb', line 108

def update(context, name, is, should)
  array_of_commands_to_run = Puppet::Provider::Vrf::CiscoIos.update_commands_from_is_should(is, should)
  array_of_commands_to_run.each do |command|
    context.transport.run_command_vrf_mode(name, command)
  end
end